| 1 |
/* OTGrammar.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1997-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. |
| 13 |
* See the GNU 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 |
/* |
| 20 |
* pb 2002/07/16 GPL |
| 21 |
* pb 2002/11/04 randomize in case of equal candidates |
| 22 |
* pb 2003/05/08 better superset violation warning |
| 23 |
* pb 2003/05/23 made superset violation warning conditional |
| 24 |
* pb 2003/10/15 backtrack in case of failing multiple chews for EDCD |
| 25 |
* pb 2003/10/15 crucial ties option |
| 26 |
* pb 2004/01/17 OTGrammar_Distributions_getFractionCorrect |
| 27 |
* pb 2004/08/08 OTGrammar_removeHarmonicallyBoundedCandidates |
| 28 |
* pb 2004/08/09 bug removal: more complete OTGrammar_save and restore (affected multiple-chew correctness), |
| 29 |
* changing the 114.5 in Boersma (Phonology 2003) to 118.1 |
| 30 |
* pb 2004/08/09 suppressed superset violation in case of identical constraint violation patterns such |
| 31 |
* as for /(L L2) L (L2 L) (L1 L)/ and /(L2 L) L (L L2) (L1 L)/, thus restricting the warning to cases |
| 32 |
* of *strict* superset violations |
| 33 |
* pb 2004/08/11 repaired memory leak in OTGrammarTableau_removeCandidate_unstripped |
| 34 |
* pb 2004/09/10 monitor rankings during learning from PairDistribution or Distributions |
| 35 |
* pb 2004/10/16 struct structOTxx |
| 36 |
* pb 2005/01/24 write to headerless spreadsheet file |
| 37 |
* pb 2005/04/19 OTHistory |
| 38 |
* pb 2005/06/30 learning from partial pairs |
| 39 |
* pb 2005/12/11 OTGrammar_honourlocalRankings: |
| 40 |
* pb 2005/12/11 OTGrammar_PairDistribution_listObligatoryRankings (depth 1) |
| 41 |
* pb 2006/01/05 new decision strategies: HarmonicGrammar and LinearOT |
| 42 |
* pb 2006/01/21 better procedure name |
| 43 |
* pb 2006/02/02 new decision strategy: ExponentialHG |
| 44 |
* pb 2006/12/08 MelderInfo |
| 45 |
* pb 2007/04/22 multiply learning step by number of violations (for HarmonicGrammar and LinearOT) |
| 46 |
* pb 2007/04/25 new decision strategy: MaximumEntropy |
| 47 |
* pb 2007/04/30 many improvements |
| 48 |
* pb 2007/05/20 new decision strategy: PositiveHG |
| 49 |
* pb 2007/06/21 corrected PositiveHG |
| 50 |
* pb 2007/06/21 made spreadsheet file readable as Table |
| 51 |
* pb 2007/07/24 leak and constraint plasticity |
| 52 |
* pb 2007/07/27 leak and constraint plasticity also written... |
| 53 |
* pb 2007/08/08 wchar |
| 54 |
* pb 2007/10/01 can write as encoding |
| 55 |
* pb 2008/03/03 EDCD with vacation |
| 56 |
* pb 2008/03/07 Demote one with vacation |
| 57 |
* pb 2008/03/07 Reset to random total ranking |
| 58 |
* pb 2008/03/27 Exponential HG: reset average weight to zero after every change |
| 59 |
* pb 2008/03/28 Exponential HG: set update rule to HG-GLA rather than OT-GLA |
| 60 |
* pb 2008/03/31 OTGrammar_PairDistribution_findPositiveWeights |
| 61 |
* pb 2008/04/08 made (OTGrammar & Distributions) learnFromPartialOutputs and getFractionCorrect five times faster |
| 62 |
* pb 2008/04/12 split off NUMlinprog |
| 63 |
* pb 2008/05/31 new decision strategy: ExponentialMaximumEntropy |
| 64 |
* pb 2009/03/09 new update rule: Weighted all up, highest down |
| 65 |
* pb 2009/07/07 OTGrammar_PairDistribution_getMinimumNumberCorrect |
| 66 |
* pb 2010/06/05 corrected colours |
| 67 |
* pb 2011/03/01 renamed "strategy" to "updateRule", "meanLearningStep" to "plasticity", "rankingSpreading" to "evaluationNoise" |
| 68 |
* pb 2011/03/22 C++ |
| 69 |
* pb 2011/04/27 Melder_debug 41 and 42 |
| 70 |
* pb 2011/07/14 C++ |
| 71 |
* pb 2014/02/27 skippable symmetric all |
| 72 |
* pb 2014/07/25 RRIP |
| 73 |
* pb 2024/12/25 OTGrammar_allOutputsAreDistinguishable |
| 74 |
*/ |
| 75 |
|
| 76 |
#include "OTGrammar.h" |
| 77 |
|
| 78 |
#include "oo_DESTROY.h" |
| 79 |
#include "OTGrammar_def.h" |
| 80 |
#include "oo_COPY.h" |
| 81 |
#include "OTGrammar_def.h" |
| 82 |
#include "oo_EQUAL.h" |
| 83 |
#include "OTGrammar_def.h" |
| 84 |
#include "oo_CAN_WRITE_AS_ENCODING.h" |
| 85 |
#include "OTGrammar_def.h" |
| 86 |
#include "oo_WRITE_BINARY.h" |
| 87 |
#include "OTGrammar_def.h" |
| 88 |
#include "oo_READ_BINARY.h" |
| 89 |
#include "OTGrammar_def.h" |
| 90 |
#include "oo_DESCRIPTION.h" |
| 91 |
#include "OTGrammar_def.h" |
| 92 |
|
| 93 |
#include "enums_getText.h" |
| 94 |
#include "OTGrammar_enums.h" |
| 95 |
#include "enums_getValue.h" |
| 96 |
#include "OTGrammar_enums.h" |
| 97 |
|
| 98 |
void structOTGrammar :: v1_info () { |
| 99 |
structDaata :: v1_info (); |
| 100 |
integer numberOfCandidates = 0, numberOfViolations = 0; |
| 101 |
for (integer itab = 1; itab <= numberOfTableaus; itab ++) { |
| 102 |
numberOfCandidates += our tableaus [itab]. numberOfCandidates; |
| 103 |
for (integer icand = 1; icand <= our tableaus [itab]. numberOfCandidates; icand ++) |
| 104 |
for (integer icons = 1; icons <= our numberOfConstraints; icons ++) |
| 105 |
numberOfViolations += our tableaus [itab]. candidates [icand]. marks [icons]; |
| 106 |
} |
| 107 |
MelderInfo_writeLine (U"Decision strategy: ", kOTGrammar_decisionStrategy_getText (decisionStrategy)); |
| 108 |
MelderInfo_writeLine (U"Number of constraints: ", numberOfConstraints); |
| 109 |
MelderInfo_writeLine (U"Number of tableaus: ", numberOfTableaus); |
| 110 |
MelderInfo_writeLine (U"Number of candidates: ", numberOfCandidates); |
| 111 |
MelderInfo_writeLine (U"Number of violation marks: ", numberOfViolations); |
| 112 |
} |
| 113 |
|
| 114 |
void structOTGrammar :: v1_writeText (MelderFile file) { |
| 115 |
MelderFile_write (file, U"\n<", kOTGrammar_decisionStrategy_getText (decisionStrategy), |
| 116 |
U">\n", leak, U" ! leak\n", our numberOfConstraints, U" constraints"); |
| 117 |
for (integer icons = 1; icons <= our numberOfConstraints; icons ++) { |
| 118 |
OTGrammarConstraint constraint = & constraints [icons]; |
| 119 |
MelderFile_write (file, U"\nconstraint [", icons, U"]: \""); |
| 120 |
for (const char32 *p = & constraint -> name [0]; *p; p ++) { |
| 121 |
if (*p == U'\"') |
| 122 |
MelderFile_writeCharacter (file, U'\"'); // double any quotes within quotes |
| 123 |
MelderFile_writeCharacter (file, *p); |
| 124 |
} |
| 125 |
MelderFile_write (file, U"\" ", constraint -> ranking, |
| 126 |
U" ", constraint -> disharmony, U" ", constraint -> plasticity, U" ! "); |
| 127 |
for (const char32 *p = & constraint -> name [0]; *p; p ++) { |
| 128 |
if (*p == U'\n') |
| 129 |
MelderFile_writeCharacter (file, U' '); |
| 130 |
else if (*p == U'\\' && p [1] == U's' && p [2] == U'{') |
| 131 |
p += 2; |
| 132 |
else if (*p == U'}') |
| 133 |
{ } |
| 134 |
else |
| 135 |
MelderFile_writeCharacter (file, *p); |
| 136 |
} |
| 137 |
} |
| 138 |
MelderFile_write (file, U"\n\n", our numberOfFixedRankings, U" fixed rankings"); |
| 139 |
for (integer irank = 1; irank <= our numberOfFixedRankings; irank ++) { |
| 140 |
OTGrammarFixedRanking fixedRanking = & our fixedRankings [irank]; |
| 141 |
MelderFile_write (file, U"\n ", fixedRanking -> higher, U" ", fixedRanking -> lower); |
| 142 |
} |
| 143 |
MelderFile_write (file, U"\n\n", our numberOfTableaus, U" tableaus"); |
| 144 |
for (integer itab = 1; itab <= our numberOfTableaus; itab ++) { |
| 145 |
OTGrammarTableau tableau = & our tableaus [itab]; |
| 146 |
MelderFile_write (file, U"\ninput [", itab, U"]: \""); |
| 147 |
for (const char32 *p = & tableau -> input [0]; *p; p ++) { |
| 148 |
if (*p == U'\"') |
| 149 |
MelderFile_writeCharacter (file, U'\"'); // double any quotes within quotes |
| 150 |
MelderFile_writeCharacter (file, *p); |
| 151 |
} |
| 152 |
MelderFile_write (file, U"\" ", tableau -> numberOfCandidates); |
| 153 |
for (integer icand = 1; icand <= tableau -> numberOfCandidates; icand ++) { |
| 154 |
OTGrammarCandidate candidate = & tableau -> candidates [icand]; |
| 155 |
MelderFile_write (file, U"\n candidate [", icand, U"]: \""); |
| 156 |
for (const char32 *p = & candidate -> output [0]; *p; p ++) { |
| 157 |
if (*p == U'\"') |
| 158 |
MelderFile_writeCharacter (file, U'\"'); // double any quotes within quotes |
| 159 |
MelderFile_writeCharacter (file, *p); |
| 160 |
} |
| 161 |
MelderFile_writeCharacter (file, U'\"'); |
| 162 |
for (integer icons = 1; icons <= candidate -> numberOfConstraints; icons ++) { |
| 163 |
MelderFile_write (file, U" ", candidate -> marks [icons]); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
void OTGrammar_checkIndex (OTGrammar me) { |
| 170 |
if (my index.size != 0) return; |
| 171 |
my index = to_INTVEC (my numberOfConstraints); |
| 172 |
OTGrammar_sort (me); |
| 173 |
} |
| 174 |
|
| 175 |
void structOTGrammar :: v1_readText (MelderReadText text, int formatVersion) { |
| 176 |
OTGrammar_Parent :: v1_readText (text, formatVersion); |
| 177 |
if (formatVersion >= 1) { |
| 178 |
try { |
| 179 |
our decisionStrategy = (kOTGrammar_decisionStrategy) texgete8 (text, (enum_generic_getValue) kOTGrammar_decisionStrategy_getValue); |
| 180 |
} catch (MelderError) { |
| 181 |
Melder_throw (U"Trying to read decision strategy."); |
| 182 |
} |
| 183 |
} |
| 184 |
if (formatVersion >= 2) { |
| 185 |
try { |
| 186 |
our leak = texgetr64 (text); |
| 187 |
} catch (MelderError) { |
| 188 |
Melder_throw (U"Trying to read leak."); |
| 189 |
} |
| 190 |
} |
| 191 |
try { |
| 192 |
our numberOfConstraints = texgeti32 (text); |
| 193 |
} catch (MelderError) { |
| 194 |
Melder_throw (U"Trying to read number of constraints."); |
| 195 |
} |
| 196 |
if (our numberOfConstraints < 1) |
| 197 |
Melder_throw (U"No constraints."); |
| 198 |
our constraints = newvectorzero <structOTGrammarConstraint> (our numberOfConstraints); |
| 199 |
for (integer icons = 1; icons <= our numberOfConstraints; icons ++) { |
| 200 |
OTGrammarConstraint constraint = & constraints [icons]; |
| 201 |
try { |
| 202 |
constraint -> name = texgetw16 (text); |
| 203 |
} catch (MelderError) { |
| 204 |
Melder_throw (U"Trying to read name of constraint ", icons, U"."); |
| 205 |
} |
| 206 |
try { |
| 207 |
constraint -> ranking = texgetr64 (text); |
| 208 |
} catch (MelderError) { |
| 209 |
Melder_throw (U"Trying to read ranking of constraint ", icons, U"."); |
| 210 |
} |
| 211 |
try { |
| 212 |
constraint -> disharmony = texgetr64 (text); |
| 213 |
} catch (MelderError) { |
| 214 |
Melder_throw (U"Trying to read disharmony of constraint ", icons, U"."); |
| 215 |
} |
| 216 |
if (formatVersion < 2) { |
| 217 |
constraint -> plasticity = 1.0; |
| 218 |
} else { |
| 219 |
try { |
| 220 |
constraint -> plasticity = texgetr64 (text); |
| 221 |
} catch (MelderError) { |
| 222 |
Melder_throw (U"Trying to read plasticity of constraint ", icons, U"."); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
try { |
| 227 |
our numberOfFixedRankings = texgeti32 (text); |
| 228 |
} catch (MelderError) { |
| 229 |
Melder_throw (U"Trying to read number of fixed rankings."); |
| 230 |
} |
| 231 |
if (our numberOfFixedRankings >= 1) { |
| 232 |
our fixedRankings = newvectorzero <structOTGrammarFixedRanking> (numberOfFixedRankings); |
| 233 |
for (integer irank = 1; irank <= our numberOfFixedRankings; irank ++) { |
| 234 |
OTGrammarFixedRanking fixedRanking = & our fixedRankings [irank]; |
| 235 |
try { |
| 236 |
fixedRanking -> higher = texgeti32 (text); |
| 237 |
} catch (MelderError) { |
| 238 |
Melder_throw (U"Trying to read the higher of constraint pair ", irank, U"."); |
| 239 |
} |
| 240 |
try { |
| 241 |
fixedRanking -> lower = texgeti32 (text); |
| 242 |
} catch (MelderError) { |
| 243 |
Melder_throw (U"Trying to read the lower of constraint pair ", irank, U"."); |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
try { |
| 248 |
our numberOfTableaus = texgeti32 (text); |
| 249 |
} catch (MelderError) { |
| 250 |
Melder_throw (U"Trying to read number of tableaus."); |
| 251 |
} |
| 252 |
if (our numberOfTableaus < 1) |
| 253 |
Melder_throw (U"No tableaus."); |
| 254 |
our tableaus = newvectorzero <structOTGrammarTableau> (numberOfTableaus); |
| 255 |
for (integer itab = 1; itab <= our numberOfTableaus; itab ++) { |
| 256 |
OTGrammarTableau tableau = & our tableaus [itab]; |
| 257 |
try { |
| 258 |
tableau -> input = texgetw16 (text); |
| 259 |
} catch (MelderError) { |
| 260 |
Melder_throw (U"Trying to read input of tableau ", itab, U"."); |
| 261 |
} |
| 262 |
try { |
| 263 |
tableau -> numberOfCandidates = texgeti32 (text); |
| 264 |
} catch (MelderError) { |
| 265 |
Melder_throw (U"Trying to read number of candidates of tableau ", itab, U"."); |
| 266 |
} |
| 267 |
Melder_require (tableau -> numberOfCandidates > 0, |
| 268 |
U"No candidates in tableau ", itab, |
| 269 |
U" (input: ", tableau -> input.get(), U")" |
| 270 |
U" in line ", MelderReadText_getLineNumber (text), |
| 271 |
itab == 1 ? U"." : U", or perhaps wrong number of candidates for input «", |
| 272 |
itab == 1 ? nullptr : our tableaus [itab - 1]. input.get(), |
| 273 |
itab == 1 ? nullptr : U"»." |
| 274 |
); |
| 275 |
tableau -> candidates = newvectorzero <structOTGrammarCandidate> (tableau -> numberOfCandidates); |
| 276 |
for (integer icand = 1; icand <= tableau -> numberOfCandidates; icand ++) { |
| 277 |
OTGrammarCandidate candidate = & tableau -> candidates [icand]; |
| 278 |
try { |
| 279 |
candidate -> output = texgetw16 (text); |
| 280 |
} catch (MelderError) { |
| 281 |
Melder_throw (U"Trying to read candidate ", icand, U" of tableau ", itab, |
| 282 |
U" (input: ", tableau -> input.get(), U") in line ", MelderReadText_getLineNumber (text), U"."); |
| 283 |
} |
| 284 |
candidate -> numberOfConstraints = numberOfConstraints; // redundancy, needed for writing binary |
| 285 |
candidate -> marks = zero_INTVEC (candidate -> numberOfConstraints); |
| 286 |
for (integer icons = 1; icons <= candidate -> numberOfConstraints; icons ++) { |
| 287 |
try { |
| 288 |
candidate -> marks [icons] = texgeti16 (text); |
| 289 |
} catch (MelderError) { |
| 290 |
Melder_throw |
| 291 |
(U"Trying to read number of violations of constraint ", icons, |
| 292 |
U" (", our constraints [icons]. name.get(), U")" |
| 293 |
U" of candidate ", icand, |
| 294 |
U" (", candidate -> output.get(), U")" |
| 295 |
U" of tableau ", itab, |
| 296 |
U" (input: ", tableau -> input.get(), U")" |
| 297 |
U" in line ", MelderReadText_getLineNumber (text), U"."); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
OTGrammar_checkIndex (this); |
| 303 |
} |
| 304 |
|
| 305 |
Thing_implement (OTGrammar, Daata, 2); |
| 306 |
|
| 307 |
Thing_implement (OTHistory, TableOfReal, 0); |
| 308 |
|
| 309 |
void OTGrammar_sort (OTGrammar me) { |
| 310 |
std::sort (my index.begin(), my index.end(), |
| 311 |
[me] (integer icons, integer jcons) { |
| 312 |
OTGrammarConstraint ci = & my constraints [icons], cj = & my constraints [jcons]; |
| 313 |
/* |
| 314 |
Sort primarily by disharmony. |
| 315 |
*/ |
| 316 |
if (ci -> disharmony > cj -> disharmony) // already in descending order? |
| 317 |
return true; // yes, already in descending order |
| 318 |
if (ci -> disharmony < cj -> disharmony) |
| 319 |
return false; |
| 320 |
/* |
| 321 |
Tied constraints are sorted alphabetically. |
| 322 |
*/ |
| 323 |
return str32cmp (my constraints [icons]. name.get(), my constraints [jcons]. name.get()) < 0; |
| 324 |
} |
| 325 |
); |
| 326 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 327 |
OTGrammarConstraint constraint = & my constraints [my index [icons]]; |
| 328 |
constraint -> tiedToTheLeft = ( icons > 1 && |
| 329 |
my constraints [my index [icons - 1]]. disharmony == constraint -> disharmony ); |
| 330 |
constraint -> tiedToTheRight = ( icons < my numberOfConstraints && |
| 331 |
my constraints [my index [icons + 1]]. disharmony == constraint -> disharmony ); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
void OTGrammar_newDisharmonies (OTGrammar me, double spreading) { |
| 336 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 337 |
OTGrammarConstraint constraint = & my constraints [icons]; |
| 338 |
constraint -> disharmony = constraint -> ranking + NUMrandomGauss (0, spreading) |
| 339 |
/*NUMrandomUniform (-spreading, spreading)*/; |
| 340 |
} |
| 341 |
OTGrammar_sort (me); |
| 342 |
} |
| 343 |
|
| 344 |
integer OTGrammar_getTableau (constOTGrammar me, conststring32 input) { |
| 345 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) |
| 346 |
if (str32equ (my tableaus [itab]. input.get(), input)) |
| 347 |
return itab; |
| 348 |
Melder_throw (U"Input \"", input, U"\" not in list of tableaus."); |
| 349 |
} |
| 350 |
|
| 351 |
static void _OTGrammar_fillInHarmonies (OTGrammar me, integer itab) { |
| 352 |
if (my decisionStrategy == kOTGrammar_decisionStrategy::OPTIMALITY_THEORY) |
| 353 |
return; |
| 354 |
OTGrammarTableau tableau = & my tableaus [itab]; |
| 355 |
for (integer icand = 1; icand <= tableau -> numberOfCandidates; icand ++) { |
| 356 |
OTGrammarCandidate candidate = & tableau -> candidates [icand]; |
| 357 |
INTVEC marks = candidate -> marks.get(); |
| 358 |
longdouble disharmony = 0.0; |
| 359 |
if (my decisionStrategy == kOTGrammar_decisionStrategy::HARMONIC_GRAMMAR || |
| 360 |
my decisionStrategy == kOTGrammar_decisionStrategy::MAXIMUM_ENTROPY) |
| 361 |
{ |
| 362 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 363 |
disharmony += my constraints [icons]. disharmony * marks [icons]; |
| 364 |
} else if (my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_HG || |
| 365 |
my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_MAXIMUM_ENTROPY) |
| 366 |
{ |
| 367 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 368 |
disharmony += exp (my constraints [icons]. disharmony) * marks [icons]; |
| 369 |
} else if (my decisionStrategy == kOTGrammar_decisionStrategy::LINEAR_OT || my decisionStrategy == kOTGrammar_decisionStrategy::NONNEGATIVE_MAXIMUM_ENTROPY) { |
| 370 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 371 |
if (my constraints [icons]. disharmony > 0.0) |
| 372 |
disharmony += my constraints [icons]. disharmony * marks [icons]; |
| 373 |
} else if (my decisionStrategy == kOTGrammar_decisionStrategy::POSITIVE_HG) { |
| 374 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 375 |
const double constraintDisharmony = std::max (my constraints [icons]. disharmony, 1.0); |
| 376 |
disharmony += constraintDisharmony * marks [icons]; |
| 377 |
} |
| 378 |
} else { |
| 379 |
Melder_crash (U"_OTGrammar_fillInHarmonies: unimplemented decision strategy."); |
| 380 |
} |
| 381 |
candidate -> harmony = - (double) disharmony; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
int OTGrammar_compareCandidates (OTGrammar me, integer itab1, integer icand1, integer itab2, integer icand2) { |
| 386 |
INTVEC marks1 = my tableaus [itab1]. candidates [icand1]. marks.get(); |
| 387 |
INTVEC marks2 = my tableaus [itab2]. candidates [icand2]. marks.get(); |
| 388 |
if (my decisionStrategy == kOTGrammar_decisionStrategy::OPTIMALITY_THEORY) { |
| 389 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 390 |
integer numberOfMarks1 = marks1 [my index [icons]]; |
| 391 |
integer numberOfMarks2 = marks2 [my index [icons]]; |
| 392 |
/* |
| 393 |
Count tied constraints as one. |
| 394 |
*/ |
| 395 |
while (my constraints [my index [icons]]. tiedToTheRight) { |
| 396 |
icons ++; |
| 397 |
numberOfMarks1 += marks1 [my index [icons]]; |
| 398 |
numberOfMarks2 += marks2 [my index [icons]]; |
| 399 |
} |
| 400 |
if (numberOfMarks1 < numberOfMarks2) |
| 401 |
return -1; // candidate 1 is better than candidate 2 |
| 402 |
if (numberOfMarks1 > numberOfMarks2) |
| 403 |
return +1; // candidate 2 is better than candidate 1 |
| 404 |
} |
| 405 |
/* |
| 406 |
If we arrive here, none of the comparisons found a difference |
| 407 |
between the two candidates. Hence, they are equally good. |
| 408 |
*/ |
| 409 |
return 0; |
| 410 |
} else if (my decisionStrategy == kOTGrammar_decisionStrategy::HARMONIC_GRAMMAR || |
| 411 |
my decisionStrategy == kOTGrammar_decisionStrategy::MAXIMUM_ENTROPY) |
| 412 |
{ |
| 413 |
double disharmony1 = 0.0, disharmony2 = 0.0; |
| 414 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 415 |
disharmony1 += my constraints [icons]. disharmony * marks1 [icons]; |
| 416 |
disharmony2 += my constraints [icons]. disharmony * marks2 [icons]; |
| 417 |
} |
| 418 |
if (disharmony1 < disharmony2) |
| 419 |
return -1; // candidate 1 is better than candidate 2 |
| 420 |
if (disharmony1 > disharmony2) |
| 421 |
return +1; // candidate 2 is better than candidate 1 |
| 422 |
} else if (my decisionStrategy == kOTGrammar_decisionStrategy::LINEAR_OT || my decisionStrategy == kOTGrammar_decisionStrategy::NONNEGATIVE_MAXIMUM_ENTROPY) { |
| 423 |
double disharmony1 = 0.0, disharmony2 = 0.0; |
| 424 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 425 |
if (my constraints [icons]. disharmony > 0.0) { |
| 426 |
disharmony1 += my constraints [icons]. disharmony * marks1 [icons]; |
| 427 |
disharmony2 += my constraints [icons]. disharmony * marks2 [icons]; |
| 428 |
} |
| 429 |
} |
| 430 |
if (disharmony1 < disharmony2) |
| 431 |
return -1; // candidate 1 is better than candidate 2 |
| 432 |
if (disharmony1 > disharmony2) |
| 433 |
return +1; // candidate 2 is better than candidate 1 |
| 434 |
} else if (my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_HG || |
| 435 |
my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_MAXIMUM_ENTROPY) |
| 436 |
{ |
| 437 |
double disharmony1 = 0.0, disharmony2 = 0.0; |
| 438 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 439 |
disharmony1 += exp (my constraints [icons]. disharmony) * marks1 [icons]; |
| 440 |
disharmony2 += exp (my constraints [icons]. disharmony) * marks2 [icons]; |
| 441 |
} |
| 442 |
if (disharmony1 < disharmony2) |
| 443 |
return -1; // candidate 1 is better than candidate 2 |
| 444 |
if (disharmony1 > disharmony2) |
| 445 |
return +1; // candidate 2 is better than candidate 1 |
| 446 |
} else if (my decisionStrategy == kOTGrammar_decisionStrategy::POSITIVE_HG) { |
| 447 |
double disharmony1 = 0.0, disharmony2 = 0.0; |
| 448 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 449 |
double constraintDisharmony = std::max (my constraints [icons]. disharmony, 1.0); |
| 450 |
disharmony1 += constraintDisharmony * marks1 [icons]; |
| 451 |
disharmony2 += constraintDisharmony * marks2 [icons]; |
| 452 |
} |
| 453 |
if (disharmony1 < disharmony2) |
| 454 |
return -1; // candidate 1 is better than candidate 2 |
| 455 |
if (disharmony1 > disharmony2) |
| 456 |
return +1; // candidate 2 is better than candidate 1 |
| 457 |
} else |
| 458 |
Melder_crash (U"Unimplemented decision strategy."); |
| 459 |
return 0; // the two total disharmonies are equal |
| 460 |
} |
| 461 |
|
| 462 |
static void _OTGrammar_fillInProbabilities (OTGrammar me, integer itab) { |
| 463 |
OTGrammarTableau tableau = & my tableaus [itab]; |
| 464 |
double maximumHarmony = tableau -> candidates [1]. harmony; |
| 465 |
for (integer icand = 2; icand <= tableau -> numberOfCandidates; icand ++) { |
| 466 |
OTGrammarCandidate candidate = & tableau -> candidates [icand]; |
| 467 |
if (candidate -> harmony > maximumHarmony) |
| 468 |
maximumHarmony = candidate -> harmony; |
| 469 |
} |
| 470 |
for (integer icand = 1; icand <= tableau -> numberOfCandidates; icand ++) { |
| 471 |
OTGrammarCandidate candidate = & tableau -> candidates [icand]; |
| 472 |
candidate -> probability = exp (candidate -> harmony - maximumHarmony); |
| 473 |
Melder_assert (candidate -> probability >= 0.0 && candidate -> probability <= 1.0); |
| 474 |
} |
| 475 |
longdouble sumOfProbabilities = 0.0; |
| 476 |
for (integer icand = 1; icand <= tableau -> numberOfCandidates; icand ++) { |
| 477 |
OTGrammarCandidate candidate = & tableau -> candidates [icand]; |
| 478 |
sumOfProbabilities += candidate -> probability; |
| 479 |
} |
| 480 |
Melder_assert (sumOfProbabilities > 0.0); // because at least one of them is 1.0 |
| 481 |
for (integer icand = 1; icand <= tableau -> numberOfCandidates; icand ++) { |
| 482 |
OTGrammarCandidate candidate = & tableau -> candidates [icand]; |
| 483 |
candidate -> probability /= double (sumOfProbabilities); |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
integer OTGrammar_getWinner (OTGrammar me, integer itab) { |
| 488 |
integer icand_best = 1; |
| 489 |
if (my decisionStrategy == kOTGrammar_decisionStrategy::MAXIMUM_ENTROPY || |
| 490 |
my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_MAXIMUM_ENTROPY || |
| 491 |
my decisionStrategy == kOTGrammar_decisionStrategy::NONNEGATIVE_MAXIMUM_ENTROPY) |
| 492 |
{ |
| 493 |
_OTGrammar_fillInHarmonies (me, itab); |
| 494 |
_OTGrammar_fillInProbabilities (me, itab); |
| 495 |
double cutOff = NUMrandomUniform (0.0, 1.0); |
| 496 |
longdouble sumOfProbabilities = 0.0; |
| 497 |
for (integer icand = 1; icand <= my tableaus [itab]. numberOfCandidates; icand ++) { |
| 498 |
sumOfProbabilities += my tableaus [itab]. candidates [icand]. probability; |
| 499 |
if (sumOfProbabilities > cutOff) { |
| 500 |
icand_best = icand; |
| 501 |
break; |
| 502 |
} |
| 503 |
} |
| 504 |
} else { |
| 505 |
integer numberOfBestCandidates = 1; |
| 506 |
for (integer icand = 2; icand <= my tableaus [itab]. numberOfCandidates; icand ++) { |
| 507 |
int comparison = OTGrammar_compareCandidates (me, itab, icand, itab, icand_best); |
| 508 |
if (comparison == -1) { |
| 509 |
icand_best = icand; // the current candidate is the unique best candidate found so far |
| 510 |
numberOfBestCandidates = 1; |
| 511 |
} else if (comparison == 0) { |
| 512 |
numberOfBestCandidates += 1; // the current candidate is equally good as the best found before |
| 513 |
/* |
| 514 |
Give all candidates that are equally good an equal chance to become the winner. |
| 515 |
*/ |
| 516 |
if (Melder_debug == 41) { |
| 517 |
icand_best = icand_best; // keep first |
| 518 |
} else if (Melder_debug == 42) { |
| 519 |
icand_best = icand; // take last |
| 520 |
} else if (NUMrandomUniform (0.0, numberOfBestCandidates) < 1.0) { // default: take random |
| 521 |
icand_best = icand; |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
return icand_best; |
| 527 |
} |
| 528 |
|
| 529 |
integer OTGrammar_getNumberOfOptimalCandidates (OTGrammar me, integer itab) { |
| 530 |
if (my decisionStrategy == kOTGrammar_decisionStrategy::MAXIMUM_ENTROPY || |
| 531 |
my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_MAXIMUM_ENTROPY || |
| 532 |
my decisionStrategy == kOTGrammar_decisionStrategy::NONNEGATIVE_MAXIMUM_ENTROPY) return 1; |
| 533 |
integer icand_best = 1, numberOfBestCandidates = 1; |
| 534 |
for (integer icand = 2; icand <= my tableaus [itab]. numberOfCandidates; icand ++) { |
| 535 |
int comparison = OTGrammar_compareCandidates (me, itab, icand, itab, icand_best); |
| 536 |
if (comparison == -1) { |
| 537 |
icand_best = icand; // the current candidate is the best candidate found so far |
| 538 |
numberOfBestCandidates = 1; |
| 539 |
} else if (comparison == 0) { |
| 540 |
numberOfBestCandidates += 1; // the current candidate is equally good as the best found before |
| 541 |
} |
| 542 |
} |
| 543 |
return numberOfBestCandidates; |
| 544 |
} |
| 545 |
|
| 546 |
bool OTGrammar_isCandidateGrammatical (OTGrammar me, integer itab, integer icand) { |
| 547 |
for (integer jcand = 1; jcand <= my tableaus [itab]. numberOfCandidates; jcand ++) |
| 548 |
if (jcand != icand && OTGrammar_compareCandidates (me, itab, jcand, itab, icand) < 0) |
| 549 |
return false; |
| 550 |
return true; |
| 551 |
} |
| 552 |
|
| 553 |
bool OTGrammar_isCandidateSinglyGrammatical (OTGrammar me, integer itab, integer icand) { |
| 554 |
for (integer jcand = 1; jcand <= my tableaus [itab]. numberOfCandidates; jcand ++) |
| 555 |
if (jcand != icand && OTGrammar_compareCandidates (me, itab, jcand, itab, icand) <= 0) |
| 556 |
return false; |
| 557 |
return true; |
| 558 |
} |
| 559 |
|
| 560 |
void OTGrammar_getInterpretiveParse (OTGrammar me, conststring32 partialOutput, integer *out_bestTableau, integer *out_bestCandidate) { |
| 561 |
try { |
| 562 |
integer itab_best = 0, icand_best = 0, numberOfBestCandidates = 0; |
| 563 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) { |
| 564 |
OTGrammarTableau tableau = & my tableaus [itab]; |
| 565 |
for (integer icand = 1; icand <= tableau -> numberOfCandidates; icand ++) { |
| 566 |
OTGrammarCandidate cand = & tableau -> candidates [icand]; |
| 567 |
if (str32str (cand -> output.get(), partialOutput)) { // T&S' idea of surface->overt mapping |
| 568 |
if (itab_best == 0) { |
| 569 |
itab_best = itab; // the first compatible input/output pair found is the first guess for the best candidate |
| 570 |
icand_best = icand; |
| 571 |
numberOfBestCandidates = 1; |
| 572 |
} else { |
| 573 |
int comparison = OTGrammar_compareCandidates (me, itab, icand, itab_best, icand_best); |
| 574 |
if (comparison == -1) { |
| 575 |
itab_best = itab; // the current input/output pair is the best candidate found so far |
| 576 |
icand_best = icand; |
| 577 |
numberOfBestCandidates = 1; |
| 578 |
} else if (comparison == 0) { |
| 579 |
numberOfBestCandidates += 1; // the current input/output pair is equally good as the best found before |
| 580 |
/* |
| 581 |
* Give all candidates that are equally good an equal chance to become the winner. |
| 582 |
*/ |
| 583 |
if (Melder_debug == 41) { |
| 584 |
itab_best = itab_best; |
| 585 |
icand_best = icand_best; // keep first |
| 586 |
} else if (Melder_debug == 42) { |
| 587 |
itab_best = itab; |
| 588 |
icand_best = icand; // take last |
| 589 |
} else if (NUMrandomUniform (0.0, numberOfBestCandidates) < 1.0) { // default: take random |
| 590 |
itab_best = itab; |
| 591 |
icand_best = icand; |
| 592 |
} |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
if (itab_best == 0) |
| 599 |
Melder_throw (U"The partial output \"", partialOutput, U"\" does not match any candidate for any input form."); |
| 600 |
if (out_bestTableau) |
| 601 |
*out_bestTableau = itab_best; |
| 602 |
if (out_bestCandidate) |
| 603 |
*out_bestCandidate = icand_best; |
| 604 |
} catch (MelderError) { |
| 605 |
Melder_throw (U"Interpretive parse not computed."); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
static void OTGrammar_getInterpretiveParse_opt (OTGrammar me, integer ipartialOutput, integer *out_bestTableau, integer *out_bestCandidate) { |
| 610 |
try { |
| 611 |
integer itab_best = 0, icand_best = 0, numberOfBestCandidates = 0; |
| 612 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) { |
| 613 |
OTGrammarTableau tableau = & my tableaus [itab]; |
| 614 |
for (integer icand = 1; icand <= tableau -> numberOfCandidates; icand ++) { |
| 615 |
OTGrammarCandidate cand = & tableau -> candidates [icand]; |
| 616 |
if (cand -> partialOutputMatches [ipartialOutput]) { // T&S' idea of surface->overt mapping |
| 617 |
if (itab_best == 0) { |
| 618 |
itab_best = itab; // the first compatible input/output pair found is the first guess for the best candidate |
| 619 |
icand_best = icand; |
| 620 |
numberOfBestCandidates = 1; |
| 621 |
} else { |
| 622 |
int comparison = OTGrammar_compareCandidates (me, itab, icand, itab_best, icand_best); |
| 623 |
if (comparison == -1) { |
| 624 |
itab_best = itab; // the current input/output pair is the best candidate found so far |
| 625 |
icand_best = icand; |
| 626 |
numberOfBestCandidates = 1; |
| 627 |
} else if (comparison == 0) { |
| 628 |
numberOfBestCandidates += 1; // the current input/output pair is equally good as the best found before |
| 629 |
/* |
| 630 |
* Give all candidates that are equally good an equal chance to become the winner. |
| 631 |
*/ |
| 632 |
if (Melder_debug == 41) { |
| 633 |
itab_best = itab_best; |
| 634 |
icand_best = icand_best; // keep first |
| 635 |
} else if (Melder_debug == 42) { |
| 636 |
itab_best = itab; |
| 637 |
icand_best = icand; // take last |
| 638 |
} else if (NUMrandomUniform (0.0, numberOfBestCandidates) < 1.0) { // default: take random |
| 639 |
itab_best = itab; |
| 640 |
icand_best = icand; |
| 641 |
} |
| 642 |
} |
| 643 |
} |
| 644 |
} |
| 645 |
} |
| 646 |
} |
| 647 |
Melder_assert (itab_best != 0); |
| 648 |
if (out_bestTableau) |
| 649 |
*out_bestTableau = itab_best; |
| 650 |
if (out_bestCandidate) |
| 651 |
*out_bestCandidate = icand_best; |
| 652 |
} catch (MelderError) { |
| 653 |
Melder_throw (U"Interpretive parse not computed."); |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
bool OTGrammar_isPartialOutputGrammatical (OTGrammar me, conststring32 partialOutput) { |
| 658 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) { |
| 659 |
OTGrammarTableau tableau = & my tableaus [itab]; |
| 660 |
for (integer icand = 1; icand <= tableau -> numberOfCandidates; icand ++) { |
| 661 |
if (str32str (tableau -> candidates [icand]. output.get(), partialOutput)) |
| 662 |
if (OTGrammar_isCandidateGrammatical (me, itab, icand)) |
| 663 |
return true; |
| 664 |
} |
| 665 |
} |
| 666 |
return false; |
| 667 |
} |
| 668 |
|
| 669 |
bool OTGrammar_areAllPartialOutputsGrammatical (OTGrammar me, Strings thee) { |
| 670 |
for (integer ioutput = 1; ioutput <= thy numberOfStrings; ioutput ++) { |
| 671 |
conststring32 partialOutput = thy strings [ioutput].get(); |
| 672 |
if (! OTGrammar_isPartialOutputGrammatical (me, partialOutput)) |
| 673 |
return false; |
| 674 |
} |
| 675 |
return true; |
| 676 |
} |
| 677 |
|
| 678 |
bool OTGrammar_isPartialOutputSinglyGrammatical (OTGrammar me, conststring32 partialOutput) { |
| 679 |
bool found = false; |
| 680 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) { |
| 681 |
OTGrammarTableau tableau = & my tableaus [itab]; |
| 682 |
for (integer icand = 1; icand <= tableau -> numberOfCandidates; icand ++) { |
| 683 |
if (str32str (tableau -> candidates [icand]. output.get(), partialOutput)) { |
| 684 |
if (OTGrammar_isCandidateGrammatical (me, itab, icand)) { |
| 685 |
found = true; |
| 686 |
/* |
| 687 |
All other grammatical candidates should match. |
| 688 |
*/ |
| 689 |
for (integer jcand = 1; jcand <= tableau -> numberOfCandidates; jcand ++) { |
| 690 |
if (OTGrammar_compareCandidates (me, itab, jcand, itab, icand) == 0) |
| 691 |
if (! str32str (tableau -> candidates [jcand]. output.get(), partialOutput)) |
| 692 |
return false; // partial output is multiply optimal |
| 693 |
} |
| 694 |
} |
| 695 |
} |
| 696 |
} |
| 697 |
} |
| 698 |
return found; |
| 699 |
} |
| 700 |
|
| 701 |
bool OTGrammar_areAllPartialOutputsSinglyGrammatical (OTGrammar me, Strings thee) { |
| 702 |
for (integer ioutput = 1; ioutput <= thy numberOfStrings; ioutput ++) { |
| 703 |
conststring32 partialOutput = thy strings [ioutput].get(); |
| 704 |
if (! OTGrammar_isPartialOutputSinglyGrammatical (me, partialOutput)) |
| 705 |
return false; |
| 706 |
} |
| 707 |
return true; |
| 708 |
} |
| 709 |
|
| 710 |
static integer OTGrammar_crucialCell (OTGrammar me, integer itab, integer icand, integer iwinner, integer numberOfOptimalCandidates) { |
| 711 |
OTGrammarTableau tableau = & my tableaus [itab]; |
| 712 |
if (tableau -> numberOfCandidates < 2) return 0; // if there is only one candidate, all cells can be greyed |
| 713 |
if (my decisionStrategy != kOTGrammar_decisionStrategy::OPTIMALITY_THEORY) return my numberOfConstraints; // nothing grey |
| 714 |
if (OTGrammar_compareCandidates (me, itab, icand, itab, iwinner) == 0) { // candidate equally good as winner? |
| 715 |
if (numberOfOptimalCandidates > 1) { |
| 716 |
/* All cells are important. */ |
| 717 |
} else { |
| 718 |
integer secondBest = 0; |
| 719 |
for (integer jcand = 1; jcand <= tableau -> numberOfCandidates; jcand ++) { |
| 720 |
if (OTGrammar_compareCandidates (me, itab, jcand, itab, iwinner) != 0) { // a non-optimal candidate? |
| 721 |
if (secondBest == 0) { |
| 722 |
secondBest = jcand; // first guess |
| 723 |
} else if (OTGrammar_compareCandidates (me, itab, jcand, itab, secondBest) < 0) { |
| 724 |
secondBest = jcand; // better guess |
| 725 |
} |
| 726 |
} |
| 727 |
} |
| 728 |
if (secondBest == 0) return 0; // if all candidates are equally good, all cells can be greyed |
| 729 |
return OTGrammar_crucialCell (me, itab, secondBest, iwinner, 1); |
| 730 |
} |
| 731 |
} else { |
| 732 |
const constINTVEC candidateMarks = tableau -> candidates [icand]. marks.get(); |
| 733 |
const constINTVEC winnerMarks = tableau -> candidates [iwinner]. marks.get(); |
| 734 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 735 |
integer numberOfCandidateMarks = candidateMarks [my index [icons]]; |
| 736 |
integer numberOfWinnerMarks = winnerMarks [my index [icons]]; |
| 737 |
while (my constraints [my index [icons]]. tiedToTheRight) { |
| 738 |
icons ++; |
| 739 |
numberOfCandidateMarks += candidateMarks [my index [icons]]; |
| 740 |
numberOfWinnerMarks += winnerMarks [my index [icons]]; |
| 741 |
} |
| 742 |
if (numberOfCandidateMarks > numberOfWinnerMarks) |
| 743 |
return icons; |
| 744 |
} |
| 745 |
} |
| 746 |
return my numberOfConstraints; // nothing grey |
| 747 |
} |
| 748 |
|
| 749 |
static double OTGrammar_constraintWidth (Graphics g, conststring32 name) { |
| 750 |
char32 text [100]; |
| 751 |
Melder_sprint (text,100, name); |
| 752 |
char32 *newLine = str32chr (text, U'\n'); |
| 753 |
if (newLine) { |
| 754 |
double firstWidth, secondWidth; |
| 755 |
*newLine = U'\0'; |
| 756 |
firstWidth = Graphics_textWidth (g, text); |
| 757 |
secondWidth = Graphics_textWidth (g, newLine + 1); |
| 758 |
return firstWidth > secondWidth ? firstWidth : secondWidth; |
| 759 |
} |
| 760 |
return Graphics_textWidth (g, text); |
| 761 |
} |
| 762 |
|
| 763 |
void OTGrammar_drawTableau (OTGrammar me, Graphics g, bool vertical, conststring32 input) { |
| 764 |
try { |
| 765 |
const double fontSize = Graphics_inqFontSize (g); |
| 766 |
MelderColour colour = Graphics_inqColour (g); |
| 767 |
const integer itab = OTGrammar_getTableau (me, input); |
| 768 |
_OTGrammar_fillInHarmonies (me, itab); |
| 769 |
const integer winner = OTGrammar_getWinner (me, itab); |
| 770 |
|
| 771 |
Graphics_setWindow (g, 0.0, 1.0, 0.0, 1.0); |
| 772 |
const double margin = Graphics_dxMMtoWC (g, 1.0); |
| 773 |
const double fingerWidth = Graphics_dxMMtoWC (g, 7.0) * fontSize / 12.0; |
| 774 |
const double doubleLineDx = Graphics_dxMMtoWC (g, 0.9); |
| 775 |
const double doubleLineDy = Graphics_dyMMtoWC (g, 0.9); |
| 776 |
const double rowHeight = Graphics_dyMMtoWC (g, 1.5 * fontSize * 25.4 / 72); |
| 777 |
const double descent = rowHeight * 0.5; |
| 778 |
const double worldAspectRatio = Graphics_dyMMtoWC (g, 1.0) / Graphics_dxMMtoWC (g, 1.0); // because Graphics_textWidth measures in the x direction only |
| 779 |
/* |
| 780 |
Compute the height of the header row. |
| 781 |
*/ |
| 782 |
double headerHeight; |
| 783 |
if (vertical) { |
| 784 |
headerHeight = 0.0; |
| 785 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 786 |
const OTGrammarConstraint constraint = & my constraints [icons]; |
| 787 |
const double constraintTextWidth = Graphics_textWidth (g, constraint -> name.get()); |
| 788 |
if (constraintTextWidth > headerHeight) |
| 789 |
headerHeight = constraintTextWidth; |
| 790 |
} |
| 791 |
headerHeight += margin * 2; |
| 792 |
headerHeight *= worldAspectRatio; |
| 793 |
} else { |
| 794 |
headerHeight = rowHeight; |
| 795 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 796 |
OTGrammarConstraint constraint = & my constraints [icons]; |
| 797 |
if (str32chr (constraint -> name.get(), U'\n')) { |
| 798 |
headerHeight *= 1.6; |
| 799 |
break; |
| 800 |
} |
| 801 |
} |
| 802 |
} |
| 803 |
/* |
| 804 |
Compute longest candidate string. |
| 805 |
Also count the number of optimal candidates (if there are more than one, the fingers will be drawn in red). |
| 806 |
*/ |
| 807 |
double candWidth = Graphics_textWidth (g, input); |
| 808 |
OTGrammarTableau tableau = & my tableaus [itab]; |
| 809 |
integer numberOfOptimalCandidates = 0; |
| 810 |
for (integer icand = 1; icand <= tableau -> numberOfCandidates; icand ++) { |
| 811 |
double width = Graphics_textWidth (g, tableau -> candidates [icand]. output.get()); |
| 812 |
if (OTGrammar_compareCandidates (me, itab, icand, itab, winner) == 0) { |
| 813 |
width += fingerWidth; |
| 814 |
numberOfOptimalCandidates ++; |
| 815 |
} |
| 816 |
if (width > candWidth) |
| 817 |
candWidth = width; |
| 818 |
} |
| 819 |
candWidth += margin * 3; |
| 820 |
/* |
| 821 |
Compute tableau width. |
| 822 |
*/ |
| 823 |
double tableauWidth = candWidth + doubleLineDx; |
| 824 |
if (vertical) { |
| 825 |
tableauWidth += rowHeight * my numberOfConstraints / worldAspectRatio; |
| 826 |
} else { |
| 827 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 828 |
OTGrammarConstraint constraint = & my constraints [icons]; |
| 829 |
tableauWidth += OTGrammar_constraintWidth (g, constraint -> name.get()); |
| 830 |
} |
| 831 |
tableauWidth += margin * 2 * my numberOfConstraints; |
| 832 |
} |
| 833 |
/* |
| 834 |
Draw box. |
| 835 |
*/ |
| 836 |
double x = doubleLineDx; // left side of tableau |
| 837 |
double y = 1.0 - doubleLineDy; |
| 838 |
Graphics_rectangle (g, x, x + tableauWidth, |
| 839 |
y - headerHeight - tableau -> numberOfCandidates * rowHeight - doubleLineDy, y); |
| 840 |
/* |
| 841 |
Draw input. |
| 842 |
*/ |
| 843 |
y -= headerHeight; |
| 844 |
Graphics_setTextAlignment (g, Graphics_CENTRE, Graphics_HALF); |
| 845 |
Graphics_text (g, x + 0.5 * candWidth, y + 0.5 * headerHeight, input); |
| 846 |
Graphics_rectangle (g, x, x + candWidth, y, y + headerHeight); |
| 847 |
/* |
| 848 |
Draw constraint names. |
| 849 |
*/ |
| 850 |
x += candWidth + doubleLineDx; |
| 851 |
if (vertical) |
| 852 |
Graphics_setTextRotation (g, 90.0); |
| 853 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 854 |
const OTGrammarConstraint constraint = & my constraints [my index [icons]]; |
| 855 |
const double width = vertical ? rowHeight / worldAspectRatio : OTGrammar_constraintWidth (g, constraint -> name.get()) + margin * 2; |
| 856 |
if (str32chr (constraint -> name.get(), U'\n') && ! vertical) { |
| 857 |
autoMelderString text; |
| 858 |
MelderString_copy (& text, constraint -> name.get()); |
| 859 |
char32 *newLine = str32chr (text.string, U'\n'); |
| 860 |
*newLine = U'\0'; |
| 861 |
Graphics_setTextAlignment (g, Graphics_CENTRE, Graphics_TOP); |
| 862 |
Graphics_text (g, x + 0.5 * width, y + headerHeight, text.string); |
| 863 |
Graphics_setTextAlignment (g, Graphics_CENTRE, Graphics_BOTTOM); |
| 864 |
Graphics_text (g, x + 0.5 * width, y, newLine + 1); |
| 865 |
} else if (vertical) { |
| 866 |
Graphics_setTextAlignment (g, Graphics_LEFT, Graphics_HALF); |
| 867 |
Graphics_text (g, x + 0.5 * width, y + margin, constraint -> name.get()); |
| 868 |
} else { |
| 869 |
Graphics_setTextAlignment (g, Graphics_CENTRE, Graphics_HALF); |
| 870 |
Graphics_text (g, x + 0.5 * width, y + 0.5 * headerHeight, constraint -> name.get()); |
| 871 |
} |
| 872 |
if (constraint -> tiedToTheLeft) |
| 873 |
Graphics_setLineType (g, Graphics_DOTTED); |
| 874 |
Graphics_line (g, x, y, x, y + headerHeight); |
| 875 |
Graphics_setLineType (g, Graphics_DRAWN); |
| 876 |
Graphics_line (g, x, y, x + width, y); |
| 877 |
x += width; |
| 878 |
} |
| 879 |
if (vertical) |
| 880 |
Graphics_setTextRotation (g, 0.0); |
| 881 |
/* |
| 882 |
Draw candidates. |
| 883 |
*/ |
| 884 |
y -= doubleLineDy; |
| 885 |
for (integer icand = 1; icand <= tableau -> numberOfCandidates; icand ++) { |
| 886 |
integer crucialCell = OTGrammar_crucialCell (me, itab, icand, winner, numberOfOptimalCandidates); |
| 887 |
bool candidateIsOptimal = OTGrammar_compareCandidates (me, itab, icand, itab, winner) == 0; |
| 888 |
/* |
| 889 |
Draw candidate transcription. |
| 890 |
*/ |
| 891 |
x = doubleLineDx; |
| 892 |
y -= rowHeight; |
| 893 |
Graphics_setTextAlignment (g, Graphics_RIGHT, Graphics_HALF); |
| 894 |
Graphics_text (g, x + candWidth - margin, y + descent, tableau -> candidates [icand]. output.get()); |
| 895 |
if (candidateIsOptimal) { |
| 896 |
Graphics_setTextAlignment (g, Graphics_LEFT, Graphics_HALF); |
| 897 |
Graphics_setFontSize (g, 1.5 * fontSize); |
| 898 |
if (numberOfOptimalCandidates > 1) Graphics_setColour (g, Melder_RED); |
| 899 |
Graphics_text (g, x + margin, y + descent - Graphics_dyMMtoWC (g, 1.0) * fontSize / 12.0, U"☞"); |
| 900 |
Graphics_setColour (g, colour); |
| 901 |
Graphics_setFontSize (g, fontSize); |
| 902 |
} |
| 903 |
Graphics_rectangle (g, x, x + candWidth, y, y + rowHeight); |
| 904 |
/* |
| 905 |
Draw grey cell backgrounds. |
| 906 |
*/ |
| 907 |
x = candWidth + 2 * doubleLineDx; |
| 908 |
Graphics_setGrey (g, 0.9); |
| 909 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 910 |
const integer index = my index [icons]; |
| 911 |
OTGrammarConstraint constraint = & my constraints [index]; |
| 912 |
const double width = ( vertical ? rowHeight / worldAspectRatio : |
| 913 |
OTGrammar_constraintWidth (g, constraint -> name.get()) + margin * 2 ); |
| 914 |
if (icons > crucialCell) |
| 915 |
Graphics_fillRectangle (g, x, x + width, y, y + rowHeight); |
| 916 |
x += width; |
| 917 |
} |
| 918 |
Graphics_setColour (g, colour); |
| 919 |
/* |
| 920 |
Draw cell marks. |
| 921 |
*/ |
| 922 |
x = candWidth + 2 * doubleLineDx; |
| 923 |
Graphics_setTextAlignment (g, Graphics_CENTRE, Graphics_HALF); |
| 924 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 925 |
const integer index = my index [icons]; |
| 926 |
OTGrammarConstraint constraint = & my constraints [index]; |
| 927 |
const double width = vertical ? rowHeight / worldAspectRatio : OTGrammar_constraintWidth (g, constraint -> name.get()) + margin * 2; |
| 928 |
static MelderString markString; |
| 929 |
MelderString_empty (& markString); |
| 930 |
if (my decisionStrategy == kOTGrammar_decisionStrategy::OPTIMALITY_THEORY) { |
| 931 |
/* |
| 932 |
An exclamation mark can be drawn in this cell only if all of the following conditions are met: |
| 933 |
1. the candidate is not optimal; |
| 934 |
2. the constraint is not tied; |
| 935 |
3. this is the crucial cell, i.e. the cells after it are drawn in grey. |
| 936 |
*/ |
| 937 |
if (icons == crucialCell && ! candidateIsOptimal && ! constraint -> tiedToTheLeft && ! constraint -> tiedToTheRight) { |
| 938 |
const integer winnerMarks = tableau -> candidates [winner]. marks [index]; |
| 939 |
for (integer imark = 1; imark <= winnerMarks + 1; imark ++) |
| 940 |
MelderString_appendCharacter (& markString, U'*'); |
| 941 |
for (integer imark = tableau -> candidates [icand]. marks [index]; imark < 0; imark ++) |
| 942 |
MelderString_appendCharacter (& markString, U'+'); |
| 943 |
MelderString_appendCharacter (& markString, U'!'); |
| 944 |
for (integer imark = winnerMarks + 2; imark <= tableau -> candidates [icand]. marks [index]; imark ++) |
| 945 |
MelderString_appendCharacter (& markString, U'*'); |
| 946 |
} else { |
| 947 |
if (! candidateIsOptimal && (constraint -> tiedToTheLeft || constraint -> tiedToTheRight) && |
| 948 |
crucialCell >= 1 && constraint -> disharmony == my constraints [my index [crucialCell]]. disharmony) |
| 949 |
{ |
| 950 |
Graphics_setColour (g, Melder_RED); |
| 951 |
} |
| 952 |
for (integer imark = 1; imark <= tableau -> candidates [icand]. marks [index]; imark ++) |
| 953 |
MelderString_appendCharacter (& markString, U'*'); |
| 954 |
for (integer imark = tableau -> candidates [icand]. marks [index]; imark < 0; imark ++) |
| 955 |
MelderString_appendCharacter (& markString, U'+'); |
| 956 |
} |
| 957 |
} else { |
| 958 |
for (integer imark = 1; imark <= tableau -> candidates [icand]. marks [index]; imark ++) |
| 959 |
MelderString_appendCharacter (& markString, U'*'); |
| 960 |
for (integer imark = tableau -> candidates [icand]. marks [index]; imark < 0; imark ++) |
| 961 |
MelderString_appendCharacter (& markString, U'+'); |
| 962 |
} |
| 963 |
Graphics_text (g, x + 0.5 * width, y + descent, markString.string); |
| 964 |
Graphics_setColour (g, colour); |
| 965 |
if (constraint -> tiedToTheLeft) |
| 966 |
Graphics_setLineType (g, Graphics_DOTTED); |
| 967 |
Graphics_line (g, x, y, x, y + rowHeight); |
| 968 |
Graphics_setLineType (g, Graphics_DRAWN); |
| 969 |
Graphics_line (g, x, y + rowHeight, x + width, y + rowHeight); |
| 970 |
x += width; |
| 971 |
} |
| 972 |
/* |
| 973 |
Draw harmony. |
| 974 |
*/ |
| 975 |
if (my decisionStrategy != kOTGrammar_decisionStrategy::OPTIMALITY_THEORY) { |
| 976 |
Graphics_setTextAlignment (g, Graphics_LEFT, Graphics_HALF); |
| 977 |
const double value = tableau -> candidates [icand]. harmony; |
| 978 |
if (my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_HG || |
| 979 |
my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_MAXIMUM_ENTROPY) |
| 980 |
{ |
| 981 |
Graphics_text (g, x, y + descent, Melder_graphicalHalf (value)); |
| 982 |
} else { |
| 983 |
Graphics_text (g, x, y + descent, Melder_fixed (value, 3)); |
| 984 |
} |
| 985 |
} |
| 986 |
} |
| 987 |
/* |
| 988 |
Draw box. |
| 989 |
*/ |
| 990 |
x = doubleLineDx; // left side of tableau |
| 991 |
y = 1.0 - doubleLineDy; |
| 992 |
Graphics_rectangle (g, x, x + tableauWidth, |
| 993 |
y - headerHeight - tableau -> numberOfCandidates * rowHeight - doubleLineDy, y); |
| 994 |
} catch (MelderError) { |
| 995 |
Melder_throw (me, U": tableau not drawn."); |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
autoStrings OTGrammar_generateInputs (OTGrammar me, integer numberOfTrials) { |
| 1000 |
try { |
| 1001 |
autoStrings thee = Thing_new (Strings); |
| 1002 |
thy strings = autoSTRVEC (thy numberOfStrings = numberOfTrials); |
| 1003 |
for (integer i = 1; i <= numberOfTrials; i ++) { |
| 1004 |
integer itab = NUMrandomInteger (1, my numberOfTableaus); |
| 1005 |
thy strings [i] = Melder_dup (my tableaus [itab]. input.get()); |
| 1006 |
} |
| 1007 |
return thee; |
| 1008 |
} catch (MelderError) { |
| 1009 |
Melder_throw (me, U": inputs not generated."); |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
autoStrings OTGrammar_getInputs (OTGrammar me) { |
| 1014 |
try { |
| 1015 |
autoStrings thee = Thing_new (Strings); |
| 1016 |
thy strings = autoSTRVEC (thy numberOfStrings = my numberOfTableaus); |
| 1017 |
for (integer i = 1; i <= my numberOfTableaus; i ++) |
| 1018 |
thy strings [i] = Melder_dup (my tableaus [i]. input.get()); |
| 1019 |
return thee; |
| 1020 |
} catch (MelderError) { |
| 1021 |
Melder_throw (me, U": inputs not gotten."); |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
autostring32 OTGrammar_inputToOutput (OTGrammar me, conststring32 input, double evaluationNoise) { |
| 1026 |
try { |
| 1027 |
OTGrammar_newDisharmonies (me, evaluationNoise); |
| 1028 |
integer itab = OTGrammar_getTableau (me, input); |
| 1029 |
integer winner = OTGrammar_getWinner (me, itab); |
| 1030 |
if (winner == 0) |
| 1031 |
Melder_throw (U"No winner"); |
| 1032 |
return Melder_dup (my tableaus [itab]. candidates [winner]. output.get()); |
| 1033 |
} catch (MelderError) { |
| 1034 |
Melder_throw (me, U": output not computed from input \"", input, U"\"."); |
| 1035 |
} |
| 1036 |
} |
| 1037 |
|
| 1038 |
autoStrings OTGrammar_inputsToOutputs (OTGrammar me, Strings inputs, double evaluationNoise) { |
| 1039 |
try { |
| 1040 |
autoStrings him = Thing_new (Strings); |
| 1041 |
integer n = inputs -> numberOfStrings; |
| 1042 |
his numberOfStrings = n; |
| 1043 |
his strings = autoSTRVEC (n); |
| 1044 |
for (integer i = 1; i <= n; i ++) |
| 1045 |
his strings [i] = OTGrammar_inputToOutput (me, inputs -> strings [i].get(), evaluationNoise); |
| 1046 |
return him; |
| 1047 |
} catch (MelderError) { |
| 1048 |
Melder_throw (me, U": outputs not computed."); |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
autoStrings OTGrammar_inputToOutputs (OTGrammar me, conststring32 input, integer n, double evaluationNoise) { |
| 1053 |
try { |
| 1054 |
autoStrings thee = Thing_new (Strings); |
| 1055 |
thy numberOfStrings = n; |
| 1056 |
thy strings = autoSTRVEC (n); |
| 1057 |
for (integer i = 1; i <= n; i ++) |
| 1058 |
thy strings [i] = OTGrammar_inputToOutput (me, input, evaluationNoise); |
| 1059 |
return thee; |
| 1060 |
} catch (MelderError) { |
| 1061 |
Melder_throw (me, U": output not computed."); |
| 1062 |
} |
| 1063 |
} |
| 1064 |
|
| 1065 |
autoDistributions OTGrammar_to_Distribution (OTGrammar me, integer trialsPerInput, double noise) { |
| 1066 |
try { |
| 1067 |
integer totalNumberOfOutputs = 0, nout = 0; |
| 1068 |
/* |
| 1069 |
Count the total number of outputs. |
| 1070 |
*/ |
| 1071 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) |
| 1072 |
totalNumberOfOutputs += my tableaus [itab]. numberOfCandidates; |
| 1073 |
/* |
| 1074 |
Create the distribution. One row for every output form. |
| 1075 |
*/ |
| 1076 |
autoDistributions thee = Distributions_create (totalNumberOfOutputs, 1); |
| 1077 |
/* |
| 1078 |
Measure every input form. |
| 1079 |
*/ |
| 1080 |
autoMelderProgress progress (U"OTGrammar: compute output distribution."); |
| 1081 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) { |
| 1082 |
OTGrammarTableau tableau = & my tableaus [itab]; |
| 1083 |
Melder_progress ((itab - 0.5) / my numberOfTableaus, U"Measuring input \"", tableau -> input.get(), U"\""); |
| 1084 |
/* |
| 1085 |
Set the row labels to the output strings. |
| 1086 |
*/ |
| 1087 |
for (integer icand = 1; icand <= tableau -> numberOfCandidates; icand ++) { |
| 1088 |
thy rowLabels [nout + icand] = Melder_dup ( |
| 1089 |
Melder_cat (tableau -> input.get(), U" \\-> ", tableau -> candidates [icand]. output.get()) |
| 1090 |
); |
| 1091 |
} |
| 1092 |
/* |
| 1093 |
Compute a number of outputs and store the results. |
| 1094 |
*/ |
| 1095 |
for (integer itrial = 1; itrial <= trialsPerInput; itrial ++) { |
| 1096 |
OTGrammar_newDisharmonies (me, noise); |
| 1097 |
integer iwinner = OTGrammar_getWinner (me, itab); |
| 1098 |
thy data [nout + iwinner] [1] += 1; |
| 1099 |
} |
| 1100 |
/* |
| 1101 |
Update the offset. |
| 1102 |
*/ |
| 1103 |
nout += tableau -> numberOfCandidates; |
| 1104 |
} |
| 1105 |
return thee; |
| 1106 |
} catch (MelderError) { |
| 1107 |
Melder_throw (me, U": output distribution not computed."); |
| 1108 |
} |
| 1109 |
} |
| 1110 |
|
| 1111 |
autoPairDistribution OTGrammar_to_PairDistribution (OTGrammar me, integer trialsPerInput, double noise) { |
| 1112 |
try { |
| 1113 |
integer nout = 0; |
| 1114 |
/* |
| 1115 |
Create the distribution. One row for every output form. |
| 1116 |
*/ |
| 1117 |
autoPairDistribution thee = PairDistribution_create (); |
| 1118 |
/* |
| 1119 |
Measure every input form. |
| 1120 |
*/ |
| 1121 |
autoMelderProgress progress (U"OTGrammar: compute output distribution."); |
| 1122 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) { |
| 1123 |
OTGrammarTableau tableau = & my tableaus [itab]; |
| 1124 |
Melder_progress ((itab - 0.5) / my numberOfTableaus, U"Measuring input \"", tableau -> input.get(), U"\""); |
| 1125 |
/* |
| 1126 |
Copy the input and output strings to the target object. |
| 1127 |
*/ |
| 1128 |
for (integer icand = 1; icand <= tableau -> numberOfCandidates; icand ++) { |
| 1129 |
PairDistribution_add (thee.get(), tableau -> input.get(), tableau -> candidates [icand]. output.get(), 0.0); |
| 1130 |
} |
| 1131 |
/* |
| 1132 |
Compute a number of outputs and store the results. |
| 1133 |
*/ |
| 1134 |
for (integer itrial = 1; itrial <= trialsPerInput; itrial ++) { |
| 1135 |
OTGrammar_newDisharmonies (me, noise); |
| 1136 |
integer iwinner = OTGrammar_getWinner (me, itab); |
| 1137 |
thy pairs.at [nout + iwinner] -> weight += 1.0; |
| 1138 |
} |
| 1139 |
/* |
| 1140 |
Update the offset. |
| 1141 |
*/ |
| 1142 |
nout += tableau -> numberOfCandidates; |
| 1143 |
} |
| 1144 |
return thee; |
| 1145 |
} catch (MelderError) { |
| 1146 |
Melder_throw (me, U": output distribution not computed."); |
| 1147 |
} |
| 1148 |
} |
| 1149 |
|
| 1150 |
static bool honoursFixedRankings (OTGrammar me) { |
| 1151 |
for (integer i = 1; i <= my numberOfFixedRankings; i ++) { |
| 1152 |
integer higher = my fixedRankings [i]. higher, lower = my fixedRankings [i]. lower; |
| 1153 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1154 |
if (my index [icons] == higher) |
| 1155 |
break; // detected higher before lower: OK |
| 1156 |
if (my index [icons] == lower) |
| 1157 |
return false; |
| 1158 |
} |
| 1159 |
} |
| 1160 |
return true; |
| 1161 |
} |
| 1162 |
|
| 1163 |
autoDistributions OTGrammar_measureTypology_WEAK (OTGrammar me) { |
| 1164 |
try { |
| 1165 |
integer totalNumberOfOutputs = 0, nout = 0, nperm, factorial [1+12]; |
| 1166 |
if (my numberOfConstraints > 12) |
| 1167 |
Melder_throw (U"Cannot handle more than 12 constraints."); |
| 1168 |
factorial [0] = 1; |
| 1169 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 1170 |
factorial [icons] = factorial [icons - 1] * icons; |
| 1171 |
nperm = factorial [my numberOfConstraints]; |
| 1172 |
/* |
| 1173 |
Count the total number of outputs. |
| 1174 |
*/ |
| 1175 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) |
| 1176 |
totalNumberOfOutputs += my tableaus [itab]. numberOfCandidates; |
| 1177 |
/* |
| 1178 |
Create the distribution. One row for every output form. |
| 1179 |
*/ |
| 1180 |
autoDistributions thee = Distributions_create (totalNumberOfOutputs, 1); |
| 1181 |
/* |
| 1182 |
Measure every input form. |
| 1183 |
*/ |
| 1184 |
autoMelderProgress progress (U"Measuring typology..."); |
| 1185 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) { |
| 1186 |
OTGrammarTableau tableau = & my tableaus [itab]; |
| 1187 |
Melder_progress ((itab - 0.5) / my numberOfTableaus, U"Measuring input \"", tableau -> input.get(), U"\""); |
| 1188 |
/* |
| 1189 |
Set the row labels to the output strings. |
| 1190 |
*/ |
| 1191 |
for (integer icand = 1; icand <= tableau -> numberOfCandidates; icand ++) { |
| 1192 |
thy rowLabels [nout + icand] = Melder_dup ( |
| 1193 |
Melder_cat (tableau -> input.get(), U" \\-> ", tableau -> candidates [icand]. output.get()) |
| 1194 |
); |
| 1195 |
} |
| 1196 |
/* |
| 1197 |
Compute a number of outputs and store the results. |
| 1198 |
*/ |
| 1199 |
for (integer iperm = 0; iperm < nperm; iperm ++) { |
| 1200 |
integer permleft = iperm, iwinner; |
| 1201 |
/* Initialize to 12345 before permuting. */ |
| 1202 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 1203 |
my index [icons] = icons; |
| 1204 |
for (integer icons = 1; icons < my numberOfConstraints; icons ++) { |
| 1205 |
const integer fac = factorial [my numberOfConstraints - icons], shift = permleft / fac; |
| 1206 |
std::swap (my index [icons], my index [icons + shift]); |
| 1207 |
permleft %= fac; |
| 1208 |
} |
| 1209 |
if (honoursFixedRankings (me)) { |
| 1210 |
iwinner = OTGrammar_getWinner (me, itab); |
| 1211 |
thy data [nout + iwinner] [1] += 1; |
| 1212 |
} |
| 1213 |
} |
| 1214 |
/* |
| 1215 |
Update the offset. |
| 1216 |
*/ |
| 1217 |
nout += tableau -> numberOfCandidates; |
| 1218 |
} |
| 1219 |
return thee; |
| 1220 |
} catch (MelderError) { |
| 1221 |
Melder_throw (me, U": typology not measured."); |
| 1222 |
} |
| 1223 |
} |
| 1224 |
|
| 1225 |
static double learningStep (double mean, double relativeSpreading) { |
| 1226 |
return relativeSpreading == 0.0 ? mean : NUMrandomGauss (mean, relativeSpreading * mean); |
| 1227 |
} |
| 1228 |
|
| 1229 |
static void OTGrammar_honourLocalRankings (OTGrammar me, double plasticity, double relativePlasticityNoise, bool *grammarHasChanged) { |
| 1230 |
bool improved; |
| 1231 |
do { |
| 1232 |
improved = false; |
| 1233 |
for (integer irank = 1; irank <= my numberOfFixedRankings; irank ++) { |
| 1234 |
OTGrammarFixedRanking fixedRanking = & my fixedRankings [irank]; |
| 1235 |
OTGrammarConstraint higher = & my constraints [fixedRanking -> higher], lower = & my constraints [fixedRanking -> lower]; |
| 1236 |
while (higher -> ranking <= lower -> ranking) { |
| 1237 |
lower -> ranking -= learningStep (plasticity, relativePlasticityNoise); |
| 1238 |
if (grammarHasChanged) |
| 1239 |
*grammarHasChanged = true; |
| 1240 |
improved = true; |
| 1241 |
} |
| 1242 |
} |
| 1243 |
} while (improved); |
| 1244 |
} |
| 1245 |
|
| 1246 |
static void OTGrammar_modifyRankings (OTGrammar me, integer itab, integer iwinner, integer iadult, |
| 1247 |
kOTGrammar_rerankingStrategy updateRule, bool honourLocalRankings, |
| 1248 |
double plasticity, double relativePlasticityNoise, bool warnIfStalled, bool *out_grammarHasChanged) |
| 1249 |
{ |
| 1250 |
try { |
| 1251 |
OTGrammarTableau tableau = & my tableaus [itab]; |
| 1252 |
OTGrammarCandidate winner = & tableau -> candidates [iwinner], adult = & tableau -> candidates [iadult]; |
| 1253 |
const double step = learningStep (plasticity, relativePlasticityNoise); |
| 1254 |
bool multiplyStepByNumberOfViolations = |
| 1255 |
my decisionStrategy == kOTGrammar_decisionStrategy::HARMONIC_GRAMMAR || |
| 1256 |
my decisionStrategy == kOTGrammar_decisionStrategy::LINEAR_OT || |
| 1257 |
my decisionStrategy == kOTGrammar_decisionStrategy::MAXIMUM_ENTROPY || |
| 1258 |
my decisionStrategy == kOTGrammar_decisionStrategy::POSITIVE_HG || |
| 1259 |
my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_HG || |
| 1260 |
my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_MAXIMUM_ENTROPY || |
| 1261 |
my decisionStrategy == kOTGrammar_decisionStrategy::NONNEGATIVE_MAXIMUM_ENTROPY; |
| 1262 |
if (Melder_debug != 0) { |
| 1263 |
/* |
| 1264 |
* Perhaps override the standard update rule. |
| 1265 |
*/ |
| 1266 |
if (Melder_debug == 26) |
| 1267 |
multiplyStepByNumberOfViolations = false; // OT-GLA |
| 1268 |
else if (Melder_debug == 27) |
| 1269 |
multiplyStepByNumberOfViolations = true; // HG-GLA |
| 1270 |
} |
| 1271 |
if (updateRule == kOTGrammar_rerankingStrategy::SYMMETRIC_ONE) { |
| 1272 |
const integer icons = NUMrandomInteger (1, my numberOfConstraints); |
| 1273 |
const OTGrammarConstraint constraint = & my constraints [icons]; |
| 1274 |
double constraintStep = step * constraint -> plasticity; |
| 1275 |
const integer winnerMarks = winner -> marks [icons]; |
| 1276 |
const integer adultMarks = adult -> marks [icons]; |
| 1277 |
if (adultMarks > winnerMarks) { |
| 1278 |
if (multiplyStepByNumberOfViolations) |
| 1279 |
constraintStep *= adultMarks - winnerMarks; |
| 1280 |
constraint -> ranking -= constraintStep * (1.0 + constraint -> ranking * my leak); |
| 1281 |
if (out_grammarHasChanged) |
| 1282 |
*out_grammarHasChanged = true; |
| 1283 |
} |
| 1284 |
if (winnerMarks > adultMarks) { |
| 1285 |
if (multiplyStepByNumberOfViolations) |
| 1286 |
constraintStep *= winnerMarks - adultMarks; |
| 1287 |
constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak); |
| 1288 |
if (out_grammarHasChanged) |
| 1289 |
*out_grammarHasChanged = true; |
| 1290 |
} |
| 1291 |
} else if (updateRule == kOTGrammar_rerankingStrategy::SYMMETRIC_ALL) { |
| 1292 |
bool changed = false; |
| 1293 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1294 |
const OTGrammarConstraint constraint = & my constraints [icons]; |
| 1295 |
double constraintStep = step * constraint -> plasticity; |
| 1296 |
const integer winnerMarks = winner -> marks [icons]; |
| 1297 |
const integer adultMarks = adult -> marks [icons]; |
| 1298 |
if (adultMarks > winnerMarks) { |
| 1299 |
if (multiplyStepByNumberOfViolations) |
| 1300 |
constraintStep *= adultMarks - winnerMarks; |
| 1301 |
constraint -> ranking -= constraintStep * (1.0 + constraint -> ranking * my leak); |
| 1302 |
changed = true; |
| 1303 |
} |
| 1304 |
if (winnerMarks > adultMarks) { |
| 1305 |
if (multiplyStepByNumberOfViolations) |
| 1306 |
constraintStep *= winnerMarks - adultMarks; |
| 1307 |
constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak); |
| 1308 |
changed = true; |
| 1309 |
} |
| 1310 |
} |
| 1311 |
if (changed && my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_HG) |
| 1312 |
{ |
| 1313 |
longdouble sumOfWeights = 0.0; |
| 1314 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 1315 |
sumOfWeights += my constraints [icons]. ranking; |
| 1316 |
const double averageWeight = (double) sumOfWeights / my numberOfConstraints; |
| 1317 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 1318 |
my constraints [icons]. ranking -= averageWeight; |
| 1319 |
} |
| 1320 |
if (out_grammarHasChanged) |
| 1321 |
*out_grammarHasChanged = changed; |
| 1322 |
} else if (updateRule == kOTGrammar_rerankingStrategy::SYMMETRIC_ALL_SKIPPABLE) { |
| 1323 |
bool changed = false; |
| 1324 |
integer winningConstraints = 0, adultConstraints = 0; |
| 1325 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1326 |
const integer winnerMarks = winner -> marks [icons]; |
| 1327 |
const integer adultMarks = adult -> marks [icons]; |
| 1328 |
if (adultMarks > winnerMarks) |
| 1329 |
adultConstraints ++; |
| 1330 |
if (winnerMarks > adultMarks) |
| 1331 |
winningConstraints ++; |
| 1332 |
} |
| 1333 |
if (winningConstraints != 0) for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1334 |
const OTGrammarConstraint constraint = & my constraints [icons]; |
| 1335 |
double constraintStep = step * constraint -> plasticity; |
| 1336 |
const integer winnerMarks = winner -> marks [icons]; |
| 1337 |
const integer adultMarks = adult -> marks [icons]; |
| 1338 |
if (adultMarks > winnerMarks) { |
| 1339 |
if (multiplyStepByNumberOfViolations) |
| 1340 |
constraintStep *= adultMarks - winnerMarks; |
| 1341 |
constraint -> ranking -= constraintStep * (1.0 + constraint -> ranking * my leak); |
| 1342 |
changed = true; |
| 1343 |
} |
| 1344 |
if (winnerMarks > adultMarks) { |
| 1345 |
if (multiplyStepByNumberOfViolations) |
| 1346 |
constraintStep *= winnerMarks - adultMarks; |
| 1347 |
constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak); |
| 1348 |
changed = true; |
| 1349 |
} |
| 1350 |
} |
| 1351 |
if (changed && my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_HG) { |
| 1352 |
longdouble sumOfWeights = 0.0; |
| 1353 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 1354 |
sumOfWeights += my constraints [icons]. ranking; |
| 1355 |
const double averageWeight = (double) sumOfWeights / my numberOfConstraints; |
| 1356 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 1357 |
my constraints [icons]. ranking -= averageWeight; |
| 1358 |
} |
| 1359 |
if (out_grammarHasChanged) |
| 1360 |
*out_grammarHasChanged = changed; |
| 1361 |
} else if (updateRule == kOTGrammar_rerankingStrategy::WEIGHTED_UNCANCELLED) { |
| 1362 |
integer winningConstraints = 0, adultConstraints = 0; |
| 1363 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1364 |
const integer winnerMarks = winner -> marks [icons]; |
| 1365 |
const integer adultMarks = adult -> marks [icons]; |
| 1366 |
if (adultMarks > winnerMarks) |
| 1367 |
adultConstraints ++; |
| 1368 |
if (winnerMarks > adultMarks) |
| 1369 |
winningConstraints ++; |
| 1370 |
} |
| 1371 |
if (winningConstraints != 0) for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1372 |
const OTGrammarConstraint constraint = & my constraints [icons]; |
| 1373 |
double constraintStep = step * constraint -> plasticity; |
| 1374 |
const integer winnerMarks = winner -> marks [icons]; |
| 1375 |
const integer adultMarks = adult -> marks [icons]; |
| 1376 |
if (adultMarks > winnerMarks) { |
| 1377 |
if (multiplyStepByNumberOfViolations) |
| 1378 |
constraintStep *= adultMarks - winnerMarks; |
| 1379 |
constraint -> ranking -= constraintStep * (1.0 + constraint -> ranking * my leak) / adultConstraints; |
| 1380 |
//constraint -> ranking -= constraintStep * (1.0 + constraint -> ranking * my leak) * winningConstraints; |
| 1381 |
if (out_grammarHasChanged) |
| 1382 |
*out_grammarHasChanged = true; |
| 1383 |
} |
| 1384 |
if (winnerMarks > adultMarks) { |
| 1385 |
if (multiplyStepByNumberOfViolations) |
| 1386 |
constraintStep *= winnerMarks - adultMarks; |
| 1387 |
constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak) / winningConstraints; |
| 1388 |
//constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak) * adultConstraints; |
| 1389 |
if (out_grammarHasChanged) |
| 1390 |
*out_grammarHasChanged = true; |
| 1391 |
} |
| 1392 |
} |
| 1393 |
} else if (updateRule == kOTGrammar_rerankingStrategy::WEIGHTED_ALL) { |
| 1394 |
integer winningConstraints = 0, adultConstraints = 0; |
| 1395 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1396 |
const integer winnerMarks = winner -> marks [icons]; |
| 1397 |
const integer adultMarks = adult -> marks [icons]; |
| 1398 |
if (adultMarks > 0) |
| 1399 |
adultConstraints ++; |
| 1400 |
if (winnerMarks > 0) |
| 1401 |
winningConstraints ++; |
| 1402 |
} |
| 1403 |
if (winningConstraints != 0) for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1404 |
const OTGrammarConstraint constraint = & my constraints [icons]; |
| 1405 |
double constraintStep = step * constraint -> plasticity; |
| 1406 |
const integer winnerMarks = winner -> marks [icons]; |
| 1407 |
const integer adultMarks = adult -> marks [icons]; |
| 1408 |
if (adultMarks > 0) { |
| 1409 |
if (multiplyStepByNumberOfViolations) |
| 1410 |
constraintStep *= adultMarks /*- winnerMarks*/; |
| 1411 |
constraint -> ranking -= constraintStep * (1.0 + constraint -> ranking * my leak) / adultConstraints; |
| 1412 |
if (out_grammarHasChanged) |
| 1413 |
*out_grammarHasChanged = true; |
| 1414 |
} |
| 1415 |
if (winnerMarks > 0) { |
| 1416 |
if (multiplyStepByNumberOfViolations) |
| 1417 |
constraintStep *= winnerMarks /*- adultMarks*/; |
| 1418 |
constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak) / winningConstraints; |
| 1419 |
if (out_grammarHasChanged) |
| 1420 |
*out_grammarHasChanged = true; |
| 1421 |
} |
| 1422 |
} |
| 1423 |
} else if (updateRule == kOTGrammar_rerankingStrategy::EDCD || updateRule == kOTGrammar_rerankingStrategy::EDCD_WITH_VACATION) { |
| 1424 |
/* |
| 1425 |
Determine the crucial winner mark. |
| 1426 |
*/ |
| 1427 |
double pivotRanking; |
| 1428 |
bool equivalent = true; |
| 1429 |
integer icons = 1; |
| 1430 |
for (; icons <= my numberOfConstraints; icons ++) { |
| 1431 |
const integer winnerMarks = winner -> marks [my index [icons]]; // the order is important, therefore indirect |
| 1432 |
const integer adultMarks = adult -> marks [my index [icons]]; |
| 1433 |
if (adultMarks < winnerMarks) |
| 1434 |
break; |
| 1435 |
if (adultMarks > winnerMarks) |
| 1436 |
equivalent = false; |
| 1437 |
} |
| 1438 |
if (icons > my numberOfConstraints) { // completed the loop? |
| 1439 |
if (warnIfStalled && ! equivalent) |
| 1440 |
Melder_warning (U"Correct output is harmonically bounded (by having strict superset violations as compared to the learner's output)! EDCD stalls.\n" |
| 1441 |
U"Input: ", tableau -> input.get(), U"\nCorrect output: ", adult -> output.get(), U"\nLearner's output: ", winner -> output.get()); |
| 1442 |
return; // Tesar & Smolensky (2000: 67): "stopped dead in its tracks" |
| 1443 |
} |
| 1444 |
/* |
| 1445 |
Determine the stratum into which some constraints will be demoted. |
| 1446 |
*/ |
| 1447 |
pivotRanking = my constraints [my index [icons]]. ranking; |
| 1448 |
if (updateRule == kOTGrammar_rerankingStrategy::EDCD_WITH_VACATION) { |
| 1449 |
integer numberOfConstraintsToDemote = 0; |
| 1450 |
for (icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1451 |
const integer winnerMarks = winner -> marks [icons]; |
| 1452 |
const integer adultMarks = adult -> marks [icons]; |
| 1453 |
if (adultMarks > winnerMarks) { |
| 1454 |
const OTGrammarConstraint constraint = & my constraints [icons]; |
| 1455 |
if (constraint -> ranking >= pivotRanking) |
| 1456 |
numberOfConstraintsToDemote += 1; |
| 1457 |
} |
| 1458 |
} |
| 1459 |
if (numberOfConstraintsToDemote > 0) { |
| 1460 |
for (icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1461 |
const OTGrammarConstraint constraint = & my constraints [icons]; |
| 1462 |
if (constraint -> ranking < pivotRanking) { |
| 1463 |
constraint -> ranking -= numberOfConstraintsToDemote * step * constraint -> plasticity; |
| 1464 |
if (out_grammarHasChanged) |
| 1465 |
*out_grammarHasChanged = true; |
| 1466 |
} |
| 1467 |
} |
| 1468 |
} |
| 1469 |
} |
| 1470 |
/* |
| 1471 |
Demote all the uniquely violated constraints in the adult form |
| 1472 |
that have rankings not lower than the pivot. |
| 1473 |
*/ |
| 1474 |
for (icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1475 |
integer numberOfConstraintsDemoted = 0; |
| 1476 |
const integer winnerMarks = winner -> marks [my index [icons]]; // for the vacation version, the order is important, therefore indirect |
| 1477 |
const integer adultMarks = adult -> marks [my index [icons]]; |
| 1478 |
if (adultMarks > winnerMarks) { |
| 1479 |
const OTGrammarConstraint constraint = & my constraints [my index [icons]]; |
| 1480 |
const double constraintStep = step * constraint -> plasticity; |
| 1481 |
if (constraint -> ranking >= pivotRanking) { |
| 1482 |
numberOfConstraintsDemoted += 1; |
| 1483 |
constraint -> ranking = pivotRanking - numberOfConstraintsDemoted * constraintStep; // this preserves the order of the demotees |
| 1484 |
if (out_grammarHasChanged) |
| 1485 |
*out_grammarHasChanged = true; |
| 1486 |
} |
| 1487 |
} |
| 1488 |
} |
| 1489 |
} else if (updateRule == kOTGrammar_rerankingStrategy::DEMOTION_ONLY) { |
| 1490 |
/* |
| 1491 |
Determine the crucial adult mark. |
| 1492 |
*/ |
| 1493 |
integer crucialAdultMark; |
| 1494 |
OTGrammarConstraint offendingConstraint; |
| 1495 |
integer icons = 1; |
| 1496 |
for (; icons <= my numberOfConstraints; icons ++) { |
| 1497 |
const integer winnerMarks = winner -> marks [my index [icons]]; // the order is important, so we indirect |
| 1498 |
const integer adultMarks = adult -> marks [my index [icons]]; |
| 1499 |
if (my constraints [my index [icons]]. tiedToTheRight) |
| 1500 |
Melder_throw (U"Demotion-only learning cannot handle tied constraints."); |
| 1501 |
if (adultMarks < winnerMarks) |
| 1502 |
Melder_throw (U"Demotion-only learning step: Adult form wins! Should never happen."); |
| 1503 |
if (adultMarks > winnerMarks) |
| 1504 |
break; |
| 1505 |
} |
| 1506 |
if (icons > my numberOfConstraints) // completed the loop? |
| 1507 |
Melder_throw (U"Adult form equals correct candidate."); |
| 1508 |
crucialAdultMark = icons; |
| 1509 |
/* |
| 1510 |
Demote the highest uniquely violated constraint in the adult form. |
| 1511 |
*/ |
| 1512 |
offendingConstraint = & my constraints [my index [crucialAdultMark]]; |
| 1513 |
double constraintStep = step * offendingConstraint -> plasticity; |
| 1514 |
offendingConstraint -> ranking -= constraintStep; |
| 1515 |
if (out_grammarHasChanged) |
| 1516 |
*out_grammarHasChanged = true; |
| 1517 |
} else if (updateRule == kOTGrammar_rerankingStrategy::WEIGHTED_ALL_UP_HIGHEST_DOWN) { |
| 1518 |
integer numberOfUp = 0; |
| 1519 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1520 |
const integer winnerMarks = winner -> marks [icons]; |
| 1521 |
const integer adultMarks = adult -> marks [icons]; |
| 1522 |
if (winnerMarks > adultMarks) |
| 1523 |
numberOfUp ++; |
| 1524 |
} |
| 1525 |
if (numberOfUp > 0) { |
| 1526 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1527 |
const OTGrammarConstraint constraint = & my constraints [icons]; |
| 1528 |
double constraintStep = step * constraint -> plasticity; |
| 1529 |
const integer winnerMarks = winner -> marks [icons]; |
| 1530 |
const integer adultMarks = adult -> marks [icons]; |
| 1531 |
if (winnerMarks > adultMarks) { |
| 1532 |
if (multiplyStepByNumberOfViolations) |
| 1533 |
constraintStep *= winnerMarks - adultMarks; |
| 1534 |
constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak) / numberOfUp; |
| 1535 |
if (out_grammarHasChanged) |
| 1536 |
*out_grammarHasChanged = true; |
| 1537 |
} |
| 1538 |
} |
| 1539 |
integer winnerMarks = 0, adultMarks = 0; |
| 1540 |
integer icons = 1; |
| 1541 |
for (; icons <= my numberOfConstraints; icons ++) { |
| 1542 |
winnerMarks = winner -> marks [my index [icons]]; // the order is important, therefore indirect |
| 1543 |
adultMarks = adult -> marks [my index [icons]]; |
| 1544 |
if (my constraints [my index [icons]]. tiedToTheRight) |
| 1545 |
Melder_throw (U"Demotion-only learning cannot handle tied constraints."); |
| 1546 |
if (adultMarks < winnerMarks) |
| 1547 |
Melder_throw (U"Demotion-only learning step: Adult form wins! Should never happen."); |
| 1548 |
if (adultMarks > winnerMarks) break; |
| 1549 |
} |
| 1550 |
if (icons > my numberOfConstraints) // completed the loop? |
| 1551 |
Melder_throw (U"Adult form equals correct candidate."); |
| 1552 |
const integer crucialAdultMark = icons; |
| 1553 |
/* |
| 1554 |
Demote the highest uniquely violated constraint in the adult form. |
| 1555 |
*/ |
| 1556 |
const OTGrammarConstraint offendingConstraint = & my constraints [my index [crucialAdultMark]]; |
| 1557 |
double constraintStep = step * offendingConstraint -> plasticity; |
| 1558 |
if (multiplyStepByNumberOfViolations) |
| 1559 |
constraintStep *= winnerMarks - adultMarks; |
| 1560 |
offendingConstraint -> ranking -= /*numberOfUp **/ constraintStep * (1.0 - offendingConstraint -> ranking * my leak); |
| 1561 |
if (out_grammarHasChanged) |
| 1562 |
*out_grammarHasChanged = true; |
| 1563 |
} |
| 1564 |
} else if (updateRule == kOTGrammar_rerankingStrategy::WEIGHTED_ALL_UP_HIGHEST_DOWN_2012) { |
| 1565 |
integer numberOfUp = 0; |
| 1566 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1567 |
const integer winnerMarks = winner -> marks [icons]; |
| 1568 |
const integer adultMarks = adult -> marks [icons]; |
| 1569 |
if (winnerMarks > adultMarks) |
| 1570 |
numberOfUp ++; |
| 1571 |
} |
| 1572 |
if (numberOfUp > 0) { |
| 1573 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1574 |
const OTGrammarConstraint constraint = & my constraints [icons]; |
| 1575 |
double constraintStep = step * constraint -> plasticity; |
| 1576 |
const integer winnerMarks = winner -> marks [icons]; |
| 1577 |
const integer adultMarks = adult -> marks [icons]; |
| 1578 |
if (winnerMarks > adultMarks) { |
| 1579 |
if (multiplyStepByNumberOfViolations) |
| 1580 |
constraintStep *= winnerMarks - adultMarks; |
| 1581 |
constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak) / (numberOfUp + 1); |
| 1582 |
if (out_grammarHasChanged) |
| 1583 |
*out_grammarHasChanged = true; |
| 1584 |
} |
| 1585 |
} |
| 1586 |
integer winnerMarks = 0, adultMarks = 0; |
| 1587 |
integer icons = 1; |
| 1588 |
for (; icons <= my numberOfConstraints; icons ++) { |
| 1589 |
winnerMarks = winner -> marks [my index [icons]]; // the order is important, therefore indirect |
| 1590 |
adultMarks = adult -> marks [my index [icons]]; |
| 1591 |
if (my constraints [my index [icons]]. tiedToTheRight) |
| 1592 |
Melder_throw (U"Demotion-only learning cannot handle tied constraints."); |
| 1593 |
if (adultMarks < winnerMarks) |
| 1594 |
Melder_throw (U"Demotion-only learning step: Adult form wins! Should never happen."); |
| 1595 |
if (adultMarks > winnerMarks) break; |
| 1596 |
} |
| 1597 |
if (icons > my numberOfConstraints) // completed the loop? |
| 1598 |
Melder_throw (U"Adult form equals correct candidate."); |
| 1599 |
const integer crucialAdultMark = icons; |
| 1600 |
/* |
| 1601 |
Demote the highest uniquely violated constraint in the adult form. |
| 1602 |
*/ |
| 1603 |
const OTGrammarConstraint offendingConstraint = & my constraints [my index [crucialAdultMark]]; |
| 1604 |
double constraintStep = step * offendingConstraint -> plasticity; |
| 1605 |
if (multiplyStepByNumberOfViolations) |
| 1606 |
constraintStep *= winnerMarks - adultMarks; |
| 1607 |
offendingConstraint -> ranking -= /*numberOfUp **/ constraintStep * (1.0 - offendingConstraint -> ranking * my leak); |
| 1608 |
if (out_grammarHasChanged) |
| 1609 |
*out_grammarHasChanged = true; |
| 1610 |
} |
| 1611 |
} else if (updateRule == kOTGrammar_rerankingStrategy::WEIGHTED_ALL_UP_HIGH_DOWN) { |
| 1612 |
integer numberOfDown = 0, numberOfUp = 0, lowestDemotableConstraint = 0; |
| 1613 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1614 |
const integer winnerMarks = winner -> marks [my index [icons]]; // the order is important, therefore indirect |
| 1615 |
const integer adultMarks = adult -> marks [my index [icons]]; |
| 1616 |
if (adultMarks < winnerMarks) { |
| 1617 |
numberOfUp ++; |
| 1618 |
} else if (adultMarks > winnerMarks) { |
| 1619 |
if (numberOfUp == 0) { |
| 1620 |
numberOfDown ++; |
| 1621 |
lowestDemotableConstraint = icons; |
| 1622 |
} |
| 1623 |
} |
| 1624 |
} |
| 1625 |
if (warnIfStalled && numberOfDown == 0) { |
| 1626 |
Melder_warning (U"Correct output is harmonically bounded (by having strict superset violations as compared to the learner's output)! EDCD stalls.\n" |
| 1627 |
U"Input: ", tableau -> input.get(), U"\nCorrect output: ", adult -> output.get(), U"\nLearner's output: ", winner -> output.get()); |
| 1628 |
return; |
| 1629 |
} |
| 1630 |
if (numberOfUp > 0) { |
| 1631 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1632 |
const integer constraintIndex = my index [icons]; |
| 1633 |
if (my constraints [constraintIndex]. tiedToTheRight) |
| 1634 |
Melder_throw (U"Demotion-only learning cannot handle tied constraints."); |
| 1635 |
const OTGrammarConstraint constraint = & my constraints [constraintIndex]; |
| 1636 |
double constraintStep = step * constraint -> plasticity; |
| 1637 |
const integer winnerMarks = winner -> marks [constraintIndex]; // the order is important, therefore indirect |
| 1638 |
const integer adultMarks = adult -> marks [constraintIndex]; |
| 1639 |
if (adultMarks < winnerMarks) { |
| 1640 |
if (multiplyStepByNumberOfViolations) |
| 1641 |
constraintStep *= winnerMarks - adultMarks; |
| 1642 |
constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak) * numberOfDown / (numberOfUp + 0.0); |
| 1643 |
} else if (adultMarks > winnerMarks) { |
| 1644 |
if (icons <= lowestDemotableConstraint) { |
| 1645 |
if (multiplyStepByNumberOfViolations) |
| 1646 |
constraintStep *= adultMarks - winnerMarks; |
| 1647 |
constraint -> ranking -= constraintStep * (1.0 - constraint -> ranking * my leak); |
| 1648 |
} |
| 1649 |
} |
| 1650 |
} |
| 1651 |
if (out_grammarHasChanged) *out_grammarHasChanged = true; |
| 1652 |
} |
| 1653 |
} else if (updateRule == kOTGrammar_rerankingStrategy::WEIGHTED_ALL_UP_HIGH_DOWN_2012) { |
| 1654 |
integer numberOfDown = 0, numberOfUp = 0, lowestDemotableConstraint = 0; |
| 1655 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1656 |
const integer winnerMarks = winner -> marks [my index [icons]]; // the order is important, therefore indirect |
| 1657 |
const integer adultMarks = adult -> marks [my index [icons]]; |
| 1658 |
if (adultMarks < winnerMarks) { |
| 1659 |
numberOfUp ++; |
| 1660 |
} else if (adultMarks > winnerMarks) { |
| 1661 |
if (numberOfUp == 0) { |
| 1662 |
numberOfDown ++; |
| 1663 |
lowestDemotableConstraint = icons; |
| 1664 |
} |
| 1665 |
} |
| 1666 |
} |
| 1667 |
if (warnIfStalled && numberOfDown == 0) { |
| 1668 |
Melder_warning (U"Correct output is harmonically bounded (by having strict superset violations as compared to the learner's output)! EDCD stalls.\n" |
| 1669 |
U"Input: ", tableau -> input.get(), U"\nCorrect output: ", adult -> output.get(), U"\nLearner's output: ", winner -> output.get()); |
| 1670 |
return; |
| 1671 |
} |
| 1672 |
if (numberOfUp > 0) { |
| 1673 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1674 |
const integer constraintIndex = my index [icons]; |
| 1675 |
if (my constraints [constraintIndex]. tiedToTheRight) |
| 1676 |
Melder_throw (U"Demotion-only learning cannot handle tied constraints."); |
| 1677 |
const OTGrammarConstraint constraint = & my constraints [constraintIndex]; |
| 1678 |
double constraintStep = step * constraint -> plasticity; |
| 1679 |
const integer winnerMarks = winner -> marks [constraintIndex]; // the order is important, therefore indirect |
| 1680 |
const integer adultMarks = adult -> marks [constraintIndex]; |
| 1681 |
if (adultMarks < winnerMarks) { |
| 1682 |
if (multiplyStepByNumberOfViolations) |
| 1683 |
constraintStep *= winnerMarks - adultMarks; |
| 1684 |
constraint -> ranking += constraintStep * (1.0 - constraint -> ranking * my leak) * numberOfDown / (numberOfUp + 1.0); |
| 1685 |
} else if (adultMarks > winnerMarks) { |
| 1686 |
if (icons <= lowestDemotableConstraint) { |
| 1687 |
if (multiplyStepByNumberOfViolations) |
| 1688 |
constraintStep *= adultMarks - winnerMarks; |
| 1689 |
constraint -> ranking -= constraintStep * (1.0 - constraint -> ranking * my leak); |
| 1690 |
} |
| 1691 |
} |
| 1692 |
} |
| 1693 |
if (out_grammarHasChanged) |
| 1694 |
*out_grammarHasChanged = true; |
| 1695 |
} |
| 1696 |
} |
| 1697 |
if (honourLocalRankings && my numberOfFixedRankings) |
| 1698 |
OTGrammar_honourLocalRankings (me, plasticity, relativePlasticityNoise, out_grammarHasChanged); |
| 1699 |
} catch (MelderError) { |
| 1700 |
Melder_throw (me, U": rankings not modified."); |
| 1701 |
} |
| 1702 |
} |
| 1703 |
|
| 1704 |
void OTGrammar_learnOne (OTGrammar me, conststring32 input, conststring32 adultOutput, |
| 1705 |
double evaluationNoise, enum kOTGrammar_rerankingStrategy updateRule, bool honourLocalRankings, |
| 1706 |
double plasticity, double relativePlasticityNoise, bool newDisharmonies, bool warnIfStalled, bool *out_grammarHasChanged) |
| 1707 |
{ |
| 1708 |
try { |
| 1709 |
if (newDisharmonies) |
| 1710 |
OTGrammar_newDisharmonies (me, evaluationNoise); |
| 1711 |
if (out_grammarHasChanged) |
| 1712 |
*out_grammarHasChanged = false; |
| 1713 |
|
| 1714 |
/* |
| 1715 |
Evaluate the input in the learner's hypothesis. |
| 1716 |
*/ |
| 1717 |
integer itab = OTGrammar_getTableau (me, input); |
| 1718 |
OTGrammarTableau tableau = & my tableaus [itab]; |
| 1719 |
|
| 1720 |
/* |
| 1721 |
Determine the "winner", i.e. the candidate that wins in the learner's grammar |
| 1722 |
(Tesar & Smolensky call this the "loser"). |
| 1723 |
*/ |
| 1724 |
integer iwinner = OTGrammar_getWinner (me, itab); |
| 1725 |
OTGrammarCandidate winner = & tableau -> candidates [iwinner]; |
| 1726 |
|
| 1727 |
/* |
| 1728 |
Error-driven: compare the adult winner (the correct candidate) and the learner's winner. |
| 1729 |
*/ |
| 1730 |
if (str32equ (winner -> output.get(), adultOutput)) |
| 1731 |
return; // as far as we know, the grammar is already correct: don't update rankings |
| 1732 |
|
| 1733 |
/* |
| 1734 |
Find (perhaps the learner's interpretation of) the adult output in the learner's own tableau |
| 1735 |
(Tesar & Smolensky call this the "winner"). |
| 1736 |
*/ |
| 1737 |
integer iadult = 1; |
| 1738 |
for (; iadult <= tableau -> numberOfCandidates; iadult ++) { |
| 1739 |
OTGrammarCandidate cand = & tableau -> candidates [iadult]; |
| 1740 |
if (str32equ (cand -> output.get(), adultOutput)) |
| 1741 |
break; |
| 1742 |
} |
| 1743 |
if (iadult > tableau -> numberOfCandidates) |
| 1744 |
Melder_throw (U"Cannot generate adult output \"", adultOutput, U"\"."); |
| 1745 |
|
| 1746 |
/* |
| 1747 |
Now we know that the current hypothesis prefers the (wrong) learner's winner over the (correct) adult output. |
| 1748 |
The grammar will have to change. |
| 1749 |
*/ |
| 1750 |
OTGrammar_modifyRankings (me, itab, iwinner, iadult, updateRule, honourLocalRankings, |
| 1751 |
plasticity, relativePlasticityNoise, warnIfStalled, out_grammarHasChanged); |
| 1752 |
} catch (MelderError) { |
| 1753 |
Melder_throw (me, U": not learned from input \"", input, U"\" and adult output \"", adultOutput, U"\"."); |
| 1754 |
} |
| 1755 |
} |
| 1756 |
|
| 1757 |
void OTGrammar_learn (OTGrammar me, Strings inputs, Strings outputs, |
| 1758 |
double evaluationNoise, enum kOTGrammar_rerankingStrategy updateRule, bool honourLocalRankings, |
| 1759 |
double plasticity, double relativePlasticityNoise, integer numberOfChews) |
| 1760 |
{ |
| 1761 |
if (! inputs) |
| 1762 |
inputs = outputs; |
| 1763 |
try { |
| 1764 |
const integer n = inputs -> numberOfStrings; |
| 1765 |
Melder_require (outputs -> numberOfStrings == n, |
| 1766 |
U"Numbers of strings in input and output should be equal."); |
| 1767 |
for (integer i = 1; i <= n; i ++) { |
| 1768 |
for (integer ichew = 1; ichew <= numberOfChews; ichew ++) |
| 1769 |
OTGrammar_learnOne (me, inputs -> strings [i].get(), outputs -> strings [i].get(), |
| 1770 |
evaluationNoise, updateRule, honourLocalRankings, |
| 1771 |
plasticity, relativePlasticityNoise, true, true, nullptr |
| 1772 |
); |
| 1773 |
} |
| 1774 |
} catch (MelderError) { |
| 1775 |
Melder_throw (me, U": not learned from ", inputs, U" (inputs) and ", outputs, U" (outputs)."); |
| 1776 |
} |
| 1777 |
} |
| 1778 |
|
| 1779 |
void OTGrammar_PairDistribution_learn (OTGrammar me, PairDistribution thee, |
| 1780 |
double evaluationNoise, enum kOTGrammar_rerankingStrategy updateRule, bool honourLocalRankings, |
| 1781 |
double initialPlasticity, integer replicationsPerPlasticity, double plasticityDecrement, |
| 1782 |
integer numberOfPlasticities, double relativePlasticityNoise, integer numberOfChews) |
| 1783 |
{ |
| 1784 |
integer idatum = 0, numberOfData = numberOfPlasticities * replicationsPerPlasticity; |
| 1785 |
try { |
| 1786 |
double plasticity = initialPlasticity; |
| 1787 |
autoMelderMonitor monitor (U"Learning with full knowledge..."); |
| 1788 |
for (integer iplasticity = 1; iplasticity <= numberOfPlasticities; iplasticity ++) { |
| 1789 |
for (integer ireplication = 1; ireplication <= replicationsPerPlasticity; ireplication ++) { |
| 1790 |
conststring32 input, output; |
| 1791 |
PairDistribution_peekPair (thee, & input, & output); |
| 1792 |
++ idatum; |
| 1793 |
if (monitor.graphics() && idatum % (numberOfData / 400 + 1) == 0) { |
| 1794 |
Graphics_beginMovieFrame (monitor.graphics(), nullptr); |
| 1795 |
Graphics_setWindow (monitor.graphics(), 0, numberOfData, 50, 150); |
| 1796 |
for (integer icons = 1; icons <= 14 && icons <= my numberOfConstraints; icons ++) { |
| 1797 |
Graphics_setGrey (monitor.graphics(), (double) icons / 14); |
| 1798 |
Graphics_line (monitor.graphics(), |
| 1799 |
idatum, my constraints [icons]. ranking, |
| 1800 |
idatum, my constraints [icons]. ranking + 1.0 |
| 1801 |
); |
| 1802 |
} |
| 1803 |
Graphics_endMovieFrame (monitor.graphics(), 0.0); |
| 1804 |
} |
| 1805 |
Melder_monitor ((double) idatum / numberOfData, |
| 1806 |
U"Processing input-output pair ", idatum, |
| 1807 |
U" out of ", numberOfData, U": ", input, U" -> ", output |
| 1808 |
); |
| 1809 |
for (integer ichew = 1; ichew <= numberOfChews; ichew ++) |
| 1810 |
OTGrammar_learnOne (me, input, output, |
| 1811 |
evaluationNoise, updateRule, honourLocalRankings, |
| 1812 |
plasticity, relativePlasticityNoise, true, true, nullptr |
| 1813 |
); |
| 1814 |
} |
| 1815 |
plasticity *= plasticityDecrement; |
| 1816 |
} |
| 1817 |
} catch (MelderError) { |
| 1818 |
if (idatum > 1) |
| 1819 |
Melder_appendError (U"Only ", idatum - 1, U" input-output pairs out of ", numberOfData, U" were processed."); |
| 1820 |
Melder_throw (me, U": did not complete learning from ", thee, U"."); |
| 1821 |
} |
| 1822 |
} |
| 1823 |
|
| 1824 |
static integer PairDistribution_getNumberOfAttestedOutputs (PairDistribution me, conststring32 input, conststring32 *out_attestedOutput) { |
| 1825 |
integer result = 0; |
| 1826 |
for (integer ipair = 1; ipair <= my pairs.size; ipair ++) { |
| 1827 |
PairProbability pair = my pairs.at [ipair]; |
| 1828 |
if (str32equ (pair -> string1.get(), input) && pair -> weight > 0.0) { |
| 1829 |
if (out_attestedOutput) |
| 1830 |
*out_attestedOutput = pair -> string2.get(); |
| 1831 |
result ++; |
| 1832 |
} |
| 1833 |
} |
| 1834 |
return result; |
| 1835 |
} |
| 1836 |
|
| 1837 |
bool OTGrammar_PairDistribution_findPositiveWeights (OTGrammar me, PairDistribution thee, double weightFloor, double marginOfSeparation) { |
| 1838 |
NUMlinprog linprog = nullptr; |
| 1839 |
try { |
| 1840 |
bool result = false; |
| 1841 |
if (my decisionStrategy != kOTGrammar_decisionStrategy::HARMONIC_GRAMMAR && |
| 1842 |
my decisionStrategy != kOTGrammar_decisionStrategy::LINEAR_OT && |
| 1843 |
my decisionStrategy != kOTGrammar_decisionStrategy::POSITIVE_HG && |
| 1844 |
my decisionStrategy != kOTGrammar_decisionStrategy::EXPONENTIAL_HG) |
| 1845 |
{ |
| 1846 |
Melder_throw (U"To find positive weights, the decision strategy should be HarmonicGrammar, LinearOT, PositiveHG, or ExponentialHG."); |
| 1847 |
} |
| 1848 |
autoINTVEC optimalCandidates = raw_INTVEC (my numberOfTableaus); |
| 1849 |
/* |
| 1850 |
Check that there is exactly one optimal output for each input. |
| 1851 |
*/ |
| 1852 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) { |
| 1853 |
OTGrammarTableau tab = & my tableaus [itab]; |
| 1854 |
conststring32 attestedOutput = nullptr; |
| 1855 |
integer numberOfAttestedOutputs = PairDistribution_getNumberOfAttestedOutputs (thee, tab -> input.get(), & attestedOutput); |
| 1856 |
if (numberOfAttestedOutputs == 0) { |
| 1857 |
Melder_throw (U"Input \"", tab -> input.get(), U"\" has no attested output."); |
| 1858 |
} else if (numberOfAttestedOutputs > 1) { |
| 1859 |
Melder_throw (U"Input \"", tab -> input.get(), U"\" has more than one attested output."); |
| 1860 |
} else { |
| 1861 |
Melder_assert (attestedOutput); |
| 1862 |
for (integer icand = 1; icand <= tab -> numberOfCandidates; icand ++) { |
| 1863 |
OTGrammarCandidate cand = & tab -> candidates [icand]; |
| 1864 |
if (str32equ (attestedOutput, cand -> output.get())) |
| 1865 |
optimalCandidates [itab] = icand; |
| 1866 |
} |
| 1867 |
} |
| 1868 |
Melder_assert (optimalCandidates [itab] != 0); |
| 1869 |
} |
| 1870 |
/* |
| 1871 |
Create linear programming problem. |
| 1872 |
*/ |
| 1873 |
linprog = NUMlinprog_new (false); |
| 1874 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 1875 |
NUMlinprog_addVariable (linprog, weightFloor, undefined, 1.0); |
| 1876 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) { |
| 1877 |
OTGrammarTableau tab = & my tableaus [itab]; |
| 1878 |
const integer ioptimalCandidate = optimalCandidates [itab]; |
| 1879 |
Melder_assert (ioptimalCandidate >= 1); |
| 1880 |
Melder_assert (ioptimalCandidate <= tab -> numberOfCandidates); |
| 1881 |
OTGrammarCandidate optimalCandidate = & tab -> candidates [ioptimalCandidate]; |
| 1882 |
for (integer icand = 1; icand <= tab -> numberOfCandidates; icand ++) { |
| 1883 |
if (icand != ioptimalCandidate) { |
| 1884 |
OTGrammarCandidate cand = & tab -> candidates [icand]; |
| 1885 |
NUMlinprog_addConstraint (linprog, marginOfSeparation, undefined); |
| 1886 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 1887 |
NUMlinprog_addConstraintCoefficient (linprog, cand -> marks [icons] - optimalCandidate -> marks [icons]); |
| 1888 |
} |
| 1889 |
} |
| 1890 |
} |
| 1891 |
NUMlinprog_run (linprog); |
| 1892 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1893 |
const double weighting = NUMlinprog_getPrimalValue (linprog, icons); |
| 1894 |
Melder_assert (weighting >= weightFloor); |
| 1895 |
my constraints [icons]. ranking = my constraints [icons]. disharmony = |
| 1896 |
my decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_HG ? log (weighting) : weighting; |
| 1897 |
} |
| 1898 |
NUMlinprog_delete (linprog); |
| 1899 |
return result; |
| 1900 |
} catch (MelderError) { |
| 1901 |
NUMlinprog_delete (linprog); |
| 1902 |
Melder_throw (me, U": positive weights not found."); |
| 1903 |
} |
| 1904 |
} |
| 1905 |
|
| 1906 |
void OTGrammar_reset (OTGrammar me, double ranking) { |
| 1907 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1908 |
OTGrammarConstraint constraint = & my constraints [icons]; |
| 1909 |
constraint -> disharmony = constraint -> ranking = ranking; |
| 1910 |
} |
| 1911 |
OTGrammar_sort (me); |
| 1912 |
} |
| 1913 |
|
| 1914 |
void OTGrammar_resetToRandomRanking (OTGrammar me, double mean, double standardDeviation) { |
| 1915 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1916 |
OTGrammarConstraint constraint = & my constraints [my index [icons]]; |
| 1917 |
constraint -> disharmony = constraint -> ranking = NUMrandomGauss (mean, standardDeviation); |
| 1918 |
} |
| 1919 |
OTGrammar_sort (me); |
| 1920 |
} |
| 1921 |
|
| 1922 |
void OTGrammar_resetToRandomTotalRanking (OTGrammar me, double maximumRanking, double rankingDistance) { |
| 1923 |
/* |
| 1924 |
First put the constraints in a random order and build a random index. |
| 1925 |
*/ |
| 1926 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1927 |
OTGrammarConstraint constraint = & my constraints [icons]; |
| 1928 |
constraint -> ranking = 0.0; |
| 1929 |
} |
| 1930 |
OTGrammar_newDisharmonies (me, 1.0); |
| 1931 |
/* |
| 1932 |
Then use the random index to yield a cascade of rankings. |
| 1933 |
*/ |
| 1934 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1935 |
OTGrammarConstraint constraint = & my constraints [my index [icons]]; |
| 1936 |
constraint -> disharmony = constraint -> ranking = maximumRanking - (icons - 1) * rankingDistance; |
| 1937 |
} |
| 1938 |
OTGrammar_sort (me); |
| 1939 |
} |
| 1940 |
|
| 1941 |
void OTGrammar_setRanking (OTGrammar me, integer constraint, double ranking, double disharmony) { |
| 1942 |
try { |
| 1943 |
Melder_require (constraint > 0 && constraint <= my numberOfConstraints, |
| 1944 |
U"There is no constraint with number ", constraint, U"."); |
| 1945 |
my constraints [constraint]. ranking = ranking; |
| 1946 |
my constraints [constraint]. disharmony = disharmony; |
| 1947 |
OTGrammar_sort (me); |
| 1948 |
} catch (MelderError) { |
| 1949 |
Melder_throw (me, U": ranking of constraint ", constraint, U" not set."); |
| 1950 |
} |
| 1951 |
} |
| 1952 |
|
| 1953 |
void OTGrammar_setConstraintPlasticity (OTGrammar me, integer constraint, double plasticity) { |
| 1954 |
try { |
| 1955 |
Melder_require (constraint > 0 && constraint <= my numberOfConstraints, |
| 1956 |
U"There is no constraint with number ", constraint, U"."); |
| 1957 |
my constraints [constraint]. plasticity = plasticity; |
| 1958 |
} catch (MelderError) { |
| 1959 |
Melder_throw (me, U": plasticity of constraint ", constraint, U" not set."); |
| 1960 |
} |
| 1961 |
} |
| 1962 |
|
| 1963 |
integer theSaveNumberOfConstraints; |
| 1964 |
autoINTVEC theSaveIndex; |
| 1965 |
autoVEC theSaveRankings, theSaveDisharmonies; |
| 1966 |
autoBOOLVEC theSaveTiedToTheLeft, theSaveTiedToTheRight; |
| 1967 |
static void OTGrammar_save (OTGrammar me) { |
| 1968 |
if (my numberOfConstraints != theSaveNumberOfConstraints) { |
| 1969 |
theSaveIndex = raw_INTVEC (my numberOfConstraints); |
| 1970 |
theSaveRankings = raw_VEC (my numberOfConstraints); |
| 1971 |
theSaveDisharmonies = raw_VEC (my numberOfConstraints); |
| 1972 |
theSaveTiedToTheLeft = raw_BOOLVEC (my numberOfConstraints); |
| 1973 |
theSaveTiedToTheRight = raw_BOOLVEC (my numberOfConstraints); |
| 1974 |
theSaveNumberOfConstraints = my numberOfConstraints; |
| 1975 |
} |
| 1976 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1977 |
theSaveIndex [icons] = my index [icons]; |
| 1978 |
theSaveRankings [icons] = my constraints [icons]. ranking; |
| 1979 |
theSaveDisharmonies [icons] = my constraints [icons]. disharmony; |
| 1980 |
theSaveTiedToTheLeft [icons] = my constraints [icons]. tiedToTheLeft; |
| 1981 |
theSaveTiedToTheRight [icons] = my constraints [icons]. tiedToTheRight; |
| 1982 |
} |
| 1983 |
} |
| 1984 |
static void OTGrammar_restore (OTGrammar me) { |
| 1985 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 1986 |
my index [icons] = theSaveIndex [icons]; |
| 1987 |
my constraints [icons]. ranking = theSaveRankings [icons]; |
| 1988 |
my constraints [icons]. disharmony = theSaveDisharmonies [icons]; |
| 1989 |
my constraints [icons]. tiedToTheLeft = theSaveTiedToTheLeft [icons]; |
| 1990 |
my constraints [icons]. tiedToTheRight = theSaveTiedToTheRight [icons]; |
| 1991 |
} |
| 1992 |
} |
| 1993 |
|
| 1994 |
void OTGrammar_learnOneFromPartialOutput (OTGrammar me, conststring32 partialAdultOutput, |
| 1995 |
double evaluationNoise, enum kOTGrammar_rerankingStrategy updateRule, bool honourLocalRankings, |
| 1996 |
double plasticity, double relativePlasticityNoise, integer numberOfChews, bool warnIfStalled) |
| 1997 |
{ |
| 1998 |
try { |
| 1999 |
OTGrammar_newDisharmonies (me, evaluationNoise); |
| 2000 |
if (numberOfChews > 1 && updateRule == kOTGrammar_rerankingStrategy::EDCD) |
| 2001 |
OTGrammar_save (me); |
| 2002 |
integer ichew = 1; |
| 2003 |
for (; ichew <= numberOfChews; ichew ++) { |
| 2004 |
integer assumedAdultInputTableau, assumedAdultCandidate; |
| 2005 |
OTGrammar_getInterpretiveParse (me, partialAdultOutput, & assumedAdultInputTableau, & assumedAdultCandidate); |
| 2006 |
bool grammarHasChanged = false; |
| 2007 |
OTGrammar_learnOne (me, |
| 2008 |
my tableaus [assumedAdultInputTableau]. input.get(), |
| 2009 |
my tableaus [assumedAdultInputTableau]. candidates [assumedAdultCandidate]. output.get(), |
| 2010 |
evaluationNoise, updateRule, honourLocalRankings, |
| 2011 |
plasticity, relativePlasticityNoise, Melder_debug == 47, warnIfStalled, & grammarHasChanged |
| 2012 |
); |
| 2013 |
if (! grammarHasChanged) |
| 2014 |
return; |
| 2015 |
} |
| 2016 |
if (numberOfChews > 1 && updateRule == kOTGrammar_rerankingStrategy::EDCD && ichew > numberOfChews) { |
| 2017 |
/* |
| 2018 |
Is the partial output form grammatical by now? |
| 2019 |
*/ |
| 2020 |
integer assumedAdultInputTableau, assumedAdultCandidate; |
| 2021 |
OTGrammar_getInterpretiveParse (me, partialAdultOutput, & assumedAdultInputTableau, & assumedAdultCandidate); |
| 2022 |
const integer ilearnerCandidate = OTGrammar_getWinner (me, assumedAdultInputTableau); |
| 2023 |
OTGrammarCandidate learnerCandidate = & my tableaus [assumedAdultInputTableau]. candidates [ilearnerCandidate]; |
| 2024 |
if (! str32equ (learnerCandidate -> output.get(), |
| 2025 |
my tableaus [assumedAdultInputTableau]. candidates [assumedAdultCandidate]. output.get())) |
| 2026 |
{ /* Still ungrammatical? */ |
| 2027 |
/* |
| 2028 |
Backtrack as in Tesar & Smolensky 2000:69. |
| 2029 |
*/ |
| 2030 |
OTGrammar_restore (me); |
| 2031 |
} |
| 2032 |
} |
| 2033 |
} catch (MelderError) { |
| 2034 |
Melder_throw (me, U": not learned from partial adult output \"", partialAdultOutput, U"\"."); |
| 2035 |
} |
| 2036 |
} |
| 2037 |
|
| 2038 |
static void OTGrammar_learnOneFromPartialOutput_opt (OTGrammar me, conststring32 partialAdultOutput, integer ipartialAdultOutput, |
| 2039 |
double evaluationNoise, enum kOTGrammar_rerankingStrategy updateRule, bool honourLocalRankings, |
| 2040 |
double plasticity, double relativePlasticityNoise, integer numberOfChews, bool warnIfStalled, |
| 2041 |
bool resampleForVirtualProduction, bool compareOnlyPartialOutput, integer resampleForCorrectForm) |
| 2042 |
{ |
| 2043 |
try { |
| 2044 |
OTGrammar_newDisharmonies (me, evaluationNoise); |
| 2045 |
if (numberOfChews > 1 && updateRule == kOTGrammar_rerankingStrategy::EDCD) |
| 2046 |
OTGrammar_save (me); |
| 2047 |
integer ichew = 1; |
| 2048 |
for (; ichew <= numberOfChews; ichew ++) { |
| 2049 |
integer assumedAdultInputTableau, assumedAdultCandidate; |
| 2050 |
OTGrammar_getInterpretiveParse_opt (me, ipartialAdultOutput, & assumedAdultInputTableau, & assumedAdultCandidate); |
| 2051 |
OTGrammarTableau tableau = & my tableaus [assumedAdultInputTableau]; |
| 2052 |
OTGrammarCandidate assumedCorrect = & tableau -> candidates [assumedAdultCandidate]; |
| 2053 |
|
| 2054 |
/* |
| 2055 |
Determine the "winner", i.e. the candidate that wins in the learner's grammar |
| 2056 |
(Tesar & Smolensky call this the "loser"). |
| 2057 |
*/ |
| 2058 |
if (resampleForVirtualProduction) |
| 2059 |
OTGrammar_newDisharmonies (me, evaluationNoise); |
| 2060 |
integer iwinner = OTGrammar_getWinner (me, assumedAdultInputTableau); |
| 2061 |
OTGrammarCandidate winner = & tableau -> candidates [iwinner]; |
| 2062 |
|
| 2063 |
/* |
| 2064 |
Error-driven: compare the adult winner (the correct candidate) and the learner's winner. |
| 2065 |
*/ |
| 2066 |
if (compareOnlyPartialOutput) { |
| 2067 |
if (str32str (winner -> output.get(), partialAdultOutput)) |
| 2068 |
return; // as far as we know, the grammar is already correct: don't update rankings |
| 2069 |
} else { |
| 2070 |
if (str32equ (winner -> output.get(), assumedCorrect -> output.get())) |
| 2071 |
return; // as far as we know, the grammar is already correct: don't update rankings |
| 2072 |
} |
| 2073 |
if (resampleForCorrectForm) { |
| 2074 |
integer itry = 1; |
| 2075 |
for (; itry <= resampleForCorrectForm; itry ++) { |
| 2076 |
OTGrammar_newDisharmonies (me, evaluationNoise); |
| 2077 |
integer iwinner2 = OTGrammar_getWinner (me, assumedAdultInputTableau); |
| 2078 |
OTGrammarCandidate winner2 = & tableau -> candidates [iwinner2]; |
| 2079 |
if (compareOnlyPartialOutput) { |
| 2080 |
if (str32str (winner2 -> output.get(), partialAdultOutput)) { |
| 2081 |
assumedAdultCandidate = iwinner2; |
| 2082 |
break; |
| 2083 |
} |
| 2084 |
} else { |
| 2085 |
if (str32equ (winner2 -> output.get(), assumedCorrect -> output.get())) { |
| 2086 |
assumedAdultCandidate = iwinner2; |
| 2087 |
break; |
| 2088 |
} |
| 2089 |
} |
| 2090 |
} |
| 2091 |
if (itry > resampleForCorrectForm) |
| 2092 |
return; // no match, so bail out |
| 2093 |
} |
| 2094 |
|
| 2095 |
/* |
| 2096 |
Now we know that the current hypothesis prefers the (wrong) learner's winner over the (correct) adult output. |
| 2097 |
The grammar will have to change. |
| 2098 |
*/ |
| 2099 |
bool grammarHasChanged = false; |
| 2100 |
OTGrammar_modifyRankings (me, assumedAdultInputTableau, iwinner, assumedAdultCandidate, updateRule, honourLocalRankings, |
| 2101 |
plasticity, relativePlasticityNoise, warnIfStalled, & grammarHasChanged); |
| 2102 |
if (! grammarHasChanged) |
| 2103 |
return; |
| 2104 |
} |
| 2105 |
if (numberOfChews > 1 && updateRule == kOTGrammar_rerankingStrategy::EDCD && ichew > numberOfChews) { |
| 2106 |
/* |
| 2107 |
Is the partial output form grammatical by now? |
| 2108 |
*/ |
| 2109 |
integer assumedAdultInputTableau, assumedAdultCandidate; |
| 2110 |
OTGrammar_getInterpretiveParse_opt (me, ipartialAdultOutput, & assumedAdultInputTableau, & assumedAdultCandidate); |
| 2111 |
const integer ilearnerCandidate = OTGrammar_getWinner (me, assumedAdultInputTableau); |
| 2112 |
OTGrammarCandidate learnerCandidate = & my tableaus [assumedAdultInputTableau]. candidates [ilearnerCandidate]; |
| 2113 |
if (! str32equ (learnerCandidate -> output.get(), |
| 2114 |
my tableaus [assumedAdultInputTableau]. candidates [assumedAdultCandidate]. output.get())) |
| 2115 |
{ /* Still ungrammatical? */ |
| 2116 |
/* |
| 2117 |
Backtrack as in Tesar & Smolensky 2000:69. |
| 2118 |
*/ |
| 2119 |
OTGrammar_restore (me); |
| 2120 |
} |
| 2121 |
} |
| 2122 |
} catch (MelderError) { |
| 2123 |
Melder_throw (me, U": not learned from partial adult output ", partialAdultOutput, U"."); |
| 2124 |
} |
| 2125 |
} |
| 2126 |
|
| 2127 |
static autoOTHistory OTGrammar_createHistory (OTGrammar me, integer storeHistoryEvery, integer numberOfData) { |
| 2128 |
try { |
| 2129 |
integer numberOfSamplingPoints = numberOfData / storeHistoryEvery; // e.g. 0, 20, 40, ... |
| 2130 |
autoOTHistory thee = Thing_new (OTHistory); |
| 2131 |
TableOfReal_init (thee.get(), 2 + numberOfSamplingPoints * 2, 1 + my numberOfConstraints); |
| 2132 |
TableOfReal_setColumnLabel (thee.get(), 1, U"Datum"); |
| 2133 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 2134 |
TableOfReal_setColumnLabel (thee.get(), icons + 1, my constraints [icons]. name.get()); |
| 2135 |
TableOfReal_setRowLabel (thee.get(), 1, U"Initial state"); |
| 2136 |
thy data [1] [1] = 0.0; |
| 2137 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 2138 |
thy data [1] [icons + 1] = my constraints [icons]. ranking; |
| 2139 |
return thee; |
| 2140 |
} catch (MelderError) { |
| 2141 |
Melder_throw (me, U": history not created."); |
| 2142 |
} |
| 2143 |
} |
| 2144 |
|
| 2145 |
static void OTGrammar_updateHistory (OTGrammar me, OTHistory thee, integer storeHistoryEvery, integer datumNumber, conststring32 input) { |
| 2146 |
try { |
| 2147 |
if (datumNumber % storeHistoryEvery == 0) { |
| 2148 |
integer rowNumber = 2 * datumNumber / storeHistoryEvery; |
| 2149 |
TableOfReal_setRowLabel (thee, rowNumber, input); |
| 2150 |
thy data [rowNumber] [1] = datumNumber; |
| 2151 |
thy data [rowNumber + 1] [1] = datumNumber; |
| 2152 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 2153 |
thy data [rowNumber] [icons + 1] = my constraints [icons]. disharmony; |
| 2154 |
thy data [rowNumber + 1] [icons + 1] = my constraints [icons]. ranking; |
| 2155 |
} |
| 2156 |
} |
| 2157 |
} catch (MelderError) { |
| 2158 |
Melder_throw (me, U": history not updated."); |
| 2159 |
} |
| 2160 |
} |
| 2161 |
|
| 2162 |
static void OTGrammar_finalizeHistory (OTGrammar me, OTHistory thee, integer datumNumber) { |
| 2163 |
try { |
| 2164 |
TableOfReal_setRowLabel (thee, thy numberOfRows, U"Final state"); |
| 2165 |
thy data [thy numberOfRows] [1] = datumNumber; |
| 2166 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 2167 |
thy data [thy numberOfRows] [icons + 1] = my constraints [icons]. ranking; |
| 2168 |
} catch (MelderError) { |
| 2169 |
Melder_throw (me, U": history not finalized."); |
| 2170 |
} |
| 2171 |
} |
| 2172 |
|
| 2173 |
void OTGrammar_learnFromPartialOutputs (OTGrammar me, Strings partialOutputs, |
| 2174 |
double evaluationNoise, enum kOTGrammar_rerankingStrategy updateRule, bool honourLocalRankings, |
| 2175 |
double plasticity, double relativePlasticityNoise, integer numberOfChews, |
| 2176 |
integer storeHistoryEvery, autoOTHistory *history_out) |
| 2177 |
{ |
| 2178 |
try { |
| 2179 |
autoOTHistory history; |
| 2180 |
if (storeHistoryEvery) |
| 2181 |
history = OTGrammar_createHistory (me, storeHistoryEvery, partialOutputs -> numberOfStrings); |
| 2182 |
try { |
| 2183 |
for (integer idatum = 1; idatum <= partialOutputs -> numberOfStrings; idatum ++) { |
| 2184 |
try { |
| 2185 |
OTGrammar_learnOneFromPartialOutput (me, partialOutputs -> strings [idatum].get(), |
| 2186 |
evaluationNoise, updateRule, honourLocalRankings, |
| 2187 |
plasticity, relativePlasticityNoise, numberOfChews, false); |
| 2188 |
} catch (MelderError) { |
| 2189 |
if (history) |
| 2190 |
OTGrammar_updateHistory (me, history.get(), storeHistoryEvery, idatum, partialOutputs -> strings [idatum].get()); // so that we can inspect |
| 2191 |
throw; |
| 2192 |
} |
| 2193 |
if (history) |
| 2194 |
OTGrammar_updateHistory (me, history.get(), storeHistoryEvery, idatum, partialOutputs -> strings [idatum].get()); |
| 2195 |
} |
| 2196 |
if (history) |
| 2197 |
OTGrammar_finalizeHistory (me, history.get(), partialOutputs -> numberOfStrings); |
| 2198 |
*history_out = history.move(); |
| 2199 |
} catch (MelderError) { |
| 2200 |
*history_out = history.move(); // so that we can inspect |
| 2201 |
throw; |
| 2202 |
} |
| 2203 |
} catch (MelderError) { |
| 2204 |
Melder_throw (me, U": not learned from partial outputs ", partialOutputs, U"."); |
| 2205 |
} |
| 2206 |
} |
| 2207 |
|
| 2208 |
static void OTGrammar_opt_deleteOutputMatching (OTGrammar me) { |
| 2209 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) { |
| 2210 |
OTGrammarTableau tab = & my tableaus [itab]; |
| 2211 |
for (integer icand = 1; icand <= tab -> numberOfCandidates; icand ++) { |
| 2212 |
OTGrammarCandidate cand = & tab -> candidates [icand]; |
| 2213 |
cand -> numberOfPotentialPartialOutputsMatching = 0; |
| 2214 |
cand -> partialOutputMatches. reset(); |
| 2215 |
} |
| 2216 |
} |
| 2217 |
} |
| 2218 |
|
| 2219 |
static void OTGrammar_Distributions_opt_createOutputMatching (OTGrammar me, Distributions thee, integer columnNumber) { |
| 2220 |
try { |
| 2221 |
if (columnNumber > thy numberOfColumns) |
| 2222 |
Melder_throw (U"No column ", columnNumber, U" in Distributions."); |
| 2223 |
if (thy numberOfRows < 1) |
| 2224 |
Melder_throw (U"No candidates in Distributions."); |
| 2225 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) { |
| 2226 |
OTGrammarTableau tab = & my tableaus [itab]; |
| 2227 |
for (integer icand = 1; icand <= tab -> numberOfCandidates; icand ++) { |
| 2228 |
OTGrammarCandidate cand = & tab -> candidates [icand]; |
| 2229 |
cand -> numberOfPotentialPartialOutputsMatching = thy numberOfRows; |
| 2230 |
cand -> partialOutputMatches = zero_BOOLVEC (thy numberOfRows); |
| 2231 |
} |
| 2232 |
} |
| 2233 |
for (integer ipartialOutput = 1; ipartialOutput <= thy numberOfRows; ipartialOutput ++) { |
| 2234 |
if (thy data [ipartialOutput] [columnNumber] > 0.0) { |
| 2235 |
conststring32 partialOutput = thy rowLabels [ipartialOutput].get(); |
| 2236 |
bool foundPartialOutput = false; |
| 2237 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) { |
| 2238 |
OTGrammarTableau tab = & my tableaus [itab]; |
| 2239 |
for (integer icand = 1; icand <= tab -> numberOfCandidates; icand ++) { |
| 2240 |
OTGrammarCandidate cand = & tab -> candidates [icand]; |
| 2241 |
if (str32str (cand -> output.get(), partialOutput)) { |
| 2242 |
foundPartialOutput = true; |
| 2243 |
cand -> partialOutputMatches [ipartialOutput] = true; |
| 2244 |
} |
| 2245 |
} |
| 2246 |
} |
| 2247 |
if (! foundPartialOutput) |
| 2248 |
Melder_throw (U"The partial output \"", partialOutput, U"\" does not match any candidate for any input form."); |
| 2249 |
} |
| 2250 |
} |
| 2251 |
} catch (MelderError) { |
| 2252 |
OTGrammar_opt_deleteOutputMatching (me); |
| 2253 |
throw; |
| 2254 |
} |
| 2255 |
} |
| 2256 |
|
| 2257 |
void OTGrammar_Distributions_learnFromPartialOutputs (OTGrammar me, Distributions thee, integer columnNumber, |
| 2258 |
double evaluationNoise, enum kOTGrammar_rerankingStrategy updateRule, bool honourLocalRankings, |
| 2259 |
double initialPlasticity, integer replicationsPerPlasticity, double plasticityDecrement, |
| 2260 |
integer numberOfPlasticities, double relativePlasticityNoise, integer numberOfChews, |
| 2261 |
integer storeHistoryEvery, autoOTHistory *history_out, |
| 2262 |
bool resampleForVirtualProduction, bool compareOnlyPartialOutput, integer resampleForCorrectForm) |
| 2263 |
{ |
| 2264 |
integer idatum = 0; |
| 2265 |
const integer numberOfData = numberOfPlasticities * replicationsPerPlasticity; |
| 2266 |
try { |
| 2267 |
autoOTHistory history; |
| 2268 |
OTGrammar_Distributions_opt_createOutputMatching (me, thee, columnNumber); |
| 2269 |
autoMelderMonitor monitor (U"Learning with limited knowledge..."); |
| 2270 |
if (storeHistoryEvery) |
| 2271 |
history = OTGrammar_createHistory (me, storeHistoryEvery, numberOfData); |
| 2272 |
try { |
| 2273 |
double plasticity = initialPlasticity; |
| 2274 |
for (integer iplasticity = 1; iplasticity <= numberOfPlasticities; iplasticity ++) { |
| 2275 |
for (integer ireplication = 1; ireplication <= replicationsPerPlasticity; ireplication ++) { |
| 2276 |
conststring32 partialOutput; |
| 2277 |
integer ipartialOutput; |
| 2278 |
Distributions_peek (thee, columnNumber, & partialOutput, & ipartialOutput); |
| 2279 |
++ idatum; |
| 2280 |
if (monitor.graphics() && idatum % (numberOfData / 400 + 1) == 0) { |
| 2281 |
Graphics_beginMovieFrame (monitor.graphics(), nullptr); |
| 2282 |
Graphics_setWindow (monitor.graphics(), 0, numberOfData, 50.0, 150.0); |
| 2283 |
for (integer icons = 1; icons <= 14 && icons <= my numberOfConstraints; icons ++) { |
| 2284 |
Graphics_setGrey (monitor.graphics(), (double) icons / 14); |
| 2285 |
Graphics_line (monitor.graphics(), |
| 2286 |
idatum, my constraints [icons]. ranking, |
| 2287 |
idatum, my constraints [icons]. ranking + 10.0 |
| 2288 |
); |
| 2289 |
} |
| 2290 |
Graphics_endMovieFrame (monitor.graphics(), 0.0); |
| 2291 |
} |
| 2292 |
Melder_monitor ((double) idatum / numberOfData, |
| 2293 |
U"Processing partial output ", idatum, U" out of ", numberOfData, U": ", |
| 2294 |
thy rowLabels [ipartialOutput].get() |
| 2295 |
); |
| 2296 |
try { |
| 2297 |
OTGrammar_learnOneFromPartialOutput_opt (me, partialOutput, ipartialOutput, |
| 2298 |
evaluationNoise, updateRule, honourLocalRankings, |
| 2299 |
plasticity, relativePlasticityNoise, numberOfChews, false, |
| 2300 |
resampleForVirtualProduction, compareOnlyPartialOutput, resampleForCorrectForm |
| 2301 |
); // no warning if stalled: RIP form is allowed to be harmonically bounded |
| 2302 |
} catch (MelderError) { |
| 2303 |
if (history) |
| 2304 |
OTGrammar_updateHistory (me, history.get(), storeHistoryEvery, idatum, thy rowLabels [ipartialOutput].get()); |
| 2305 |
throw; |
| 2306 |
} |
| 2307 |
if (history) |
| 2308 |
OTGrammar_updateHistory (me, history.get(), storeHistoryEvery, idatum, thy rowLabels [ipartialOutput].get()); |
| 2309 |
} |
| 2310 |
plasticity *= plasticityDecrement; |
| 2311 |
} |
| 2312 |
if (history) |
| 2313 |
OTGrammar_finalizeHistory (me, history.get(), numberOfData); |
| 2314 |
OTGrammar_opt_deleteOutputMatching (me); |
| 2315 |
if (history_out) |
| 2316 |
*history_out = history.move(); |
| 2317 |
} catch (MelderError) { |
| 2318 |
OTGrammar_opt_deleteOutputMatching (me); |
| 2319 |
if (history_out) |
| 2320 |
*history_out = history.move(); // so that we can inspect |
| 2321 |
throw; |
| 2322 |
} |
| 2323 |
} catch (MelderError) { |
| 2324 |
if (idatum > 1) |
| 2325 |
Melder_appendError (U"Only ", idatum - 1, U" input-output pairs out of ", numberOfData, U" were processed."); |
| 2326 |
Melder_throw (me, U" & ", thee, U": not learned from partial outputs."); |
| 2327 |
} |
| 2328 |
} |
| 2329 |
|
| 2330 |
double OTGrammar_PairDistribution_getFractionCorrect (OTGrammar me, PairDistribution thee, |
| 2331 |
double evaluationNoise, integer numberOfInputs) |
| 2332 |
{ |
| 2333 |
try { |
| 2334 |
integer numberOfCorrect = 0; |
| 2335 |
for (integer ireplication = 1; ireplication <= numberOfInputs; ireplication ++) { |
| 2336 |
conststring32 input, adultOutput; |
| 2337 |
PairDistribution_peekPair (thee, & input, & adultOutput); |
| 2338 |
OTGrammar_newDisharmonies (me, evaluationNoise); |
| 2339 |
integer inputTableau = OTGrammar_getTableau (me, input); |
| 2340 |
const integer ilearnerCandidate = OTGrammar_getWinner (me, inputTableau); |
| 2341 |
OTGrammarCandidate learnerCandidate = & my tableaus [inputTableau]. candidates [ilearnerCandidate]; |
| 2342 |
if (str32equ (learnerCandidate -> output.get(), adultOutput)) |
| 2343 |
numberOfCorrect ++; |
| 2344 |
} |
| 2345 |
return (double) numberOfCorrect / numberOfInputs; |
| 2346 |
} catch (MelderError) { |
| 2347 |
Melder_throw (me, U" & ", thee, U": fraction correct not computed."); |
| 2348 |
} |
| 2349 |
} |
| 2350 |
|
| 2351 |
integer OTGrammar_PairDistribution_getMinimumNumberCorrect (OTGrammar me, PairDistribution thee, |
| 2352 |
double evaluationNoise, integer numberOfReplications) |
| 2353 |
{ |
| 2354 |
try { |
| 2355 |
integer minimumNumberCorrect = numberOfReplications; |
| 2356 |
for (integer ipair = 1; ipair <= thy pairs.size; ipair ++) { |
| 2357 |
PairProbability prob = thy pairs.at [ipair]; |
| 2358 |
if (prob -> weight > 0.0) { |
| 2359 |
integer numberOfCorrect = 0; |
| 2360 |
conststring32 input = prob -> string1.get(), adultOutput = prob -> string2.get(); |
| 2361 |
integer inputTableau = OTGrammar_getTableau (me, input); |
| 2362 |
for (integer ireplication = 1; ireplication <= numberOfReplications; ireplication ++) { |
| 2363 |
OTGrammar_newDisharmonies (me, evaluationNoise); |
| 2364 |
const integer ilearnerCandidate = OTGrammar_getWinner (me, inputTableau); |
| 2365 |
OTGrammarCandidate learnerCandidate = & my tableaus [inputTableau]. candidates [ilearnerCandidate]; |
| 2366 |
if (str32equ (learnerCandidate -> output.get(), adultOutput)) |
| 2367 |
numberOfCorrect ++; |
| 2368 |
} |
| 2369 |
if (numberOfCorrect < minimumNumberCorrect) |
| 2370 |
minimumNumberCorrect = numberOfCorrect; |
| 2371 |
} |
| 2372 |
} |
| 2373 |
return minimumNumberCorrect; |
| 2374 |
} catch (MelderError) { |
| 2375 |
Melder_throw (me, U" & ", thee, U": minimum number correct not computed."); |
| 2376 |
} |
| 2377 |
} |
| 2378 |
|
| 2379 |
double OTGrammar_Distributions_getFractionCorrect (OTGrammar me, Distributions thee, integer columnNumber, |
| 2380 |
double evaluationNoise, integer numberOfInputs) |
| 2381 |
{ |
| 2382 |
try { |
| 2383 |
integer numberOfCorrect = 0; |
| 2384 |
OTGrammar_Distributions_opt_createOutputMatching (me, thee, columnNumber); |
| 2385 |
for (integer ireplication = 1; ireplication <= numberOfInputs; ireplication ++) { |
| 2386 |
integer ipartialOutput; |
| 2387 |
Distributions_peek (thee, columnNumber, nullptr, & ipartialOutput); |
| 2388 |
OTGrammar_newDisharmonies (me, evaluationNoise); |
| 2389 |
integer assumedAdultInputTableau, assumedAdultCandidate; |
| 2390 |
OTGrammar_getInterpretiveParse_opt (me, ipartialOutput, & assumedAdultInputTableau, & assumedAdultCandidate); |
| 2391 |
const integer ilearnerCandidate = OTGrammar_getWinner (me, assumedAdultInputTableau); |
| 2392 |
OTGrammarCandidate learnerCandidate = & my tableaus [assumedAdultInputTableau]. candidates [ilearnerCandidate]; |
| 2393 |
if (str32equ (learnerCandidate -> output.get(), my tableaus [assumedAdultInputTableau]. candidates [assumedAdultCandidate]. output.get())) |
| 2394 |
numberOfCorrect ++; |
| 2395 |
} |
| 2396 |
OTGrammar_opt_deleteOutputMatching (me); |
| 2397 |
return (double) numberOfCorrect / numberOfInputs; |
| 2398 |
} catch (MelderError) { |
| 2399 |
Melder_throw (me, U" & ", thee, U": fraction correct not computed."); |
| 2400 |
} |
| 2401 |
} |
| 2402 |
|
| 2403 |
void OTGrammar_removeConstraint (OTGrammar me, conststring32 constraintName) { |
| 2404 |
try { |
| 2405 |
integer removed = 0; |
| 2406 |
Melder_require (my numberOfConstraints > 1, |
| 2407 |
U"Cannot remove last remaining constraint."); |
| 2408 |
|
| 2409 |
/* |
| 2410 |
Look for the constraint to be removed. |
| 2411 |
*/ |
| 2412 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 2413 |
OTGrammarConstraint constraint = & my constraints [icons]; |
| 2414 |
if (str32equ (constraint -> name.get(), constraintName)) { |
| 2415 |
removed = icons; |
| 2416 |
break; |
| 2417 |
} |
| 2418 |
} |
| 2419 |
if (removed == 0) |
| 2420 |
Melder_throw (U"No such constraint."); |
| 2421 |
/* |
| 2422 |
Remove the constraint while reusing the memory space. |
| 2423 |
*/ |
| 2424 |
my constraints [removed]. destroy (); |
| 2425 |
Melder_assert (! my constraints [removed]. name); |
| 2426 |
my constraints. remove (removed); |
| 2427 |
my numberOfConstraints -= 1; // maintain invariant |
| 2428 |
Melder_assert (my numberOfConstraints == my constraints.size); |
| 2429 |
/* |
| 2430 |
Remove or shift fixed rankings. |
| 2431 |
*/ |
| 2432 |
for (integer ifixed = my numberOfFixedRankings; ifixed > 0; ifixed --) { |
| 2433 |
OTGrammarFixedRanking fixed = & my fixedRankings [ifixed]; |
| 2434 |
if (fixed -> higher == removed || fixed -> lower == removed) { |
| 2435 |
/* |
| 2436 |
Remove fixed ranking. |
| 2437 |
*/ |
| 2438 |
my fixedRankings [ifixed]. destroy(); |
| 2439 |
my fixedRankings. remove (ifixed); |
| 2440 |
my numberOfFixedRankings -= 1; // maintain invariant |
| 2441 |
Melder_assert (my numberOfFixedRankings == my fixedRankings.size); |
| 2442 |
} else { |
| 2443 |
/* |
| 2444 |
Shift fixed ranking. |
| 2445 |
*/ |
| 2446 |
if (fixed -> higher > removed) |
| 2447 |
fixed -> higher -= 1; |
| 2448 |
if (fixed -> lower > removed) |
| 2449 |
fixed -> lower -= 1; |
| 2450 |
} |
| 2451 |
} |
| 2452 |
/* |
| 2453 |
Shift tableau rows. |
| 2454 |
*/ |
| 2455 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) { |
| 2456 |
OTGrammarTableau tableau = & my tableaus [itab]; |
| 2457 |
for (integer icand = 1; icand <= tableau -> numberOfCandidates; icand ++) { |
| 2458 |
OTGrammarCandidate candidate = & tableau -> candidates [icand]; |
| 2459 |
candidate -> marks. remove (removed); |
| 2460 |
candidate -> numberOfConstraints -= 1; // maintain invariant |
| 2461 |
Melder_assert (candidate -> numberOfConstraints == candidate -> marks.size); |
| 2462 |
} |
| 2463 |
} |
| 2464 |
/* |
| 2465 |
Rebuild index. |
| 2466 |
*/ |
| 2467 |
my index. resize (my numberOfConstraints); |
| 2468 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 2469 |
my index [icons] = icons; |
| 2470 |
OTGrammar_sort (me); |
| 2471 |
} catch (MelderError) { |
| 2472 |
Melder_throw (me, U": constraint \"", constraintName, U"\" not removed."); |
| 2473 |
} |
| 2474 |
} |
| 2475 |
|
| 2476 |
static void OTGrammarTableau_removeCandidate_unstripped (OTGrammarTableau me, integer candidateNumber) { |
| 2477 |
Melder_assert (candidateNumber >= 1); |
| 2478 |
if (candidateNumber > my numberOfCandidates) |
| 2479 |
Melder_crash (U"icand ", candidateNumber, U", ncand ", my numberOfCandidates); |
| 2480 |
|
| 2481 |
my candidates [candidateNumber]. destroy (); |
| 2482 |
Melder_assert (! my candidates [candidateNumber]. output); // check leak |
| 2483 |
Melder_assert (my candidates [candidateNumber]. marks.size == 0); |
| 2484 |
Melder_assert (my candidates [candidateNumber]. marks.cells == nullptr); // check leak |
| 2485 |
my candidates. remove (candidateNumber); |
| 2486 |
my numberOfCandidates -= 1; // maintain invariant |
| 2487 |
Melder_assert (my numberOfCandidates == my candidates.size); |
| 2488 |
} |
| 2489 |
|
| 2490 |
static bool OTGrammarTableau_isHarmonicallyBounded (OTGrammarTableau me, integer icand, integer jcand) { |
| 2491 |
OTGrammarCandidate candi = & my candidates [icand], candj = & my candidates [jcand]; |
| 2492 |
bool equal = true; |
| 2493 |
if (icand == jcand) |
| 2494 |
return false; |
| 2495 |
for (integer icons = 1; icons <= candi -> numberOfConstraints; icons ++) { |
| 2496 |
if (candi -> marks [icons] < candj -> marks [icons]) |
| 2497 |
return false; |
| 2498 |
if (candi -> marks [icons] > candj -> marks [icons]) |
| 2499 |
equal = false; |
| 2500 |
} |
| 2501 |
return ! equal; |
| 2502 |
} |
| 2503 |
|
| 2504 |
static bool OTGrammarTableau_candidateIsPossibleWinner (OTGrammar me, integer itab, integer icand) { |
| 2505 |
OTGrammar_save (me); |
| 2506 |
try { |
| 2507 |
OTGrammar_reset (me, 100.0); |
| 2508 |
for (;;) { |
| 2509 |
bool grammarHasChanged = false; |
| 2510 |
OTGrammar_learnOne (me, my tableaus [itab]. input.get(), my tableaus [itab]. candidates [icand]. output.get(), |
| 2511 |
1e-3, kOTGrammar_rerankingStrategy::EDCD, false, 1.0, 0.0, true, true, & grammarHasChanged); |
| 2512 |
if (! grammarHasChanged) { |
| 2513 |
OTGrammar_restore (me); |
| 2514 |
return true; |
| 2515 |
} |
| 2516 |
double previousStratum = 101.0; |
| 2517 |
OTGrammar_newDisharmonies (me, 0.0); |
| 2518 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 2519 |
const double stratum = my constraints [my index [icons]]. ranking; |
| 2520 |
#if 0 |
| 2521 |
if (stratum < 50.0 - my numberOfConstraints) { |
| 2522 |
OTGrammar_restore (me); |
| 2523 |
return false; // we detected a tumble |
| 2524 |
} |
| 2525 |
#else |
| 2526 |
if (stratum < previousStratum) { |
| 2527 |
if (stratum < previousStratum - 1.0) { |
| 2528 |
OTGrammar_restore (me); |
| 2529 |
return false; // we detected a vacated stratum |
| 2530 |
} |
| 2531 |
previousStratum = stratum; |
| 2532 |
} |
| 2533 |
#endif |
| 2534 |
} |
| 2535 |
} |
| 2536 |
} catch (MelderError) { |
| 2537 |
OTGrammar_restore (me); // strong exception guarantee |
| 2538 |
throw; |
| 2539 |
} |
| 2540 |
return false; // cannot occur |
| 2541 |
} |
| 2542 |
|
| 2543 |
void OTGrammar_removeHarmonicallyBoundedCandidates (OTGrammar me, bool singly) { |
| 2544 |
try { |
| 2545 |
/* |
| 2546 |
First, the candidates that are harmonically bounded by one or more single other candidates have to be removed; |
| 2547 |
otherwise, EDCD will stall. |
| 2548 |
*/ |
| 2549 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) { |
| 2550 |
OTGrammarTableau tab = & my tableaus [itab]; |
| 2551 |
for (integer icand = tab -> numberOfCandidates; icand >= 1; icand --) { |
| 2552 |
for (integer jcand = 1; jcand <= tab -> numberOfCandidates; jcand ++) { |
| 2553 |
if (OTGrammarTableau_isHarmonicallyBounded (tab, icand, jcand)) { |
| 2554 |
OTGrammarTableau_removeCandidate_unstripped (tab, icand); |
| 2555 |
break; |
| 2556 |
} |
| 2557 |
} |
| 2558 |
} |
| 2559 |
//tab -> candidates.shrinkToFit(); |
| 2560 |
} |
| 2561 |
if (! singly) { |
| 2562 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) { |
| 2563 |
OTGrammarTableau tab = & my tableaus [itab]; |
| 2564 |
for (integer icand = tab -> numberOfCandidates; icand >= 1; icand --) { |
| 2565 |
if (! OTGrammarTableau_candidateIsPossibleWinner (me, itab, icand)) |
| 2566 |
OTGrammarTableau_removeCandidate_unstripped (tab, icand); |
| 2567 |
} |
| 2568 |
//tab -> candidates.shrinkToFit(); |
| 2569 |
} |
| 2570 |
} |
| 2571 |
} catch (MelderError) { |
| 2572 |
Melder_throw (me, U": not all harmonically bounded candidates were removed."); |
| 2573 |
} |
| 2574 |
} |
| 2575 |
|
| 2576 |
static bool OTGrammarTableau_candidatesAreDistinguishable (constOTGrammarTableau me, integer icand, integer jcand) { |
| 2577 |
OTGrammarCandidate candi = & my candidates [icand], candj = & my candidates [jcand]; |
| 2578 |
for (integer icons = 1; icons <= candi -> numberOfConstraints; icons ++) |
| 2579 |
if (candi -> marks [icons] != candj -> marks [icons]) |
| 2580 |
return true; |
| 2581 |
return false; |
| 2582 |
} |
| 2583 |
|
| 2584 |
static bool OTGrammarTableau_areAllCandidatesDistinguishable (constOTGrammarTableau me) { |
| 2585 |
for (integer icand = 1; icand < my numberOfCandidates; icand ++) |
| 2586 |
for (integer jcand = icand + 1; jcand <= my numberOfCandidates; jcand ++) |
| 2587 |
if (! OTGrammarTableau_candidatesAreDistinguishable (me, icand, jcand)) |
| 2588 |
return false; |
| 2589 |
return true; |
| 2590 |
} |
| 2591 |
|
| 2592 |
bool OTGrammar_areAllOutputsDistinguishable (OTGrammar me) { |
| 2593 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) { |
| 2594 |
OTGrammarTableau tab = & my tableaus [itab]; |
| 2595 |
if (! OTGrammarTableau_areAllCandidatesDistinguishable (tab)) |
| 2596 |
return false; |
| 2597 |
} |
| 2598 |
return true; |
| 2599 |
} |
| 2600 |
|
| 2601 |
autoTable OTGrammar_tabulateEquivalentPairs (OTGrammar me) { |
| 2602 |
try { |
| 2603 |
autoTable thee = Table_createWithoutColumnNames (0, 6 + my numberOfConstraints); |
| 2604 |
thy columnHeaders [1]. label = Melder_dup (U"tableau"); |
| 2605 |
thy columnHeaders [2]. label = Melder_dup (U"Tableau"); |
| 2606 |
thy columnHeaders [3]. label = Melder_dup (U"candi"); |
| 2607 |
thy columnHeaders [4]. label = Melder_dup (U"Candi"); |
| 2608 |
thy columnHeaders [5]. label = Melder_dup (U"candj"); |
| 2609 |
thy columnHeaders [6]. label = Melder_dup (U"Candj"); |
| 2610 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 2611 |
thy columnHeaders [6 + icons]. label = Melder_dup (my constraints [icons]. name.get()); |
| 2612 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) { |
| 2613 |
const constOTGrammarTableau tab = & my tableaus [itab]; |
| 2614 |
for (integer icand = 1; icand < tab -> numberOfCandidates; icand ++) { |
| 2615 |
for (integer jcand = icand + 1; jcand <= tab -> numberOfCandidates; jcand ++) { |
| 2616 |
if (! OTGrammarTableau_candidatesAreDistinguishable (tab, icand, jcand)) { |
| 2617 |
Table_appendRow (thee.get()); |
| 2618 |
const integer rowNumber = thy rows.size; |
| 2619 |
Table_setNumericValue (thee.get(), rowNumber, 1, itab); |
| 2620 |
Table_setStringValue (thee.get(), rowNumber, 2, tab -> input.get()); |
| 2621 |
Table_setNumericValue (thee.get(), rowNumber, 3, icand); |
| 2622 |
const constOTGrammarCandidate candi = & tab -> candidates [icand]; |
| 2623 |
Table_setStringValue (thee.get(), rowNumber, 4, candi -> output.get()); |
| 2624 |
Table_setNumericValue (thee.get(), rowNumber, 5, jcand); |
| 2625 |
Table_setStringValue (thee.get(), rowNumber, 6, tab -> candidates [jcand]. output.get()); |
| 2626 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) |
| 2627 |
Table_setNumericValue (thee.get(), rowNumber, 6 + icons, candi -> marks [icons]); |
| 2628 |
} |
| 2629 |
} |
| 2630 |
} |
| 2631 |
} |
| 2632 |
return thee; |
| 2633 |
} catch (MelderError) { |
| 2634 |
Melder_throw (me, U": equivalent pairs not tabulated."); |
| 2635 |
} |
| 2636 |
} |
| 2637 |
|
| 2638 |
Thing_define (OTGrammar_List4, Daata) { |
| 2639 |
// new data: |
| 2640 |
integer hi1, lo1, hi2, lo2; |
| 2641 |
}; |
| 2642 |
|
| 2643 |
Thing_implement (OTGrammar_List4, Daata, 0); |
| 2644 |
|
| 2645 |
void OTGrammar_PairDistribution_listObligatoryRankings (OTGrammar me, PairDistribution thee) { |
| 2646 |
/* |
| 2647 |
Save. |
| 2648 |
*/ |
| 2649 |
integer savedNumberOfFixedRankings = my numberOfFixedRankings; |
| 2650 |
autovector <structOTGrammarFixedRanking> savedFixedRankings = my fixedRankings.move(); // BUG: is not restored upon throw |
| 2651 |
OTGrammar_save (me); |
| 2652 |
try { |
| 2653 |
integer ipair = 0, npair = my numberOfConstraints * (my numberOfConstraints - 1); |
| 2654 |
integer itrial; |
| 2655 |
const double evaluationNoise = 1e-9; |
| 2656 |
/* |
| 2657 |
Add room for two more fixed rankings. |
| 2658 |
*/ |
| 2659 |
my fixedRankings = newvectorzero <structOTGrammarFixedRanking> (my numberOfFixedRankings + 2); |
| 2660 |
for (integer ifixedRanking = 1; ifixedRanking <= my numberOfFixedRankings; ifixedRanking ++) { |
| 2661 |
my fixedRankings [ifixedRanking]. higher = savedFixedRankings [ifixedRanking]. higher; |
| 2662 |
my fixedRankings [ifixedRanking]. lower = savedFixedRankings [ifixedRanking]. lower; |
| 2663 |
} |
| 2664 |
/* |
| 2665 |
Test whether there are rankings at all for these output data. |
| 2666 |
*/ |
| 2667 |
OTGrammar_reset (me, 100.0); |
| 2668 |
for (itrial = 1; itrial <= 40; itrial ++) { |
| 2669 |
bool grammarHasChangedDuringCycle = false; |
| 2670 |
OTGrammar_honourLocalRankings (me, 1.0, 0.0, & grammarHasChangedDuringCycle); |
| 2671 |
OTGrammar_newDisharmonies (me, evaluationNoise); |
| 2672 |
for (integer iform = 1; iform <= thy pairs.size; iform ++) { |
| 2673 |
PairProbability prob = thy pairs.at [iform]; |
| 2674 |
if (prob -> weight > 0.0) { |
| 2675 |
bool grammarHasChanged = false; |
| 2676 |
OTGrammar_learnOne (me, prob -> string1.get(), prob -> string2.get(), |
| 2677 |
evaluationNoise, kOTGrammar_rerankingStrategy::EDCD, true /* honour fixed rankings; very important */, |
| 2678 |
1.0, 0.0, false, true, & grammarHasChanged |
| 2679 |
); |
| 2680 |
if (grammarHasChanged) |
| 2681 |
OTGrammar_newDisharmonies (me, evaluationNoise); |
| 2682 |
grammarHasChangedDuringCycle |= grammarHasChanged; |
| 2683 |
} |
| 2684 |
} |
| 2685 |
if (! grammarHasChangedDuringCycle) |
| 2686 |
break; |
| 2687 |
} |
| 2688 |
if (itrial > 40) { |
| 2689 |
MelderInfo_writeLine (U"There are no total rankings that generate these input-output pairs."); |
| 2690 |
throw MelderError (); |
| 2691 |
} |
| 2692 |
/* |
| 2693 |
Test learnability of every possible ranked pair. |
| 2694 |
*/ |
| 2695 |
my numberOfFixedRankings ++; |
| 2696 |
autoBOOLMAT obligatory = zero_BOOLMAT (my numberOfConstraints, my numberOfConstraints); |
| 2697 |
MelderInfo_open (); |
| 2698 |
autoMelderProgress progress (U"Finding obligatory rankings."); |
| 2699 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 2700 |
for (integer jcons = 1; jcons <= my numberOfConstraints; jcons ++) if (icons != jcons) { |
| 2701 |
my fixedRankings [my numberOfFixedRankings]. higher = icons; |
| 2702 |
my fixedRankings [my numberOfFixedRankings]. lower = jcons; |
| 2703 |
OTGrammar_reset (me, 100.0); |
| 2704 |
Melder_progress ((double) ipair / npair, ipair + 1, U"/", npair, U": Trying ranking ", |
| 2705 |
my constraints [icons]. name.get(), U" >> ", my constraints [jcons]. name.get()); |
| 2706 |
ipair ++; |
| 2707 |
for (itrial = 1; itrial <= 40; itrial ++) { |
| 2708 |
bool grammarHasChangedDuringCycle = false; |
| 2709 |
OTGrammar_honourLocalRankings (me, 1.0, 0.0, & grammarHasChangedDuringCycle); |
| 2710 |
OTGrammar_newDisharmonies (me, evaluationNoise); |
| 2711 |
for (integer iform = 1; iform <= thy pairs.size; iform ++) { |
| 2712 |
PairProbability prob = thy pairs.at [iform]; |
| 2713 |
if (prob -> weight > 0.0) { |
| 2714 |
bool grammarHasChanged = false; |
| 2715 |
OTGrammar_learnOne (me, prob -> string1.get(), prob -> string2.get(), |
| 2716 |
evaluationNoise, kOTGrammar_rerankingStrategy::EDCD, true /* honour fixed rankings; very important */, |
| 2717 |
1.0, 0.0, false, true, & grammarHasChanged); |
| 2718 |
if (grammarHasChanged) |
| 2719 |
OTGrammar_newDisharmonies (me, evaluationNoise); |
| 2720 |
grammarHasChangedDuringCycle |= grammarHasChanged; |
| 2721 |
} |
| 2722 |
} |
| 2723 |
if (! grammarHasChangedDuringCycle) |
| 2724 |
break; |
| 2725 |
} |
| 2726 |
if (itrial > 40) { |
| 2727 |
obligatory [jcons] [icons] = true; |
| 2728 |
MelderInfo_writeLine (my constraints [jcons]. name.get(), U" >> ", my constraints [icons]. name.get()); |
| 2729 |
MelderInfo_close (); |
| 2730 |
} |
| 2731 |
} |
| 2732 |
} |
| 2733 |
my numberOfFixedRankings ++; |
| 2734 |
Melder_progress (0.0, U""); |
| 2735 |
npair = npair * npair; |
| 2736 |
OrderedOf<structOTGrammar_List4> list; |
| 2737 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 2738 |
for (integer jcons = 1; jcons <= my numberOfConstraints; jcons ++) if (icons != jcons && ! obligatory [jcons] [icons]) { |
| 2739 |
my fixedRankings [my numberOfFixedRankings - 1]. higher = icons; |
| 2740 |
my fixedRankings [my numberOfFixedRankings - 1]. lower = jcons; |
| 2741 |
for (integer kcons = icons; kcons <= my numberOfConstraints; kcons ++) { |
| 2742 |
for (integer lcons = 1; lcons <= my numberOfConstraints; lcons ++) if (kcons != lcons && ! obligatory [lcons] [kcons]) { |
| 2743 |
if (icons == kcons && jcons >= lcons) |
| 2744 |
continue; |
| 2745 |
if (icons == lcons && jcons == kcons) |
| 2746 |
continue; |
| 2747 |
if (jcons == kcons && obligatory [lcons] [icons]) |
| 2748 |
continue; |
| 2749 |
if (icons == lcons && obligatory [jcons] [kcons]) |
| 2750 |
continue; |
| 2751 |
if (obligatory [lcons] [icons] && obligatory [jcons] [kcons]) |
| 2752 |
continue; |
| 2753 |
my fixedRankings [my numberOfFixedRankings]. higher = kcons; |
| 2754 |
my fixedRankings [my numberOfFixedRankings]. lower = lcons; |
| 2755 |
OTGrammar_reset (me, 100.0); |
| 2756 |
Melder_progress ((double) ipair / npair, ipair + 1, U"/", npair); |
| 2757 |
ipair ++; |
| 2758 |
for (itrial = 1; itrial <= 40; itrial ++) { |
| 2759 |
bool grammarHasChangedDuringCycle = false; |
| 2760 |
OTGrammar_honourLocalRankings (me, 1.0, 0.0, & grammarHasChangedDuringCycle); |
| 2761 |
OTGrammar_newDisharmonies (me, evaluationNoise); |
| 2762 |
for (integer iform = 1; iform <= thy pairs.size; iform ++) { |
| 2763 |
PairProbability prob = thy pairs.at [iform]; |
| 2764 |
if (prob -> weight > 0.0) { |
| 2765 |
bool grammarHasChanged = false; |
| 2766 |
OTGrammar_learnOne (me, prob -> string1.get(), prob -> string2.get(), |
| 2767 |
evaluationNoise, kOTGrammar_rerankingStrategy::EDCD, true /* honour fixed rankings; very important */, |
| 2768 |
1.0, 0.0, false, true, & grammarHasChanged); |
| 2769 |
if (grammarHasChanged) |
| 2770 |
OTGrammar_newDisharmonies (me, evaluationNoise); |
| 2771 |
grammarHasChangedDuringCycle |= grammarHasChanged; |
| 2772 |
} |
| 2773 |
} |
| 2774 |
if (! grammarHasChangedDuringCycle) |
| 2775 |
break; |
| 2776 |
} |
| 2777 |
if (itrial > 40) { |
| 2778 |
autoOTGrammar_List4 listElement = Thing_new (OTGrammar_List4); |
| 2779 |
listElement -> hi1 = jcons; |
| 2780 |
listElement -> lo1 = icons; |
| 2781 |
listElement -> hi2 = lcons; |
| 2782 |
listElement -> lo2 = kcons; |
| 2783 |
list. addItem_move (listElement.move()); |
| 2784 |
} |
| 2785 |
} |
| 2786 |
} |
| 2787 |
} |
| 2788 |
} |
| 2789 |
Melder_progress (1.0); |
| 2790 |
/* |
| 2791 |
Improve list. |
| 2792 |
*/ |
| 2793 |
bool improved = true; |
| 2794 |
while (improved) { |
| 2795 |
improved = false; |
| 2796 |
for (integer ilist = 1; ilist <= list.size; ilist ++) { |
| 2797 |
for (integer jlist = 1; jlist <= list.size; jlist ++) if (ilist != jlist) { |
| 2798 |
OTGrammar_List4 elA = list.at [ilist]; |
| 2799 |
OTGrammar_List4 elB = list.at [jlist]; |
| 2800 |
integer ahi1 = elA -> hi1, alo1 = elA -> lo1, ahi2 = elA -> hi2, alo2 = elA -> lo2; |
| 2801 |
integer bhi1 = elB -> hi1, blo1 = elB -> lo1, bhi2 = elB -> hi2, blo2 = elB -> lo2; |
| 2802 |
improved |= (ahi1 == bhi1 || obligatory [bhi1] [ahi1]) && (ahi2 == bhi2 || obligatory [bhi2] [ahi2]) && |
| 2803 |
(alo1 == blo1 || obligatory [alo1] [blo1]) && (alo2 == blo2 || obligatory [alo2] [blo2]); |
| 2804 |
improved |= (ahi1 == bhi2 || obligatory [bhi2] [ahi1]) && (ahi2 == bhi1 || obligatory [bhi1] [ahi2]) && |
| 2805 |
(alo1 == blo2 || obligatory [alo1] [blo2]) && (alo2 == blo1 || obligatory [alo2] [blo1]); |
| 2806 |
if (improved) { |
| 2807 |
list. removeItem (jlist); |
| 2808 |
break; |
| 2809 |
} |
| 2810 |
} |
| 2811 |
if (improved) break; |
| 2812 |
} |
| 2813 |
} |
| 2814 |
improved = true; |
| 2815 |
while (improved) { |
| 2816 |
improved = false; |
| 2817 |
for (integer ilist = 1; ilist <= list.size; ilist ++) { |
| 2818 |
for (integer jlist = 1; jlist <= list.size; jlist ++) if (ilist != jlist) { |
| 2819 |
OTGrammar_List4 elA = list.at [ilist]; |
| 2820 |
OTGrammar_List4 elB = list.at [jlist]; |
| 2821 |
integer ahi1 = elA -> hi1, alo1 = elA -> lo1, ahi2 = elA -> hi2, alo2 = elA -> lo2; |
| 2822 |
integer bhi1 = elB -> hi1, blo1 = elB -> lo1, bhi2 = elB -> hi2, blo2 = elB -> lo2; |
| 2823 |
improved |= ahi1 == bhi1 && alo1 == blo1 && ahi2 == bhi2 && blo2 == bhi1 && alo2 == alo1; |
| 2824 |
improved |= ahi1 == bhi2 && alo1 == blo2 && ahi2 == bhi1 && blo1 == bhi2 && alo2 == alo1; |
| 2825 |
improved |= ahi2 == bhi1 && alo2 == blo1 && ahi1 == bhi2 && blo2 == bhi1 && alo1 == alo2; |
| 2826 |
improved |= ahi2 == bhi2 && alo2 == blo2 && ahi1 == bhi1 && blo1 == bhi2 && alo1 == alo2; |
| 2827 |
if (improved) { |
| 2828 |
list. removeItem (jlist); |
| 2829 |
break; |
| 2830 |
} |
| 2831 |
} |
| 2832 |
if (improved) break; |
| 2833 |
} |
| 2834 |
} |
| 2835 |
for (integer ilist = 1; ilist <= list.size; ilist ++) { |
| 2836 |
OTGrammar_List4 el = list.at [ilist]; |
| 2837 |
MelderInfo_write (my constraints [el -> hi1]. name.get(), U" >> ", my constraints [el -> lo1]. name.get(), U" OR "); |
| 2838 |
MelderInfo_writeLine (my constraints [el -> hi2]. name.get(), U" >> ", my constraints [el -> lo2]. name.get()); |
| 2839 |
MelderInfo_close (); |
| 2840 |
} |
| 2841 |
MelderInfo_close (); |
| 2842 |
|
| 2843 |
/* |
| 2844 |
Restore. |
| 2845 |
*/ |
| 2846 |
my numberOfFixedRankings = savedNumberOfFixedRankings; |
| 2847 |
my fixedRankings = savedFixedRankings.move(); |
| 2848 |
OTGrammar_restore (me); |
| 2849 |
} catch (MelderError) { |
| 2850 |
MelderInfo_close (); |
| 2851 |
/* |
| 2852 |
Restore. |
| 2853 |
*/ |
| 2854 |
my numberOfFixedRankings = savedNumberOfFixedRankings; |
| 2855 |
my fixedRankings = savedFixedRankings.move(); |
| 2856 |
OTGrammar_restore (me); |
| 2857 |
Melder_throw (me, U": obligatory rankings not listed."); |
| 2858 |
} |
| 2859 |
} |
| 2860 |
|
| 2861 |
void OTGrammar_Distributions_listObligatoryRankings (OTGrammar me, Distributions thee, integer columnNumber) { |
| 2862 |
/* |
| 2863 |
Save. |
| 2864 |
*/ |
| 2865 |
autovector <structOTGrammarFixedRanking> savedFixedRankings = my fixedRankings.move(); |
| 2866 |
OTGrammar_save (me); |
| 2867 |
try { |
| 2868 |
integer ipair = 0, npair = my numberOfConstraints * (my numberOfConstraints - 1); |
| 2869 |
/* |
| 2870 |
Add room for one more fixed ranking. |
| 2871 |
*/ |
| 2872 |
my numberOfFixedRankings ++; |
| 2873 |
my fixedRankings = newvectorzero <structOTGrammarFixedRanking> (my numberOfFixedRankings); |
| 2874 |
for (integer ifixedRanking = 1; ifixedRanking < my numberOfFixedRankings; ifixedRanking ++) { |
| 2875 |
my fixedRankings [ifixedRanking]. higher = savedFixedRankings [ifixedRanking]. higher; |
| 2876 |
my fixedRankings [ifixedRanking]. lower = savedFixedRankings [ifixedRanking]. lower; |
| 2877 |
} |
| 2878 |
/* |
| 2879 |
Test learnability of every possible ranked pair. |
| 2880 |
*/ |
| 2881 |
MelderInfo_open (); |
| 2882 |
autoMelderProgress progress (U"Finding obligatory rankings."); |
| 2883 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 2884 |
for (integer jcons = 1; jcons <= my numberOfConstraints; jcons ++) if (icons != jcons) { |
| 2885 |
my fixedRankings [my numberOfFixedRankings]. higher = icons; |
| 2886 |
my fixedRankings [my numberOfFixedRankings]. lower = jcons; |
| 2887 |
OTGrammar_reset (me, 100.0); |
| 2888 |
Melder_progress ((double) ipair / npair, ipair + 1, U"/", npair, U": Trying ranking ", |
| 2889 |
my constraints [icons]. name.get(), U" >> ", my constraints [jcons]. name.get()); |
| 2890 |
ipair ++; |
| 2891 |
Melder_progressOff (); |
| 2892 |
OTGrammar_Distributions_learnFromPartialOutputs (me, thee, columnNumber, |
| 2893 |
1e-9, kOTGrammar_rerankingStrategy::EDCD, true /* honour fixed rankings; very important */, |
| 2894 |
1.0, 1000, 0.0, 1, 0.0, 1, 0, nullptr, false, false, 0); |
| 2895 |
Melder_progressOn (); |
| 2896 |
for (integer kcons = 1; kcons <= my numberOfConstraints; kcons ++) { |
| 2897 |
if (my constraints [kcons]. ranking < 0.0) { |
| 2898 |
MelderInfo_writeLine (my constraints [jcons]. name.get(), U" >> ", my constraints [icons]. name.get()); |
| 2899 |
break; |
| 2900 |
} |
| 2901 |
} |
| 2902 |
} |
| 2903 |
} |
| 2904 |
MelderInfo_close (); |
| 2905 |
/* |
| 2906 |
Restore. |
| 2907 |
*/ |
| 2908 |
my numberOfFixedRankings --; |
| 2909 |
my fixedRankings = savedFixedRankings.move(); |
| 2910 |
OTGrammar_restore (me); |
| 2911 |
} catch (MelderError) { |
| 2912 |
MelderInfo_close (); |
| 2913 |
/* |
| 2914 |
Restore. |
| 2915 |
*/ |
| 2916 |
my numberOfFixedRankings --; |
| 2917 |
my fixedRankings = savedFixedRankings.move(); |
| 2918 |
OTGrammar_restore (me); |
| 2919 |
Melder_throw (me, U": obligatory rankings not listed."); |
| 2920 |
} |
| 2921 |
} |
| 2922 |
|
| 2923 |
autoTable OTGrammar_tabulateAllCorrectRankings (constOTGrammar me, constStrings inputs, constStrings partialOutputs) { |
| 2924 |
try { |
| 2925 |
/* |
| 2926 |
Check the arguments. |
| 2927 |
*/ |
| 2928 |
if (my decisionStrategy != kOTGrammar_decisionStrategy::OPTIMALITY_THEORY) |
| 2929 |
Melder_throw (U"Rankings make sense only for Optimality-Theoretic grammars."); |
| 2930 |
const integer numberOfInputOutputPairs = inputs -> numberOfStrings; |
| 2931 |
Melder_require (partialOutputs -> numberOfStrings == numberOfInputOutputPairs, |
| 2932 |
U"The number of input forms (", numberOfInputOutputPairs, |
| 2933 |
U") should be equal to the number of partial output forms (", partialOutputs -> numberOfStrings, U")."); |
| 2934 |
/* |
| 2935 |
Compute a big number. |
| 2936 |
*/ |
| 2937 |
const integer numberOfConstraints = my numberOfConstraints; |
| 2938 |
if (numberOfConstraints > factorial_int53_MAXIMUM_ARGUMENT) |
| 2939 |
Melder_throw (U"Cannot handle more than ", factorial_int53_MAXIMUM_ARGUMENT, U" constraints."); |
| 2940 |
const int64 numberOfPermutations = factorial_int53 [numberOfConstraints]; |
| 2941 |
/* |
| 2942 |
Create the result table. |
| 2943 |
*/ |
| 2944 |
autoTable thee = Table_createWithoutColumnNames (0, numberOfConstraints);; |
| 2945 |
for (integer icons = 1; icons <= numberOfConstraints; icons ++) |
| 2946 |
thy columnHeaders [icons]. label = Melder_dup (my constraints [icons]. name.get()); |
| 2947 |
/* |
| 2948 |
Cycle many times. |
| 2949 |
*/ |
| 2950 |
autoMelderProgress progress (U"Tabulating all correct rankings..."); |
| 2951 |
integer rankToConstraintNumber [1 + factorial_int53_MAXIMUM_ARGUMENT]; |
| 2952 |
int64 numberOfCorrectRankings = 0, numberOfPossibleRankings = 0; |
| 2953 |
for (int64 iperm_base0 = 0; iperm_base0 < numberOfPermutations; iperm_base0 ++) { |
| 2954 |
if (iperm_base0 % 1000 == 0) |
| 2955 |
Melder_progress ((iperm_base0 + 0.5) / numberOfPermutations, |
| 2956 |
U"Testing permutation ", Melder_bigInteger (iperm_base0), U" out of ", Melder_bigInteger (numberOfPermutations), |
| 2957 |
U" (", Melder_bigInteger (numberOfCorrectRankings), U" correct)" |
| 2958 |
); |
| 2959 |
/* mutable cycle */ int64 permleft = iperm_base0; |
| 2960 |
/* |
| 2961 |
Initialize to { 1, 2, 3, 4, 5, ... } before permuting. |
| 2962 |
*/ |
| 2963 |
for (integer rank = 1; rank <= numberOfConstraints; rank ++) |
| 2964 |
rankToConstraintNumber [rank] = rank; |
| 2965 |
/* |
| 2966 |
Create the permutation, i.e. put the constraints in a new unique order. |
| 2967 |
*/ |
| 2968 |
for (integer rank = 1; rank < numberOfConstraints; rank ++) { |
| 2969 |
const int64 fac = factorial_int53 [numberOfConstraints - rank]; |
| 2970 |
const integer shift = permleft / fac; |
| 2971 |
permleft = permleft % fac; |
| 2972 |
std::swap (rankToConstraintNumber [rank], rankToConstraintNumber [rank + shift]); |
| 2973 |
} |
| 2974 |
/* |
| 2975 |
Ignore permutations that violate our fixed rankings. |
| 2976 |
*/ |
| 2977 |
bool fixedRankingsAreHonoured = true; |
| 2978 |
if (my numberOfFixedRankings > 0) { |
| 2979 |
integer ranks [1 + factorial_int53_MAXIMUM_ARGUMENT]; |
| 2980 |
for (integer rank = 1; rank < numberOfConstraints; rank ++) |
| 2981 |
ranks [rankToConstraintNumber [rank]] = rank; // invert the index |
| 2982 |
for (integer ifixedRanking = 1; ifixedRanking <= my numberOfFixedRankings; ifixedRanking ++) { |
| 2983 |
// in the next line, ">" means 'lower ranked than'! (the highest ranking is 1) |
| 2984 |
if (ranks [my fixedRankings [ifixedRanking]. higher] > ranks [my fixedRankings [ifixedRanking]. lower]) { |
| 2985 |
fixedRankingsAreHonoured = false; |
| 2986 |
break; |
| 2987 |
} |
| 2988 |
} |
| 2989 |
} |
| 2990 |
if (! fixedRankingsAreHonoured) |
| 2991 |
continue; |
| 2992 |
numberOfPossibleRankings += 1; // we count |
| 2993 |
/* |
| 2994 |
Check correctness. |
| 2995 |
*/ |
| 2996 |
bool correct = true; |
| 2997 |
for (integer ipair = 1; ipair <= numberOfInputOutputPairs; ipair ++) { |
| 2998 |
const conststring32 input = inputs -> strings [ipair].get(); |
| 2999 |
const conststring32 partialOutput = partialOutputs -> strings [ipair].get(); |
| 3000 |
const integer tableauNumber = OTGrammar_getTableau (me, input); |
| 3001 |
OTGrammarTableau tableau = & my tableaus [tableauNumber]; |
| 3002 |
/* |
| 3003 |
In the case of fully specified desired outputs, there should be exactly one winner. |
| 3004 |
In the case of partial desired outputs, all winners should share that partial output. |
| 3005 |
*/ |
| 3006 |
/* mutable search */ integer icand_best = 1; |
| 3007 |
constINTVEC marks_best = tableau -> candidates [1]. marks.get(); |
| 3008 |
/* mutable count */ integer numberOfBestCandidates = 1; |
| 3009 |
for (integer icand = 2; icand <= tableau -> numberOfCandidates; icand ++) { |
| 3010 |
bool candidatesAreEquivalent = true; // no difference found yet; hence, all candidates are still equally good |
| 3011 |
const constINTVEC marks_cand = tableau -> candidates [icand]. marks.get(); |
| 3012 |
for (integer rank = 1; rank <= numberOfConstraints; rank ++) { |
| 3013 |
const integer numberOfMarks_best = marks_best [rankToConstraintNumber [rank]]; |
| 3014 |
const integer numberOfMarks_cand = marks_cand [rankToConstraintNumber [rank]]; |
| 3015 |
if (numberOfMarks_cand != numberOfMarks_best) { |
| 3016 |
if (numberOfMarks_cand < numberOfMarks_best) { |
| 3017 |
/* |
| 3018 |
The new candidate is better than the earlier winner(s). |
| 3019 |
*/ |
| 3020 |
icand_best = icand; // the current candidate is the unique best candidate found so far |
| 3021 |
marks_best = marks_cand; |
| 3022 |
numberOfBestCandidates = 1; |
| 3023 |
} |
| 3024 |
candidatesAreEquivalent = false; |
| 3025 |
break; |
| 3026 |
} |
| 3027 |
} |
| 3028 |
if (candidatesAreEquivalent) { |
| 3029 |
/* |
| 3030 |
The current candidate is equally good as the best found before. |
| 3031 |
We keep the first winner as "the" winner, and later check that the other winners match the output as well. |
| 3032 |
*/ |
| 3033 |
numberOfBestCandidates += 1; |
| 3034 |
} |
| 3035 |
} |
| 3036 |
if (numberOfBestCandidates == 1) { |
| 3037 |
const constOTGrammarCandidate singleWinner = & tableau -> candidates [icand_best]; |
| 3038 |
const bool singleWinnerMatches = !! str32str (singleWinner -> output.get(), partialOutput); |
| 3039 |
if (! singleWinnerMatches) { |
| 3040 |
correct = false; |
| 3041 |
break; |
| 3042 |
} |
| 3043 |
} else { |
| 3044 |
/* |
| 3045 |
The winners should either all match or all not match. |
| 3046 |
*/ |
| 3047 |
const constOTGrammarCandidate firstWinner = & tableau -> candidates [icand_best]; |
| 3048 |
const bool firstWinnerMatches = !! str32str (firstWinner -> output.get(), partialOutput); |
| 3049 |
for (integer icand = icand_best + 1; icand <= tableau -> numberOfCandidates; icand ++) { // start above icand_best |
| 3050 |
bool candidatesAreEquivalent = true; |
| 3051 |
const constINTVEC marks_cand = tableau -> candidates [icand]. marks.get(); |
| 3052 |
for (integer icons = 1; icons <= numberOfConstraints; icons ++) { |
| 3053 |
if (marks_cand [icons] != marks_best [icons]) { // no need for indirection |
| 3054 |
candidatesAreEquivalent = false; |
| 3055 |
break; |
| 3056 |
} |
| 3057 |
} |
| 3058 |
if (candidatesAreEquivalent) { |
| 3059 |
const constOTGrammarCandidate alternativeWinner = & tableau -> candidates [icand]; |
| 3060 |
const bool alternativeWinnerMatches = !! str32str (alternativeWinner -> output.get(), partialOutput); |
| 3061 |
if (alternativeWinnerMatches != firstWinnerMatches) { |
| 3062 |
const constOTGrammarCandidate matchingWinner = ( firstWinnerMatches ? firstWinner : alternativeWinner ); |
| 3063 |
const constOTGrammarCandidate nonmatchingWinner = ( firstWinnerMatches ? alternativeWinner : firstWinner ); |
| 3064 |
Melder_throw (U"Indeterminate grammar: " |
| 3065 |
"one of the winners (", matchingWinner -> output.get(), U") " |
| 3066 |
"does match the desired output (", partialOutput, U"), " |
| 3067 |
"but another winner (", nonmatchingWinner -> output.get(), U") doesn’t." |
| 3068 |
); |
| 3069 |
} |
| 3070 |
} |
| 3071 |
} |
| 3072 |
if (! firstWinnerMatches) { |
| 3073 |
correct = false; |
| 3074 |
break; |
| 3075 |
} |
| 3076 |
} |
| 3077 |
} |
| 3078 |
/* |
| 3079 |
Record the correct ranking in the table. |
| 3080 |
*/ |
| 3081 |
if (correct) { |
| 3082 |
if (numberOfCorrectRankings == INTEGER_MAX) // can occur on a 32-bit machine |
| 3083 |
Melder_throw (U"Cannot put more than ", Melder_bigInteger (INTEGER_MAX), U" rows in a Table."); |
| 3084 |
numberOfCorrectRankings += 1; |
| 3085 |
Table_appendRow (thee.get()); |
| 3086 |
const integer rowNumber = thy rows.size; |
| 3087 |
for (integer rank = 1; rank <= numberOfConstraints; rank ++) |
| 3088 |
Table_setNumericValue (thee.get(), rowNumber, rankToConstraintNumber [rank], rank); |
| 3089 |
} |
| 3090 |
} |
| 3091 |
Melder_casual (U"Number of correct rankings: ", Melder_bigInteger (numberOfCorrectRankings)); |
| 3092 |
Melder_casual (U"Number of possible rankings: ", Melder_bigInteger (numberOfPossibleRankings)); |
| 3093 |
Melder_casual (U"Number of permutations: ", Melder_bigInteger (numberOfPermutations)); |
| 3094 |
return thee; |
| 3095 |
} catch (MelderError) { |
| 3096 |
Melder_throw (me, U": correct rankings not tabulated."); |
| 3097 |
} |
| 3098 |
} |
| 3099 |
|
| 3100 |
static void printConstraintNames (OTGrammar me, MelderString *buffer) { |
| 3101 |
char32 text [200]; |
| 3102 |
bool secondLine = false; |
| 3103 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 3104 |
OTGrammarConstraint constraint = & my constraints [my index [icons]]; |
| 3105 |
if (str32chr (constraint -> name.get(), U'\n')) { |
| 3106 |
char32 *newLine; |
| 3107 |
str32cpy (text, constraint -> name.get()); |
| 3108 |
newLine = str32chr (text, U'\n'); |
| 3109 |
*newLine = U'\0'; |
| 3110 |
MelderString_append (buffer, U"\t", text); |
| 3111 |
secondLine = true; |
| 3112 |
} else { |
| 3113 |
MelderString_append (buffer, U"\t", constraint -> name.get()); |
| 3114 |
} |
| 3115 |
} |
| 3116 |
MelderString_appendCharacter (buffer, U'\n'); |
| 3117 |
if (secondLine) { |
| 3118 |
MelderString_appendCharacter (buffer, U'\t'); |
| 3119 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 3120 |
OTGrammarConstraint constraint = & my constraints [my index [icons]]; |
| 3121 |
char32 *newLine = str32chr (constraint -> name.get(), U'\n'); |
| 3122 |
MelderString_append (buffer, U"\t", newLine ? newLine + 1 : U""); |
| 3123 |
} |
| 3124 |
MelderString_appendCharacter (buffer, U'\n'); |
| 3125 |
} |
| 3126 |
} |
| 3127 |
|
| 3128 |
void OTGrammar_writeToHeaderlessSpreadsheetFile (OTGrammar me, MelderFile file) { |
| 3129 |
try { |
| 3130 |
autoMelderString buffer; |
| 3131 |
MelderString_copy (& buffer, U"CONSTRAINTS\t"); |
| 3132 |
printConstraintNames (me, & buffer); |
| 3133 |
MelderString_append (& buffer, U"rankings\t"); |
| 3134 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 3135 |
OTGrammarConstraint constraint = & my constraints [my index [icons]]; |
| 3136 |
MelderString_append (& buffer, U"\t", constraint -> ranking); |
| 3137 |
} |
| 3138 |
MelderString_append (& buffer, U"\ndisharmonies\t"); |
| 3139 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 3140 |
OTGrammarConstraint constraint = & my constraints [my index [icons]]; |
| 3141 |
MelderString_append (& buffer, U"\t", constraint -> disharmony); |
| 3142 |
} |
| 3143 |
MelderString_appendCharacter (& buffer, U'\n'); |
| 3144 |
for (integer itab = 1; itab <= my numberOfTableaus; itab ++) { |
| 3145 |
OTGrammarTableau tableau = & my tableaus [itab]; |
| 3146 |
integer winner = OTGrammar_getWinner (me, itab), numberOfOptimalCandidates = 0; |
| 3147 |
for (integer icons = 1; icons <= my numberOfConstraints + 1; icons ++) |
| 3148 |
MelderString_appendCharacter (& buffer, U'\t'); |
| 3149 |
MelderString_append (& buffer, U"\nINPUT\t", tableau -> input.get()); |
| 3150 |
printConstraintNames (me, & buffer); |
| 3151 |
for (integer icand = 1; icand <= tableau -> numberOfCandidates; icand ++) { |
| 3152 |
if (OTGrammar_compareCandidates (me, itab, icand, itab, winner) == 0) |
| 3153 |
numberOfOptimalCandidates ++; |
| 3154 |
} |
| 3155 |
for (integer icand = 1; icand <= tableau -> numberOfCandidates; icand ++) { |
| 3156 |
OTGrammarCandidate candidate = & tableau -> candidates [icand]; |
| 3157 |
bool candidateIsOptimal = OTGrammar_compareCandidates (me, itab, icand, itab, winner) == 0; |
| 3158 |
integer crucialCell = OTGrammar_crucialCell (me, itab, icand, winner, numberOfOptimalCandidates); |
| 3159 |
MelderString_append (& buffer, |
| 3160 |
candidateIsOptimal == false ? U"loser" : numberOfOptimalCandidates > 1 ? U"co-winner" : U"winner", |
| 3161 |
U"\t", |
| 3162 |
candidate -> output.get()); |
| 3163 |
for (integer icons = 1; icons <= my numberOfConstraints; icons ++) { |
| 3164 |
integer index = my index [icons]; |
| 3165 |
OTGrammarConstraint constraint = & my constraints [index]; |
| 3166 |
static MelderString markString; |
| 3167 |
MelderString_empty (& markString); |
| 3168 |
/* |
| 3169 |
An exclamation mark can be drawn in this cell only if all of the following conditions are met: |
| 3170 |
1. the candidate is not optimal; |
| 3171 |
2. the constraint is not tied; |
| 3172 |
3. this is the crucial cell, i.e. the cells after it are drawn in grey. |
| 3173 |
*/ |
| 3174 |
if (icons == crucialCell && ! candidateIsOptimal && ! constraint -> tiedToTheLeft && ! constraint -> tiedToTheRight) { |
| 3175 |
const integer winnerMarks = tableau -> candidates [winner]. marks [index]; |
| 3176 |
for (integer imark = 1; imark <= winnerMarks + 1; imark ++) |
| 3177 |
MelderString_appendCharacter (& markString, U'*'); |
| 3178 |
MelderString_appendCharacter (& markString, U'!'); |
| 3179 |
for (integer imark = winnerMarks + 2; imark <= candidate -> marks [index]; imark ++) |
| 3180 |
MelderString_appendCharacter (& markString, U'*'); |
| 3181 |
} else { |
| 3182 |
if (! candidateIsOptimal && (constraint -> tiedToTheLeft || constraint -> tiedToTheRight) && |
| 3183 |
crucialCell >= 1 && constraint -> disharmony == my constraints [my index [crucialCell]]. disharmony) |
| 3184 |
{ |
| 3185 |
MelderString_appendCharacter (& markString, U'='); |
| 3186 |
} |
| 3187 |
for (integer imark = 1; imark <= candidate -> marks [index]; imark ++) |
| 3188 |
MelderString_appendCharacter (& markString, U'*'); |
| 3189 |
} |
| 3190 |
MelderString_append (& buffer, U"\t", markString.string); |
| 3191 |
} |
| 3192 |
MelderString_appendCharacter (& buffer, U'\n'); |
| 3193 |
} |
| 3194 |
} |
| 3195 |
MelderFile_writeText_e (file, buffer.string, Melder_getOutputEncoding ()); |
| 3196 |
} catch (MelderError) { |
| 3197 |
Melder_throw (me, U": not saved to tab-separated file ", file, U"."); |
| 3198 |
} |
| 3199 |
} |
| 3200 |
|
| 3201 |
/* End of file OTGrammar.cpp */ |