• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdespell2
 

tdespell2

  • tdespell2
  • plugins
  • ispell
good.cpp
1/* enchant
2 * Copyright (C) 2003 Dom Lachowicz
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 * In addition, as a special exception, Dom Lachowicz
20 * gives permission to link the code of this program with
21 * non-LGPL Spelling Provider libraries (eg: a MSFT Office
22 * spell checker backend) and distribute linked combinations including
23 * the two. You must obey the GNU Lesser General Public License in all
24 * respects for all of the code used other than said providers. If you modify
25 * this file, you may extend this exception to your version of the
26 * file, but you are not obligated to do so. If you do not wish to
27 * do so, delete this exception statement from your version.
28 */
29
30/*
31 * good.c - see if a word or its root word
32 * is in the dictionary.
33 *
34 * Pace Willisson, 1983
35 *
36 * Copyright 1992, 1993, Geoff Kuenning, Granada Hills, CA
37 * All rights reserved.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 *
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All modifications to the source code must be clearly marked as
49 * such. Binary redistributions based on modified source code
50 * must be clearly marked as modified versions in the documentation
51 * and/or other materials provided with the distribution.
52 * 4. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgment:
54 * This product includes software developed by Geoff Kuenning and
55 * other unpaid contributors.
56 * 5. The name of Geoff Kuenning may not be used to endorse or promote
57 * products derived from this software without specific prior
58 * written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY GEOFF KUENNING AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL GEOFF KUENNING OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 */
72
73/*
74 * $Log$
75 * Revision 1.1 2004/01/31 16:44:12 zrusin
76 * ISpell plugin.
77 *
78 * Revision 1.4 2003/08/14 17:51:26 dom
79 * update license - exception clause should be Lesser GPL
80 *
81 * Revision 1.3 2003/07/28 20:40:25 dom
82 * fix up the license clause, further win32-registry proof some directory getting functions
83 *
84 * Revision 1.2 2003/07/16 22:52:37 dom
85 * LGPL + exception license
86 *
87 * Revision 1.1 2003/07/15 01:15:04 dom
88 * ispell enchant backend
89 *
90 * Revision 1.2 2003/01/29 05:50:11 hippietrail
91 *
92 * Fixed my mess in EncodingManager.
93 * Changed many C casts to C++ casts.
94 *
95 * Revision 1.1 2003/01/24 05:52:32 hippietrail
96 *
97 * Refactored ispell code. Old ispell global variables had been put into
98 * an allocated structure, a pointer to which was passed to many functions.
99 * I have now made all such functions and variables private members of the
100 * ISpellChecker class. It was C OO, now it's C++ OO.
101 *
102 * I've fixed the makefiles and tested compilation but am unable to test
103 * operation. Please back out my changes if they cause problems which
104 * are not obvious or easy to fix.
105 *
106 * Revision 1.6 2003/01/06 18:48:38 dom
107 * ispell cleanup, start of using new 'add' save features
108 *
109 * Revision 1.5 2002/09/19 05:31:15 hippietrail
110 *
111 * More Ispell cleanup. Conditional globals and DEREF macros are removed.
112 * K&R function declarations removed, converted to Doxygen style comments
113 * where possible. No code has been changed (I hope). Compiles for me but
114 * unable to test.
115 *
116 * Revision 1.4 2002/09/17 03:03:29 hippietrail
117 *
118 * After seeking permission on the developer list I've reformatted all the
119 * spelling source which seemed to have parts which used 2, 3, 4, and 8
120 * spaces for tabs. It should all look good with our standard 4-space
121 * tabs now.
122 * I've concentrated just on indentation in the actual code. More prettying
123 * could be done.
124 * * NO code changes were made *
125 *
126 * Revision 1.3 2002/09/13 17:20:12 mpritchett
127 * Fix more warnings for Linux build
128 *
129 * Revision 1.2 2001/05/12 16:05:42 thomasf
130 * Big pseudo changes to ispell to make it pass around a structure rather
131 * than rely on all sorts of gloabals willy nilly here and there. Also
132 * fixed our spelling class to work with accepting suggestions once more.
133 * This code is dirty, gross and ugly (not to mention still not supporting
134 * multiple hash sized just yet) but it works on my machine and will no
135 * doubt break other machines.
136 *
137 * Revision 1.1 2001/04/15 16:01:24 tomas_f
138 * moving to spell/xp
139 *
140 * Revision 1.5 2000/02/09 22:35:25 sterwill
141 * Clean up some warnings
142 *
143 * Revision 1.4 1998/12/29 14:55:32 eric
144 *
145 * I've doctored the ispell code pretty extensively here. It is now
146 * warning-free on Win32. It also *works* on Win32 now, since I
147 * replaced all the I/O calls with ANSI standard ones.
148 *
149 * Revision 1.3 1998/12/28 23:11:30 eric
150 *
151 * modified spell code and integration to build on Windows.
152 * This is still a hack.
153 *
154 * Actually, it doesn't yet WORK on Windows. It just builds.
155 * SpellCheckInit is failing for some reason.
156 *
157 * Revision 1.2 1998/12/28 22:16:22 eric
158 *
159 * These changes begin to incorporate the spell checker into AbiWord. Most
160 * of this is a hack.
161 *
162 * 1. added other/spell to the -I list in config/abi_defs
163 * 2. replaced other/spell/Makefile with one which is more like
164 * our build system.
165 * 3. added other/spell to other/Makefile so that the build will now
166 * dive down and build the spell check library.
167 * 4. added the AbiSpell library to the Makefiles in wp/main
168 * 5. added a call to SpellCheckInit in wp/main/unix/UnixMain.cpp.
169 * This call is a HACK and should be replaced with something
170 * proper later.
171 * 6. added code to fv_View.cpp as follows:
172 * whenever you double-click on a word, the spell checker
173 * verifies that word and prints its status to stdout.
174 *
175 * Caveats:
176 * 1. This will break the Windows build. I'm going to work on fixing it
177 * now.
178 * 2. This only works if your dictionary is in /usr/lib/ispell/american.hash.
179 * The dictionary location is currently hard-coded. This will be
180 * fixed as well.
181 *
182 * Anyway, such as it is, it works.
183 *
184 * Revision 1.1 1998/12/28 18:04:43 davet
185 * Spell checker code stripped from ispell. At this point, there are
186 * two external routines... the Init routine, and a check-a-word routine
187 * which returns a boolean value, and takes a 16 bit char string.
188 * The code resembles the ispell code as much as possible still.
189 *
190 * Revision 1.43 1994/11/02 06:56:05 geoff
191 * Remove the anyword feature, which I've decided is a bad idea.
192 *
193 * Revision 1.42 1994/10/25 05:45:59 geoff
194 * Add support for an affix that will work with any word, even if there's
195 * no explicit flag.
196 *
197 * Revision 1.41 1994/05/24 06:23:06 geoff
198 * Let tgood decide capitalization questions, rather than doing it ourselves.
199 *
200 * Revision 1.40 1994/05/17 06:44:10 geoff
201 * Add support for controlled compound formation and the COMPOUNDONLY
202 * option to affix flags.
203 *
204 * Revision 1.39 1994/01/25 07:11:31 geoff
205 * Get rid of all old RCS log lines in preparation for the 3.1 release.
206 *
207 */
208
209#include <ctype.h>
210#include <stdio.h>
211#include <stdlib.h>
212#include <string.h>
213
214#include "ispell_checker.h"
215
216
217int good P ((ichar_t * word, int ignoreflagbits, int allhits,
218 int pfxopts, int sfxopts));
219
220#ifndef NO_CAPITALIZATION_SUPPORT
221
231static int entryhasaffixes (struct dent *dent, struct success *hit)
232{
233 if (hit->prefix && !TSTMASKBIT (dent->mask, hit->prefix->flagbit))
234 return 0;
235 if (hit->suffix && !TSTMASKBIT (dent->mask, hit->suffix->flagbit))
236 return 0;
237 return 1; /* Yes, these affixes are legal */
238}
239
240/*
241 * \param word
242 * \param hit
243 * \param len
244 *
245 * \return
246 */
247int ISpellChecker::cap_ok (ichar_t *word, struct success *hit, int len)
248{
249 ichar_t * dword;
250 ichar_t * w;
251 struct dent * dent;
252 ichar_t dentword[INPUTWORDLEN + MAXAFFIXLEN];
253 int preadd;
254 int prestrip;
255 int sufadd;
256 ichar_t * limit;
257 long thiscap;
258 long dentcap;
259
260 thiscap = whatcap (word);
261 /*
262 ** All caps is always legal, regardless of affixes.
263 */
264 preadd = prestrip = sufadd = 0;
265 if (thiscap == ALLCAPS)
266 return 1;
267 else if (thiscap == FOLLOWCASE)
268 {
269 /* Set up some constants for the while(1) loop below */
270 if (hit->prefix)
271 {
272 preadd = hit->prefix->affl;
273 prestrip = hit->prefix->stripl;
274 }
275 else
276 preadd = prestrip = 0;
277 sufadd = hit->suffix ? hit->suffix->affl : 0;
278 }
279 /*
280 ** Search the variants for one that matches what we have. Note
281 ** that thiscap can't be ALLCAPS, since we already returned
282 ** for that case.
283 */
284 dent = hit->dictent;
285 for ( ; ; )
286 {
287 dentcap = captype (dent->flagfield);
288 if (dentcap != thiscap)
289 {
290 if (dentcap == ANYCASE && thiscap == CAPITALIZED
291 && entryhasaffixes (dent, hit))
292 return 1;
293 }
294 else /* captypes match */
295 {
296 if (thiscap != FOLLOWCASE)
297 {
298 if (entryhasaffixes (dent, hit))
299 return 1;
300 }
301 else
302 {
303 /*
304 ** Make sure followcase matches exactly.
305 ** Life is made more difficult by the
306 ** possibility of affixes. Start with
307 ** the prefix.
308 */
309 strtoichar (dentword, dent->word, INPUTWORDLEN, 1);
310 dword = dentword;
311 limit = word + preadd;
312 if (myupper (dword[prestrip]))
313 {
314 for (w = word; w < limit; w++)
315 {
316 if (mylower (*w))
317 goto doublecontinue;
318 }
319 }
320 else
321 {
322 for (w = word; w < limit; w++)
323 {
324 if (myupper (*w))
325 goto doublecontinue;
326 }
327 }
328 dword += prestrip;
329 /* Do root part of word */
330 limit = dword + len - preadd - sufadd;
331 while (dword < limit)
332 {
333 if (*dword++ != *w++)
334 goto doublecontinue;
335 }
336 /* Do suffix */
337 dword = limit - 1;
338 if (myupper (*dword))
339 {
340 for ( ; *w; w++)
341 {
342 if (mylower (*w))
343 goto doublecontinue;
344 }
345 }
346 else
347 {
348 for ( ; *w; w++)
349 {
350 if (myupper (*w))
351 goto doublecontinue;
352 }
353 }
354 /*
355 ** All failure paths go to "doublecontinue,"
356 ** so if we get here it must match.
357 */
358 if (entryhasaffixes (dent, hit))
359 return 1;
360 doublecontinue: ;
361 }
362 }
363 if ((dent->flagfield & MOREVARIANTS) == 0)
364 break;
365 dent = dent->next;
366 }
367
368 /* No matches found */
369 return 0;
370}
371#endif
372
373#ifndef NO_CAPITALIZATION_SUPPORT
383int ISpellChecker::good (ichar_t *w, int ignoreflagbits, int allhits, int pfxopts, int sfxopts)
384#else
385/* ARGSUSED */
386int ISpellChecker::good (ichar_t *w, int ignoreflagbits, int dummy, int pfxopts, int sfxopts)
387#endif
388{
389 ichar_t nword[INPUTWORDLEN + MAXAFFIXLEN];
390 ichar_t * p;
391 ichar_t * q;
392 int n;
393 struct dent * dp;
394
395 /*
396 ** Make an uppercase copy of the word we are checking.
397 */
398 for (p = w, q = nword; *p; )
399 *q++ = mytoupper (*p++);
400 *q = 0;
401 n = q - nword;
402
403 m_numhits = 0;
404
405 if ((dp = ispell_lookup (nword, 1)) != NULL)
406 {
407 m_hits[0].dictent = dp;
408 m_hits[0].prefix = NULL;
409 m_hits[0].suffix = NULL;
410#ifndef NO_CAPITALIZATION_SUPPORT
411 if (allhits || cap_ok (w, &m_hits[0], n))
412 m_numhits = 1;
413#else
414 m_numhits = 1;
415#endif
416 }
417
418 if (m_numhits && !allhits)
419 return 1;
420
421 /* try stripping off affixes */
422
423 chk_aff (w, nword, n, ignoreflagbits, allhits, pfxopts, sfxopts);
424
425 return m_numhits;
426}
427
428
429
430

tdespell2

Skip menu "tdespell2"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

tdespell2

Skip menu "tdespell2"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdespell2 by doxygen 1.9.4
This website is maintained by Timothy Pearson.