Debian Codegraph @

fon/WordList.cpp

raw symbols SCIP
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"
20 #include "../kar/longchar.h"
21 #include "Collection.h"
22
23 #include "oo_DESTROY.h"
24 #include "WordList_def.h"
25 #include "oo_COPY.h"
26 #include "WordList_def.h"
27 #include "oo_EQUAL.h"
28 #include "WordList_def.h"
29 #include "oo_CAN_WRITE_AS_ENCODING.h"
30 #include "WordList_def.h"
31 #include "oo_WRITE_TEXT.h"
32 #include "WordList_def.h"
33 #include "oo_READ_TEXT.h"
34 #include "WordList_def.h"
35 #include "oo_WRITE_BINARY.h"
36 #include "WordList_def.h"
37 #include "oo_DESCRIPTION.h"
38 #include "WordList_def.h"
39
40 Thing_implement (WordList, Daata, 1);
41
42 static integer WordList_count (WordList me) {
43 integer numberOfWords = 0;
44 for (char32 *p = & my string [0]; *p; p ++) {
45 if (*p == U'\n')
46 numberOfWords += 1;
47 }
48 return numberOfWords;
49 }
50
51 void structWordList :: v1_info () {
52 structDaata :: v1_info ();
53 const integer n = WordList_count (this);
54 if (our length == 0)
55 our length = Melder_length (our string.get());
56 MelderInfo_writeLine (U"Number of words: ", n);
57 MelderInfo_writeLine (U"Number of characters: ", length - n);
58 }
59
60 void structWordList :: v1_readBinary (FILE *f, int formatVersion) {
61 if (formatVersion <= 0) {
62 our length = bingeti32 (f);
63 if (our length < 0)
64 Melder_throw (U"Wrong length ", our length, U".");
65 our string = autostring32 (our length);
66 char32 *current = & our string [0], *p = current;
67 int kar = 0;
68 if (our length > 0) {
69 /*
70 * Read first word.
71 */
72 for (;;) {
73 if (p - & string [0] >= length - 1) break;
74 kar = fgetc (f);
75 if (kar == EOF)
76 Melder_throw (U"Early end of file.");
77 if (kar >= 128)
78 break;
79 *p ++ = (char32) kar;
80 }
81 *p ++ = U'\n';
82 /*
83 * Read following words.
84 */
85 for (;;) {
86 char32 *previous = current;
87 int numberOfSame = kar - 128;
88 current = p;
89 str32ncpy (current, previous, numberOfSame);
90 p += numberOfSame;
91 for (;;) {
92 if (p - & string [0] >= length - 1) break;
93 kar = fgetc (f);
94 if (kar == EOF)
95 Melder_throw (U"Early end of file.");
96 if (kar >= 128)
97 break;
98 *p ++ = (char32) kar;
99 }
100 *p ++ = U'\n';
101 if (p - & string [0] >= our length)
102 break;
103 }
104 }
105 *p = U'\0';
106 if (p - & our string [0] != our length)
107 Melder_throw (U"Length in header (", our length, U") does not match length of string (", (integer) (p - & our string [0]), U").");
108 } else {
109 our string = bingetw32 (f);
110 our length = Melder_length (our string.get());
111 }
112 }
113
114 static autoStringSet Strings_to_StringSet (Strings me, bool nativize) {
115 /*
116 Make sure that the strings are normalized, sorted, and unique.
117 */
118 autoStringSet you = StringSet_create ();
119 for (integer i = 1; i <= my numberOfStrings; i ++) {
120 autoSimpleString item = SimpleString_create (my strings [i].get()); // TODO: normalize
121 your addItem_unsorted_move (item.move());
122 }
123 your sort ();
124 your unicize ();
125 return you;
126 }
127
128 autoWordList Strings_to_WordList (Strings me) {
129 try {
130 /*
131 Check whether the strings are all words.
132 */
133 for (integer i = 1; i <= my numberOfStrings; i ++)
134 if (Melder_findHorizontalOrVerticalSpace (my strings [i].get()))
135 Melder_throw (U"String ", i, U" contains a space and can therefore not be included in a WordList.");
136
137 autoStringSet you = Strings_to_StringSet (me, true);
138
139 integer totalLength = 0;
140 for (integer i = 1; i <= your size; i ++)
141 totalLength += Melder_length (your at [i]->string.get()) + 1; // include trailing newline symbol
142
143 autoWordList him = Thing_new (WordList);
144 his length = totalLength;
145 his string = autostring32 (his length);
146 /*
147 Concatenate the strings into the word list.
148 */
149 char32 *q = & his string [0];
150 for (integer i = 1; i <= your size; i ++) {
151 str32cpy (q, your at [i]->string.get());
152 q += Melder_length (your at [i]->string.get());
153 *q ++ = U'\n';
154 }
155 *q = U'\0';
156 Melder_assert (q - & his string [0] == his length);
157 return him;
158 } catch (MelderError) {
159 Melder_throw (me, U": not converted to WordList.");
160 }
161 }
162
163 autoStrings WordList_to_Strings (WordList me) {
164 try {
165 const char32 *word = & my string [0];
166 autoStrings thee = Thing_new (Strings);
167 thy numberOfStrings = WordList_count (me);
168 if (thy numberOfStrings > 0)
169 thy strings = autoSTRVEC (thy numberOfStrings);
170 for (integer i = 1; i <= thy numberOfStrings; i ++) {
171 const char32 *kar = word;
172 for (; *kar != U'\n'; kar ++) { }
173 integer length = kar - word;
174 thy strings [i] = autostring32 (length);
175 str32ncpy (thy strings [i].get(), word, length);
176 word += length + 1;
177 }
178 return thee;
179 } catch (MelderError) {
180 Melder_throw (me, U": not converted to Strings.");
181 }
182 }
183
184 static integer gotoStart (WordList me, integer p) {
185 if (p <= 0) return 0;
186 -- p;
187 while (p >= 0 && my string [p] != U'\n') p --;
188 return p + 1;
189 }
190
191 static integer gotoNext (WordList me, integer p) {
192 if (p >= my length - 1) return my length;
193 while (my string [p] != U'\n') p ++;
194 return p + 1;
195 }
196
197 static integer gotoPrevious (WordList me, integer p) {
198 if (p <= 0) return -1;
199 if (my string [-- 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 && my string [p] != U'\n') p --;
203 return p + 1;
204 }
205
206 static int compare (conststring32 word, 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 char32 buffer [3333+1];
222
223 bool WordList_hasWord (WordList me, conststring32 word) {
224 if (Melder_length (word) > 3333)
225 return false;
226 Longchar_nativize (word, buffer, false);
227 if (! my length)
228 my length = Melder_length (my string.get());
229 integer p = my length / 2, d = p / 2;
230 while (d > 20) {
231 p = gotoStart (me, p);
232 int cf = compare (buffer, my string.get() + p);
233 if (cf == 0) return true;
234 if (cf < 0) p -= d; else p += d;
235 d /= 2;
236 }
237 p = gotoStart (me, p);
238 int cf = compare (buffer, my string.get() + p);
239 if (cf == 0) return true;
240 if (cf > 0) {
241 for (;;) {
242 p = gotoNext (me, p);
243 if (p >= my length) return false;
244 cf = compare (buffer, my string.get() + p);
245 if (cf == 0) return true;
246 if (cf < 0) return false;
247 }
248 } else {
249 for (;;) {
250 p = gotoPrevious (me, p);
251 if (p < 0) return false;
252 cf = compare (buffer, my string.get() + 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 */