| 1 |
/* PairDistribution.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1997-2013,2015-2019,2021,2022,2024,2025 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 |
#include "PairDistribution.h" |
| 20 |
|
| 21 |
#include "oo_DESTROY.h" |
| 22 |
#include "PairDistribution_def.h" |
| 23 |
#include "oo_COPY.h" |
| 24 |
#include "PairDistribution_def.h" |
| 25 |
#include "oo_EQUAL.h" |
| 26 |
#include "PairDistribution_def.h" |
| 27 |
#include "oo_CAN_WRITE_AS_ENCODING.h" |
| 28 |
#include "PairDistribution_def.h" |
| 29 |
#include "oo_WRITE_TEXT.h" |
| 30 |
#include "PairDistribution_def.h" |
| 31 |
#include "oo_READ_TEXT.h" |
| 32 |
#include "PairDistribution_def.h" |
| 33 |
#include "oo_WRITE_BINARY.h" |
| 34 |
#include "PairDistribution_def.h" |
| 35 |
#include "oo_READ_BINARY.h" |
| 36 |
#include "PairDistribution_def.h" |
| 37 |
#include "oo_DESCRIPTION.h" |
| 38 |
#include "PairDistribution_def.h" |
| 39 |
|
| 40 |
Thing_implement (PairProbability, Daata, 0); |
| 41 |
|
| 42 |
Thing_implement (PairDistribution, Daata, 0); |
| 43 |
|
| 44 |
void structPairDistribution :: v1_info () { |
| 45 |
PairDistribution_Parent :: v1_info (); |
| 46 |
MelderInfo_writeLine (U"Number of pairs: ", pairs.size); |
| 47 |
} |
| 48 |
|
| 49 |
autoPairProbability PairProbability_create (conststring32 string1, conststring32 string2, double weight) { |
| 50 |
autoPairProbability me = Thing_new (PairProbability); |
| 51 |
my string1 = Melder_dup (string1); |
| 52 |
my string2 = Melder_dup (string2); |
| 53 |
my weight = weight; |
| 54 |
return me; |
| 55 |
} |
| 56 |
|
| 57 |
autoPairDistribution PairDistribution_create () { |
| 58 |
try { |
| 59 |
autoPairDistribution me = Thing_new (PairDistribution); |
| 60 |
return me; |
| 61 |
} catch (MelderError) { |
| 62 |
Melder_throw (U"PairDistribution not created."); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
int PairProbability_compare (PairProbability me, PairProbability thee) noexcept { |
| 67 |
return str32cmp (my string1.get(), thy string1.get()); |
| 68 |
} |
| 69 |
|
| 70 |
static void PairDistribution_checkSpecifiedPairNumber (PairDistribution me, const integer pairNumber) { |
| 71 |
if (pairNumber < 1) |
| 72 |
Melder_throw (me, U": the specified pair number is ", pairNumber, U", but should be at least 1."); |
| 73 |
if (pairNumber > my pairs.size) |
| 74 |
Melder_throw (me, U": the specified pair number is ", pairNumber, U", but should be at most my number of pairs (", my pairs.size, U")."); |
| 75 |
} |
| 76 |
|
| 77 |
conststring32 PairDistribution_getString1 (PairDistribution me, const integer pairNumber) { |
| 78 |
try { |
| 79 |
PairDistribution_checkSpecifiedPairNumber (me, pairNumber); |
| 80 |
PairProbability prob = my pairs.at [pairNumber]; |
| 81 |
return prob -> string1.get(); |
| 82 |
} catch (MelderError) { |
| 83 |
Melder_throw (me, U": string1 not retrieved."); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
conststring32 PairDistribution_getString2 (PairDistribution me, const integer pairNumber) { |
| 88 |
try { |
| 89 |
PairDistribution_checkSpecifiedPairNumber (me, pairNumber); |
| 90 |
PairProbability prob = my pairs.at [pairNumber]; |
| 91 |
return prob -> string2.get(); |
| 92 |
} catch (MelderError) { |
| 93 |
Melder_throw (me, U": string2 not retrieved."); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
double PairDistribution_getWeight (PairDistribution me, const integer pairNumber) { |
| 98 |
try { |
| 99 |
PairDistribution_checkSpecifiedPairNumber (me, pairNumber); |
| 100 |
PairProbability prob = my pairs.at [pairNumber]; |
| 101 |
return prob -> weight; |
| 102 |
} catch (MelderError) { |
| 103 |
Melder_throw (me, U": weight not retrieved."); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
void PairDistribution_setWeight (PairDistribution me, const integer pairNumber, const double newWeight) { |
| 108 |
try { |
| 109 |
PairDistribution_checkSpecifiedPairNumber (me, pairNumber); |
| 110 |
PairProbability prob = my pairs.at [pairNumber]; |
| 111 |
prob -> weight = newWeight; |
| 112 |
} catch (MelderError) { |
| 113 |
Melder_throw (me, U": weight not changed."); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
void PairDistribution_add (PairDistribution me, conststring32 string1, conststring32 string2, const double weight) { |
| 118 |
autoPairProbability pair = PairProbability_create (string1, string2, weight); |
| 119 |
my pairs. addItem_move (pair.move()); |
| 120 |
} |
| 121 |
|
| 122 |
void PairDistribution_removeZeroWeights (PairDistribution me) { |
| 123 |
for (integer ipair = my pairs.size; ipair > 0; ipair --) { |
| 124 |
PairProbability prob = my pairs.at [ipair]; |
| 125 |
if (prob -> weight <= 0.0) { |
| 126 |
my pairs. removeItem (ipair); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
void PairDistribution_swapInputsAndOutputs (PairDistribution me) { |
| 132 |
for (integer ipair = my pairs.size; ipair > 0; ipair --) { |
| 133 |
PairProbability prob = my pairs.at [ipair]; |
| 134 |
std::swap (prob -> string1, prob -> string2); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
static double PairDistributions_getTotalWeight_checkPositive (PairDistribution me) { |
| 139 |
longdouble totalWeight = 0.0; |
| 140 |
for (integer ipair = 1; ipair <= my pairs.size; ipair ++) { |
| 141 |
PairProbability prob = my pairs.at [ipair]; |
| 142 |
totalWeight += prob -> weight; |
| 143 |
} |
| 144 |
Melder_require (totalWeight > 0.0, |
| 145 |
me, U": the total probability weight is ", Melder_half (double (totalWeight)), |
| 146 |
U" but should be greater than zero for this operation." |
| 147 |
); |
| 148 |
return double (totalWeight); |
| 149 |
} |
| 150 |
|
| 151 |
void PairDistribution_to_Stringses (PairDistribution me, integer nout, autoStrings *strings1_out, autoStrings *strings2_out) { |
| 152 |
try { |
| 153 |
const integer nin = my pairs.size; |
| 154 |
if (nin < 1) |
| 155 |
Melder_throw (U"No candidates."); |
| 156 |
if (nout < 1) |
| 157 |
Melder_throw (U"Number of generated string pairs should be positive."); |
| 158 |
const double total = PairDistributions_getTotalWeight_checkPositive (me); |
| 159 |
autoStrings strings1 = Thing_new (Strings); |
| 160 |
strings1 -> numberOfStrings = nout; |
| 161 |
strings1 -> strings = autoSTRVEC (nout); |
| 162 |
autoStrings strings2 = Thing_new (Strings); |
| 163 |
strings2 -> numberOfStrings = nout; |
| 164 |
strings2 -> strings = autoSTRVEC (nout); |
| 165 |
for (integer iout = 1; iout <= nout; iout ++) { |
| 166 |
integer iin; |
| 167 |
do { |
| 168 |
const double rand = NUMrandomUniform (0, total); |
| 169 |
longdouble sum = 0.0; |
| 170 |
for (iin = 1; iin <= nin; iin ++) { |
| 171 |
PairProbability prob = my pairs.at [iin]; |
| 172 |
sum += prob -> weight; |
| 173 |
if (rand <= sum) break; |
| 174 |
} |
| 175 |
} while (iin > nin); /* Guard against rounding errors. */ |
| 176 |
PairProbability const prob = my pairs.at [iin]; |
| 177 |
if (! prob -> string1 || ! prob -> string2) |
| 178 |
Melder_throw (U"No string in probability pair ", iin, U"."); |
| 179 |
strings1 -> strings [iout] = Melder_dup (prob -> string1.get()); |
| 180 |
strings2 -> strings [iout] = Melder_dup (prob -> string2.get()); |
| 181 |
} |
| 182 |
*strings1_out = strings1.move(); |
| 183 |
*strings2_out = strings2.move(); |
| 184 |
} catch (MelderError) { |
| 185 |
Melder_throw (me, U": generation of Stringses not performed."); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
void PairDistribution_peekPair (PairDistribution me, conststring32 *out_string1, conststring32 *out_string2) { |
| 190 |
try { |
| 191 |
*out_string1 = *out_string2 = nullptr; |
| 192 |
longdouble total = 0.0; |
| 193 |
const integer nin = my pairs.size; |
| 194 |
PairProbability prob; |
| 195 |
if (nin < 1) Melder_throw (U"No candidates."); |
| 196 |
integer iin; |
| 197 |
for (iin = 1; iin <= nin; iin ++) { |
| 198 |
prob = my pairs.at [iin]; |
| 199 |
total += prob -> weight; |
| 200 |
} |
| 201 |
do { |
| 202 |
const double rand = NUMrandomUniform (0.0, double (total)); |
| 203 |
longdouble sum = 0.0; |
| 204 |
for (iin = 1; iin <= nin; iin ++) { |
| 205 |
prob = my pairs.at [iin]; |
| 206 |
sum += prob -> weight; |
| 207 |
if (rand <= sum) break; |
| 208 |
} |
| 209 |
} while (iin > nin); // guard against rounding errors |
| 210 |
prob = my pairs.at [iin]; |
| 211 |
if (! prob -> string1 || ! prob -> string2) Melder_throw (U"No string in probability pair ", iin, U"."); |
| 212 |
*out_string1 = prob -> string1.get(); |
| 213 |
*out_string2 = prob -> string2.get(); |
| 214 |
} catch (MelderError) { |
| 215 |
Melder_throw (me, U": pair not peeked."); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
static double PairDistribution_getFractionCorrect (PairDistribution me, int which) { |
| 220 |
try { |
| 221 |
longdouble correct = 0.0; |
| 222 |
integer pairmin = 1, ipair; |
| 223 |
autoPairDistribution thee = Data_copy (me); |
| 224 |
thy pairs.sort (PairProbability_compare); |
| 225 |
double total = PairDistributions_getTotalWeight_checkPositive (thee.get()); |
| 226 |
do { |
| 227 |
integer pairmax = pairmin; |
| 228 |
const conststring32 firstInput = thy pairs.at [pairmin] -> string1.get(); |
| 229 |
for (ipair = pairmin + 1; ipair <= thy pairs.size; ipair ++) { |
| 230 |
PairProbability const prob = thy pairs.at [ipair]; |
| 231 |
if (! str32equ (prob -> string1.get(), firstInput)) { |
| 232 |
pairmax = ipair - 1; |
| 233 |
break; |
| 234 |
} |
| 235 |
} |
| 236 |
if (ipair > thy pairs.size) pairmax = thy pairs.size; |
| 237 |
if (which == 0) { |
| 238 |
double pmax = 0.0; |
| 239 |
for (ipair = pairmin; ipair <= pairmax; ipair ++) { |
| 240 |
PairProbability const prob = thy pairs.at [ipair]; |
| 241 |
const double p = prob -> weight / total; |
| 242 |
if (p > pmax) pmax = p; |
| 243 |
} |
| 244 |
correct += pmax; |
| 245 |
} else { |
| 246 |
longdouble sum = 0.0, p2 = 0.0; |
| 247 |
for (ipair = pairmin; ipair <= pairmax; ipair ++) { |
| 248 |
PairProbability const prob = thy pairs.at [ipair]; |
| 249 |
const double p = prob -> weight / total; |
| 250 |
sum += p; |
| 251 |
p2 += p * p; |
| 252 |
} |
| 253 |
correct += p2 / sum; |
| 254 |
} |
| 255 |
pairmin = pairmax + 1; |
| 256 |
} while (pairmin <= thy pairs.size); |
| 257 |
return double (correct); |
| 258 |
} catch (MelderError) { |
| 259 |
Melder_throw (me, U": could not compute my fraction correct."); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
double PairDistribution_getFractionCorrect_maximumLikelihood (PairDistribution me) { |
| 264 |
return PairDistribution_getFractionCorrect (me, 0); |
| 265 |
} |
| 266 |
|
| 267 |
double PairDistribution_getFractionCorrect_probabilityMatching (PairDistribution me) { |
| 268 |
return PairDistribution_getFractionCorrect (me, 1); |
| 269 |
} |
| 270 |
|
| 271 |
double PairDistribution_Distributions_getFractionCorrect (PairDistribution me, Distributions dist, integer column) { |
| 272 |
try { |
| 273 |
longdouble correct = 0.0; |
| 274 |
integer pairmin = 1; |
| 275 |
char32 string [1000]; |
| 276 |
Distributions_checkSpecifiedColumnNumberWithinRange (dist, column); |
| 277 |
autoPairDistribution thee = Data_copy (me); |
| 278 |
thy pairs.sort (PairProbability_compare); |
| 279 |
double total = PairDistributions_getTotalWeight_checkPositive (thee.get()); |
| 280 |
do { |
| 281 |
integer pairmax = pairmin, ipair; |
| 282 |
longdouble sum = 0.0, sumDist = 0.0; |
| 283 |
const conststring32 firstInput = thy pairs.at [pairmin] -> string1.get(); |
| 284 |
for (ipair = pairmin + 1; ipair <= thy pairs.size; ipair ++) { |
| 285 |
PairProbability prob = thy pairs.at [ipair]; |
| 286 |
if (! str32equ (prob -> string1.get(), firstInput)) { |
| 287 |
pairmax = ipair - 1; |
| 288 |
break; |
| 289 |
} |
| 290 |
} |
| 291 |
if (ipair > thy pairs.size) pairmax = thy pairs.size; |
| 292 |
for (ipair = pairmin; ipair <= pairmax; ipair ++) { |
| 293 |
PairProbability prob = thy pairs.at [ipair]; |
| 294 |
const double p = prob -> weight / total; |
| 295 |
double pout = 0.0; |
| 296 |
Melder_sprint (string, 1000, prob -> string1.get(), U" \\-> ", prob -> string2.get()); |
| 297 |
for (integer idist = 1; idist <= dist -> numberOfRows; idist ++) { |
| 298 |
if (str32equ (string, dist -> rowLabels [idist].get())) { |
| 299 |
pout = dist -> data [idist] [column]; |
| 300 |
break; |
| 301 |
} |
| 302 |
} |
| 303 |
sum += p * pout; |
| 304 |
} |
| 305 |
Melder_sprint (string, 1000, firstInput, U" \\-> "); |
| 306 |
const integer length = Melder_length (string); |
| 307 |
for (integer idist = 1; idist <= dist -> numberOfRows; idist ++) |
| 308 |
if (str32nequ (string, dist -> rowLabels [idist].get(), length)) |
| 309 |
sumDist += dist -> data [idist] [column]; |
| 310 |
if (sumDist != 0.0) correct += sum / sumDist; |
| 311 |
pairmin = pairmax + 1; |
| 312 |
} while (pairmin <= thy pairs.size); |
| 313 |
return double (correct); |
| 314 |
} catch (MelderError) { |
| 315 |
Melder_throw (me, U" & ", dist, U": could not compute our fraction correct."); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
autoTable PairDistribution_to_Table (PairDistribution me) { |
| 320 |
try { |
| 321 |
autoTable thee = Table_createWithColumnNames (my pairs.size, |
| 322 |
autoSTRVEC ({ U"string1", U"string2", U"weight" }).get()); |
| 323 |
for (integer ipair = 1; ipair <= my pairs.size; ipair ++) { |
| 324 |
PairProbability prob = my pairs.at [ipair]; |
| 325 |
Table_setStringValue (thee.get(), ipair, 1, prob -> string1.get()); |
| 326 |
Table_setStringValue (thee.get(), ipair, 2, prob -> string2.get()); |
| 327 |
Table_setNumericValue (thee.get(), ipair, 3, prob -> weight); |
| 328 |
} |
| 329 |
return thee; |
| 330 |
} catch (MelderError) { |
| 331 |
Melder_throw (me, U": not converted to Table."); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
/* End of file PairDistribution.cpp */ |