| 1 | /* SpellingChecker.cpp |
| 2 | * |
| 3 | * Copyright (C) 1999-2007,2011,2012,2015-2020,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 "SpellingChecker.h"cxx . . $ `<file>/fon/SpellingChecker.h`/ |
| 20 | |
| 21 | #include "oo_DESTROY.h"cxx . . $ `<file>/sys/oo_DESTROY.h`/ |
| 22 | #include "SpellingChecker_def.h"cxx . . $ `<file>/fon/SpellingChecker_def.h`/ |
| 23 | #include "oo_COPY.h"cxx . . $ `<file>/sys/oo_COPY.h`/ |
| 24 | #include "SpellingChecker_def.h"cxx . . $ `<file>/fon/SpellingChecker_def.h`/ |
| 25 | #include "oo_EQUAL.h"cxx . . $ `<file>/sys/oo_EQUAL.h`/ |
| 26 | #include "SpellingChecker_def.h"cxx . . $ `<file>/fon/SpellingChecker_def.h`/ |
| 27 | #include "oo_CAN_WRITE_AS_ENCODING.h"cxx . . $ `<file>/sys/oo_CAN_WRITE_AS_ENCODING.h`/ |
| 28 | #include "SpellingChecker_def.h"cxx . . $ `<file>/fon/SpellingChecker_def.h`/ |
| 29 | #include "oo_WRITE_TEXT.h"cxx . . $ `<file>/sys/oo_WRITE_TEXT.h`/ |
| 30 | #include "SpellingChecker_def.h"cxx . . $ `<file>/fon/SpellingChecker_def.h`/ |
| 31 | #include "oo_WRITE_BINARY.h"cxx . . $ `<file>/sys/oo_WRITE_BINARY.h`/ |
| 32 | #include "SpellingChecker_def.h"cxx . . $ `<file>/fon/SpellingChecker_def.h`/ |
| 33 | #include "oo_READ_TEXT.h"cxx . . $ `<file>/sys/oo_READ_TEXT.h`/ |
| 34 | #include "SpellingChecker_def.h"cxx . . $ `<file>/fon/SpellingChecker_def.h`/ |
| 35 | #include "oo_READ_BINARY.h"cxx . . $ `<file>/sys/oo_READ_BINARY.h`/ |
| 36 | #include "SpellingChecker_def.h"cxx . . $ `<file>/fon/SpellingChecker_def.h`/ |
| 37 | #include "oo_DESCRIPTION.h"cxx . . $ `<file>/sys/oo_DESCRIPTION.h`/ |
| 38 | #include "SpellingChecker_def.h"cxx . . $ `<file>/fon/SpellingChecker_def.h`/ |
| 39 | |
| 40 | #include "../kar/longchar.h"cxx . . $ `<file>/kar/longchar.h`/ |
| 41 | |
| 42 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (SpellingChecker, Daata, 1); |
| 43 | |
| 44 | autoSpellingChecker WordList_upto_SpellingCheckercxx . . $ WordList_upto_SpellingChecker(8ee5f44e307da535). (WordListcxx . . $ WordList# me) { |
| 45 | try { |
| 46 | autoSpellingChecker thee = Thing_newcxx . . $ `sys/Thing.h:514:9`! (SpellingChecker); |
| 47 | thycxx . . $ `melder/melder.h:45:9`! wordList =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Data_copycxx . . $ Data_copy(97a542a26794c80e). (me); |
| 48 | thycxx . . $ `melder/melder.h:45:9`! userDictionary =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). StringSet_createcxx . . $ StringSet_create(3c0f0334e2cf88fa). (); |
| 49 | thycxx . . $ `melder/melder.h:45:9`! separatingCharacters =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (U".,;:()\""); |
| 50 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 51 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 52 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to SpellingChecker."); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | autoWordListcxx . . $ autoWordList# SpellingChecker_extractWordListcxx . . $ SpellingChecker_extractWordList(d0cf669130acb3b0). (SpellingChecker me) { |
| 57 | return Data_copycxx . . $ Data_copy(97a542a26794c80e). (mycxx . . $ `melder/melder.h:44:9`! wordList.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 58 | } |
| 59 | |
| 60 | void SpellingChecker_replaceWordListcxx . . $ SpellingChecker_replaceWordList(496f5cb9e90b6e37). (SpellingChecker me, WordListcxx . . $ WordList# list) { |
| 61 | try { |
| 62 | mycxx . . $ `melder/melder.h:44:9`! wordList =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Data_copycxx . . $ Data_copy(97a542a26794c80e). (list); |
| 63 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 64 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": word list not replaced."); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | autoStringSetcxx . . $ autoStringSet# SpellingChecker_extractUserDictionarycxx . . $ SpellingChecker_extractUserDictionary(73eb431fe4a43a1a). (SpellingChecker me) { |
| 69 | try { |
| 70 | if (mycxx . . $ `melder/melder.h:44:9`! userDictionary->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).sizecxx . . $ CollectionOf#size. == 0) |
| 71 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"This spelling checker does not contain a user dictionary."); |
| 72 | return Data_copycxx . . $ Data_copy(97a542a26794c80e). (mycxx . . $ `melder/melder.h:44:9`! userDictionary.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 73 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 74 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": user dictionary not extracted."); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void SpellingChecker_replaceUserDictionarycxx . . $ SpellingChecker_replaceUserDictionary(36a1be529293f187). (SpellingChecker me, StringSetcxx . . $ StringSet# userDictionary) { |
| 79 | try { |
| 80 | mycxx . . $ `melder/melder.h:44:9`! userDictionary =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Data_copycxx . . $ Data_copy(97a542a26794c80e). (userDictionary); |
| 81 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 82 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": user dictionary not replaced."); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static int startsWithCapitalcxx . . $ startsWithCapital(e049ab16a3067c7). (conststring32cxx . . $ conststring32# word) { |
| 87 | return Melder_isUpperCaseLettercxx . . $ Melder_isUpperCaseLetter(b8d0204ce8416907). (word [0]); |
| 88 | } |
| 89 | |
| 90 | bool SpellingChecker_isWordAllowedcxx . . $ SpellingChecker_isWordAllowed(609085fd9e48eb3f). (SpellingChecker me, conststring32cxx . . $ conststring32# word) { |
| 91 | const integercxx . . $ integer# wordLength = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (word); |
| 92 | if (mycxx . . $ `melder/melder.h:44:9`! allowAllWordsContaining && mycxx . . $ `melder/melder.h:44:9`! allowAllWordsContaining [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]) { |
| 93 | char32cxx . . $ char32# *p = & mycxx . . $ `melder/melder.h:44:9`! allowAllWordsContaining [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]; |
| 94 | while (*p) { |
| 95 | /* |
| 96 | Find next token in list of allowed string parts. |
| 97 | */ |
| 98 | char32cxx . . $ char32# token [100], *q = & token [0]; |
| 99 | Melder_skipHorizontalOrVerticalSpacecxx . . $ Melder_skipHorizontalOrVerticalSpace(28edbb7a9cd1ae27). (& p); |
| 100 | /* |
| 101 | Collect one token string from list. |
| 102 | */ |
| 103 | while (*p != U'\0' && *p != U' ') |
| 104 | *q ++ = *p ++; |
| 105 | *q = U'\0'; // trailing null character |
| 106 | /* |
| 107 | Allow word if it contains this token. |
| 108 | */ |
| 109 | if (str32strcxx . . $ str32str(2de17cd8f7c584a6). (word, token)) |
| 110 | return true; |
| 111 | } |
| 112 | } |
| 113 | if (mycxx . . $ `melder/melder.h:44:9`! allowAllNames) { |
| 114 | /* |
| 115 | Allow word if it starts with a capital. |
| 116 | */ |
| 117 | if (startsWithCapitalcxx . . $ startsWithCapital(e049ab16a3067c7). (word)) |
| 118 | return true; |
| 119 | if (mycxx . . $ `melder/melder.h:44:9`! namePrefixes && mycxx . . $ `melder/melder.h:44:9`! namePrefixes [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]) { |
| 120 | char32cxx . . $ char32# *p = & mycxx . . $ `melder/melder.h:44:9`! namePrefixes [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]; |
| 121 | while (*p) { |
| 122 | char32cxx . . $ char32# token [100], *q = & token [0]; |
| 123 | Melder_skipHorizontalOrVerticalSpacecxx . . $ Melder_skipHorizontalOrVerticalSpace(28edbb7a9cd1ae27). (& p); |
| 124 | while (*p != U'\0' && *p != U' ') *q ++ = *p ++; |
| 125 | *q = U'\0'; // trailing null character |
| 126 | /* |
| 127 | Allow word if starts with this prefix |
| 128 | and this prefix is followed by a capital. |
| 129 | */ |
| 130 | if (str32strcxx . . $ str32str(2de17cd8f7c584a6). (word, token) == word && startsWithCapitalcxx . . $ startsWithCapital(e049ab16a3067c7). (word + Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (token))) |
| 131 | return true; |
| 132 | } |
| 133 | } |
| 134 | } else if (mycxx . . $ `melder/melder.h:44:9`! allowAllAbbreviations && startsWithCapitalcxx . . $ startsWithCapital(e049ab16a3067c7). (word)) { |
| 135 | const char32cxx . . $ char32# *p = & word [0]; |
| 136 | for (;;) { |
| 137 | if (*p == U'\0') |
| 138 | return true; |
| 139 | if (Melder_isLowerCaseLettercxx . . $ Melder_isLowerCaseLetter(b8d0204ce8416907). (*p)) |
| 140 | break; |
| 141 | p ++; |
| 142 | } |
| 143 | } |
| 144 | if (mycxx . . $ `melder/melder.h:44:9`! allowAllWordsStartingWith && mycxx . . $ `melder/melder.h:44:9`! allowAllWordsStartingWith [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]) { |
| 145 | const char32cxx . . $ char32# *p = & mycxx . . $ `melder/melder.h:44:9`! allowAllWordsStartingWith [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]; |
| 146 | while (*p) { |
| 147 | char32cxx . . $ char32# token [100], *q = & token [0]; |
| 148 | Melder_skipHorizontalOrVerticalSpacecxx . . $ Melder_skipHorizontalOrVerticalSpace(1babf4dbe7380927). (& p); |
| 149 | while (*p != U'\0' && *p != U' ') |
| 150 | *q ++ = *p ++; |
| 151 | *q = U'\0'; // trailing null character |
| 152 | const integercxx . . $ integer# tokenLength = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (token); |
| 153 | if (wordLength >= tokenLength && str32nequcxx . . $ `melder/melder_str32.h:123:9`! (token, word, tokenLength)) |
| 154 | return true; |
| 155 | } |
| 156 | } |
| 157 | if (mycxx . . $ `melder/melder.h:44:9`! allowAllWordsEndingIn && mycxx . . $ `melder/melder.h:44:9`! allowAllWordsEndingIn [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]) { |
| 158 | const char32cxx . . $ char32# *p = & mycxx . . $ `melder/melder.h:44:9`! allowAllWordsEndingIn [cxx . . $ _autostring#`operator[]`(b190d49faf30c5be).0]; |
| 159 | while (*p) { |
| 160 | char32cxx . . $ char32# token [100], *q = & token [0]; |
| 161 | Melder_skipHorizontalOrVerticalSpacecxx . . $ Melder_skipHorizontalOrVerticalSpace(1babf4dbe7380927). (& p); |
| 162 | while (*p != U'\0' && *p != U' ') |
| 163 | *q ++ = *p ++; |
| 164 | *q = U'\0'; // trailing null character |
| 165 | const integercxx . . $ integer# tokenLength = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (token); |
| 166 | if (wordLength >= tokenLength && str32nequcxx . . $ `melder/melder_str32.h:123:9`! (token, word + wordLength - tokenLength, tokenLength)) |
| 167 | return true; |
| 168 | } |
| 169 | } |
| 170 | if (WordList_hasWordcxx . . $ WordList_hasWord(329affebebd6aea5). (mycxx . . $ `melder/melder.h:44:9`! wordList.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), word)) |
| 171 | return true; |
| 172 | if (mycxx . . $ `melder/melder.h:44:9`! userDictionary->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).sizecxx . . $ CollectionOf#size. > 0) { |
| 173 | if (Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (word) > 3333) |
| 174 | return false; // superfluous, because WordList_hasWord already checked; but safe |
| 175 | static char32cxx . . $ char32# buffer [3*3333+1]; |
| 176 | Longchar_nativizecxx . . $ Longchar_nativize(35d477e2649d0789). (word, buffer, false); |
| 177 | if (mycxx . . $ `melder/melder.h:44:9`! userDictionary ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). lookUpcxx . . $ SortedSetOfStringOf#lookUp(c94c67ff4219bf83). (buffer) != 0) |
| 178 | return true; |
| 179 | } |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | void SpellingChecker_addNewWordcxx . . $ SpellingChecker_addNewWord(f26eb0e12ac33ea9). (SpellingChecker me, conststring32cxx . . $ conststring32# word) { |
| 184 | try { |
| 185 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (word && word [0] != U'\0', |
| 186 | U"Cannot add empty word."); |
| 187 | mycxx . . $ `melder/melder.h:44:9`! userDictionary ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). addString_copycxx . . $ structStringSet#addString_copy(ff00d524ab0355ec). (word); |
| 188 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 189 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": word \"", word, U"\" not added."); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | char32cxx . . $ char32# * SpellingChecker_nextNotAllowedWordcxx . . $ SpellingChecker_nextNotAllowedWord(556484ab832eca72). (SpellingChecker me, conststring32cxx . . $ conststring32# sentence, integercxx . . $ integer# *start) { |
| 194 | const char32cxx . . $ char32# *p = & sentence [*start]; |
| 195 | for (;;) { |
| 196 | if (*p == U'\0') { |
| 197 | return nullptr; // all words allowed |
| 198 | } else if (*p == U'(' && mycxx . . $ `melder/melder.h:44:9`! allowAllParenthesized) { |
| 199 | p ++; |
| 200 | for (;;) { |
| 201 | if (*p == U'\0') { |
| 202 | return nullptr; // everything is parenthesized... |
| 203 | } else if (*p == U')') { |
| 204 | p ++; |
| 205 | break; |
| 206 | } else { |
| 207 | p ++; |
| 208 | } |
| 209 | } |
| 210 | } else if (*p == U' ' || (mycxx . . $ `melder/melder.h:44:9`! separatingCharacters && str32chrcxx . . $ str32chr(328a9ede0fba853a). (mycxx . . $ `melder/melder.h:44:9`! separatingCharacters.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), *p))) { |
| 211 | p ++; |
| 212 | } else { |
| 213 | static char32cxx . . $ char32# word [100]; |
| 214 | char32cxx . . $ char32# *q = & word [0]; |
| 215 | *start = p - sentence; |
| 216 | for (;;) { |
| 217 | if (*p == U'\0' || *p == U' ' || (mycxx . . $ `melder/melder.h:44:9`! separatingCharacters && str32chrcxx . . $ str32chr(328a9ede0fba853a). (mycxx . . $ `melder/melder.h:44:9`! separatingCharacters.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), *p))) { |
| 218 | *q ++ = U'\0'; |
| 219 | if (SpellingChecker_isWordAllowedcxx . . $ SpellingChecker_isWordAllowed(609085fd9e48eb3f). (me, word)) { |
| 220 | /* Don't increment p (may contain a zero or a parenthesis). */ |
| 221 | break; |
| 222 | } else { |
| 223 | return word; |
| 224 | } |
| 225 | } else { |
| 226 | *q ++ = *p ++; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | return nullptr; // all words allowed |
| 232 | } |
| 233 | |
| 234 | /* End of file SpellingChecker.cpp */ |