| 1 | /* WordList.cpp |
| 2 | * |
| 3 | * Copyright (C) 1999-2007,2011,2012,2015-2019,2022 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 2 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 "WordList.h"cxx . . $ `<file>/fon/WordList.h`/ |
| 20 | #include "../kar/longchar.h"cxx . . $ `<file>/kar/longchar.h`/ |
| 21 | #include "Collection.h"cxx . . $ `<file>/sys/Collection.h`/ |
| 22 | |
| 23 | #include "oo_DESTROY.h"cxx . . $ `<file>/sys/oo_DESTROY.h`/ |
| 24 | #include "WordList_def.h"cxx . . $ `<file>/fon/WordList_def.h`/ |
| 25 | #include "oo_COPY.h"cxx . . $ `<file>/sys/oo_COPY.h`/ |
| 26 | #include "WordList_def.h"cxx . . $ `<file>/fon/WordList_def.h`/ |
| 27 | #include "oo_EQUAL.h"cxx . . $ `<file>/sys/oo_EQUAL.h`/ |
| 28 | #include "WordList_def.h"cxx . . $ `<file>/fon/WordList_def.h`/ |
| 29 | #include "oo_CAN_WRITE_AS_ENCODING.h"cxx . . $ `<file>/sys/oo_CAN_WRITE_AS_ENCODING.h`/ |
| 30 | #include "WordList_def.h"cxx . . $ `<file>/fon/WordList_def.h`/ |
| 31 | #include "oo_WRITE_TEXT.h"cxx . . $ `<file>/sys/oo_WRITE_TEXT.h`/ |
| 32 | #include "WordList_def.h"cxx . . $ `<file>/fon/WordList_def.h`/ |
| 33 | #include "oo_READ_TEXT.h"cxx . . $ `<file>/sys/oo_READ_TEXT.h`/ |
| 34 | #include "WordList_def.h"cxx . . $ `<file>/fon/WordList_def.h`/ |
| 35 | #include "oo_WRITE_BINARY.h"cxx . . $ `<file>/sys/oo_WRITE_BINARY.h`/ |
| 36 | #include "WordList_def.h"cxx . . $ `<file>/fon/WordList_def.h`/ |
| 37 | #include "oo_DESCRIPTION.h"cxx . . $ `<file>/sys/oo_DESCRIPTION.h`/ |
| 38 | #include "WordList_def.h"cxx . . $ `<file>/fon/WordList_def.h`/ |
| 39 | |
| 40 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (WordList, Daata, 1); |
| 41 | |
| 42 | static integercxx . . $ integer# WordList_countcxx . . $ WordList_count(725171f15ab170ef). (WordList me) { |
| 43 | integercxx . . $ integer# numberOfWords = 0; |
| 44 | for (char32cxx . . $ char32# *p = & mycxx . . $ `melder/melder.h:44:9`! string [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]; *p; p ++) { |
| 45 | if (*p == U'\n') |
| 46 | numberOfWords += 1; |
| 47 | } |
| 48 | return numberOfWords; |
| 49 | } |
| 50 | |
| 51 | void structWordList :: v1_info () { |
| 52 | structDaatacxx . . $ structDaata# :: v1_infocxx . . $ structThing#v1_info(49f6e7a06ebc5aa8). (); |
| 53 | const integercxx . . $ integer# n = WordList_countcxx . . $ WordList_count(725171f15ab170ef). (this); |
| 54 | if (ourcxx . . $ `melder/melder.h:42:9`! length == 0) |
| 55 | ourcxx . . $ `melder/melder.h:42:9`! length = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (ourcxx . . $ `melder/melder.h:42:9`! string.getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 56 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Number of words: ", n); |
| 57 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Number of characters: ", length - n); |
| 58 | } |
| 59 | |
| 60 | void structWordList :: v1_readBinary (FILE *f, int formatVersion) { |
| 61 | if (formatVersion <= 0) { |
| 62 | ourcxx . . $ `melder/melder.h:42:9`! length = bingeti32cxx . . $ bingeti32(89bc1efcdac0ca22). (f); |
| 63 | if (ourcxx . . $ `melder/melder.h:42:9`! length < 0) |
| 64 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Wrong length ", ourcxx . . $ `melder/melder.h:42:9`! length, U"."); |
| 65 | ourcxx . . $ `melder/melder.h:42:9`! string =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). autostring32cxx . . $ autostring32# (ourcxx . . $ `melder/melder.h:42:9`! length); |
| 66 | char32cxx . . $ char32# *current = & ourcxx . . $ `melder/melder.h:42:9`! string [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0], *p = current; |
| 67 | int kar = 0; |
| 68 | if (ourcxx . . $ `melder/melder.h:42:9`! length > 0) { |
| 69 | /* |
| 70 | * Read first word. |
| 71 | */ |
| 72 | for (;;) { |
| 73 | if (p - & string [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0] >= length - 1) break; |
| 74 | kar = fgetc (f); |
| 75 | if (kar == EOFcxx . . $ `<external>/232824719582829767/stdio.h:105:9`!) |
| 76 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Early end of file."); |
| 77 | if (kar >= 128) |
| 78 | break; |
| 79 | *p ++ = (char32cxx . . $ char32#) kar; |
| 80 | } |
| 81 | *p ++ = U'\n'; |
| 82 | /* |
| 83 | * Read following words. |
| 84 | */ |
| 85 | for (;;) { |
| 86 | char32cxx . . $ char32# *previous = current; |
| 87 | int numberOfSame = kar - 128; |
| 88 | current = p; |
| 89 | str32ncpycxx . . $ str32ncpy(a0fb29e7f5bbd1dc). (current, previous, numberOfSame); |
| 90 | p += numberOfSame; |
| 91 | for (;;) { |
| 92 | if (p - & string [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0] >= length - 1) break; |
| 93 | kar = fgetc (f); |
| 94 | if (kar == EOFcxx . . $ `<external>/232824719582829767/stdio.h:105:9`!) |
| 95 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Early end of file."); |
| 96 | if (kar >= 128) |
| 97 | break; |
| 98 | *p ++ = (char32cxx . . $ char32#) kar; |
| 99 | } |
| 100 | *p ++ = U'\n'; |
| 101 | if (p - & string [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0] >= ourcxx . . $ `melder/melder.h:42:9`! length) |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | *p = U'\0'; |
| 106 | if (p - & ourcxx . . $ `melder/melder.h:42:9`! string [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0] != ourcxx . . $ `melder/melder.h:42:9`! length) |
| 107 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Length in header (", ourcxx . . $ `melder/melder.h:42:9`! length, U") does not match length of string (", (integer) (p - & ourcxx . . $ `melder/melder.h:42:9`! string [0]), U")."); |
| 108 | } else { |
| 109 | ourcxx . . $ `melder/melder.h:42:9`! string =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). bingetw32cxx . . $ bingetw32(6856a237bc2beebc). (f); |
| 110 | ourcxx . . $ `melder/melder.h:42:9`! length = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (ourcxx . . $ `melder/melder.h:42:9`! string.getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | static autoStringSetcxx . . $ autoStringSet# Strings_to_StringSetcxx . . $ Strings_to_StringSet(b8ebe5a95888d88b). (Stringscxx . . $ Strings# me, bool nativize) { |
| 115 | /* |
| 116 | Make sure that the strings are normalized, sorted, and unique. |
| 117 | */ |
| 118 | autoStringSetcxx . . $ autoStringSet# you = StringSet_createcxx . . $ StringSet_create(3c0f0334e2cf88fa). (); |
| 119 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! numberOfStringscxx . . $ structStrings#numberOfStrings.; i ++) { |
| 120 | autoSimpleStringcxx . . $ autoSimpleString# item = SimpleString_createcxx . . $ SimpleString_create(d4d4ea089a1063c6). (mycxx . . $ `melder/melder.h:44:9`! stringscxx . . $ structStrings#strings. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i].getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); // TODO: normalize |
| 121 | yourcxx . . $ `melder/melder.h:46:9`! addItem_unsorted_movecxx . . $ SortedOf#addItem_unsorted_move(7a1ccebf8c9794a2). (itemcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f)..movecxx . . $ autoSomeThing#move(25acc3554b7d1642).()); |
| 122 | } |
| 123 | yourcxx . . $ `melder/melder.h:46:9`! sortcxx . . $ SortedOf#sort(49f6e7a06ebc5aa8). (); |
| 124 | yourcxx . . $ `melder/melder.h:46:9`! unicizecxx . . $ SortedSetOf#unicize(49f6e7a06ebc5aa8). (); |
| 125 | return youcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 126 | } |
| 127 | |
| 128 | autoWordList Strings_to_WordListcxx . . $ Strings_to_WordList(ed1f8af029f28fee). (Stringscxx . . $ Strings# me) { |
| 129 | try { |
| 130 | /* |
| 131 | Check whether the strings are all words. |
| 132 | */ |
| 133 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! numberOfStringscxx . . $ structStrings#numberOfStrings.; i ++) |
| 134 | if (Melder_findHorizontalOrVerticalSpacecxx . . $ Melder_findHorizontalOrVerticalSpace(efea2b2856d283a0). (mycxx . . $ `melder/melder.h:44:9`! stringscxx . . $ structStrings#strings. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i].getcxx . . $ _autostring#get(5a2a78a048fb49a8).())) |
| 135 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"String ", i, U" contains a space and can therefore not be included in a WordList."); |
| 136 | |
| 137 | autoStringSetcxx . . $ autoStringSet# you = Strings_to_StringSetcxx . . $ Strings_to_StringSet(b8ebe5a95888d88b). (me, true); |
| 138 | |
| 139 | integercxx . . $ integer# totalLength = 0; |
| 140 | for (integercxx . . $ integer# i = 1; i <= yourcxx . . $ `melder/melder.h:46:9`! sizecxx . . $ CollectionOf#size.; i ++) |
| 141 | totalLength += Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (yourcxx . . $ `melder/melder.h:46:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i]->stringcxx . . $ structSimpleString#string..getcxx . . $ _autostring#get(5a2a78a048fb49a8).()) + 1; // include trailing newline symbol |
| 142 | |
| 143 | autoWordList him = Thing_newcxx . . $ `sys/Thing.h:514:9`! (WordList); |
| 144 | hiscxx . . $ `melder/melder.h:47:9`! length = totalLength; |
| 145 | hiscxx . . $ `melder/melder.h:47:9`! string =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). autostring32cxx . . $ autostring32# (hiscxx . . $ `melder/melder.h:47:9`! length); |
| 146 | /* |
| 147 | Concatenate the strings into the word list. |
| 148 | */ |
| 149 | char32cxx . . $ char32# *q = & hiscxx . . $ `melder/melder.h:47:9`! string [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]; |
| 150 | for (integercxx . . $ integer# i = 1; i <= yourcxx . . $ `melder/melder.h:46:9`! sizecxx . . $ CollectionOf#size.; i ++) { |
| 151 | str32cpycxx . . $ str32cpy(6ab934322573b179). (q, yourcxx . . $ `melder/melder.h:46:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i]->stringcxx . . $ structSimpleString#string..getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 152 | q += Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (yourcxx . . $ `melder/melder.h:46:9`! atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i]->stringcxx . . $ structSimpleString#string..getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 153 | *q ++ = U'\n'; |
| 154 | } |
| 155 | *q = U'\0'; |
| 156 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (q - & hiscxx . . $ `melder/melder.h:47:9`! string [0] == hiscxx . . $ `melder/melder.h:47:9`! length); |
| 157 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 158 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 159 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to WordList."); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | autoStringscxx . . $ autoStrings# WordList_to_Stringscxx . . $ WordList_to_Strings(35ade345febb7fbf). (WordList me) { |
| 164 | try { |
| 165 | const char32cxx . . $ char32# *word = & mycxx . . $ `melder/melder.h:44:9`! string [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]; |
| 166 | autoStringscxx . . $ autoStrings# thee = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Strings); |
| 167 | thycxx . . $ `melder/melder.h:45:9`! numberOfStringscxx . . $ structStrings#numberOfStrings. = WordList_countcxx . . $ WordList_count(725171f15ab170ef). (me); |
| 168 | if (thycxx . . $ `melder/melder.h:45:9`! numberOfStringscxx . . $ structStrings#numberOfStrings. > 0) |
| 169 | thycxx . . $ `melder/melder.h:45:9`! stringscxx . . $ structStrings#strings. =cxx . . $ _autostringautovector#`operator=`(150f7ba7aac25b7e). autoSTRVECcxx . . $ autoSTRVEC# (thycxx . . $ `melder/melder.h:45:9`! numberOfStringscxx . . $ structStrings#numberOfStrings.); |
| 170 | for (integercxx . . $ integer# i = 1; i <= thycxx . . $ `melder/melder.h:45:9`! numberOfStringscxx . . $ structStrings#numberOfStrings.; i ++) { |
| 171 | const char32cxx . . $ char32# *kar = word; |
| 172 | for (; *kar != U'\n'; kar ++) { } |
| 173 | integercxx . . $ integer# length = kar - word; |
| 174 | thycxx . . $ `melder/melder.h:45:9`! stringscxx . . $ structStrings#strings. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i] =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). autostring32cxx . . $ autostring32# (length); |
| 175 | str32ncpycxx . . $ str32ncpy(a0fb29e7f5bbd1dc). (thycxx . . $ `melder/melder.h:45:9`! stringscxx . . $ structStrings#strings. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).i].getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), word, length); |
| 176 | word += length + 1; |
| 177 | } |
| 178 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 179 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 180 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to Strings."); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | static integercxx . . $ integer# gotoStartcxx . . $ gotoStart(49b49ab208fa7058). (WordList me, integercxx . . $ integer# p) { |
| 185 | if (p <= 0) return 0; |
| 186 | -- p; |
| 187 | while (p >= 0 && mycxx . . $ `melder/melder.h:44:9`! string [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).p] != U'\n') p --; |
| 188 | return p + 1; |
| 189 | } |
| 190 | |
| 191 | static integercxx . . $ integer# gotoNextcxx . . $ gotoNext(49b49ab208fa7058). (WordList me, integercxx . . $ integer# p) { |
| 192 | if (p >= mycxx . . $ `melder/melder.h:44:9`! length - 1) return mycxx . . $ `melder/melder.h:44:9`! length; |
| 193 | while (mycxx . . $ `melder/melder.h:44:9`! string [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).p] != U'\n') p ++; |
| 194 | return p + 1; |
| 195 | } |
| 196 | |
| 197 | static integercxx . . $ integer# gotoPreviouscxx . . $ gotoPrevious(49b49ab208fa7058). (WordList me, integercxx . . $ integer# p) { |
| 198 | if (p <= 0) return -1; |
| 199 | if (mycxx . . $ `melder/melder.h:44:9`! string [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).-- p] != U'\n') return -1; // should not occur |
| 200 | if (p <= 0) return 0; // if first word is empty |
| 201 | -- p; // step from newline |
| 202 | while (p >= 0 && mycxx . . $ `melder/melder.h:44:9`! string [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).p] != U'\n') p --; |
| 203 | return p + 1; |
| 204 | } |
| 205 | |
| 206 | static int comparecxx . . $ compare(469b66957bde4a2a). (conststring32cxx . . $ conststring32# word, conststring32cxx . . $ conststring32# p) { |
| 207 | for (;;) { |
| 208 | if (*word == U'\0') { |
| 209 | if (*p == U'\n') return 0; |
| 210 | else return -1; // word is substring of p |
| 211 | } |
| 212 | if (*p == U'\n') return +1; // p is substring of word |
| 213 | if (*word < *p) return -1; |
| 214 | if (*word > *p) return +1; |
| 215 | word ++; |
| 216 | p ++; |
| 217 | } |
| 218 | return 0; // should not occur |
| 219 | } |
| 220 | |
| 221 | static char32cxx . . $ char32# buffercxx . . $ buffer. [3333+1]; |
| 222 | |
| 223 | bool WordList_hasWordcxx . . $ WordList_hasWord(329affebebd6aea5). (WordList me, conststring32cxx . . $ conststring32# word) { |
| 224 | if (Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (word) > 3333) |
| 225 | return false; |
| 226 | Longchar_nativizecxx . . $ Longchar_nativize(35d477e2649d0789). (word, buffercxx . . $ buffer., false); |
| 227 | if (! mycxx . . $ `melder/melder.h:44:9`! length) |
| 228 | mycxx . . $ `melder/melder.h:44:9`! length = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (mycxx . . $ `melder/melder.h:44:9`! string.getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 229 | integercxx . . $ integer# p = mycxx . . $ `melder/melder.h:44:9`! length / 2, d = p / 2; |
| 230 | while (d > 20) { |
| 231 | p = gotoStartcxx . . $ gotoStart(49b49ab208fa7058). (me, p); |
| 232 | int cf = comparecxx . . $ compare(469b66957bde4a2a). (buffercxx . . $ buffer., mycxx . . $ `melder/melder.h:44:9`! string.getcxx . . $ _autostring#get(5a2a78a048fb49a8).() + p); |
| 233 | if (cf == 0) return true; |
| 234 | if (cf < 0) p -= d; else p += d; |
| 235 | d /= 2; |
| 236 | } |
| 237 | p = gotoStartcxx . . $ gotoStart(49b49ab208fa7058). (me, p); |
| 238 | int cf = comparecxx . . $ compare(469b66957bde4a2a). (buffercxx . . $ buffer., mycxx . . $ `melder/melder.h:44:9`! string.getcxx . . $ _autostring#get(5a2a78a048fb49a8).() + p); |
| 239 | if (cf == 0) return true; |
| 240 | if (cf > 0) { |
| 241 | for (;;) { |
| 242 | p = gotoNextcxx . . $ gotoNext(49b49ab208fa7058). (me, p); |
| 243 | if (p >= mycxx . . $ `melder/melder.h:44:9`! length) return false; |
| 244 | cf = comparecxx . . $ compare(469b66957bde4a2a). (buffercxx . . $ buffer., mycxx . . $ `melder/melder.h:44:9`! string.getcxx . . $ _autostring#get(5a2a78a048fb49a8).() + p); |
| 245 | if (cf == 0) return true; |
| 246 | if (cf < 0) return false; |
| 247 | } |
| 248 | } else { |
| 249 | for (;;) { |
| 250 | p = gotoPreviouscxx . . $ gotoPrevious(49b49ab208fa7058). (me, p); |
| 251 | if (p < 0) return false; |
| 252 | cf = comparecxx . . $ compare(469b66957bde4a2a). (buffercxx . . $ buffer., mycxx . . $ `melder/melder.h:44:9`! string.getcxx . . $ _autostring#get(5a2a78a048fb49a8).() + p); |
| 253 | if (cf == 0) return true; |
| 254 | if (cf > 0) return false; |
| 255 | } |
| 256 | } |
| 257 | return false; // should not occur |
| 258 | } |
| 259 | |
| 260 | /* End of file WordList.cpp */ |