| 1 | /* OTMulti_ex_metrics.cpp |
| 2 | * |
| 3 | * Copyright (C) 2014-2020,2025,2026 Paul Boersma |
| 4 | * Forked from OTGrammar_ex_metrics.cpp, Copyright (C) 2001-2007,2009,2011,2012 Paul Boersma |
| 5 | * |
| 6 | * This code is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or (at |
| 9 | * your option) any later version. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | * See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "OTMulti.h" |
| 21 | |
| 22 | #define WSP 1 |
| 23 | #define FtNonfinal 2 |
| 24 | #define Iambic 3 |
| 25 | #define Parse 4 |
| 26 | #define FootBin 5 |
| 27 | #define WFL 6 |
| 28 | #define WFR 7 |
| 29 | #define Main_L 8 |
| 30 | #define Main_R 9 |
| 31 | #define AFL 10 |
| 32 | #define AFR 11 |
| 33 | #define Nonfinal 12 |
| 34 | |
| 35 | #define Trochaic 13 |
| 36 | #define FootBimoraic 14 |
| 37 | #define FootBisyllabic 15 |
| 38 | #define Peripheral 16 |
| 39 | #define MainNonfinal 17 |
| 40 | #define HeadNonfinal 18 |
| 41 | #define Clash 19 |
| 42 | #define Lapse 20 |
| 43 | |
| 44 | #define WeightByPosition 21 |
| 45 | #define MoraicConsonant 22 |
| 46 | |
| 47 | #define NUMBER_OF_CONSTRAINTS 22 |
| 48 | |
| 49 | constexpr integer MAXIMUM_NUMBER_OF_SYLLABLES = 10; |
| 50 | |
| 51 | static const conststring32 constraintNames [1+NUMBER_OF_CONSTRAINTS] { 0, |
| 52 | U"WSP", U"FtNonfinal", U"Iambic", U"Parse", U"FootBin", U"WFL", U"WFR", U"Main-L", U"Main-R", U"AFL", U"AFR", U"Nonfinal", |
| 53 | U"Trochaic", U"FtBimor", U"FtBisyl", U"Peripheral", U"MainNonfinal", U"HeadNonfinal", U"*Clash", U"*Lapse", U"WeightByPosition", U"*C\\mu" }; |
| 54 | |
| 55 | static void addCandidate (OTMulti me, conststring32 underlyingForm, integer numberOfSyllables, int stress [], |
| 56 | bool footedToTheLeft [], bool footedToTheRight [], int surfaceWeightPattern [], |
| 57 | int overtFormsHaveSecondaryStress) |
| 58 | { |
| 59 | static const conststring32 syllable [] { U"L", U"L1", U"L2", U"H", U"H1", U"H2", U"K", U"K1", U"K2", U"J", U"J1", U"J2" }; |
| 60 | static const conststring32 syllableWithoutSecondaryStress [] { U"L", U"L1", U"L", U"H", U"H1", U"H", U"K", U"K1", U"K", U"J", U"J1", U"J" }; |
| 61 | char32 string [150]; |
| 62 | str32cpy (string, underlyingForm); |
| 63 | str32cat (string, U" /"); |
| 64 | for (integer isyll = 1; isyll <= numberOfSyllables; isyll ++) { |
| 65 | if (isyll > 1) |
| 66 | str32cat (string, U" "); |
| 67 | if (footedToTheRight [isyll] || (! footedToTheLeft [isyll] && stress [isyll] != 0)) |
| 68 | str32cat (string, U"("); |
| 69 | str32cat (string, syllable [stress [isyll] + 3 * (surfaceWeightPattern [isyll] - 1)]); |
| 70 | if (footedToTheLeft [isyll] || (! footedToTheRight [isyll] && stress [isyll] != 0)) |
| 71 | str32cat (string, U")"); |
| 72 | } |
| 73 | str32cat (string, U"/ ["); |
| 74 | for (integer isyll = 1; isyll <= numberOfSyllables; isyll ++) { |
| 75 | if (isyll > 1) |
| 76 | str32cat (string, U" "); |
| 77 | str32cat (string, ( overtFormsHaveSecondaryStress ? syllable : syllableWithoutSecondaryStress ) |
| 78 | [stress [isyll] + 3 * (surfaceWeightPattern [isyll] - 1)]); |
| 79 | } |
| 80 | str32cat (string, U"]"); |
| 81 | my candidates [++ my numberOfCandidates]. string = Melder_dup (string); |
| 82 | } |
| 83 | |
| 84 | static void fillSurfaceWeightPattern (OTMulti me, conststring32 underlyingForm, integer numberOfSyllables, int stress [], |
| 85 | bool footedToTheLeft [], bool footedToTheRight [], int underlyingWeightPattern [], |
| 86 | int overtFormsHaveSecondaryStress) |
| 87 | { |
| 88 | int surfaceWeightPattern [1+7], minSurfaceWeight [1+7], maxSurfaceWeight [1+7]; |
| 89 | for (integer isyll = 1; isyll <= numberOfSyllables; isyll ++) { |
| 90 | if (underlyingWeightPattern [isyll] < 3) { |
| 91 | minSurfaceWeight [isyll] = maxSurfaceWeight [isyll] = underlyingWeightPattern [isyll]; // L -> L; H -> H |
| 92 | } else { |
| 93 | minSurfaceWeight [isyll] = 3; |
| 94 | maxSurfaceWeight [isyll] = 4; // C -> { J, K } |
| 95 | } |
| 96 | } |
| 97 | surfaceWeightPattern [6] = surfaceWeightPattern [7] = 1; // constant L |
| 98 | for (int weight1 = minSurfaceWeight [1]; weight1 <= maxSurfaceWeight [1]; weight1 ++) { |
| 99 | surfaceWeightPattern [1] = weight1; |
| 100 | for (int weight2 = minSurfaceWeight [2]; weight2 <= maxSurfaceWeight [2]; weight2 ++) { |
| 101 | surfaceWeightPattern [2] = weight2; |
| 102 | if (numberOfSyllables == 2) { |
| 103 | addCandidate (me, underlyingForm, 2, stress, footedToTheLeft, footedToTheRight, surfaceWeightPattern, overtFormsHaveSecondaryStress); |
| 104 | } else for (int weight3 = minSurfaceWeight [3]; weight3 <= maxSurfaceWeight [3]; weight3 ++) { |
| 105 | surfaceWeightPattern [3] = weight3; |
| 106 | if (numberOfSyllables == 3) { |
| 107 | addCandidate (me, underlyingForm, 3, stress, footedToTheLeft, footedToTheRight, surfaceWeightPattern, overtFormsHaveSecondaryStress); |
| 108 | } else for (int weight4 = minSurfaceWeight [4]; weight4 <= maxSurfaceWeight [4]; weight4 ++) { |
| 109 | surfaceWeightPattern [4] = weight4; |
| 110 | if (numberOfSyllables == 4) { |
| 111 | addCandidate (me, underlyingForm, 4, stress, footedToTheLeft, footedToTheRight, surfaceWeightPattern, overtFormsHaveSecondaryStress); |
| 112 | } else for (int weight5 = minSurfaceWeight [5]; weight5 <= maxSurfaceWeight [5]; weight5 ++) { |
| 113 | surfaceWeightPattern [5] = weight5; |
| 114 | addCandidate (me, underlyingForm, numberOfSyllables, stress, footedToTheLeft, footedToTheRight, surfaceWeightPattern, overtFormsHaveSecondaryStress); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | static void path (OTMulti me, conststring32 underlyingForm, integer numberOfSyllables, int stress [], |
| 123 | int startingSyllable, bool footedToTheLeft_in [], bool footedToTheRight_in [], int underlyingWeightPattern [], |
| 124 | int overtFormsHaveSecondaryStress) |
| 125 | { |
| 126 | Melder_assert (startingSyllable < MAXIMUM_NUMBER_OF_SYLLABLES); |
| 127 | Melder_assert (numberOfSyllables < MAXIMUM_NUMBER_OF_SYLLABLES); |
| 128 | |
| 129 | bool footedToTheLeft [MAXIMUM_NUMBER_OF_SYLLABLES], footedToTheRight [MAXIMUM_NUMBER_OF_SYLLABLES]; |
| 130 | integer isyll; |
| 131 | /* Localize all arguments. */ |
| 132 | for (isyll = 1; isyll <= startingSyllable; isyll ++) { |
| 133 | footedToTheLeft [isyll] = footedToTheLeft_in [isyll]; |
| 134 | footedToTheRight [isyll] = footedToTheRight_in [isyll]; |
| 135 | } |
| 136 | for (isyll = startingSyllable + 1; isyll <= numberOfSyllables; isyll ++) |
| 137 | footedToTheLeft [isyll] = footedToTheRight [isyll] = 0; |
| 138 | if (startingSyllable > numberOfSyllables) { |
| 139 | fillSurfaceWeightPattern (me, underlyingForm, numberOfSyllables, stress, footedToTheLeft, footedToTheRight, underlyingWeightPattern, overtFormsHaveSecondaryStress); |
| 140 | } else { |
| 141 | path (me, underlyingForm, numberOfSyllables, stress, startingSyllable + 1, |
| 142 | footedToTheLeft, footedToTheRight, underlyingWeightPattern, overtFormsHaveSecondaryStress); |
| 143 | if (stress [startingSyllable] == 0 && startingSyllable < numberOfSyllables && stress [startingSyllable + 1] != 0) { |
| 144 | footedToTheLeft [startingSyllable + 1] = true; |
| 145 | footedToTheRight [startingSyllable] = true; |
| 146 | path (me, underlyingForm, numberOfSyllables, stress, startingSyllable + 1, |
| 147 | footedToTheLeft, footedToTheRight, underlyingWeightPattern, overtFormsHaveSecondaryStress); |
| 148 | footedToTheLeft [startingSyllable + 1] = false; |
| 149 | footedToTheRight [startingSyllable] = false; |
| 150 | } |
| 151 | if (stress [startingSyllable] == 0 && startingSyllable > 1 && stress [startingSyllable - 1] != 0 |
| 152 | && ! footedToTheLeft [startingSyllable - 1]) |
| 153 | { |
| 154 | footedToTheRight [startingSyllable - 1] = true; |
| 155 | footedToTheLeft [startingSyllable] = true; |
| 156 | path (me, underlyingForm, numberOfSyllables, stress, startingSyllable + 1, |
| 157 | footedToTheLeft, footedToTheRight, underlyingWeightPattern, overtFormsHaveSecondaryStress); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | static void fillOvertStressPattern (OTMulti me, conststring32 underlyingForm, integer numberOfSyllables, int stress [], int underlyingWeightPattern [], |
| 163 | int overtFormsHaveSecondaryStress) |
| 164 | { |
| 165 | bool footedToTheLeft [MAXIMUM_NUMBER_OF_SYLLABLES], footedToTheRight [MAXIMUM_NUMBER_OF_SYLLABLES]; |
| 166 | for (int isyll = 1; isyll <= numberOfSyllables; isyll ++) |
| 167 | footedToTheLeft [isyll] = footedToTheRight [isyll] = 0; |
| 168 | path (me, underlyingForm, numberOfSyllables, stress, 1, footedToTheLeft, footedToTheRight, underlyingWeightPattern, overtFormsHaveSecondaryStress); |
| 169 | } |
| 170 | |
| 171 | static int numberOfCandidates_noCodas [1+7] { 0, 1, 6, 24, 88, 300, 984, 3136 }; |
| 172 | static int numberOfCandidates_codas [1+7] { 0, 1, 24, 192, 1408, 9600, 984, 3136 }; |
| 173 | |
| 174 | static void fillTableau (OTMulti me, integer numberOfSyllables, int underlyingWeightPattern [], int overtFormsHaveSecondaryStress, int includeCodas) { |
| 175 | char32 underlyingForm [100]; |
| 176 | str32cpy (underlyingForm, U"|"); |
| 177 | for (integer isyll = 1; isyll <= numberOfSyllables; isyll ++) { |
| 178 | static const conststring32 syllable_noCodas [] = { U"", U"L", U"H" }; |
| 179 | static const conststring32 syllable_codas [] = { U"", U"cv", U"cv:", U"cvc" }; |
| 180 | if (isyll > 1) |
| 181 | str32cat (underlyingForm, includeCodas ? U"." : U" "); |
| 182 | str32cat (underlyingForm, ( includeCodas ? syllable_codas : syllable_noCodas ) [underlyingWeightPattern [isyll]]); |
| 183 | } |
| 184 | str32cat (underlyingForm, U"|"); |
| 185 | for (integer mainStressed = 1; mainStressed <= numberOfSyllables; mainStressed ++) { |
| 186 | int stress [10]; |
| 187 | stress [mainStressed] = 1; |
| 188 | for (int secondary1 = false; secondary1 <= true; secondary1 ++) { |
| 189 | stress [mainStressed <= 1 ? 2 : 1] = secondary1 ? 2 : 0; |
| 190 | if (numberOfSyllables == 2) { |
| 191 | fillOvertStressPattern (me, underlyingForm, 2, stress, underlyingWeightPattern, overtFormsHaveSecondaryStress); |
| 192 | } else for (int secondary2 = false; secondary2 <= true; secondary2 ++) { |
| 193 | stress [mainStressed <= 2 ? 3 : 2] = secondary2 ? 2 : 0; |
| 194 | if (numberOfSyllables == 3) { |
| 195 | fillOvertStressPattern (me, underlyingForm, 3, stress, underlyingWeightPattern, overtFormsHaveSecondaryStress); |
| 196 | } else for (int secondary3 = false; secondary3 <= true; secondary3 ++) { |
| 197 | stress [mainStressed <= 3 ? 4 : 3] = secondary3 ? 2 : 0; |
| 198 | if (numberOfSyllables == 4) { |
| 199 | fillOvertStressPattern (me, underlyingForm, 4, stress, underlyingWeightPattern, overtFormsHaveSecondaryStress); |
| 200 | } else for (int secondary4 = false; secondary4 <= true; secondary4 ++) { |
| 201 | stress [mainStressed <= 4 ? 5 : 4] = secondary4 ? 2 : 0; |
| 202 | if (numberOfSyllables == 5) { |
| 203 | fillOvertStressPattern (me, underlyingForm, 5, stress, underlyingWeightPattern, overtFormsHaveSecondaryStress); |
| 204 | } else for (int secondary5 = false; secondary5 <= true; secondary5 ++) { |
| 205 | stress [mainStressed <= 5 ? 6 : 5] = secondary5 ? 2 : 0; |
| 206 | if (numberOfSyllables == 6) { |
| 207 | fillOvertStressPattern (me, underlyingForm, 6, stress, underlyingWeightPattern, overtFormsHaveSecondaryStress); |
| 208 | } else for (int secondary6 = false; secondary6 <= true; secondary6 ++) { |
| 209 | stress [mainStressed <= 6 ? 7 : 6] = secondary6 ? 2 : 0; |
| 210 | fillOvertStressPattern (me, underlyingForm, 7, stress, underlyingWeightPattern, overtFormsHaveSecondaryStress); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | static void computeViolationMarks (OTCandidate me) { |
| 221 | #define isHeavy(s) ((s) == U'H' || (s) == U'J') |
| 222 | #define isLight(s) ((s) == U'L' || (s) == U'K') |
| 223 | #define isSyllable(s) (isHeavy (s) || isLight (s)) |
| 224 | #define isStress(s) ((s) == U'1' || (s) == U'2') |
| 225 | const char32 *const firstSlash = str32chr (my string.get(), U'/'); |
| 226 | const char32 *const lastSlash = str32chr (firstSlash + 1, U'/'); |
| 227 | my marks = zero_INTVEC (my numberOfConstraints = NUMBER_OF_CONSTRAINTS); |
| 228 | /* Violations of WSP: count all H not followed by 1 or 2. */ |
| 229 | for (const char32 *p = firstSlash + 1; p != lastSlash; p ++) { |
| 230 | if (isHeavy (p [0]) && ! isStress (p [1])) |
| 231 | my marks [WSP] ++; |
| 232 | } |
| 233 | /* Violations of FtNonfinal: count all heads followed by ). */ |
| 234 | for (const char32 *p = firstSlash + 1; p != lastSlash; p ++) { |
| 235 | if (isStress (p [0]) && p [1] == ')') |
| 236 | my marks [FtNonfinal] ++; |
| 237 | } |
| 238 | /* Violations of Iambic: count all heads not followed by ). */ |
| 239 | for (const char32 *p = firstSlash + 1; p != lastSlash; p ++) { |
| 240 | if (isStress (p [0]) && p [1] != ')') |
| 241 | my marks [Iambic] ++; |
| 242 | } |
| 243 | /* Violations of Parse and Peripheral: count all syllables not between (). */ |
| 244 | int depth = 0; |
| 245 | for (const char32 *p = firstSlash + 1; p != lastSlash; p ++) { |
| 246 | if (p [0] == U'(') depth ++; |
| 247 | else if (p [0] == U')') depth --; |
| 248 | else if (isSyllable (p [0]) && depth != 1) { |
| 249 | my marks [Parse] ++; |
| 250 | if (p != firstSlash + 1 && p != lastSlash - 1) |
| 251 | my marks [Peripheral] ++; |
| 252 | } |
| 253 | } |
| 254 | /* Violations of FootBin: count all (L1) and (L2). */ |
| 255 | for (const char32 *p = firstSlash + 1; p != lastSlash; p ++) { |
| 256 | if (isLight (p [0]) && p [-1] == '(' && isStress (p [1]) && p [2] == U')') |
| 257 | my marks [FootBin] ++; |
| 258 | } |
| 259 | /* Violations of WFL: count all initial / not followed by (. */ |
| 260 | if (firstSlash [1] != U'(') |
| 261 | my marks [WFL] = 1; |
| 262 | /* Violations of WFR: count all final / not preceded by ). */ |
| 263 | if (lastSlash [-1] != ')') |
| 264 | my marks [WFR] = 1; |
| 265 | /* Violations of Main_L: count syllables from foot containing X1 to left edge. */ |
| 266 | { |
| 267 | const char32 *p = str32chr (firstSlash, U'1'); |
| 268 | for (; *p != U'('; p --) { } |
| 269 | for (; p != firstSlash; p --) { |
| 270 | if (isSyllable (p [0])) |
| 271 | my marks [Main_L] ++; |
| 272 | } |
| 273 | } |
| 274 | /* Violations of Main_R: count syllables from foot containing X1 to right edge. */ |
| 275 | { |
| 276 | const char32 *p = str32chr (firstSlash, U'1'); |
| 277 | for (; *p != U')'; p ++) { } |
| 278 | for (; p != lastSlash; p ++) { |
| 279 | if (isSyllable (p [0])) |
| 280 | my marks [Main_R] ++; |
| 281 | } |
| 282 | } |
| 283 | /* Violations of AFL: count syllables from every foot to left edge. */ |
| 284 | for (const char32 *p = firstSlash + 1; p != lastSlash; p ++) { |
| 285 | if (p [0] == U'(') { |
| 286 | for (const char32 *q = p; q != firstSlash; q --) { |
| 287 | if (isSyllable (q [0])) |
| 288 | my marks [AFL] ++; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | /* Violations of AFR: count syllables from every foot to right edge. */ |
| 293 | for (const char32 *p = firstSlash + 1; p != lastSlash; p ++) { |
| 294 | if (p [0] == U')') { |
| 295 | for (const char32 *q = p; q != lastSlash; q ++) { |
| 296 | if (isSyllable (q [0])) |
| 297 | my marks [AFR] ++; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | /* Violations of Nonfinal: count all final / preceded by ). */ |
| 302 | if (lastSlash [-1] == U')') |
| 303 | my marks [Nonfinal] = 1; |
| 304 | /* Violations of Trochaic: count all heads not preceded by (. */ |
| 305 | for (const char32 *p = firstSlash + 1; p != lastSlash; p ++) { |
| 306 | if (isStress (p [0]) && p [-2] != '(') |
| 307 | my marks [Trochaic] ++; |
| 308 | } |
| 309 | /* Violations of FootBimoraic: count weight between (). */ |
| 310 | for (const char32 *p = firstSlash + 1; p != lastSlash; p ++) { |
| 311 | if (p [0] == U'(') { |
| 312 | int weight = 0; |
| 313 | for (p ++; p [0] != ')'; p ++) { |
| 314 | if (isHeavy (p [0])) weight += 2; |
| 315 | else if (isLight (p [0])) weight += 1; |
| 316 | } |
| 317 | if (weight != 2) my marks [FootBimoraic] ++; |
| 318 | } |
| 319 | } |
| 320 | /* Violations of FootBisyllabic: count all (X1) and (X2). */ |
| 321 | for (const char32 *p = firstSlash + 1; p != lastSlash; p ++) { |
| 322 | if (isSyllable (p [0]) && p [-1] == U'(' && isStress (p [1]) && p [2] == U')') |
| 323 | my marks [FootBisyllabic] ++; |
| 324 | } |
| 325 | /* Violations of MainNonfinal: count all final / preceded by ) preceded by 1 in the same foot. */ |
| 326 | if (lastSlash [-1] == U')') { |
| 327 | for (const char32 *p = lastSlash - 2; ; p --) { |
| 328 | if (p [0] == '2') break; |
| 329 | if (p [0] == '1') { |
| 330 | my marks [MainNonfinal] = 1; |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | /* Violations of HeadNonfinal: count all final / preceded by ) directly preceded by 1, plus MainNonfinal. */ |
| 336 | if (lastSlash [-1] == U')') { |
| 337 | if (lastSlash [-2] == U'1') { |
| 338 | my marks [HeadNonfinal] = 2; |
| 339 | } else { |
| 340 | for (const char32 *p = lastSlash - 2; ; p --) { |
| 341 | if (p [0] == U'2') break; |
| 342 | if (p [0] == U'1') { |
| 343 | my marks [HeadNonfinal] = 1; |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | /* Violations of *Clash: count all 1 and 2 followed by an 1 or 2 after the next L or H. */ |
| 350 | for (const char32 *p = firstSlash + 1; p != lastSlash; p ++) { |
| 351 | if (isStress (p [0])) { |
| 352 | for (const char32 *q = p + 1; q != lastSlash; q ++) { |
| 353 | if (isSyllable (q [0])) { |
| 354 | if (isStress (q [1])) { |
| 355 | my marks [Clash] ++; |
| 356 | } |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | /* Violations of *Lapse: count all sequences of three unstressed syllables. */ |
| 363 | depth = 0; |
| 364 | for (const char32 *p = firstSlash + 1; p != lastSlash; p ++) { |
| 365 | if (isSyllable (p [0])) { |
| 366 | if (isStress (p [1])) { |
| 367 | depth = 0; |
| 368 | } else { |
| 369 | if (++ depth > 2) { |
| 370 | my marks [Lapse] ++; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | /* Violations of WeightByPosition: count all K. */ |
| 376 | for (const char32 *p = firstSlash + 1; p != lastSlash; p ++) { |
| 377 | if (p [0] == U'K') |
| 378 | my marks [WeightByPosition] ++; |
| 379 | } |
| 380 | /* Violations of *MoraicConsonant: count all J. */ |
| 381 | for (const char32 *p = firstSlash + 1; p != lastSlash; p ++) { |
| 382 | if (p [0] == U'J') |
| 383 | my marks [MoraicConsonant] ++; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | static void replaceOutput (OTCandidate me) { |
| 388 | bool abstract = false; |
| 389 | Melder_assert (my string); |
| 390 | char32 newString [150], *q = & newString [0]; |
| 391 | for (const char32 *p = & my string [0]; *p != U'\0'; p ++) { |
| 392 | if (p [0] == U' ') { |
| 393 | *q ++ = p [-1] == U']' || p [1] == U'/' ? U' ' : U'.'; |
| 394 | } else if (isSyllable (p [0])) { |
| 395 | *q ++ = U'c'; |
| 396 | if (abstract) { |
| 397 | *q ++ = U'V'; |
| 398 | if (isStress (p [1])) { |
| 399 | *q ++ = p [1]; |
| 400 | } |
| 401 | if (p [0] == U'L') { |
| 402 | ; |
| 403 | } else if (p [0] == U'H') { |
| 404 | *q ++ = U'V'; |
| 405 | } else if (p [0] == U'K') { |
| 406 | *q ++ = U'c'; |
| 407 | } else { |
| 408 | *q ++ = U'C'; |
| 409 | } |
| 410 | } else { |
| 411 | *q ++ = U'v'; |
| 412 | if (p [0] == U'L') { |
| 413 | ; |
| 414 | } else if (p [0] == 'H') { |
| 415 | *q ++ = U':'; |
| 416 | } else { |
| 417 | *q ++ = U'c'; |
| 418 | } |
| 419 | } |
| 420 | } else if (isStress (p [0]) && abstract) { |
| 421 | ; |
| 422 | } else { |
| 423 | if (p [0] == U'/') abstract = true; |
| 424 | *q ++ = p [0]; |
| 425 | } |
| 426 | } |
| 427 | *q = U'\0'; |
| 428 | my string = Melder_dup (newString); |
| 429 | } |
| 430 | |
| 431 | autoOTMulti OTMulti_create_metrics ( |
| 432 | kOTGrammar_createMetricsGrammar_initialRanking equal_footForm_wsp, |
| 433 | int trochaicityConstraint, int includeFootBimoraic, int includeFootBisyllabic, |
| 434 | int includePeripheral, int nonfinalityConstraint, int overtFormsHaveSecondaryStress, |
| 435 | int includeClashAndLapse, int includeCodas) |
| 436 | { |
| 437 | try { |
| 438 | int underlyingWeightPattern [1+7], maximumUnderlyingWeight = includeCodas ? 3 : 2; |
| 439 | autoOTMulti me = Thing_new (OTMulti); |
| 440 | my constraints = newvectorzero <structOTConstraint> (my numberOfConstraints = NUMBER_OF_CONSTRAINTS); |
| 441 | for (integer icons = 1; icons <= NUMBER_OF_CONSTRAINTS; icons ++) { |
| 442 | const OTConstraint constraint = & my constraints [icons]; |
| 443 | constraint -> name = Melder_dup (constraintNames [icons]); |
| 444 | constraint -> ranking = 100.0; |
| 445 | constraint -> plasticity = 1.0; |
| 446 | } |
| 447 | if (equal_footForm_wsp >= kOTGrammar_createMetricsGrammar_initialRanking::FOOT_FORM_HIGH) { |
| 448 | /* |
| 449 | Foot form constraints high. |
| 450 | */ |
| 451 | my constraints [FtNonfinal]. ranking = 101.0; |
| 452 | my constraints [Iambic]. ranking = 101.0; |
| 453 | my constraints [Trochaic]. ranking = -1e9; |
| 454 | } |
| 455 | if (equal_footForm_wsp == kOTGrammar_createMetricsGrammar_initialRanking::WSP_HIGH) { |
| 456 | /* |
| 457 | Quantity sensitivity high, foot form constraints in the second stratum. |
| 458 | */ |
| 459 | my constraints [WSP]. ranking = 102.0; |
| 460 | } |
| 461 | integer numberOfCandidates = 0; |
| 462 | for (int numberOfSyllables = 2; numberOfSyllables <= 7; numberOfSyllables ++) { |
| 463 | const integer numberOfUnderlyingWeightPatterns = ( numberOfSyllables > 5 ? 1 : Melder_iround (pow (maximumUnderlyingWeight, numberOfSyllables)) ); |
| 464 | numberOfCandidates += ( includeCodas ? numberOfCandidates_codas : numberOfCandidates_noCodas ) [numberOfSyllables] * numberOfUnderlyingWeightPatterns; |
| 465 | } |
| 466 | my candidates = newvectorzero <structOTCandidate> (numberOfCandidates); |
| 467 | my numberOfCandidates = 0; |
| 468 | for (int numberOfSyllables = 2; numberOfSyllables <= 7; numberOfSyllables ++) { |
| 469 | const integer numberOfUnderlyingWeightPatterns = ( numberOfSyllables > 5 ? 1 : Melder_iround (pow (maximumUnderlyingWeight, numberOfSyllables)) ); |
| 470 | for (integer isyll = 1; isyll <= numberOfSyllables; isyll ++) |
| 471 | underlyingWeightPattern [isyll] = 1; // L or cv |
| 472 | for (integer iweightPattern = 1; iweightPattern <= numberOfUnderlyingWeightPatterns; iweightPattern ++) { |
| 473 | fillTableau (me.get(), numberOfSyllables, underlyingWeightPattern, overtFormsHaveSecondaryStress, includeCodas); |
| 474 | /* |
| 475 | Cycle to next underlying weight pattern. |
| 476 | */ |
| 477 | underlyingWeightPattern [numberOfSyllables] += 1; |
| 478 | for (integer isyll = numberOfSyllables; isyll >= 2; isyll --) { |
| 479 | if (underlyingWeightPattern [isyll] > maximumUnderlyingWeight) { |
| 480 | underlyingWeightPattern [isyll] = 1; |
| 481 | underlyingWeightPattern [isyll - 1] += 1; |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | Melder_assert (my numberOfCandidates == numberOfCandidates); |
| 487 | for (integer icand = 1; icand <= my numberOfCandidates; icand ++) |
| 488 | computeViolationMarks (& my candidates [icand]); |
| 489 | OTMulti_checkIndex (me.get()); |
| 490 | OTMulti_newDisharmonies (me.get(), 0.0); |
| 491 | if (trochaicityConstraint == 1) |
| 492 | OTMulti_removeConstraint (me.get(), U"Trochaic"); |
| 493 | else |
| 494 | OTMulti_removeConstraint (me.get(), U"FtNonfinal"); |
| 495 | if (! includeFootBimoraic) |
| 496 | OTMulti_removeConstraint (me.get(), U"FtBimor"); |
| 497 | if (! includeFootBisyllabic) |
| 498 | OTMulti_removeConstraint (me.get(), U"FtBisyl"); |
| 499 | if (! includePeripheral) |
| 500 | OTMulti_removeConstraint (me.get(), U"Peripheral"); |
| 501 | if (nonfinalityConstraint == 1) { |
| 502 | OTMulti_removeConstraint (me.get(), U"MainNonfinal"); |
| 503 | OTMulti_removeConstraint (me.get(), U"HeadNonfinal"); |
| 504 | } else if (nonfinalityConstraint == 2) { |
| 505 | OTMulti_removeConstraint (me.get(), U"HeadNonfinal"); |
| 506 | OTMulti_removeConstraint (me.get(), U"Nonfinal"); |
| 507 | } else { |
| 508 | OTMulti_removeConstraint (me.get(), U"MainNonfinal"); |
| 509 | OTMulti_removeConstraint (me.get(), U"Nonfinal"); |
| 510 | } |
| 511 | if (! includeClashAndLapse) { |
| 512 | OTMulti_removeConstraint (me.get(), U"*Clash"); |
| 513 | OTMulti_removeConstraint (me.get(), U"*Lapse"); |
| 514 | } |
| 515 | if (! includeCodas) { |
| 516 | OTMulti_removeConstraint (me.get(), U"WeightByPosition"); |
| 517 | OTMulti_removeConstraint (me.get(), U"*C\\mu"); |
| 518 | } |
| 519 | if (includeCodas) { |
| 520 | for (integer icand = 1; icand <= my numberOfCandidates; icand ++) { |
| 521 | replaceOutput (& my candidates [icand]); |
| 522 | } |
| 523 | } |
| 524 | return me; |
| 525 | } catch (MelderError) { |
| 526 | Melder_throw (U"Metrics grammar not created."); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /* End of file OTMulti_ex_metrics.cpp */ |