source: trunk/athena/bin/ispell/good.c @ 362

Revision 362, 10.0 KB checked in by ambar, 37 years ago (diff)
Initial revision
Line 
1/*
2 * good.c - see if a word or its root word
3 * is in the dictionary.
4 *
5 * Pace Willisson, 1983
6 */
7
8#include <stdio.h>
9#include <ctype.h>
10#include "ispell.h"
11#include "config.h"
12
13struct dent *lookup();
14
15static int wordok;
16
17extern int cflag;
18
19good (w)
20register char *w;
21{
22        char nword[100];
23        register char *p, *q;
24        register n;
25
26        for (p = w, q = nword; *p; p++, q++) {
27                if (mylower (*p))
28                        *q = toupper (*p);
29                else
30                        *q = *p;
31        }
32        *q = 0;
33
34        rootword[0] = 0;
35
36        if (cflag)
37                printf ("%s\n", nword);
38        else if (lookup (nword, q - nword, 1) != NULL) {
39                return (1);
40        }
41
42        /* try stripping off suffixes */
43
44        n = strlen (w);
45        if (n == 1)
46                return (1);
47
48        if (n < 4)
49                return 0;
50
51        wordok = 0;
52
53        /* this part from 'check.mid' */
54        switch (q[-1]) {
55        case 'D': d_ending (nword); break;      /* FOR "CREATED", "IMPLIED", "CROSSED" */
56        case 'T': t_ending (nword); break;      /* FOR "LATEST", "DIRTIEST", "BOLDEST" */
57        case 'R': r_ending (nword); break;      /* FOR "LATER", "DIRTIER", "BOLDER" */
58        case 'G': g_ending (nword); break;      /* FOR "CREATING", "FIXING" */
59        case 'H': h_ending (nword); break;      /* FOR "HUNDREDTH", "TWENTIETH" */
60        case 'S': s_ending (nword); break;      /* FOR ALL SORTS OF THINGS ENDING IN "S" */
61        case 'N': n_ending (nword); break;      /* "TIGHTEN", "CREATION", "MULIPLICATION" */
62        case 'E': e_ending (nword); break;      /* FOR "CREATIVE", "PREVENTIVE" */
63        case 'Y': y_ending (nword); break;      /* FOR "QUICKLY" */
64        default:
65                break;
66        }
67       
68        if (wordok) {
69                (void) strcpy (rootword, lastdent->word);
70        }
71        return (wordok);
72
73}
74
75
76g_ending (w)
77char *w;
78{
79        char *p;
80        struct dent *dent;
81
82        p = w + strlen (w) - 3; /* if the word ends in 'ing', then *p == 'i' */
83       
84        if (strcmp (p, "ING") != 0)
85                return;
86
87        *p = 'E';       /* change I to E, like in CREATING */
88        *(p+1) = 0;
89
90        if (strlen (w) < 2)
91                return;
92
93        if (cflag)
94                printf ("%s/G\n", w);
95        else if ((dent = lookup (w, strlen (w), 1)) != NULL
96          &&  dent->g_flag) {
97                wordok = 1;
98                return;
99        }
100
101
102        *p = 0;
103
104        if (strlen (w) < 2)
105                return;
106
107        if (p[-1] == 'E')
108                return; /* this stops CREATEING */
109
110        if (cflag)
111                printf ("%s/G\n", w);
112        else if ((dent = lookup (w, strlen (w), 1)) != NULL) {
113                if (dent->g_flag)
114                        wordok = 1;
115                return;
116        }
117        return;
118}
119
120d_ending (w)
121char *w;
122{
123        char *p;
124        struct dent *dent;
125
126        p = w + strlen (w) - 2;
127
128        if (strcmp (p, "ED") != 0)
129                return;
130
131        p[1] = 0;       /* kill 'D' */
132
133        if (cflag)
134                printf ("%s/D\n", w);
135        else if ((dent = lookup (w, strlen (w), 1)) != NULL) { /* eg CREATED */
136                if (dent->d_flag)
137                        wordok = 1;
138                return;
139        }
140
141        if (strlen (w) < 3)
142                return;
143
144        p[0] = 0;
145        p--;
146
147        /* ED is now completely gone */
148
149        if (p[0] == 'I' && !vowel (p[-1])) {
150                p[0] = 'Y';
151                if (cflag)
152                        printf ("%s/D\n", w);
153                else if ((dent = lookup (w, strlen (w), 1)) != NULL
154                    &&  dent->d_flag) {
155                        wordok = 1;
156                        return;
157                }
158                p[0] = 'I';
159        }
160
161        if ((p[0] != 'E' && p[0] != 'Y') ||
162            (p[0] == 'Y' && vowel (p[-1]))) {
163                if (cflag)
164                        printf ("%s/D\n", w);
165                else if ((dent = lookup (w, strlen (w), 1)) != NULL) {
166                        if (dent->d_flag)
167                                wordok = 1;
168                        return;
169                }
170        }
171}
172
173t_ending (w)
174char *w;
175{
176
177        char *p;
178        struct dent *dent;
179
180        p = w + strlen (w) - 3;
181
182        if (strcmp (p, "EST") != 0)
183                return;
184
185        p[1] = 0;       /* kill 'S' */
186
187        if (cflag)
188                printf ("%s/T\n", w);
189        else if ((dent = lookup (w, strlen (w), 1)) != NULL
190            &&  dent->t_flag) {
191                wordok = 1;
192                return;
193        }
194
195        if (strlen (w) < 3)
196                return;
197
198        p[0] = 0;
199        p--;
200
201        /* EST is now completely gone */
202
203        if (p[0] == 'I' && !vowel (p[-1])) {
204                p[0] = 'Y';
205                if (cflag)
206                        printf ("%s/T\n", w);
207                else if ((dent = lookup (w, strlen (w), 1)) != NULL
208                    &&  dent->t_flag) {
209                        wordok = 1;
210                        return;
211                }
212                p[0] = 'I';
213        }
214
215        if ((p[0] != 'E' && p[0] != 'Y') ||
216            (p[0] == 'Y' && vowel (p[-1]))) {
217                if (cflag)
218                        printf ("%s/T\n", w);
219                else if ((dent = lookup (w, strlen (w), 1)) != NULL) {
220                        if (dent->t_flag)
221                                wordok = 1;
222                        return;
223                }
224        }
225
226}
227
228
229r_ending (w)
230char *w;
231{
232        char *p;
233        struct dent *dent;
234
235        p = w + strlen (w) - 2;
236
237        if (strcmp (p, "ER") != 0)
238                return;
239
240        p[1] = 0;       /* kill 'R' */
241
242        if (cflag)
243                printf ("%s/R\n", w);
244        else if ((dent = lookup (w, strlen (w), 1)) != NULL
245            &&  dent->r_flag) {
246                wordok = 1;
247                return;
248        }
249
250        if (strlen (w) < 3)
251                return;
252
253        p[0] = 0;
254        p--;
255
256        /* ER is now completely gone */
257
258        if (p[0] == 'I' && !vowel (p[-1])) {
259                p[0] = 'Y';
260                if (cflag)
261                        printf ("%s/R\n", w);
262                else if ((dent = lookup (w, strlen (w), 1)) != NULL
263                    &&  dent->r_flag) {
264                        wordok = 1;
265                        return;
266                }
267                p[0] = 'I';
268        }
269
270        if ((p[0] != 'E' && p[0] != 'Y') ||
271            (p[0] == 'Y' && vowel (p[-1]))) {
272                if (cflag)
273                        printf ("%s/R\n", w);
274                else if ((dent = lookup (w, strlen (w), 1)) != NULL) {
275                        if (dent->r_flag)
276                                wordok = 1;
277                        return;
278                }
279        }
280
281}
282
283h_ending (w)
284char *w;
285{
286        char *p;
287        struct dent *dent;
288
289        p = w + strlen (w) - 2;
290
291        if (strcmp (p, "TH") != 0)
292                return;
293
294        *p = 0;
295
296        p -= 2;
297
298        if (p[1] != 'Y') {
299                if (cflag)
300                        printf ("%s/H\n", w);
301                else if ((dent = lookup (w, strlen (w), 1)) != NULL
302                    &&  dent->h_flag)
303                        wordok = 1;
304        }
305
306        if (strcmp (p, "IE") != 0)
307                return;
308
309        p[0] = 'Y';
310        p[1] = 0;
311
312        if (cflag)
313                printf ("%s/H\n", w);
314        else if ((dent = lookup (w, strlen (w), 1)) != NULL)
315                if (dent->h_flag)
316                        wordok = 1;
317
318}
319
320/*
321 * check for flags: X, J, Z, S, P, M
322 *
323 * X    -ions or -ications or -ens
324 * J    -ings
325 * Z    -ers or -iers
326 * S    -ies or -es or -s
327 * P    -iness or -ness
328 * M    -'S
329 */
330
331s_ending (w)
332char *w;
333{
334        char *p;
335        struct dent *dent;
336
337        p = w + strlen (w);
338
339        p[-1] = 0;
340
341        if (index ("SXZHY", p[-2]) == NULL || (p[-2] == 'Y' && vowel (p[-3]))) {
342                if (cflag)
343                        printf ("%s/S\n", w);
344                else if ((dent = lookup (w, strlen (w), 1)) != NULL
345                    &&  dent->s_flag) {
346                        wordok = 1;
347                        return;
348                }
349        }
350
351
352        switch (p[-2]) {        /* letter before S */
353        case 'N':       /* X */
354                if (strcmp (p-4, "ION") == 0) {
355                        p[-4] = 'E';
356                        p[-3] = 0;
357                        if (cflag)
358                                printf ("%s/X\n", w);
359                        else if ((dent = lookup (w, strlen (w), 1)) != NULL
360                          &&  dent->x_flag) {
361                                wordok = 1;
362                                return;
363                        }
364                }
365                if (strcmp (p-8, "ICATE") == 0) {
366                        p[-8] = 'Y';
367                        p[-7] = 0;
368                        if (cflag)
369                                printf ("%s/X\n", w);
370                        else if ((dent = lookup (w, strlen (w), 1)) != NULL
371                            && dent->x_flag)
372                                wordok = 1;
373                        return;
374                }
375                if (strcmp (p-3, "EN") == 0 && p[-4] != 'E' && p[-4] != 'Y') {
376                        p[-3] = 0;
377                        if (cflag)
378                                printf ("%s/X\n", w);
379                        else if ((dent = lookup (w, strlen (w), 1)) != NULL
380                            && dent->x_flag)
381                                wordok = 1;
382                        return;
383                }
384                return;
385        case 'G':       /* J */
386                if (strcmp (p-4, "ING") != 0)
387                        return;
388                p[-4] = 'E';
389                p[-3] = 0;
390                if (cflag)
391                        printf ("%s/J\n", w);
392                else if ((dent = lookup (w, strlen (w), 1)) != NULL
393                    &&  dent->j_flag) {
394                        wordok = 1;
395                        return;
396                }
397                if (p[-5] == 'E')
398                        return;         /* This stops CREATEING */
399                p[-4] = 0;
400                if (cflag)
401                        printf ("%s/J\n", w);
402                else if ((dent = lookup (w, strlen (w), 1)) != NULL
403                    && dent->j_flag)
404                        wordok = 1;
405                return;
406        case 'R':       /* Z */
407                if (strcmp (p-3, "ER") != 0)
408                        return;
409
410                p[-2] = 0;
411                if (cflag)
412                        printf ("%s/Z\n", w);
413                else if ((dent = lookup (w, strlen (w), 1)) != NULL
414                    &&  dent->z_flag) {
415                        wordok = 1;
416                        return;
417                }
418                if (p[-4] == 'I'  &&  !vowel (p[-5])) {
419                        p[-4] = 'Y';
420                        p[-3] = 0;
421                        if (cflag)
422                                printf ("%s/Z\n", w);
423                        else if ((dent = lookup (w, strlen (w), 1)) != NULL
424                            && dent->z_flag) {
425                                wordok = 1;
426                                return;
427                        }
428                        p[-4] = 'I';
429                }
430                if ((p[-4] != 'E' && p[-4] != 'Y') ||
431                    (p[-4] == 'Y' && vowel (p[-5]))) {
432                        p[-3] = 0;
433                        if (cflag)
434                                printf ("%s/Z\n", w);
435                        else if ((dent = lookup (w, strlen (w), 1)) != NULL
436                          && dent->z_flag)
437                                wordok = 1;
438                }
439                return;
440        case 'E': /* S (except simple adding of an S) */
441                p[-2] = 0;      /* drop the ES */
442                if (index ("SXZH", p[-3]) != NULL) {
443                        if (cflag)
444                                printf ("%s/S\n", w);
445                        else if ((dent = lookup (w, strlen (w), 1)) != NULL) {
446                                if (dent->s_flag)
447                                        wordok = 1;;
448                                return;
449                        }
450                }
451                if (p[-3] == 'I') {
452                        p[-3] = 'Y';
453                        if (cflag)
454                                printf ("%s/S\n", w);
455                        else if ((dent = lookup (w, strlen (w), 1)) != NULL
456                            && dent->s_flag)
457                                wordok = 1;
458                        return;
459                }
460                return;
461
462        case 'S':       /* P */
463                if (strcmp (p-4, "NES") != 0)
464                        return;
465
466                p[-4] = 0;      /* kill 'N' */
467                if (p[-5] != 'Y' || vowel (p[-6])) {
468                        if (cflag)
469                                printf ("%s/P\n", w);
470                        else if ((dent = lookup (w, strlen (w), 1)) != NULL
471                            &&  dent->p_flag) {
472                                wordok = 1;
473                                return;
474                        }
475                }
476                if (p[-5] == 'I') {
477                        p[-5] = 'Y';
478                        if (cflag)
479                                printf ("%s/P\n", w);
480                        else if ((dent = lookup (w, strlen (w), 1)) != NULL
481                            && dent->p_flag)
482                                wordok = 1;
483                }
484                return;
485        case '\'':      /* M */
486                p[-2] = '\0';
487                if (cflag)
488                        printf ("%s/M\n", w);
489                else if ((dent = lookup (w, strlen (w), 1)) != NULL
490                    &&  dent->m_flag)
491                        wordok = 1;
492                return;
493        }
494}
495
496/* only the N flag */
497n_ending (w)
498char *w;
499{
500        char *p;
501        struct dent *dent;
502
503        p = w + strlen (w);
504
505        if (p[-2] == 'E') {
506                if (p[-3] == 'E' || p[-3] == 'Y')
507                        return;
508                p[-2] = 0;
509                if (cflag)
510                        printf ("%s/N\n", w);
511                else if ((dent = lookup (w, strlen (w), 1)) != NULL
512                    && dent->n_flag)
513                        wordok = 1;
514                return;
515        }
516
517        if (strcmp (p-3, "ION") != 0)
518                return;
519
520        p[-3] = 'E';
521        p[-2] = 0;
522
523        if (cflag)
524                printf ("%s/N\n", w);
525        else if ((dent = lookup (w, strlen (w), 1)) != NULL) {
526                if (dent->n_flag)
527                        wordok = 1;
528                return;
529        }
530
531        if (strcmp (p-7, "ICATE") != 0) /* check is really against "ICATION" */
532                return;
533
534        p[-7] = 'Y';
535        p[-6] = 0;
536       
537        if (cflag)
538                printf ("%s/N\n", w);
539        else if ((dent = lookup (w, strlen (w), 1)) != NULL && dent->n_flag)
540                wordok = 1;
541        return;
542}
543
544/* flags: v */
545e_ending (w)
546char *w;
547{
548        char *p;
549        struct dent *dent;
550
551        p = w + strlen (w);
552
553        if (strcmp (p-3, "IVE") != 0)
554                return;
555        p[-3] = 'E';
556        p[-2] = 0;
557
558        if (cflag)
559                printf ("%s/V\n", w);
560        else if ((dent = lookup (w, strlen (w), 1)) != NULL
561          &&  dent->v_flag) {
562                wordok = 1;
563                return;
564        }
565
566        if (p[-4] == 'E')
567                return;
568
569        p[-3] = 0;
570
571        if (cflag)
572                printf ("%s/V\n", w);
573        else if ((dent = lookup (w, strlen (w), 1)) != NULL && dent->v_flag)
574                wordok = 1;
575        return;
576}
577
578/* flags: y */
579y_ending (w)
580char *w;
581{
582        char *p;
583        struct dent *dent;
584
585        p = w + strlen (w);
586
587        if (strcmp (p-2, "LY") != 0)
588                return;
589
590        p[-2] = 0;
591
592        if (cflag)
593                printf ("%s/Y\n", w);
594        else if ((dent = lookup (w, strlen (w), 1)) != NULL && dent->y_flag)
595                wordok = 1;
596        return;
597}
598
599vowel (c)
600char c;
601{
602        return (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
603}
Note: See TracBrowser for help on using the repository browser.