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

kspell2

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

kspell2

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

kspell2

Skip menu "kspell2"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kspell2 by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |