-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathKrovetzStemmer.cpp
6852 lines (6660 loc) · 367 KB
/
KrovetzStemmer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*==========================================================================
* Copyright (c) 2005 University of Massachusetts. All Rights Reserved.
*
* Use of the Lemur Toolkit for Language Modeling and Information Retrieval
* is subject to the terms of the software license set forth in the LICENSE
* file included with this software, and also available at
* http://www.lemurproject.org/license.html
*
*==========================================================================
*/
/****************************************************************************\
* Copyright (c) 1990-1995 by the *
* Applied Computing Systems Institute of Massachusetts, Inc. (ACSIOM) *
* All rights reserved. *
* The INQUERY Software was provided by the *
* Center for Intelligent Information Retrieval (CIIR), *
* University of Massachusetts Computer Science Department, *
* Amherst, Massachusetts. *
* For more information, contact ACSIOM at 413-545-6311 *
\****************************************************************************/
/*
This is a stemmer that handles inflectional morphology and the
most common forms of derivational morphology. It first checks a
word against the dictionary, and if it is found it leaves it alone.
If not, it handles inflectional endings (plurals into singular form,
and past tense and "ing" endings into present tense), and then
conflates the most common derivational variants.
Author: Bob Krovetz
6/16/04 (tds) Added kstem_allocate_memory, kstem_stem_to_buffer,
and kstem_add_table_entry. The kstem_allocate_memory/
kstem_add_table_entry calls allow stemmer initialization
without forcing the user to store stem dictionaries in
flat files.
07/29/2005 (dmf) Rewritten to be threadsafe C++.
*/
#include "KrovetzStemmer.hpp"
#include <cstdlib>
#include <cstring>
#include <iostream>
namespace stem {
/* These macros expand to expressions which evaluate to the following: */
#define wordlength (k + 1) /* the length of word (not an lvalue) */
#define stemlength (j + 1) /* length of stem within word (not an lvalue) */
#define final_c (word[k]) /* the last character of word */
#define penult_c (word[k-1]) /* the penultimate character of word */
#define ends_in(s) ends(s, (int)strlen(s)) /* s must be a string constant */
#define setsuffix(s) setsuff(s, (int)strlen(s)) /* s must be a string constant */
/* --- Hashing in a fixed sized table. */
#define stemhash(word, hval){ unsigned short int ptr[6]; strncpy((char *)ptr, word, 12); hval = ((ptr[0]<<4)^ptr[1]^ptr[2]^ptr[3]^ptr[4]^ptr[5]) % stemhtsize; }
/* ------------------------- Definitions -------------------------------*/
KrovetzStemmer::KrovetzStemmer( ) : stemhtsize (30013), k(0), j(0), word(0)
{
stemCache = new cacheEntry[stemhtsize];
for (int i = 0; i < stemhtsize; i++) {
/* Set things up so that the first slot is used first */
stemCache[i].flag = 2;
stemCache[i].word1[0] = stemCache[i].word2[0] = '\0';
stemCache[i].stem1[0] = stemCache[i].stem2[0] = '\0';
}
loadTables();
}
KrovetzStemmer::~KrovetzStemmer()
{
delete[](stemCache);
}
/* Adds a stem entry into the hash table; forces the stemmer to stem
* <variant> to <word>. If <word> == "", <variant> is stemmed to itself.
*/
void KrovetzStemmer::kstem_add_table_entry( const char* variant,
const char* word,
bool exc) {
dictTable::iterator it = dictEntries.find(variant);
if (it != dictEntries.end()) {
// duplicate.
std::cerr << "kstem_add_table_entry: Duplicate word "
<< variant << " will be ignored." << std::endl;
return;
}
dictEntry entry;
entry.exception = exc;
entry.root = word;
// should test for duplicates here.
dictEntries[variant] = entry;
}
/* getdep(word) returns NULL if word is not found in the dictionary,
and returns a pointer to a dictentry if found */
inline KrovetzStemmer::dictEntry *KrovetzStemmer::getdep(char *word)
{
dictEntry *dep = 0;
/* don't bother to check for words that are short */
if (strlen(word) <= 1)
return (dep);
else {
dictTable::iterator it = dictEntries.find(word);
if (it != dictEntries.end())
dep = &((*it).second);
}
return(dep);
}
/* lookup(word) returns false if word is not found in the dictionary,
and true if it is */
inline bool KrovetzStemmer::lookup(char *word)
{
return (getdep(word) != (dictEntry *)0);
}
/* cons() returns TRUE if word[i] is a consonant. */
inline bool KrovetzStemmer::cons(int i)
{
char ch = word[i];
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
return(false);
if (ch != 'y' || i == 0)
return(true);
else {
/* ch == y, test previous char. If vowel, y is consonant
the case of yy (previously handled via recursion) is ignored.
*/
ch = word[i - 1];
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}
}
inline bool KrovetzStemmer::vowel(int i)
{
return ! cons(i);
}
/* This routine is useful for ensuring that we don't stem acronyms */
inline bool KrovetzStemmer::vowelinstem()
{
for (int i = 0; i < stemlength; i++)
if (vowel(i)) return(true);
return(false);
}
/* return TRUE if word ends with a double consonant */
inline bool KrovetzStemmer::doublec (int i)
{
if (i < 1)
return(false);
if (word[i] != word[i - 1])
return(false);
return(cons(i));
}
inline bool KrovetzStemmer::ends(const char *str, int sufflength)
{
int r = wordlength - sufflength; /* length of word before this suffix */
bool match;
if (sufflength > k)
return(false);
match = (strcmp((char *)word+r, (char *)str) == 0);
j = (match ? r-1 : k); /* use r-1 since j is an index rather than length */
return(match);
}
/* replace old suffix with str */
inline void KrovetzStemmer::setsuff(const char *str, int length)
{
strcpy((char *)word+j+1, str);
k = j + length;
word[k+1] = '\0';
}
/* convert plurals to singular form, and `-ies' to `y' */
inline void KrovetzStemmer::plural ()
{
if (final_c == 's') {
if (ends_in("ies")) {
word[j+3] = '\0';
k--;
if (lookup(word)) /* ensure calories -> calorie */
return;
k++;
word[j+3] = 's';
setsuffix("y");
}
else
if (ends_in("es")) {
/* try just removing the "s" */
word[j+2] = '\0';
k--;
/* note: don't check for exceptions here. So, `aides' -> `aide',
but `aided' -> `aid'. The exception for double s is used to prevent
crosses -> crosse. This is actually correct if crosses is a plural
noun (a type of racket used in lacrosse), but the verb is much more
common */
if ((lookup(word)) && j>0 && !((word[j] == 's') && (word[j-1] == 's')))
return;
/* try removing the "es" */
word[j+1] = '\0';
k--;
if (lookup(word))
return;
/* the default is to retain the "e" */
word[j+1] = 'e';
word[j+2] = '\0';
k++;
return;
}
else {
if (wordlength > 3 && penult_c != 's' && !ends_in("ous")) {
/* unless the word ends in "ous" or a double "s", remove the final "s" */
word[k] = '\0';
k--;
}
}
}
}
/* convert past tense (-ed) to present, and `-ied' to `y' */
inline void KrovetzStemmer::past_tense ()
{
/* Handle words less than 5 letters with a direct mapping
This prevents (fled -> fl). */
if (wordlength <= 4)
return;
dictEntry *dep = 0;
if (ends_in("ied")) {
word[j+3] = '\0';
k--;
if (lookup(word)) /* we almost always want to convert -ied to -y, but */
return; /* this isn't true for short words (died->die) */
k++; /* I don't know any long words that this applies to, */
word[j+3] = 'd'; /* but just in case... */
setsuffix("y");
return;
}
/* the vowelinstem() is necessary so we don't stem acronyms */
if (ends_in("ed") && vowelinstem()) {
/* see if the root ends in `e' */
word[j+2] = '\0';
k = j + 1;
if ((dep = getdep(word)) != (dictEntry *)NULL)
if (!(dep->exception)) /* if it's in the dictionary and not an exception */
return;
/* try removing the "ed" */
word[j+1] = '\0';
k = j;
if (lookup(word))
return;
/* try removing a doubled consonant. if the root isn't found in
the dictionary, the default is to leave it doubled. This will
correctly capture `backfilled' -> `backfill' instead of
`backfill' -> `backfille', and seems correct most of the time */
if (doublec(k)) {
word[k] = '\0';
k--;
if (lookup(word))
return;
word[k+1] = word[k];
k++;
return;
}
/* if we have a `un-' prefix, then leave the word alone */
/* (this will sometimes screw up with `under-', but we */
/* will take care of that later) */
if ((word[0] == 'u') && (word[1] == 'n')) {
word[k+1] = 'e';
word[k+2] = 'd';
k = k+2;
return;
}
/* it wasn't found by just removing the `d' or the `ed', so prefer to
end with an `e' (e.g., `microcoded' -> `microcode'). */
word[j+1] = 'e';
word[j+2] = '\0';
k = j + 1;
return;
}
}
/* handle `-ing' endings */
inline void KrovetzStemmer::aspect ()
{
/* handle short words (aging -> age) via a direct mapping. This
prevents (thing -> the) in the version of this routine that
ignores inflectional variants that are mentioned in the dictionary
(when the root is also present) */
if (wordlength <= 5)
return;
dictEntry *dep = 0;
/* the vowelinstem() is necessary so we don't stem acronyms */
if (ends_in("ing") && vowelinstem()) {
/* try adding an `e' to the stem and check against the dictionary */
word[j+1] = 'e';
word[j+2] = '\0';
k = j+1;
if ((dep = getdep(word)) != (dictEntry *)NULL)
if (!(dep->exception)) /* if it's in the dictionary and not an exception */
return;
/* adding on the `e' didn't work, so remove it */
word[k] = '\0';
k--; /* note that `ing' has also been removed */
if (lookup(word))
return;
/* if I can remove a doubled consonant and get a word, then do so */
if (doublec(k)) {
k--;
word[k+1] = '\0';
if (lookup(word))
return;
word[k+1] = word[k]; /* restore the doubled consonant */
/* the default is to leave the consonant doubled */
/* (e.g.,`fingerspelling' -> `fingerspell'). Unfortunately */
/* `bookselling' -> `booksell' and `mislabelling' -> `mislabell'). */
/* Without making the algorithm significantly more complicated, this */
/* is the best I can do */
k++;
return;
}
/* the word wasn't in the dictionary after removing the stem, and then
checking with and without a final `e'. The default is to add an `e'
unless the word ends in two consonants, so `microcoding' -> `microcode'.
The two consonants restriction wouldn't normally be necessary, but is
needed because we don't try to deal with prefixes and compounds, and
most of the time it is correct (e.g., footstamping -> footstamp, not
footstampe; however, decoupled -> decoupl). We can prevent almost all
of the incorrect stems if we try to do some prefix analysis first */
if (j>0 && cons(j) && cons(j-1)) {
k = j;
word[k+1] = '\0';
return;
}
word[j+1] = 'e';
word[j+2] = '\0';
k = j+1;
return;
}
}
/* handle some derivational endings */
/* this routine deals with -ion, -ition, -ation, -ization, and -ication. The
-ization ending is always converted to -ize */
inline void KrovetzStemmer::ion_endings ()
{
int old_k = k;
if (ends_in("ization")) { /* the -ize ending is very productive, so simply accept it as the root */
word[j+3] = 'e';
word[j+4] = '\0';
k = j+3;
return;
}
if (ends_in("ition")) {
word[j+1] = 'e';
word[j+2] = '\0';
k = j+1;
if (lookup(word)) /* remove -ition and add `e', and check against the dictionary */
return; /* (e.g., definition->define, opposition->oppose) */
/* restore original values */
word[j+1] = 'i';
word[j+2] = 't';
k = old_k;
}
if (ends_in("ation")) {
word[j+3] = 'e';
word[j+4] = '\0';
k = j+3;
if (lookup(word)) /* remove -ion and add `e', and check against the dictionary */
return; /* (elmination -> eliminate) */
word[j+1] = 'e'; /* remove -ation and add `e', and check against the dictionary */
word[j+2] = '\0'; /* (allegation -> allege) */
k = j+1;
if (lookup(word))
return;
word[j+1] = '\0'; /* just remove -ation (resignation->resign) and check dictionary */
k = j;
if (lookup(word))
return;
/* restore original values */
word[j+1] = 'a';
word[j+2] = 't';
word[j+3] = 'i';
word[j+4] = 'o'; /* no need to restore word[j+5] (n); it was never changed */
k = old_k;
}
/* test -ication after -ation is attempted (e.g., `complication->complicate'
rather than `complication->comply') */
if (ends_in("ication")) {
word[j+1] = 'y';
word[j+2] = '\0';
k = j+1;
if (lookup(word)) /* remove -ication and add `y', and check against the dictionary */
return; /* (e.g., amplification -> amplify) */
/* restore original values */
word[j+1] = 'i';
word[j+2] = 'c';
k = old_k;
}
if (ends_in("ion")) {
word[j+1] = 'e';
word[j+2] = '\0';
k = j+1;
if (lookup(word)) /* remove -ion and add `e', and check against the dictionary */
return;
word[j+1] = '\0';
k = j;
if (lookup(word)) /* remove -ion, and if it's found, treat that as the root */
return;
/* restore original values */
word[j+1] = 'i';
word[j+2] = 'o';
k = old_k;
}
return;
}
/* this routine deals with -er, -or, -ier, and -eer. The -izer ending is always converted to
-ize */
inline void KrovetzStemmer::er_and_or_endings ()
{
int old_k = k;
char word_char; /* so we can remember if it was -er or -or */
if (ends_in("izer")) { /* -ize is very productive, so accept it as the root */
word[j+4] = '\0';
k = j+3;
return;
}
if (ends_in("er") || ends_in("or")) {
word_char = word[j+1];
if (doublec(j)) {
word[j] = '\0';
k = j - 1;
if (lookup(word))
return;
word[j] = word[j-1]; /* restore the doubled consonant */
}
if (word[j] == 'i') { /* do we have a -ier ending? */
word[j] = 'y';
word[j+1] = '\0';
k = j;
if (lookup(word)) /* yes, so check against the dictionary */
return;
word[j] = 'i'; /* restore the endings */
word[j+1] = 'e';
}
if (word[j] == 'e') { /* handle -eer */
word[j] = '\0';
k = j - 1;
if (lookup(word))
return;
word[j] = 'e';
}
word[j+2] = '\0'; /* remove the -r ending */
k = j+1;
if (lookup(word))
return;
word[j+1] = '\0'; /* try removing -er/-or */
k = j;
if (lookup(word))
return;
word[j+1] = 'e'; /* try removing -or and adding -e */
word[j+2] = '\0';
k = j+1;
if (lookup(word))
return;
word[j+1] = word_char; /* restore the word to the way it was */
word[j+2] = 'r';
k = old_k;
}
}
/* this routine deals with -ly endings. The -ally ending is always converted to -al
Sometimes this will temporarily leave us with a non-word (e.g., heuristically
maps to heuristical), but then the -al is removed in the next step. */
inline void KrovetzStemmer::ly_endings ()
{
int old_k = k;
if (ends_in("ly")) {
word[j+2] = 'e'; /* try converting -ly to -le */
if (lookup(word))
return;
word[j+2] = 'y';
word[j+1] = '\0'; /* try just removing the -ly */
k = j;
if (lookup(word))
return;
if (j>0 && (word[j-1] == 'a') && (word[j] == 'l')) /* always convert -ally to -al */
return;
word[j+1] = 'l';
k = old_k;
if (j>0 && (word[j-1] == 'a') && (word[j] == 'b')) { /* always convert -ably to -able */
word[j+2] = 'e';
k = j+2;
return;
}
if (word[j] == 'i') { /* e.g., militarily -> military */
word[j] = 'y';
word[j+1] = '\0';
k = j;
if (lookup(word))
return;
word[j] = 'i';
word[j+1] = 'l';
k = old_k;
}
word[j+1] = '\0'; /* the default is to remove -ly */
k = j;
}
return;
}
/* this routine deals with -al endings. Some of the endings from the previous routine
are finished up here. */
inline void KrovetzStemmer::al_endings()
{
int old_k = k;
if (ends_in("al")) {
word[j+1] = '\0';
k = j;
if (lookup(word)) /* try just removing the -al */
return;
if (doublec(j)) { /* allow for a doubled consonant */
word[j] = '\0';
k = j-1;
if (lookup(word))
return;
word[j] = word[j-1];
}
word[j+1] = 'e'; /* try removing the -al and adding -e */
word[j+2] = '\0';
k = j+1;
if (lookup(word))
return;
word[j+1] = 'u'; /* try converting -al to -um */
word[j+2] = 'm'; /* (e.g., optimal - > optimum ) */
k = j+2;
if (lookup(word))
return;
word[j+1] = 'a'; /* restore the ending to the way it was */
word[j+2] = 'l';
word[j+3] = '\0';
k = old_k;
if (j>0 && (word[j-1] == 'i') && (word[j] == 'c')) {
word[j-1] = '\0'; /* try removing -ical */
k = j-2;
if (lookup(word))
return;
word[j-1] = 'y'; /* try turning -ical to -y (e.g., bibliographical) */
word[j] = '\0';
k = j-1;
if (lookup(word))
return;
word[j-1] = 'i';
word[j] = 'c';
word[j+1] = '\0'; /* the default is to convert -ical to -ic */
k = j;
return;
}
if (word[j] == 'i') { /* sometimes -ial endings should be removed */
word[j] = '\0'; /* (sometimes it gets turned into -y, but we */
k = j-1; /* aren't dealing with that case for now) */
if (lookup(word))
return;
word[j] = 'i';
k = old_k;
}
}
return;
}
/* this routine deals with -ive endings. It normalizes some of the
-ative endings directly, and also maps some -ive endings to -ion. */
inline void KrovetzStemmer::ive_endings()
{
int old_k = k;
if (ends_in("ive")) {
word[j+1] = '\0'; /* try removing -ive entirely */
k = j;
if (lookup(word))
return;
word[j+1] = 'e'; /* try removing -ive and adding -e */
word[j+2] = '\0';
k = j+1;
if (lookup(word))
return;
word[j+1] = 'i';
word[j+2] = 'v';
if (j>0 && (word[j-1] == 'a') && (word[j] == 't')) {
word[j-1] = 'e'; /* try removing -ative and adding -e */
word[j] = '\0'; /* (e.g., determinative -> determine) */
k = j-1;
if (lookup(word))
return;
word[j-1] = '\0'; /* try just removing -ative */
if (lookup(word))
return;
word[j-1] = 'a';
word[j] = 't';
k = old_k;
}
/* try mapping -ive to -ion (e.g., injunctive/injunction) */
word[j+2] = 'o';
word[j+3] = 'n';
if (lookup(word))
return;
word[j+2] = 'v'; /* restore the original values */
word[j+3] = 'e';
k = old_k;
}
return;
}
/* this routine deals with -ize endings. */
inline void KrovetzStemmer::ize_endings()
{
int old_k = k;
if (ends_in("ize")) {
word[j+1] = '\0'; /* try removing -ize entirely */
k = j;
if (lookup(word))
return;
word[j+1] = 'i';
if (doublec(j)) { /* allow for a doubled consonant */
word[j] = '\0';
k = j-1;
if (lookup(word))
return;
word[j] = word[j-1];
}
word[j+1] = 'e'; /* try removing -ize and adding -e */
word[j+2] = '\0';
k = j+1;
if (lookup(word))
return;
word[j+1] = 'i';
word[j+2] = 'z';
k = old_k;
}
return;
}
/* this routine deals with -ment endings. */
inline void KrovetzStemmer::ment_endings()
{
int old_k = k;
if (ends_in("ment")) {
word[j+1] = '\0';
k = j;
if (lookup(word))
return;
word[j+1] = 'm';
k = old_k;
}
return;
}
/* this routine deals with -ity endings. It accepts -ability, -ibility,
and -ality, even without checking the dictionary because they are so
productive. The first two are mapped to -ble, and the -ity is remove
for the latter */
inline void KrovetzStemmer::ity_endings()
{
int old_k = k;
if (ends_in("ity")) {
word[j+1] = '\0'; /* try just removing -ity */
k = j;
if (lookup(word))
return;
word[j+1] = 'e'; /* try removing -ity and adding -e */
word[j+2] = '\0';
k = j+1;
if (lookup(word))
return;
word[j+1] = 'i';
word[j+2] = 't';
k = old_k;
/* the -ability and -ibility endings are highly productive, so just accept them */
if (j>0 && (word[j-1] == 'i') && (word[j] == 'l')) {
word[j-1] = 'l'; /* convert to -ble */
word[j] = 'e';
word[j+1] = '\0';
k = j;
return;
}
/* ditto for -ivity */
if (j>0 && (word[j-1] == 'i') && (word[j] == 'v')) {
word[j+1] = 'e'; /* convert to -ive */
word[j+2] = '\0';
k = j+1;
return;
}
/* ditto for -ality */
if (j>0 && (word[j-1] == 'a') && (word[j] == 'l')) {
word[j+1] = '\0';
k = j;
return;
}
/* if the root isn't in the dictionary, and the variant *is*
there, then use the variant. This allows `immunity'->`immune',
but prevents `capacity'->`capac'. If neither the variant nor
the root form are in the dictionary, then remove the ending
as a default */
if (lookup(word))
return;
/* the default is to remove -ity altogether */
word[j+1] = '\0';
k = j;
return;
}
}
/* handle -able and -ible */
inline void KrovetzStemmer::ble_endings()
{
int old_k = k;
char word_char;
if (ends_in("ble")) {
if (!((word[j] == 'a') || (word[j] == 'i')))
return;
word_char = word[j];
word[j] = '\0'; /* try just removing the ending */
k = j-1;
if (lookup(word))
return;
if (doublec(k)) { /* allow for a doubled consonant */
word[k] = '\0';
k--;
if (lookup(word))
return;
k++;
word[k] = word[k-1];
}
word[j] = 'e'; /* try removing -a/ible and adding -e */
word[j+1] = '\0';
k = j;
if (lookup(word))
return;
word[j] = 'a'; /* try removing -able and adding -ate */
word[j+1] = 't'; /* (e.g., compensable/compensate) */
word[j+2] = 'e';
word[j+3] = '\0';
k = j+2;
if (lookup(word))
return;
word[j] = word_char; /* restore the original values */
word[j+1] = 'b';
word[j+2] = 'l';
word[j+3] = 'e';
k = old_k;
}
return;
}
/* handle -ness */
inline void KrovetzStemmer::ness_endings()
{
if (ends_in("ness")) { /* this is a very productive endings, so just accept it */
word[j+1] = '\0';
k = j;
if (word[j] == 'i')
word[j] = 'y';
}
return;
}
/* handle -ism */
inline void KrovetzStemmer::ism_endings()
{
if (ends_in("ism")) { /* this is a very productive ending, so just accept it */
word[j+1] = '\0';
k = j;
}
return;
}
/* handle -ic endings. This is fairly straightforward, but this is
also the only place we try *expanding* an ending, -ic -> -ical.
This is to handle cases like `canonic' -> `canonical' */
inline void KrovetzStemmer::ic_endings()
{
if (ends_in("ic")) {
word[j+3] = 'a'; /* try converting -ic to -ical */
word[j+4] = 'l';
word[j+5] = '\0';
k = j+4;
if (lookup(word))
return;
word[j+1] = 'y'; /* try converting -ic to -y */
word[j+2] = '\0';
k = j+1;
if (lookup(word))
return;
word[j+1] = 'e'; /* try converting -ic to -e */
if (lookup(word))
return;
word[j+1] = '\0'; /* try removing -ic altogether */
k = j;
if (lookup(word))
return;
word[j+1] = 'i'; /* restore the original ending */
word[j+2] = 'c';
word[j+3] = '\0';
k = j+2;
}
return;
}
/* handle -ency and -ancy */
inline void KrovetzStemmer::ncy_endings()
{
if (ends_in("ncy")) {
if (!((word[j] == 'e') || (word[j] == 'a')))
return;
word[j+2] = 't'; /* try converting -ncy to -nt */
word[j+3] = '\0'; /* (e.g., constituency -> constituent) */
k = j+2;
if (lookup(word))
return;
word[j+2] = 'c'; /* the default is to convert it to -nce */
word[j+3] = 'e';
k = j+3;
}
return;
}
/* handle -ence and -ance */
inline void KrovetzStemmer::nce_endings()
{
int old_k = k;
char word_char;
if (ends_in("nce")) {
if (!((word[j] == 'e') || (word[j] == 'a')))
return;
word_char = word[j];
word[j] = 'e'; /* try converting -e/ance to -e (adherance/adhere) */
word[j+1] = '\0';
k = j;
if (lookup(word))
return;
word[j] = '\0'; /* try removing -e/ance altogether (disappearance/disappear) */
k = j-1;
if (lookup(word))
return;
word[j] = word_char; /* restore the original ending */
word[j+1] = 'n';
k = old_k;
}
return;
}
int KrovetzStemmer::kstem_stem_tobuffer( char* term, char* buffer ) {
int i;
bool stem_it = true;
int hval;
dictEntry *dep = 0;
k = (int)strlen(term) - 1;
/* if the word is too long or too short, or not entirely
alphabetic, just lowercase copy it into stem and return */
if ((k <= 2-1) || (k >= MAX_WORD_LENGTH-1))