| 1 |
/* EditDistanceTable.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 2012-2020,2022 David Weenink, 2025,2026 Paul Boersma |
| 4 |
* |
| 5 |
* This code is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "EditDistanceTable.h" |
| 20 |
|
| 21 |
#include "oo_DESTROY.h" |
| 22 |
#include "EditDistanceTable_def.h" |
| 23 |
#include "oo_COPY.h" |
| 24 |
#include "EditDistanceTable_def.h" |
| 25 |
#include "oo_EQUAL.h" |
| 26 |
#include "EditDistanceTable_def.h" |
| 27 |
#include "oo_CAN_WRITE_AS_ENCODING.h" |
| 28 |
#include "EditDistanceTable_def.h" |
| 29 |
#include "oo_WRITE_TEXT.h" |
| 30 |
#include "EditDistanceTable_def.h" |
| 31 |
#include "oo_WRITE_BINARY.h" |
| 32 |
#include "EditDistanceTable_def.h" |
| 33 |
#include "oo_READ_TEXT.h" |
| 34 |
#include "EditDistanceTable_def.h" |
| 35 |
#include "oo_READ_BINARY.h" |
| 36 |
#include "EditDistanceTable_def.h" |
| 37 |
#include "oo_DESCRIPTION.h" |
| 38 |
#include "EditDistanceTable_def.h" |
| 39 |
|
| 40 |
/* |
| 41 |
The insertion, deletion and substitution costs are specified in a TableOfReal |
| 42 |
1..n-2 target symbols/alphabet |
| 43 |
1..m-2 source symbols/alphabet |
| 44 |
row n-1 and col m-1 specify nomatch symbols |
| 45 |
cells [n] [1..m-2] specify insertion costs |
| 46 |
cells [1..n-1] [m] specify deletion costs |
| 47 |
cell [n-1] [m-1] if nomatch target == nomatch source |
| 48 |
cell [n] [m] if nomatch target != nomatch source |
| 49 |
*/ |
| 50 |
|
| 51 |
Thing_implement (EditCostsTable, TableOfReal, 0); |
| 52 |
|
| 53 |
void structEditCostsTable :: v1_info () { |
| 54 |
EditCostsTable_Parent :: v1_info (); |
| 55 |
MelderInfo_writeLine (U"Target:", numberOfRows - 2, U" symbols."); |
| 56 |
MelderInfo_writeLine (U"Source:", numberOfColumns - 2, U" symbols."); |
| 57 |
} |
| 58 |
|
| 59 |
bool structEditCostsTable :: v_matchTargetSymbol (conststring32 targetSymbol, conststring32 symbol) { |
| 60 |
return Melder_equ (targetSymbol, symbol); |
| 61 |
} |
| 62 |
|
| 63 |
bool structEditCostsTable :: v_matchSourceSymbol (conststring32 sourceSymbol, conststring32 symbol) { |
| 64 |
return Melder_equ (sourceSymbol, symbol); |
| 65 |
} |
| 66 |
|
| 67 |
bool structEditCostsTable :: v_matchTargetWithSourceSymbol (conststring32 targetSymbol, conststring32 sourceSymbol) { |
| 68 |
return Melder_equ (targetSymbol, sourceSymbol); |
| 69 |
} |
| 70 |
|
| 71 |
autoEditCostsTable EditCostsTable_create (integer targetAlphabetSize, integer sourceAlphabetSize) { |
| 72 |
try{ |
| 73 |
autoEditCostsTable me = Thing_new (EditCostsTable); |
| 74 |
TableOfReal_init (me.get(), targetAlphabetSize + 2, sourceAlphabetSize + 2); |
| 75 |
return me; |
| 76 |
} catch (MelderError) { |
| 77 |
Melder_throw (U"EditCostsTable not created."); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
static autoEditCostsTable EditCostsTable_createDefault () { |
| 82 |
try { |
| 83 |
autoEditCostsTable me = EditCostsTable_create (0, 0); |
| 84 |
my data [1] [1] = 0.0; // default substitution cost (nomatch == nomatch) |
| 85 |
my data [2] [2] = 2.0; // default substitution cost (nomatch != nomatch) |
| 86 |
my data [2] [1] = 1.0; // default insertion cost |
| 87 |
my data [1] [2] = 1.0; // default deletion cost |
| 88 |
return me; |
| 89 |
} catch (MelderError) { |
| 90 |
Melder_throw (U"Default EditCostsTable not created."); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
void EditCostsTable_setDefaultCosts (EditCostsTable me, double insertionCosts, double deletionCosts, double substitutionCosts) { |
| 95 |
my data [my numberOfRows - 1] [my numberOfColumns - 1] = 0.0; |
| 96 |
my data [my numberOfRows] [my numberOfColumns] = substitutionCosts; |
| 97 |
my data [my numberOfRows] [my numberOfColumns - 1] = deletionCosts; |
| 98 |
my data [my numberOfRows - 1] [my numberOfColumns] = insertionCosts; |
| 99 |
} |
| 100 |
|
| 101 |
integer EditCostsTable_getTargetIndex (EditCostsTable me, conststring32 symbol) { |
| 102 |
for (integer i = 1; i <= my numberOfRows - 2; i ++) |
| 103 |
if (my v_matchTargetSymbol (my rowLabels [i].get(), symbol)) |
| 104 |
return i; |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
integer EditCostsTable_getSourceIndex (EditCostsTable me, conststring32 symbol) { |
| 109 |
for (integer j = 1; j <= my numberOfColumns - 2; j ++) |
| 110 |
if (my v_matchSourceSymbol (my columnLabels [j].get(), symbol)) |
| 111 |
return j; |
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
void EditCostsTable_setInsertionCosts (EditCostsTable me, conststring32 targets_string, double cost) { |
| 116 |
autoSTRVEC targets = splitByWhitespace_STRVEC (targets_string); |
| 117 |
for (integer itarget = 1; itarget <= targets.size; itarget ++) { |
| 118 |
integer irow = EditCostsTable_getTargetIndex (me, targets [itarget].get()); |
| 119 |
irow = ( irow > 0 ? irow : my numberOfRows - 1 ); // nomatch condition to penultimate row |
| 120 |
my data [irow] [my numberOfColumns] = cost; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
void EditCostsTable_setDeletionCosts (EditCostsTable me, conststring32 sources_string, double cost) { |
| 125 |
autoSTRVEC sources = splitByWhitespace_STRVEC (sources_string); |
| 126 |
for (integer isource = 1; isource <= sources.size; isource ++) { |
| 127 |
integer icol = EditCostsTable_getSourceIndex (me, sources [isource].get()); |
| 128 |
icol = ( icol > 0 ? icol : my numberOfColumns - 1 ); // nomatch condition to penultimate column |
| 129 |
my data [my numberOfRows] [icol] = cost; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
void EditCostsTable_setOthersCosts (EditCostsTable me, double insertionCost, double deletionCost, double substitutionCost_equal, double substitutionCost_unequal) { |
| 134 |
my data [my numberOfRows - 1] [my numberOfColumns] = insertionCost; |
| 135 |
my data [my numberOfRows] [my numberOfColumns - 1] = deletionCost; |
| 136 |
my data [my numberOfRows - 1] [my numberOfColumns - 1] = substitutionCost_unequal; |
| 137 |
my data [my numberOfRows] [my numberOfColumns] = substitutionCost_equal; |
| 138 |
} |
| 139 |
|
| 140 |
double EditCostsTable_getOthersCost (EditCostsTable me, int costType) { |
| 141 |
return ( costType == 1 ? my data [my numberOfRows - 1] [my numberOfColumns] : //insertion |
| 142 |
( costType == 2 ? my data [my numberOfRows] [my numberOfColumns - 1] : // deletion |
| 143 |
( costType == 3 ? my data [my numberOfRows] [my numberOfColumns] : // equality |
| 144 |
my data [my numberOfRows - 1] [my numberOfColumns - 1] ) ) ); // inequality |
| 145 |
} |
| 146 |
|
| 147 |
void EditCostsTable_setSubstitutionCosts (EditCostsTable me, conststring32 targets_string, conststring32 sources_string, double cost) { |
| 148 |
try { |
| 149 |
autoSTRVEC targets = splitByWhitespace_STRVEC (targets_string); |
| 150 |
autoSTRVEC sources = splitByWhitespace_STRVEC (sources_string); |
| 151 |
autoINTVEC targetIndex = zero_INTVEC (my numberOfRows); // note: this includes zero padding |
| 152 |
autoINTVEC sourceIndex = zero_INTVEC (my numberOfRows); // note: this includes zero padding |
| 153 |
integer numberOfTargetSymbols = 0; |
| 154 |
for (integer itarget = 1; itarget <= targets.size; itarget ++) { |
| 155 |
const integer index = EditCostsTable_getTargetIndex (me, targets [itarget].get()); |
| 156 |
if (index > 0) |
| 157 |
targetIndex [++ numberOfTargetSymbols] = index; |
| 158 |
} |
| 159 |
if (numberOfTargetSymbols == 0) |
| 160 |
targetIndex [++ numberOfTargetSymbols] = my numberOfRows - 1; |
| 161 |
integer numberOfSourceSymbols = 0; |
| 162 |
for (integer isource = 1; isource <= sources.size; isource ++) { |
| 163 |
const integer index = EditCostsTable_getSourceIndex (me, sources [isource].get()); |
| 164 |
if (index > 0) |
| 165 |
sourceIndex [++ numberOfSourceSymbols] = index; |
| 166 |
} |
| 167 |
if (numberOfSourceSymbols == 0) |
| 168 |
sourceIndex [++ numberOfSourceSymbols] = my numberOfColumns - 1; |
| 169 |
for (integer i = 1; i <= numberOfTargetSymbols; i ++) { |
| 170 |
const integer irow = targetIndex [i]; |
| 171 |
for (integer j = 1; j <= numberOfSourceSymbols; j ++) |
| 172 |
my data [irow] [sourceIndex [j]] = cost; |
| 173 |
} |
| 174 |
} catch (MelderError) { |
| 175 |
Melder_throw (me, U": substitution costs not set."); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
double EditCostsTable_getInsertionCost (EditCostsTable me, conststring32 symbol) { |
| 180 |
integer irow = EditCostsTable_getTargetIndex (me, symbol); |
| 181 |
irow = ( irow == 0 ? my numberOfRows - 1 : irow ); // others is penultimate row |
| 182 |
return my data [irow] [my numberOfColumns]; |
| 183 |
} |
| 184 |
|
| 185 |
double EditCostsTable_getDeletionCost (EditCostsTable me, conststring32 sourceSymbol) { |
| 186 |
integer icol = EditCostsTable_getSourceIndex (me, sourceSymbol); |
| 187 |
icol = ( icol == 0 ? my numberOfColumns - 1 : icol ); // others is penultimate column |
| 188 |
return my data [my numberOfRows] [icol]; |
| 189 |
} |
| 190 |
|
| 191 |
double EditCostsTable_getSubstitutionCost (EditCostsTable me, conststring32 symbol, conststring32 replacement) { |
| 192 |
integer irow = EditCostsTable_getTargetIndex (me, symbol); |
| 193 |
integer icol = EditCostsTable_getSourceIndex (me, replacement); |
| 194 |
if (irow == 0 && icol == 0) { // nomatch |
| 195 |
irow = my numberOfRows; |
| 196 |
icol = my numberOfColumns; |
| 197 |
if (my v_matchTargetWithSourceSymbol (symbol, replacement)) { |
| 198 |
-- irow; |
| 199 |
-- icol; |
| 200 |
} |
| 201 |
} else { |
| 202 |
irow = ( irow == 0 ? my numberOfRows - 1 : irow ); |
| 203 |
icol = ( icol == 0 ? my numberOfColumns - 1 : icol ); |
| 204 |
} |
| 205 |
return my data [irow] [icol]; |
| 206 |
} |
| 207 |
|
| 208 |
autoTableOfReal EditCostsTable_to_TableOfReal (EditCostsTable me) { |
| 209 |
try { |
| 210 |
autoTableOfReal thee = TableOfReal_create (my numberOfRows, my numberOfColumns); |
| 211 |
thy columnLabels.all() <<= my columnLabels.all(); |
| 212 |
thy rowLabels.all() <<= my rowLabels.all(); |
| 213 |
thy data.all() <<= my data.all(); |
| 214 |
return thee; |
| 215 |
} catch (MelderError) { |
| 216 |
Melder_throw (me, U": not converted to TableOfReal."); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
Thing_implement (EditDistanceTable, TableOfReal, 0); |
| 221 |
|
| 222 |
void structEditDistanceTable :: v1_info () { |
| 223 |
EditDistanceTable_Parent :: v1_info (); |
| 224 |
MelderInfo_writeLine (U"Target:", numberOfRows, U" symbols."); |
| 225 |
MelderInfo_writeLine (U"Source:", numberOfColumns, U" symbols."); |
| 226 |
} |
| 227 |
|
| 228 |
autoEditDistanceTable EditDistanceTable_create (Strings target, Strings source) { |
| 229 |
try { |
| 230 |
autoEditDistanceTable me = Thing_new (EditDistanceTable); |
| 231 |
const integer numberOfSourceSymbols = source -> numberOfStrings, numberOfTargetSymbols = target -> numberOfStrings; |
| 232 |
TableOfReal_init (me.get(), numberOfTargetSymbols + 1, numberOfSourceSymbols + 1); |
| 233 |
TableOfReal_setColumnLabel (me.get(), 1, U""); |
| 234 |
for (integer j = 1; j <= numberOfSourceSymbols; j ++) |
| 235 |
my columnLabels [j + 1] = Melder_dup (source -> strings [j].get()); |
| 236 |
TableOfReal_setRowLabel (me.get(), 1, U""); |
| 237 |
for (integer i = 1; i <= numberOfTargetSymbols; i ++) |
| 238 |
my rowLabels [i + 1] = Melder_dup (target -> strings [i].get()); |
| 239 |
my path = newvectorzero <structPairOfInteger> (numberOfTargetSymbols + numberOfSourceSymbols + 1); |
| 240 |
my pathLength = my path.size; // maintain invariant |
| 241 |
my editCostsTable = EditCostsTable_createDefault (); |
| 242 |
EditDistanceTable_findPath (me.get(), nullptr); |
| 243 |
return me; |
| 244 |
} catch (MelderError) { |
| 245 |
Melder_throw (U"EditDistanceTable not created."); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
void EditDistanceTable_setEditCosts (EditDistanceTable me, EditCostsTable thee) { |
| 250 |
try { |
| 251 |
my editCostsTable = Data_copy (thee); |
| 252 |
} catch (MelderError) { |
| 253 |
Melder_throw (me, U": edit costs not set."); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
autoEditDistanceTable EditDistanceTable_createFromCharacterStrings (const char32 chars1 [], const char32 chars2 []) { |
| 258 |
try { |
| 259 |
autoStrings s1 = Strings_createAsCharacters (chars1); |
| 260 |
autoStrings s2 = Strings_createAsCharacters (chars2); |
| 261 |
autoEditDistanceTable me = EditDistanceTable_create (s1.get(), s2.get()); |
| 262 |
return me; |
| 263 |
} catch (MelderError) { |
| 264 |
Melder_throw (U"EditDistanceTable not created from character strings."); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
static void NUMrationalize (double x, integer *numerator, integer *denominator) { |
| 269 |
constexpr double epsilon = 1e-6; |
| 270 |
*numerator = 1; |
| 271 |
for (*denominator = 1; *denominator <= 100000; (*denominator) ++) { |
| 272 |
const double numerator_d = x * *denominator, rounded = round (numerator_d); |
| 273 |
if (fabs (rounded - numerator_d) < epsilon) { |
| 274 |
*numerator = (integer) rounded; |
| 275 |
return; |
| 276 |
} |
| 277 |
} |
| 278 |
*denominator = 0; /* Failure. */ |
| 279 |
} |
| 280 |
|
| 281 |
static void fixRows (TableOfReal me, integer *rowmin, integer *rowmax) { |
| 282 |
if (*rowmax < *rowmin) { |
| 283 |
*rowmin = 1; |
| 284 |
*rowmax = my numberOfRows; |
| 285 |
} else if (*rowmin < 1) { |
| 286 |
*rowmin = 1; |
| 287 |
} else if (*rowmax > my numberOfRows) { |
| 288 |
*rowmax = my numberOfRows; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
static void print4 (char *buffer, double value, int iformat, int width, int precision) { |
| 293 |
char formatString [40]; |
| 294 |
if (iformat == 4) { |
| 295 |
integer numerator, denominator; |
| 296 |
NUMrationalize (value, & numerator, & denominator); |
| 297 |
if (numerator == 0) |
| 298 |
snprintf (buffer, 40, "0"); |
| 299 |
else if (denominator > 1) |
| 300 |
snprintf (buffer, 40, "%s/%s", Melder8_integer (numerator), Melder8_integer (denominator)); |
| 301 |
else |
| 302 |
snprintf (buffer, 40, "%.7g", value); |
| 303 |
} else { |
| 304 |
snprintf (formatString, 40, "%%%d.%d%c", width, precision, ( iformat == 1 ? 'f' : ( iformat == 2 ? 'e' : 'g' ) )); |
| 305 |
snprintf (buffer, 40, formatString, value); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
static double getMaxRowLabelWidth (TableOfReal me, Graphics graphics, integer rowmin, integer rowmax) { |
| 310 |
double maxWidth = 0.0; |
| 311 |
if (! my rowLabels) |
| 312 |
return 0.0; |
| 313 |
fixRows (me, & rowmin, & rowmax); |
| 314 |
for (integer irow = rowmin; irow <= rowmax; irow ++) { |
| 315 |
if (my rowLabels [irow] && my rowLabels [irow] [0]) { |
| 316 |
const double textWidth = Graphics_textWidth_ps (graphics, my rowLabels [irow].get(), true); /* SILIPA is bigger than XIPA */ |
| 317 |
if (textWidth > maxWidth) { |
| 318 |
maxWidth = textWidth; |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
return maxWidth; |
| 323 |
} |
| 324 |
|
| 325 |
static double getLeftMargin (Graphics graphics) { |
| 326 |
return Graphics_dxMMtoWC (graphics, 1.0); |
| 327 |
} |
| 328 |
|
| 329 |
static double getLineSpacing (Graphics graphics) { |
| 330 |
return Graphics_dyMMtoWC (graphics, 1.5 * Graphics_inqFontSize (graphics) * 25.4 / 72.0); |
| 331 |
} |
| 332 |
|
| 333 |
void EditDistanceTable_draw (EditDistanceTable me, Graphics graphics, int iformat, int precision, double angle) { |
| 334 |
const integer rowmin = 1, rowmax = my numberOfRows; |
| 335 |
Graphics_setInner (graphics); |
| 336 |
Graphics_setWindow (graphics, 0.5, my numberOfColumns + 0.5, 0.0, 1.0); |
| 337 |
const double leftMargin = getLeftMargin (graphics); // not earlier! |
| 338 |
const double lineSpacing = getLineSpacing (graphics); // not earlier! |
| 339 |
const double maxTextWidth = getMaxRowLabelWidth (me, graphics, rowmin, rowmax); |
| 340 |
double y = 1.0 + 0.1 * lineSpacing; |
| 341 |
autoBOOLMAT onPath = zero_BOOLMAT (my numberOfRows, my numberOfColumns); |
| 342 |
for (integer i = 1; i <= my pathLength; i ++) { |
| 343 |
const structPairOfInteger poi = my path [i]; |
| 344 |
onPath [poi.y] [poi.x] = true; |
| 345 |
} |
| 346 |
for (integer irow = my numberOfRows; irow > 0; irow --) { |
| 347 |
Graphics_setTextAlignment (graphics, Graphics_RIGHT, Graphics_HALF); |
| 348 |
if (my rowLabels && my rowLabels [irow] && my rowLabels [irow] [0]) |
| 349 |
Graphics_text (graphics, 0.5 - leftMargin, y, my rowLabels [irow].get()); |
| 350 |
Graphics_setTextAlignment (graphics, kGraphics_horizontalAlignment::CENTRE, Graphics_HALF); |
| 351 |
for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 352 |
char text [40]; |
| 353 |
print4 (text, my data [irow] [icol], iformat, 0, precision); |
| 354 |
Graphics_setBold (graphics, onPath [irow] [icol]); |
| 355 |
Graphics_text (graphics, icol, y, Melder_peek8to32_u (text)); |
| 356 |
if (onPath [irow] [icol]) { |
| 357 |
Graphics_rectangle (graphics, icol - 0.5, icol + 0.5, y - 0.5 * lineSpacing, y + 0.5 * lineSpacing); |
| 358 |
} |
| 359 |
} |
| 360 |
y -= lineSpacing; |
| 361 |
Graphics_setBold (graphics, false); |
| 362 |
} |
| 363 |
|
| 364 |
double left = 0.5; |
| 365 |
if (maxTextWidth > 0.0) left -= maxTextWidth + 2.0 * leftMargin; |
| 366 |
Graphics_line (graphics, left, y, my numberOfColumns + 0.5, y); |
| 367 |
|
| 368 |
Graphics_setTextRotation (graphics, angle); |
| 369 |
if (angle < 0.0) { |
| 370 |
y -= 0.3 * lineSpacing; |
| 371 |
Graphics_setTextAlignment (graphics, Graphics_LEFT, Graphics_HALF); |
| 372 |
} else if (angle > 0.0) { |
| 373 |
Graphics_setTextAlignment (graphics, Graphics_RIGHT, Graphics_HALF); |
| 374 |
y -= 0.3 * lineSpacing; |
| 375 |
} else { |
| 376 |
Graphics_setTextAlignment (graphics, kGraphics_horizontalAlignment::CENTRE, Graphics_TOP); |
| 377 |
} |
| 378 |
for (integer icol = 1; icol <= my numberOfColumns; icol ++) { |
| 379 |
if (my columnLabels && my columnLabels [icol] && my columnLabels [icol] [0]) |
| 380 |
Graphics_text (graphics, icol, y, my columnLabels [icol].get()); |
| 381 |
} |
| 382 |
Graphics_setTextRotation (graphics, 0.0); |
| 383 |
y -= lineSpacing; |
| 384 |
Graphics_line (graphics, 0.5, y, 0.5, 1 + 0.5 * lineSpacing); |
| 385 |
Graphics_unsetInner (graphics); |
| 386 |
} |
| 387 |
|
| 388 |
void EditDistanceTable_drawEditOperations (EditDistanceTable me, Graphics graphics) { |
| 389 |
const conststring32 oinsertion = U"i", insertion = U"*", odeletion = U"d", deletion = U"*", osubstitution = U"s", oequal = U""; |
| 390 |
Graphics_setWindow (graphics, 0.5, my pathLength - 0.5, 0.0, 1.0); // pathLength-1 symbols |
| 391 |
const double lineSpacing = getLineSpacing (graphics); |
| 392 |
const double ytarget = 1 - lineSpacing, ysource = ytarget - 2 * lineSpacing, yoper = ysource - lineSpacing; |
| 393 |
Graphics_setTextAlignment (graphics, kGraphics_horizontalAlignment::CENTRE, Graphics_BOTTOM); |
| 394 |
for (integer i = 2; i <= my pathLength; i ++) { |
| 395 |
const structPairOfInteger p = my path [i], p1 = my path [i - 1]; |
| 396 |
const double x = i - 1; |
| 397 |
if (p.x == p1.x) { // insertion |
| 398 |
Graphics_text (graphics, x, ytarget, my rowLabels [p.y].get()); |
| 399 |
Graphics_text (graphics, x, ysource, deletion); |
| 400 |
Graphics_text (graphics, x, yoper, oinsertion); |
| 401 |
} else if (p.y == p1.y) { // deletion |
| 402 |
Graphics_text (graphics, x, ytarget, insertion); |
| 403 |
Graphics_text (graphics, x, ysource, my columnLabels [p.x].get()); |
| 404 |
Graphics_text (graphics, x, yoper, odeletion); |
| 405 |
} else { // substitution ? |
| 406 |
Graphics_text (graphics, x, ytarget, my rowLabels [p.y].get()); |
| 407 |
Graphics_text (graphics, x, ysource, my columnLabels [p.x].get()); |
| 408 |
Graphics_text (graphics, x, yoper, ( Melder_equ (my rowLabels [p.y].get(), my columnLabels [p.x].get()) ? oequal : osubstitution )); |
| 409 |
} |
| 410 |
Graphics_line (graphics, x, ysource + lineSpacing, x, ytarget - 0.1 * lineSpacing); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
void EditDistanceTable_setDefaultCosts (EditDistanceTable me, double insertionCosts, double deletionCosts, double substitutionCosts) { |
| 415 |
EditCostsTable_setDefaultCosts (my editCostsTable.get(), insertionCosts, deletionCosts, substitutionCosts); |
| 416 |
EditDistanceTable_findPath (me, nullptr); |
| 417 |
} |
| 418 |
|
| 419 |
autoTableOfReal EditDistanceTable_to_TableOfReal_directions (EditDistanceTable me) { |
| 420 |
autoTableOfReal tor; |
| 421 |
EditDistanceTable_findPath (me, & tor); |
| 422 |
return tor; |
| 423 |
} |
| 424 |
|
| 425 |
void EditDistanceTable_findPath (EditDistanceTable me, autoTableOfReal *out_directions) { |
| 426 |
try { |
| 427 |
/* |
| 428 |
What do we have to do to source to get target? |
| 429 |
The origin [1] [1] is in the bottom-left corner. |
| 430 |
Target vertical, source horizontal. |
| 431 |
Going in the vertical direction is a deletion, horizontal is insertion, diagonal is substitution. |
| 432 |
*/ |
| 433 |
const integer numberOfSources = my numberOfColumns - 1, numberOfTargets = my numberOfRows - 1; |
| 434 |
autoINTMAT psi = zero_INTMAT (my numberOfRows, my numberOfColumns); |
| 435 |
autoMAT delta = zero_MAT (my numberOfRows, my numberOfColumns); |
| 436 |
|
| 437 |
for (integer icol = 2; icol <= my numberOfColumns; icol ++) { |
| 438 |
delta [1] [icol] = delta [1] [icol - 1] + EditCostsTable_getDeletionCost (my editCostsTable.get(), my columnLabels [icol].get()); |
| 439 |
psi [1] [icol] = WARPING_fromLeft; |
| 440 |
} |
| 441 |
for (integer irow = 2; irow <= my numberOfRows; irow ++) { |
| 442 |
delta [irow] [1] = delta [irow - 1] [1] + EditCostsTable_getInsertionCost (my editCostsTable.get(), my rowLabels [irow].get()); |
| 443 |
psi [irow] [1] = WARPING_fromBelow; |
| 444 |
} |
| 445 |
for (integer icol = 2; icol <= my numberOfColumns; icol ++) { |
| 446 |
for (integer irow = 2; irow <= my numberOfRows; irow ++) { |
| 447 |
// the substitution, deletion and insertion costs. |
| 448 |
const double left = delta [irow] [icol - 1] + |
| 449 |
EditCostsTable_getInsertionCost (my editCostsTable.get(), my rowLabels [irow].get()); |
| 450 |
const double bottom = delta [irow - 1] [icol] + |
| 451 |
EditCostsTable_getDeletionCost (my editCostsTable.get(), my columnLabels [icol].get()); |
| 452 |
double mindist = delta [irow - 1] [icol - 1] + |
| 453 |
EditCostsTable_getSubstitutionCost (my editCostsTable.get(), my rowLabels [irow].get(), my columnLabels [icol].get()); // diag |
| 454 |
psi [irow] [icol] = WARPING_fromDiag; |
| 455 |
if (bottom < mindist) { |
| 456 |
mindist = bottom; |
| 457 |
psi [irow] [icol] = WARPING_fromBelow; |
| 458 |
} |
| 459 |
if (left < mindist) { |
| 460 |
mindist = left; |
| 461 |
psi [irow] [icol] = WARPING_fromLeft; |
| 462 |
} |
| 463 |
delta [irow] [icol] = mindist; |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
/* |
| 468 |
Reset the path to its maximum length, clearing it to zero. |
| 469 |
*/ |
| 470 |
my path. resize (my numberOfRows + my numberOfColumns - 1); |
| 471 |
for (integer i = 1; i <= my path.size; i ++) |
| 472 |
my path [i]. x = my path [i]. y = 0; |
| 473 |
my pathLength = my path.size; // maintain invariant |
| 474 |
|
| 475 |
/* |
| 476 |
Compute the shortest path. |
| 477 |
*/ |
| 478 |
integer iy = numberOfTargets, ix = numberOfSources; |
| 479 |
integer index = my pathLength; |
| 480 |
my path [index]. x = ix; |
| 481 |
my path [index]. y = iy; |
| 482 |
while (! (ix == 0 && iy == 0)) { |
| 483 |
Melder_assert (ix >= 0 && iy >= 0); |
| 484 |
if (psi [1 + iy] [1 + ix] == WARPING_fromLeft) { |
| 485 |
ix --; |
| 486 |
} else if (psi [1 + iy] [1 + ix] == WARPING_fromBelow) { |
| 487 |
iy --; |
| 488 |
} else { // WARPING_fromDiag |
| 489 |
ix --; |
| 490 |
iy --; |
| 491 |
} |
| 492 |
my path [-- index]. x = ix; |
| 493 |
my path [index]. y = iy; |
| 494 |
} |
| 495 |
if (index > 1) { |
| 496 |
integer k = 1; |
| 497 |
for (integer i = index; i <= my pathLength; i ++) { |
| 498 |
my path [k ++] = my path [i]; |
| 499 |
my path [i]. x = my path [i]. y = 0; |
| 500 |
} |
| 501 |
my path. resize (k - 1); |
| 502 |
my pathLength = my path.size; // maintain invariant |
| 503 |
} |
| 504 |
|
| 505 |
/* |
| 506 |
Shift by 1. |
| 507 |
*/ |
| 508 |
for (integer i = 1; i <= my pathLength; i ++) { |
| 509 |
my path [i]. x ++; |
| 510 |
my path [i]. y ++; |
| 511 |
} |
| 512 |
|
| 513 |
my data.all() <<= delta.all(); |
| 514 |
|
| 515 |
if (out_directions) { |
| 516 |
autoTableOfReal him = EditDistanceTable_to_TableOfReal (me); |
| 517 |
for (integer irow = 1; irow <= my numberOfRows; irow ++) |
| 518 |
for (integer icol = 1; icol <= my numberOfColumns; icol ++) |
| 519 |
his data [irow] [icol] = double (psi [irow] [icol]); |
| 520 |
*out_directions = him.move(); |
| 521 |
} |
| 522 |
} catch (MelderError) { |
| 523 |
Melder_throw (me, U": minimum path not found."); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
autoTableOfReal EditDistanceTable_to_TableOfReal (EditDistanceTable me) { |
| 528 |
try { |
| 529 |
autoTableOfReal thee = TableOfReal_create (my numberOfRows, my numberOfColumns); |
| 530 |
thy columnLabels.all() <<= my columnLabels.all(); |
| 531 |
thy rowLabels.all() <<= my rowLabels.all(); |
| 532 |
thy data.all() <<= my data.all(); |
| 533 |
return thee; |
| 534 |
} catch (MelderError) { |
| 535 |
Melder_throw (me, U": no TableOfReal created."); |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
/* End of file EditDistanceTable.cpp */ |