-
Notifications
You must be signed in to change notification settings - Fork 0
/
english_stem.h
986 lines (944 loc) · 47.3 KB
/
english_stem.h
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
/***************************************************************************
english_stem.h - description
-------------------
begin : Sat May 25 2004
copyright : (C) 2004 by Blake Madden
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the BSD License. *
* *
***************************************************************************/
#ifndef __ENGLISH_STEM_H__
#define __ENGLISH_STEM_H__
#include "stemming.h"
using namespace common_lang_constants;
namespace stemming
{
/**Overview
I have made more than one attempt to improve the structure of the Porter algorithm
by making it follow the pattern of ending removal of the Romance language stemmers.
It is not hard to see why one should want to do this: step 1b of the Porter stemmer
removes ed and ing, which are i-suffixes (*) attached to verbs. If these suffixes are
removed, there should be no need to remove d-suffixes which are not verbal, although it
will try to do so. This seems to be a deficiency in the Porter stemmer, not shared by
the Romance stemmers. Again, the divisions between steps 2, 3 and 4 seem rather arbitrary,
and are not found in the Romance stemmers.
Nevertheless, these attempts at improvement have been abandoned. They seem to lead to a
more complicated algorithm with no very obvious improvements. A reason for not taking
note of the outcome of step 1b may be that English endings do not determine word categories
quite as strongly as endings in the Romance languages. For example, condition and position
in French have to be nouns, but in English they can be verbs as well as nouns,
We are all conditioned by advertising
They are positioning themselves differently today
A possible reason for having separate steps 2, 3 and 4 is that d-suffix combinations in
English are quite complex, a point which has been made elsewhere.
But it is hardly surprising that after twenty years of use of the Porter stemmer, certain
improvements do suggest themselves, and a new algorithm for English is therefore offered
here. (It could be called the 'Porter2' stemmer to distinguish it from the Porter stemmer,
from which it derives.) The changes are not so very extensive: (1) terminating y is changed
to i rather less often, (2) suffix us does not lose its s, (3) a few additional suffixes
are included for removal, including (4) suffix ly. In addition, a small list of exceptional
forms is included. In December 2001 there were two further adjustments: (5) Steps 5a and 5b
of the old Porter stemmer were combined into a single step. This means that undoubling final
ll is not done with removal of final e. (6) In Step 3 ative is removed only when in region R2.
To begin with, here is the basic algorithm without reference to the exceptional forms.
An exact comparison with the Porter algorithm needs to be done quite carefully if done at
all. Here we indicate by * points of departure, and by + additional features.
In the sample vocabulary, Porter and Porter2 stem slightly under 5% of words to different forms.
Dr. Martin Porter
Define a vowel as one of
-a e i o u y
Define a double as one of
-bb dd ff gg mm nn pp rr tt
Define a valid li-ending as one of
-c d e g h k m n r t
Define a short syllable in a word as either (a) a vowel followed by a non-vowel
other than w, x or Y and preceded by a non-vowel, or * (b) a vowel at the beginning
of the word followed by a non-vowel.
So rap, trap, entrap end with a short syllable, and ow, on, at are classed as short syllables.
But uproot, bestow, disturb do not end with a short syllable.
A word is called short if it consists of a short syllable preceded by zero or more consonants.
R1 is the region after the first non-vowel following a vowel, or the end of the word if there is no such non-vowel.
R2 is the region after the first non-vowel following a vowel in R1, or the end of the word if there is no such non-vowel.
If the word has two letters or less, leave it as it is.
Otherwise, do each of the following operations,
Set initial y, or y after a vowel, to Y, and then establish the regions R1 and R2.*/
//------------------------------------------------------
template <typename string_typeT = std::wstring>
class english_stem : public stem<string_typeT>
{
public:
english_stem() : m_first_vowel(string_typeT::npos)
{}
//---------------------------------------------
///@param text: string to stem
void operator()(string_typeT& text)
{
if (text.length() < 3)
{
return;
}
//reset internal data
m_first_vowel = string_typeT::npos;
stem<string_typeT>::reset_r_values();
trim_western_punctuation(text);
//handle exceptions first
if (is_exception(text) )
{
return;
}
hash_y(text, L"aeiouyAEIOUY");
m_first_vowel = text.find_first_of(L"aeiouyAEIOUY");
if (m_first_vowel == string_typeT::npos)
{ return; }
if (text.length() >= 5 &&
/*gener*/
(is_either<wchar_t>(text[0], LOWER_G, UPPER_G) &&
is_either<wchar_t>(text[1], LOWER_E, UPPER_E) &&
is_either<wchar_t>(text[2], LOWER_N, UPPER_N) &&
is_either<wchar_t>(text[3], LOWER_E, UPPER_E) &&
is_either<wchar_t>(text[4], LOWER_R, UPPER_R) ) )
{
stem<string_typeT>::set_r1(5);
}
else if (text.length() >= 6 &&
/*commun*/
(is_either<wchar_t>(text[0], LOWER_C, UPPER_C) &&
is_either<wchar_t>(text[1], LOWER_O, UPPER_O) &&
is_either<wchar_t>(text[2], LOWER_M, UPPER_M) &&
is_either<wchar_t>(text[3], LOWER_M, UPPER_M) &&
is_either<wchar_t>(text[4], LOWER_U, UPPER_U) &&
is_either<wchar_t>(text[5], LOWER_N, UPPER_N) ) )
{
stem<string_typeT>::set_r1(6);
}
else if (text.length() >= 5 &&
/*arsen*/
(is_either<wchar_t>(text[0], LOWER_A, UPPER_A) &&
is_either<wchar_t>(text[1], LOWER_R, UPPER_R) &&
is_either<wchar_t>(text[2], LOWER_S, UPPER_S) &&
is_either<wchar_t>(text[3], LOWER_E, UPPER_E) &&
is_either<wchar_t>(text[4], LOWER_N, UPPER_N) ) )
{
stem<string_typeT>::set_r1(5);
}
else
{
find_r1(text, L"aeiouyAEIOUY");
}
find_r2(text, L"aeiouyAEIOUY");
//step 1a:
step_1a(text);
//exception #2
if (is_exception_post_step1a(text) )
{
return;
}
//step 1b:
step_1b(text);
//step 1c:
step_1c(text);
//step 2:
step_2(text);
//step 3:
step_3(text);
//step 4:
step_4(text);
//step 5:
step_5(text);
unhash_y(text);
}
private:
//---------------------------------------------
bool is_exception(string_typeT& text)
{
//exception #0
/*skis*/
if (text.length() == 4 &&
is_either<wchar_t>(text[0], LOWER_S, UPPER_S) &&
is_either<wchar_t>(text[1], LOWER_K, UPPER_K) &&
is_either<wchar_t>(text[2], LOWER_I, UPPER_I) &&
is_either<wchar_t>(text[3], LOWER_S, UPPER_S) )
{
text = L"ski";
return true;
}
/*skies*/
else if (text.length() == 5 &&
is_either<wchar_t>(text[0], LOWER_S, UPPER_S) &&
is_either<wchar_t>(text[1], LOWER_K, UPPER_K) &&
is_either<wchar_t>(text[2], LOWER_I, UPPER_I) &&
is_either<wchar_t>(text[3], LOWER_E, UPPER_E) &&
is_either<wchar_t>(text[4], LOWER_S, UPPER_S) )
{
text = L"sky";
return true;
}
/*dying*/
else if (text.length() == 5 &&
is_either<wchar_t>(text[0], LOWER_D, UPPER_D) &&
is_either<wchar_t>(text[1], LOWER_Y, UPPER_Y) &&
is_either<wchar_t>(text[2], LOWER_I, UPPER_I) &&
is_either<wchar_t>(text[3], LOWER_N, UPPER_N) &&
is_either<wchar_t>(text[4], LOWER_G, UPPER_G) )
{
text = L"die";
return true;
}
/*lying*/
else if (text.length() == 5 &&
is_either<wchar_t>(text[0], LOWER_L, UPPER_L) &&
is_either<wchar_t>(text[1], LOWER_Y, UPPER_Y) &&
is_either<wchar_t>(text[2], LOWER_I, UPPER_I) &&
is_either<wchar_t>(text[3], LOWER_N, UPPER_N) &&
is_either<wchar_t>(text[4], LOWER_G, UPPER_G) )
{
text = L"lie";
return true;
}
/*tying*/
else if (text.length() == 5 &&
is_either<wchar_t>(text[0], LOWER_T, UPPER_T) &&
is_either<wchar_t>(text[1], LOWER_Y, UPPER_Y) &&
is_either<wchar_t>(text[2], LOWER_I, UPPER_I) &&
is_either<wchar_t>(text[3], LOWER_N, UPPER_N) &&
is_either<wchar_t>(text[4], LOWER_G, UPPER_G) )
{
text = L"tie";
return true;
}
/*idly*/
else if (text.length() == 4 &&
is_either<wchar_t>(text[0], LOWER_I, UPPER_I) &&
is_either<wchar_t>(text[1], LOWER_D, UPPER_D) &&
is_either<wchar_t>(text[2], LOWER_L, UPPER_L) &&
is_either<wchar_t>(text[3], LOWER_Y, UPPER_Y) )
{
text = L"idl";
return true;
}
/*gently*/
else if (text.length() == 6 &&
is_either<wchar_t>(text[0], LOWER_G, UPPER_G) &&
is_either<wchar_t>(text[1], LOWER_E, UPPER_E) &&
is_either<wchar_t>(text[2], LOWER_N, UPPER_N) &&
is_either<wchar_t>(text[3], LOWER_T, UPPER_T) &&
is_either<wchar_t>(text[4], LOWER_L, UPPER_L) &&
is_either<wchar_t>(text[5], LOWER_Y, UPPER_Y) )
{
text = L"gentl";
return true;
}
/*ugly*/
else if (text.length() == 4 &&
is_either<wchar_t>(text[0], LOWER_U, UPPER_U) &&
is_either<wchar_t>(text[1], LOWER_G, UPPER_G) &&
is_either<wchar_t>(text[2], LOWER_L, UPPER_L) &&
is_either<wchar_t>(text[3], LOWER_Y, UPPER_Y) )
{
text = L"ugli";
return true;
}
/*early*/
else if (text.length() == 5 &&
is_either<wchar_t>(text[0], LOWER_E, UPPER_E) &&
is_either<wchar_t>(text[1], LOWER_A, UPPER_A) &&
is_either<wchar_t>(text[2], LOWER_R, UPPER_R) &&
is_either<wchar_t>(text[3], LOWER_L, UPPER_L) &&
is_either<wchar_t>(text[4], LOWER_Y, UPPER_Y) )
{
text = L"earli";
return true;
}
/*only*/
else if (text.length() == 4 &&
is_either<wchar_t>(text[0], LOWER_O, UPPER_O) &&
is_either<wchar_t>(text[1], LOWER_N, UPPER_N) &&
is_either<wchar_t>(text[2], LOWER_L, UPPER_L) &&
is_either<wchar_t>(text[3], LOWER_Y, UPPER_Y) )
{
text = L"onli";
return true;
}
/*singly*/
else if (text.length() == 6 &&
is_either<wchar_t>(text[0], LOWER_S, UPPER_S) &&
is_either<wchar_t>(text[1], LOWER_I, UPPER_I) &&
is_either<wchar_t>(text[2], LOWER_N, UPPER_N) &&
is_either<wchar_t>(text[3], LOWER_G, UPPER_G) &&
is_either<wchar_t>(text[4], LOWER_L, UPPER_L) &&
is_either<wchar_t>(text[5], LOWER_Y, UPPER_Y) )
{
text = L"singl";
return true;
}
//exception #1
else if (
/*sky*/
(text.length() == 3 &&
is_either<wchar_t>(text[0], LOWER_S, UPPER_S) &&
is_either<wchar_t>(text[1], LOWER_K, UPPER_K) &&
is_either<wchar_t>(text[2], LOWER_Y, UPPER_Y) ) ||
/*news*/
(text.length() == 4 &&
is_either<wchar_t>(text[0], LOWER_N, UPPER_N) &&
is_either<wchar_t>(text[1], LOWER_E, UPPER_E) &&
is_either<wchar_t>(text[2], LOWER_W, UPPER_W) &&
is_either<wchar_t>(text[3], LOWER_S, UPPER_S) ) ||
/*howe*/
(text.length() == 4 &&
is_either<wchar_t>(text[0], LOWER_H, UPPER_H) &&
is_either<wchar_t>(text[1], LOWER_O, UPPER_O) &&
is_either<wchar_t>(text[2], LOWER_W, UPPER_W) &&
is_either<wchar_t>(text[3], LOWER_E, UPPER_E) ) ||
/*atlas*/
(text.length() == 5 &&
is_either<wchar_t>(text[0], LOWER_A, UPPER_A) &&
is_either<wchar_t>(text[1], LOWER_T, UPPER_T) &&
is_either<wchar_t>(text[2], LOWER_L, UPPER_L) &&
is_either<wchar_t>(text[3], LOWER_A, UPPER_A) &&
is_either<wchar_t>(text[4], LOWER_S, UPPER_S) ) ||
/*cosmos*/
(text.length() == 6 &&
is_either<wchar_t>(text[0], LOWER_C, UPPER_C) &&
is_either<wchar_t>(text[1], LOWER_O, UPPER_O) &&
is_either<wchar_t>(text[2], LOWER_S, UPPER_S) &&
is_either<wchar_t>(text[3], LOWER_M, UPPER_M) &&
is_either<wchar_t>(text[4], LOWER_O, UPPER_O) &&
is_either<wchar_t>(text[5], LOWER_S, UPPER_S) ) ||
/*bias*/
(text.length() == 4 &&
is_either<wchar_t>(text[0], LOWER_B, UPPER_B) &&
is_either<wchar_t>(text[1], LOWER_I, UPPER_I) &&
is_either<wchar_t>(text[2], LOWER_A, UPPER_A) &&
is_either<wchar_t>(text[3], LOWER_S, UPPER_S) ) ||
/*andes*/
(text.length() == 5 &&
is_either<wchar_t>(text[0], LOWER_A, UPPER_A) &&
is_either<wchar_t>(text[1], LOWER_N, UPPER_N) &&
is_either<wchar_t>(text[2], LOWER_D, UPPER_D) &&
is_either<wchar_t>(text[3], LOWER_E, UPPER_E) &&
is_either<wchar_t>(text[4], LOWER_S, UPPER_S) ) )
{
return true;
}
return false;
}
//---------------------------------------------
bool is_exception_post_step1a(string_typeT& text)
{
//exception #2
if (/*inning*/
(text.length() == 6 &&
is_either<wchar_t>(text[0], LOWER_I, UPPER_I) &&
is_either<wchar_t>(text[1], LOWER_N, UPPER_N) &&
is_either<wchar_t>(text[2], LOWER_N, UPPER_N) &&
is_either<wchar_t>(text[3], LOWER_I, UPPER_I) &&
is_either<wchar_t>(text[4], LOWER_N, UPPER_N) &&
is_either<wchar_t>(text[5], LOWER_G, UPPER_G) ) ||
/*outing*/
(text.length() == 6 &&
is_either<wchar_t>(text[0], LOWER_O, UPPER_O) &&
is_either<wchar_t>(text[1], LOWER_U, UPPER_U) &&
is_either<wchar_t>(text[2], LOWER_T, UPPER_T) &&
is_either<wchar_t>(text[3], LOWER_I, UPPER_I) &&
is_either<wchar_t>(text[4], LOWER_N, UPPER_N) &&
is_either<wchar_t>(text[5], LOWER_G, UPPER_G) ) ||
/*canning*/
(text.length() == 7 &&
is_either<wchar_t>(text[0], LOWER_C, UPPER_C) &&
is_either<wchar_t>(text[1], LOWER_A, UPPER_A) &&
is_either<wchar_t>(text[2], LOWER_N, UPPER_N) &&
is_either<wchar_t>(text[3], LOWER_N, UPPER_N) &&
is_either<wchar_t>(text[4], LOWER_I, UPPER_I) &&
is_either<wchar_t>(text[5], LOWER_N, UPPER_N) &&
is_either<wchar_t>(text[6], LOWER_G, UPPER_G) ) ||
/*herring*/
(text.length() == 7 &&
is_either<wchar_t>(text[0], LOWER_H, UPPER_H) &&
is_either<wchar_t>(text[1], LOWER_E, UPPER_E) &&
is_either<wchar_t>(text[2], LOWER_R, UPPER_R) &&
is_either<wchar_t>(text[3], LOWER_R, UPPER_R) &&
is_either<wchar_t>(text[4], LOWER_I, UPPER_I) &&
is_either<wchar_t>(text[5], LOWER_N, UPPER_N) &&
is_either<wchar_t>(text[6], LOWER_G, UPPER_G) ) ||
/*earring*/
(text.length() == 7 &&
is_either<wchar_t>(text[0], LOWER_E, UPPER_E) &&
is_either<wchar_t>(text[1], LOWER_A, UPPER_A) &&
is_either<wchar_t>(text[2], LOWER_R, UPPER_R) &&
is_either<wchar_t>(text[3], LOWER_R, UPPER_R) &&
is_either<wchar_t>(text[4], LOWER_I, UPPER_I) &&
is_either<wchar_t>(text[5], LOWER_N, UPPER_N) &&
is_either<wchar_t>(text[6], LOWER_G, UPPER_G) ) ||
/*proceed*/
(text.length() == 7 &&
is_either<wchar_t>(text[0], LOWER_P, UPPER_P) &&
is_either<wchar_t>(text[1], LOWER_R, UPPER_R) &&
is_either<wchar_t>(text[2], LOWER_O, UPPER_O) &&
is_either<wchar_t>(text[3], LOWER_C, UPPER_C) &&
is_either<wchar_t>(text[4], LOWER_E, UPPER_E) &&
is_either<wchar_t>(text[5], LOWER_E, UPPER_E) &&
is_either<wchar_t>(text[6], LOWER_D, UPPER_D) ) ||
/*exceed*/
(text.length() == 6 &&
is_either<wchar_t>(text[0], LOWER_E, UPPER_E) &&
is_either<wchar_t>(text[1], LOWER_X, UPPER_X) &&
is_either<wchar_t>(text[2], LOWER_C, UPPER_C) &&
is_either<wchar_t>(text[3], LOWER_E, UPPER_E) &&
is_either<wchar_t>(text[4], LOWER_E, UPPER_E) &&
is_either<wchar_t>(text[5], LOWER_D, UPPER_D) ) ||
/*succeed*/
(text.length() == 7 &&
is_either<wchar_t>(text[0], LOWER_S, UPPER_S) &&
is_either<wchar_t>(text[1], LOWER_U, UPPER_U) &&
is_either<wchar_t>(text[2], LOWER_C, UPPER_C) &&
is_either<wchar_t>(text[3], LOWER_C, UPPER_C) &&
is_either<wchar_t>(text[4], LOWER_E, UPPER_E) &&
is_either<wchar_t>(text[5], LOWER_E, UPPER_E) &&
is_either<wchar_t>(text[6], LOWER_D, UPPER_D) ) )
{
return true;
}
return false;
}
/**Search for the longest among the following suffixes, and perform the action indicated.
-sses
-replace by ss
-ied+ ies*
-replace by i if preceded by just one letter, otherwise by ie (so ties -> tie, cries -> cri)
-s
-delete if the preceding word part contains a vowel not immediately before the s (so gas and this retain the s, gaps and kiwis lose it)
-us+ ss
-do nothing*/
//---------------------------------------------
void step_1a(string_typeT& text)
{
if (is_suffix(text,/*sses*/LOWER_S, UPPER_S, LOWER_S, UPPER_S, LOWER_E, UPPER_E, LOWER_S, UPPER_S) )
{
text.erase(text.length()-2);
update_r_sections(text);
}
else if (is_suffix(text,/*ied*/LOWER_I, UPPER_I, LOWER_E, UPPER_E, LOWER_D, UPPER_D) ||
is_suffix(text,/*ies*/LOWER_I, UPPER_I, LOWER_E, UPPER_E, LOWER_S, UPPER_S) )
{
if (text.length() == 3 || text.length() == 4)
{
text.erase(text.length()-1);
update_r_sections(text);
}
else
{
text.erase(text.length()-2);
update_r_sections(text);
}
}
else if (text.length() >= 2 &&
is_either<wchar_t>(text[text.length()-1], LOWER_S, UPPER_S) &&
m_first_vowel < text.length()-2 &&
!string_util::is_one_of(text[text.length()-2], L"suSU") )
{
text.erase(text.length()-1);
update_r_sections(text);
}
}
/**Search for the longest among the following suffixes, and perform the action indicated.
-eed eedly+
-replace by ee if in R1
-ed edly+ ing ingly+
-delete if the preceding word part contains a vowel, and then
-if the word ends at, bl or iz add e (so luxuriat -> luxuriate), or
-if the word ends with a double remove the last letter (so hopp -> hop), or
-if the word is short, add e (so hop -> hope)*/
//---------------------------------------------
void step_1b(string_typeT& text)
{
//if the preceding word contains a vowel
bool regress_trim = false;
if (is_suffix(text,/*eed*/LOWER_E, UPPER_E, LOWER_E, UPPER_E, LOWER_D, UPPER_D) )
{
if (stem<string_typeT>::get_r1() <= text.length()-3)
{
text.erase(text.length()-1);
update_r_sections(text);
}
}
else if (is_suffix(text,/*eedly*/LOWER_E, UPPER_E, LOWER_E, UPPER_E, LOWER_D, UPPER_D, LOWER_L, UPPER_L, LOWER_Y, UPPER_Y) )
{
if (stem<string_typeT>::get_r1() <= text.length()-5)
{
text.erase(text.length()-3);
update_r_sections(text);
}
}
else if (is_suffix(text,/*ed*/LOWER_E, UPPER_E, LOWER_D, UPPER_D) &&
m_first_vowel < text.length()-2)
{
text.erase(text.length()-2);
update_r_sections(text);
regress_trim = true;
}
else if (is_suffix(text,/*edly*/LOWER_E, UPPER_E, LOWER_D, UPPER_D, LOWER_L, UPPER_L, LOWER_Y, UPPER_Y) &&
m_first_vowel < text.length()-4)
{
text.erase(text.length()-4);
update_r_sections(text);
regress_trim = true;
}
else if (is_suffix(text,/*ing*/LOWER_I, UPPER_I, LOWER_N, UPPER_N, LOWER_G, UPPER_G) &&
m_first_vowel < text.length()-3)
{
text.erase(text.length()-3);
update_r_sections(text);
regress_trim = true;
}
else if (is_suffix(text,/*ingly*/LOWER_I, UPPER_I, LOWER_N, UPPER_N, LOWER_G, UPPER_G, LOWER_L, UPPER_L, LOWER_Y, UPPER_Y) &&
m_first_vowel < text.length()-5)
{
text.erase(text.length()-5);
update_r_sections(text);
regress_trim = true;
}
if (regress_trim)
{
if (is_suffix(text,/*at*/LOWER_A, UPPER_A, LOWER_T, UPPER_T) ||
is_suffix(text,/*bl*/LOWER_B, UPPER_B, LOWER_L, UPPER_L) ||
is_suffix(text,/*iz*/LOWER_I, UPPER_I, LOWER_Z, UPPER_Z) )
{
text += LOWER_E;
//need to search for r2 again because the 'e' added here may change that
find_r2(text, L"aeiouyAEIOUY");
}
else if (is_suffix(text,/*bb*/LOWER_B, UPPER_B, LOWER_B, UPPER_B) ||
is_suffix(text,/*dd*/LOWER_D, UPPER_D, LOWER_D, UPPER_D) ||
is_suffix(text,/*ff*/LOWER_F, UPPER_F, LOWER_F, UPPER_F) ||
is_suffix(text,/*gg*/LOWER_G, UPPER_G, LOWER_G, UPPER_G) ||
is_suffix(text,/*mm*/LOWER_M, UPPER_M, LOWER_M, UPPER_M) ||
is_suffix(text,/*nn*/LOWER_N, UPPER_N, LOWER_N, UPPER_N) ||
is_suffix(text,/*pp*/LOWER_P, UPPER_P, LOWER_P, UPPER_P) ||
is_suffix(text,/*rr*/LOWER_R, UPPER_R, LOWER_R, UPPER_R) ||
is_suffix(text,/*tt*/LOWER_T, UPPER_T, LOWER_T, UPPER_T) )
{
text.erase(text.length()-1);
update_r_sections(text);
}
else if (is_short_word(text, text.length() ) )
{
text += LOWER_E;
//need to search for r2 again because the 'e' added here may change that
find_r2(text, L"aeiouyAEIOUY");
}
}
}
/**replace suffix y or Y by i if preceeded by a non-vowel which is
not the first letter of the word (so cry -> cri, by -> by, say -> say)*/
//---------------------------------------------
void step_1c(string_typeT& text)
{
//proceeding consonant cannot be first letter in word
if (text.length() > 2 &&
!is_vowel(text[text.length()-2]) )
{
if (is_either<wchar_t>(text[text.length()-1], LOWER_Y, LOWER_Y_HASH) )
{
text[text.length()-1] = LOWER_I;
}
else if (is_either<wchar_t>(text[text.length()-1], UPPER_Y, UPPER_Y_HASH) )
{
text[text.length()-1] = UPPER_I;
}
}
}
/**Search for the longest among the following suffixes, and, if found and in R1,
perform the action indicated.
-tional: replace by tion
-enci: replace by ence
-anci: replace by ance
-abli: replace by able
-entli: replace by ent
-izer ization: replace by ize
-ational ation ator: replace by ate
-alism aliti alli: replace by al
-fulness: replace by ful
-ousli ousness: replace by ous
-iveness iviti: replace by ive
-biliti bli+: replace by ble
-ogi+: replace by og if preceded by l
-fulli+: replace by ful
-lessli+: replace by less
-li+: delete if preceded by a valid li-ending*/
//---------------------------------------------
void step_2(string_typeT& text)
{
if (text.length() >= 7 &&
(is_suffix(text,/*ization*/LOWER_I, UPPER_I, LOWER_Z, UPPER_Z, LOWER_A, UPPER_A, LOWER_T, UPPER_T, LOWER_I, UPPER_I, LOWER_O, UPPER_O, LOWER_N, UPPER_N) ||
is_suffix(text,/*ational*/LOWER_A, UPPER_A, LOWER_T, UPPER_T, LOWER_I, UPPER_I, LOWER_O, UPPER_O, LOWER_N, UPPER_N, LOWER_A, UPPER_A, LOWER_L, UPPER_L) ) )
{
if (stem<string_typeT>::get_r1() <= text.length()-7)
{
text.erase(text.length()-4);
text[static_cast<int>(text.length()-1)] = LOWER_E;
update_r_sections(text);
}
}
else if (text.length() >= 7 &&
(is_suffix(text,/*fulness*/LOWER_F, UPPER_F, LOWER_U, UPPER_U, LOWER_L, UPPER_L, LOWER_N, UPPER_N, LOWER_E, UPPER_E, LOWER_S, UPPER_S, LOWER_S, UPPER_S) ||
is_suffix(text,/*ousness*/LOWER_O, UPPER_O, LOWER_U, UPPER_U, LOWER_S, UPPER_S, LOWER_N, UPPER_N, LOWER_E, UPPER_E, LOWER_S, UPPER_S, LOWER_S, UPPER_S) ||
is_suffix(text,/*iveness*/LOWER_I, UPPER_I, LOWER_V, UPPER_V, LOWER_E, UPPER_E, LOWER_N, UPPER_N, LOWER_E, UPPER_E, LOWER_S, UPPER_S, LOWER_S, UPPER_S) ) )
{
if (stem<string_typeT>::get_r1() <= text.length()-7)
{
text.erase(text.length()-4);
update_r_sections(text);
}
}
else if (text.length() >= 6 &&
(is_suffix(text,/*tional*/LOWER_T, UPPER_T, LOWER_I, UPPER_I, LOWER_O, UPPER_O, LOWER_N, UPPER_N, LOWER_A, UPPER_A, LOWER_L, UPPER_L) ||
is_suffix(text,/*lessli*/LOWER_L, UPPER_L, LOWER_E, UPPER_E, LOWER_S, UPPER_S, LOWER_S, UPPER_S, LOWER_L, UPPER_L, LOWER_I, UPPER_I) ) )
{
if (stem<string_typeT>::get_r1() <= text.length()-6)
{
text.erase(text.length()-2);
update_r_sections(text);
}
}
else if (text.length() >= 6 && is_suffix(text,/*biliti*/LOWER_B, UPPER_B, LOWER_I, UPPER_I, LOWER_L, UPPER_L, LOWER_I, UPPER_I, LOWER_T, UPPER_T, LOWER_I, UPPER_I) )
{
if (stem<string_typeT>::get_r1() <= text.length()-6)
{
text.erase(text.length()-3);
text[text.length()-2] = LOWER_L;
text[text.length()-1] = LOWER_E;
update_r_sections(text);
}
}
else if (text.length() >= 5 &&
(is_suffix(text,/*iviti*/LOWER_I, UPPER_I, LOWER_V, UPPER_V, LOWER_I, UPPER_I, LOWER_T, UPPER_T, LOWER_I, UPPER_I) ||
is_suffix(text,/*ation*/LOWER_A, UPPER_A, LOWER_T, UPPER_T, LOWER_I, UPPER_I, LOWER_O, UPPER_O, LOWER_N, UPPER_N) ) )
{
if (stem<string_typeT>::get_r1() <= text.length()-5)
{
text.erase(text.length()-2);
text[text.length()-1] = LOWER_E;
update_r_sections(text);
}
}
else if (text.length() >= 5 &&
(is_suffix(text,/*alism*/LOWER_A, UPPER_A, LOWER_L, UPPER_L, LOWER_I, UPPER_I, LOWER_S, UPPER_S, LOWER_M, UPPER_M) ||
is_suffix(text,/*aliti*/LOWER_A, UPPER_A, LOWER_L, UPPER_L, LOWER_I, UPPER_I, LOWER_T, UPPER_T, LOWER_I, UPPER_I) ) )
{
if (stem<string_typeT>::get_r1() <= text.length()-5)
{
text.erase(text.length()-3);
update_r_sections(text);
}
}
else if (text.length() >= 5 &&
(is_suffix(text,/*ousli*/LOWER_O, UPPER_O, LOWER_U, UPPER_U, LOWER_S, UPPER_S, LOWER_L, UPPER_L, LOWER_I, UPPER_I) ||
is_suffix(text,/*entli*/LOWER_E, UPPER_E, LOWER_N, UPPER_N, LOWER_T, UPPER_T, LOWER_L, UPPER_L, LOWER_I, UPPER_I) ||
is_suffix(text,/*fulli*/LOWER_F, UPPER_F, LOWER_U, UPPER_U, LOWER_L, UPPER_L, LOWER_L, UPPER_L, LOWER_I, UPPER_I) ) )
{
if (stem<string_typeT>::get_r1() <= text.length()-5)
{
text.erase(text.length()-2);
update_r_sections(text);
}
}
else if (text.length() >= 4 && is_suffix(text,/*alli*/LOWER_A, UPPER_A, LOWER_L, UPPER_L, LOWER_L, UPPER_L, LOWER_I, UPPER_I) )
{
if (stem<string_typeT>::get_r1() <= text.length()-4)
{
text.erase(text.length()-2);
update_r_sections(text);
}
}
else if (text.length() >= 4 &&
(is_suffix(text,/*enci*/LOWER_E, UPPER_E, LOWER_N, UPPER_N, LOWER_C, UPPER_C, LOWER_I, UPPER_I) ||
is_suffix(text,/*anci*/LOWER_A, UPPER_A, LOWER_N, UPPER_N, LOWER_C, UPPER_C, LOWER_I, UPPER_I) ||
is_suffix(text,/*abli*/LOWER_A, UPPER_A, LOWER_B, UPPER_B, LOWER_L, UPPER_L, LOWER_I, UPPER_I) ) )
{
if (stem<string_typeT>::get_r1() <= text.length()-4)
{
text[text.length()-1] = LOWER_E;
}
}
else if (text.length() >= 4 && is_suffix(text,/*izer*/LOWER_I, UPPER_I, LOWER_Z, UPPER_Z, LOWER_E, UPPER_E, LOWER_R, UPPER_R) )
{
if (stem<string_typeT>::get_r1() <= text.length()-4)
{
text.erase(text.length()-1);
update_r_sections(text);
}
}
else if (text.length() >= 4 && is_suffix(text,/*ator*/LOWER_A, UPPER_A, LOWER_T, UPPER_T, LOWER_O, UPPER_O, LOWER_R, UPPER_R) )
{
if (stem<string_typeT>::get_r1() <= text.length()-4)
{
text.erase(text.length()-1);
text[text.length()-1] = LOWER_E;
update_r_sections(text);
}
}
else if (text.length() >= 3 &&
stem<string_typeT>::get_r1() <= (text.length()-3) &&
is_suffix(text,/*bli*/LOWER_B, UPPER_B, LOWER_L, UPPER_L, LOWER_I, UPPER_I) )
{
text[text.length()-1] = LOWER_E;
}
else if (text.length() >= 3 &&
stem<string_typeT>::get_r1() <= (text.length()-3) &&
is_suffix(text,/*ogi*/LOWER_O, UPPER_O, LOWER_G, UPPER_G, LOWER_I, UPPER_I) )
{
if (is_either<wchar_t>(text[text.length()-4], LOWER_L, UPPER_L) )
{
text.erase(text.length()-1);
update_r_sections(text);
}
}
else if (text.length() >= 2 &&
stem<string_typeT>::get_r1() <= (text.length()-2) &&
is_suffix(text,/*li*/LOWER_L, UPPER_L, LOWER_I, UPPER_I) )
{
if (string_util::is_one_of(text[text.length()-3], L"cdeghkmnrtCDEGHKMNRT") )
{
text.erase(text.length()-2);
update_r_sections(text);
}
}
}
/**Search for the longest among the following suffixes, and, if found and in R1, perform the action indicated.
-tional+: replace by tion
-ational+: replace by ate
-alize: replace by al
-icate iciti ical: replace by ic
-ful ness: delete
-ative*: delete if in R2*/
//---------------------------------------------
void step_3(string_typeT& text)
{
if (text.length() >= 7 && is_suffix(text,/*ational*/LOWER_A, UPPER_A, LOWER_T, UPPER_T, LOWER_I, UPPER_I, LOWER_O, UPPER_O, LOWER_N, UPPER_N, LOWER_A, UPPER_A, LOWER_L, UPPER_L) )
{
if (stem<string_typeT>::get_r1() <= text.length()-7)
{
text.erase(text.length()-4);
text[text.length()-1] = LOWER_E;
update_r_sections(text);
}
}
else if (text.length() >= 6 && is_suffix(text,/*tional*/LOWER_T, UPPER_T, LOWER_I, UPPER_I, LOWER_O, UPPER_O, LOWER_N, UPPER_N, LOWER_A, UPPER_A, LOWER_L, UPPER_L) )
{
if (stem<string_typeT>::get_r1() <= text.length()-6)
{
text.erase(text.length()-2);
update_r_sections(text);
}
}
else if (text.length() >= 5 &&
(is_suffix(text,/*icate*/LOWER_I, UPPER_I, LOWER_C, UPPER_C, LOWER_A, UPPER_A, LOWER_T, UPPER_T, LOWER_E, UPPER_E) ||
is_suffix(text,/*iciti*/LOWER_I, UPPER_I, LOWER_C, UPPER_C, LOWER_I, UPPER_I, LOWER_T, UPPER_T, LOWER_I, UPPER_I) ||
is_suffix(text,/*alize*/LOWER_A, UPPER_A, LOWER_L, UPPER_L, LOWER_I, UPPER_I, LOWER_Z, UPPER_Z, LOWER_E, UPPER_E) ) )
{
if (stem<string_typeT>::get_r1() <= text.length()-5)
{
text.erase(text.length()-3);
update_r_sections(text);
}
}
else if (text.length() >= 5 && is_suffix(text,/*ative*/LOWER_A, UPPER_A, LOWER_T, UPPER_T, LOWER_I, UPPER_I, LOWER_V, UPPER_V, LOWER_E, UPPER_E) )
{
if (stem<string_typeT>::get_r2() <= text.length()-5)
{
text.erase(text.length()-5);
update_r_sections(text);
}
}
else if (text.length() >= 4 && is_suffix(text,/*ical*/LOWER_I, UPPER_I, LOWER_C, UPPER_C, LOWER_A, UPPER_A, LOWER_L, UPPER_L) )
{
if (stem<string_typeT>::get_r1() <= text.length()-4)
{
text.erase(text.length()-2);
update_r_sections(text);
}
}
else if (text.length() >= 4 && is_suffix(text,/*ness*/LOWER_N, UPPER_N, LOWER_E, UPPER_E, LOWER_S, UPPER_S, LOWER_S, UPPER_S) )
{
if (stem<string_typeT>::get_r1() <= text.length()-4)
{
text.erase(text.length()-4);
update_r_sections(text);
}
}
else if (text.length() >= 3 && is_suffix(text,/*ful*/LOWER_F, UPPER_F, LOWER_U, UPPER_U, LOWER_L, UPPER_L) )
{
if (stem<string_typeT>::get_r1() <= text.length()-3)
{
text.erase(text.length()-3);
update_r_sections(text);
}
}
}
/**Search for the longest among the following suffixes, and, if found and in R2, perform the action indicated.
-al ance ence er ic able ible ant ement ment ent ism ate iti ous ive ize
-delete
-ion
-delete if preceded by s or t*/
//---------------------------------------------
void step_4(string_typeT& text)
{
if (text.length() >= 5 &&
is_suffix(text,/*ement*/LOWER_E, UPPER_E, LOWER_M, UPPER_M, LOWER_E, UPPER_E, LOWER_N, UPPER_N, LOWER_T, UPPER_T) )
{
if (stem<string_typeT>::get_r2() <= text.length()-5)
{
text.erase(text.length()-5);
update_r_sections(text);
}
}
else if (text.length() >= 4 &&
(is_suffix(text,/*able*/LOWER_A, UPPER_A, LOWER_B, UPPER_B, LOWER_L, UPPER_L, LOWER_E, UPPER_E) ||
is_suffix(text,/*ible*/LOWER_I, UPPER_I, LOWER_B, UPPER_B, LOWER_L, UPPER_L, LOWER_E, UPPER_E) ||
is_suffix(text,/*ment*/LOWER_M, UPPER_M, LOWER_E, UPPER_E, LOWER_N, UPPER_N, LOWER_T, UPPER_T) ||
is_suffix(text,/*ence*/LOWER_E, UPPER_E, LOWER_N, UPPER_N, LOWER_C, UPPER_C, LOWER_E, UPPER_E) ||
is_suffix(text,/*ance*/LOWER_A, UPPER_A, LOWER_N, UPPER_N, LOWER_C, UPPER_C, LOWER_E, UPPER_E)) )
{
if (stem<string_typeT>::get_r2() <= text.length()-4)
{
text.erase(text.length()-4);
update_r_sections(text);
}
}
else if (text.length() >= 4 &&
(is_suffix(text,/*sion*/LOWER_S, UPPER_S, LOWER_I, UPPER_I, LOWER_O, UPPER_O, LOWER_N, UPPER_N) ||
is_suffix(text,/*tion*/LOWER_T, UPPER_T, LOWER_I, UPPER_I, LOWER_O, UPPER_O, LOWER_N, UPPER_N)) )
{
if (stem<string_typeT>::get_r2() <= text.length()-3)
{
text.erase(text.length()-3);
update_r_sections(text);
}
}
else if (text.length() >= 3 &&
(is_suffix(text,/*ant*/LOWER_A, UPPER_A, LOWER_N, UPPER_N, LOWER_T, UPPER_T) ||
is_suffix(text,/*ent*/LOWER_E, UPPER_E, LOWER_N, UPPER_N, LOWER_T, UPPER_T) ||
is_suffix(text,/*ism*/LOWER_I, UPPER_I, LOWER_S, UPPER_S, LOWER_M, UPPER_M) ||
is_suffix(text,/*ate*/LOWER_A, UPPER_A, LOWER_T, UPPER_T, LOWER_E, UPPER_E) ||
is_suffix(text,/*iti*/LOWER_I, UPPER_I, LOWER_T, UPPER_T, LOWER_I, UPPER_I) ||
is_suffix(text,/*ous*/LOWER_O, UPPER_O, LOWER_U, UPPER_U, LOWER_S, UPPER_S) ||
is_suffix(text,/*ive*/LOWER_I, UPPER_I, LOWER_V, UPPER_V, LOWER_E, UPPER_E) ||
is_suffix(text,/*ize*/LOWER_I, UPPER_I, LOWER_Z, UPPER_Z, LOWER_E, UPPER_E)) )
{
if (stem<string_typeT>::get_r2() <= text.length()-3)
{
text.erase(text.length()-3);
update_r_sections(text);
}
}
else if (text.length() >= 2 &&
(is_suffix(text,/*al*/LOWER_A, UPPER_A, LOWER_L, UPPER_L) ||
is_suffix(text,/*er*/LOWER_E, UPPER_E, LOWER_R, UPPER_R) ||
is_suffix(text,/*ic*/LOWER_I, UPPER_I, LOWER_C, UPPER_C)) )
{
if (stem<string_typeT>::get_r2() <= text.length()-2)
{
text.erase(text.length()-2);
update_r_sections(text);
}
}
}
/**Search for the the following suffixes, and, if found, perform the action indicated.
-e
-delete if in R2, or in R1 and not preceded by a short syllable
-l
-delete if in R2 and preceded by l*/
//---------------------------------------------
void step_5(string_typeT& text)
{
if (text.length() >= 1 &&
is_either<wchar_t>(text[text.length()-1], LOWER_E, UPPER_E) )
{
if (stem<string_typeT>::get_r2() != text.length())
{
text.erase(text.length()-1);
update_r_sections(text);
}
else if (stem<string_typeT>::get_r1() != text.length() &&
text.length() >= 2 &&
//look at the part of the word in front of the last 'e' to see if it ends with
//a short syllable.
!ends_with_short_syllable(text, text.length()-1))
{
text.erase(text.length()-1);
update_r_sections(text);
}
}
else if (stem<string_typeT>::get_r2() != text.length() &&
is_suffix(text,/*ll*/LOWER_L, UPPER_L, LOWER_L, UPPER_L) )
{
text.erase(text.length()-1);
update_r_sections(text);
}
}
/**Define a short syllable in a word as either
(a) a vowel followed by a non-vowel other than w, x or Y and preceded by a non-vowel, or
(b) a vowel at the beginning of the word followed by a non-vowel.
So rap, trap, entrap end with a short syllable, and ow, on, at are classed as short syllables.
But uproot, bestow, disturb do not end with a short syllable.*/
//---------------------------------------------
bool ends_with_short_syllable(string_typeT& text, const size_t length) const
{
if (length == 2)
{
if (is_vowel(text[0]) )
{
return (!is_vowel(text[1]) );
}
else
{
return false;
}
}
else if (length > 2)
{
size_t start = text.find_last_of(L"aeiouyAEIOUY", length-1);
if (start == string_typeT::npos)
{
return false;
}
if (start > 0 &&
start == (length-2) &&
//following letter
(!is_vowel(text[start+1]) &&
!string_util::is_one_of(text[start+1], L"wxWX") &&
is_neither(text[start+1], LOWER_Y_HASH, UPPER_Y_HASH)) &&
//proceeding letter
!is_vowel(text[start-1]) )
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
///A word is called short if it ends in a short syllable, and if R1 is null.
//---------------------------------------------
inline bool is_short_word(string_typeT& text, const size_t length) const
{
return (ends_with_short_syllable(text, length) && stem<string_typeT>::get_r1() == text.length());
}
//---------------------------------------------
inline bool is_vowel(wchar_t character) const
{
return (string_util::is_one_of(character, L"aeiouyAEIOUY") );
}
size_t m_first_vowel;
};
}
#endif //__ENGLISH_STEM_H__