Debian Codegraph @

fon/SpellingChecker.cpp

raw
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"
20
21 #include "oo_DESTROY.h"
22 #include "SpellingChecker_def.h"
23 #include "oo_COPY.h"
24 #include "SpellingChecker_def.h"
25 #include "oo_EQUAL.h"
26 #include "SpellingChecker_def.h"
27 #include "oo_CAN_WRITE_AS_ENCODING.h"
28 #include "SpellingChecker_def.h"
29 #include "oo_WRITE_TEXT.h"
30 #include "SpellingChecker_def.h"
31 #include "oo_WRITE_BINARY.h"
32 #include "SpellingChecker_def.h"
33 #include "oo_READ_TEXT.h"
34 #include "SpellingChecker_def.h"
35 #include "oo_READ_BINARY.h"
36 #include "SpellingChecker_def.h"
37 #include "oo_DESCRIPTION.h"
38 #include "SpellingChecker_def.h"
39
40 #include "../kar/longchar.h"
41
42 Thing_implement (SpellingChecker, Daata, 1);
43
44 autoSpellingChecker WordList_upto_SpellingChecker (WordList me) {
45 try {
46 autoSpellingChecker thee = Thing_new (SpellingChecker);
47 thy wordList = Data_copy (me);
48 thy userDictionary = StringSet_create ();
49 thy separatingCharacters = Melder_dup (U".,;:()\"");
50 return thee;
51 } catch (MelderError) {
52 Melder_throw (me, U": not converted to SpellingChecker.");
53 }
54 }
55
56 autoWordList SpellingChecker_extractWordList (SpellingChecker me) {
57 return Data_copy (my wordList.get());
58 }
59
60 void SpellingChecker_replaceWordList (SpellingChecker me, WordList list) {
61 try {
62 my wordList = Data_copy (list);
63 } catch (MelderError) {
64 Melder_throw (me, U": word list not replaced.");
65 }
66 }
67
68 autoStringSet SpellingChecker_extractUserDictionary (SpellingChecker me) {
69 try {
70 if (my userDictionary->size == 0)
71 Melder_throw (U"This spelling checker does not contain a user dictionary.");
72 return Data_copy (my userDictionary.get());
73 } catch (MelderError) {
74 Melder_throw (me, U": user dictionary not extracted.");
75 }
76 }
77
78 void SpellingChecker_replaceUserDictionary (SpellingChecker me, StringSet userDictionary) {
79 try {
80 my userDictionary = Data_copy (userDictionary);
81 } catch (MelderError) {
82 Melder_throw (me, U": user dictionary not replaced.");
83 }
84 }
85
86 static int startsWithCapital (conststring32 word) {
87 return Melder_isUpperCaseLetter (word [0]);
88 }
89
90 bool SpellingChecker_isWordAllowed (SpellingChecker me, conststring32 word) {
91 const integer wordLength = Melder_length (word);
92 if (my allowAllWordsContaining && my allowAllWordsContaining [0]) {
93 char32 *p = & my allowAllWordsContaining [0];
94 while (*p) {
95 /*
96 Find next token in list of allowed string parts.
97 */
98 char32 token [100], *q = & token [0];
99 Melder_skipHorizontalOrVerticalSpace (& 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 (str32str (word, token))
110 return true;
111 }
112 }
113 if (my allowAllNames) {
114 /*
115 Allow word if it starts with a capital.
116 */
117 if (startsWithCapital (word))
118 return true;
119 if (my namePrefixes && my namePrefixes [0]) {
120 char32 *p = & my namePrefixes [0];
121 while (*p) {
122 char32 token [100], *q = & token [0];
123 Melder_skipHorizontalOrVerticalSpace (& 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 (str32str (word, token) == word && startsWithCapital (word + Melder_length (token)))
131 return true;
132 }
133 }
134 } else if (my allowAllAbbreviations && startsWithCapital (word)) {
135 const char32 *p = & word [0];
136 for (;;) {
137 if (*p == U'\0')
138 return true;
139 if (Melder_isLowerCaseLetter (*p))
140 break;
141 p ++;
142 }
143 }
144 if (my allowAllWordsStartingWith && my allowAllWordsStartingWith [0]) {
145 const char32 *p = & my allowAllWordsStartingWith [0];
146 while (*p) {
147 char32 token [100], *q = & token [0];
148 Melder_skipHorizontalOrVerticalSpace (& p);
149 while (*p != U'\0' && *p != U' ')
150 *q ++ = *p ++;
151 *q = U'\0'; // trailing null character
152 const integer tokenLength = Melder_length (token);
153 if (wordLength >= tokenLength && str32nequ (token, word, tokenLength))
154 return true;
155 }
156 }
157 if (my allowAllWordsEndingIn && my allowAllWordsEndingIn [0]) {
158 const char32 *p = & my allowAllWordsEndingIn [0];
159 while (*p) {
160 char32 token [100], *q = & token [0];
161 Melder_skipHorizontalOrVerticalSpace (& p);
162 while (*p != U'\0' && *p != U' ')
163 *q ++ = *p ++;
164 *q = U'\0'; // trailing null character
165 const integer tokenLength = Melder_length (token);
166 if (wordLength >= tokenLength && str32nequ (token, word + wordLength - tokenLength, tokenLength))
167 return true;
168 }
169 }
170 if (WordList_hasWord (my wordList.get(), word))
171 return true;
172 if (my userDictionary->size > 0) {
173 if (Melder_length (word) > 3333)
174 return false; // superfluous, because WordList_hasWord already checked; but safe
175 static char32 buffer [3*3333+1];
176 Longchar_nativize (word, buffer, false);
177 if (my userDictionary -> lookUp (buffer) != 0)
178 return true;
179 }
180 return false;
181 }
182
183 void SpellingChecker_addNewWord (SpellingChecker me, conststring32 word) {
184 try {
185 Melder_require (word && word [0] != U'\0',
186 U"Cannot add empty word.");
187 my userDictionary -> addString_copy (word);
188 } catch (MelderError) {
189 Melder_throw (me, U": word \"", word, U"\" not added.");
190 }
191 }
192
193 char32 * SpellingChecker_nextNotAllowedWord (SpellingChecker me, conststring32 sentence, integer *start) {
194 const char32 *p = & sentence [*start];
195 for (;;) {
196 if (*p == U'\0') {
197 return nullptr; // all words allowed
198 } else if (*p == U'(' && my 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' ' || (my separatingCharacters && str32chr (my separatingCharacters.get(), *p))) {
211 p ++;
212 } else {
213 static char32 word [100];
214 char32 *q = & word [0];
215 *start = p - sentence;
216 for (;;) {
217 if (*p == U'\0' || *p == U' ' || (my separatingCharacters && str32chr (my separatingCharacters.get(), *p))) {
218 *q ++ = U'\0';
219 if (SpellingChecker_isWordAllowed (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 */