-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPorter2.php
524 lines (491 loc) · 12.9 KB
/
Porter2.php
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
<?php
/**
* @file
* PHP Implementation of the Porter2 Stemming Algorithm.
*/
/**
* PHP Implementation of the Porter2 Stemming Algorithm.
*
* See http://snowball.tartarus.org/algorithms/english/stemmer.html .
*/
class porter2 {
private $word = '';
/**
* The R1 of the word: http://snowball.tartarus.org/texts/r1r2.html.
*/
private $r1;
/**
* The R2 of the word: http://snowball.tartarus.org/texts/r1r2.html.
*/
private $r2;
/**
* Allow a key => value array of exceptions to be passed to exceptions().
*/
public $custom_exceptions = array();
/**
* Prepare the word form for processing.
*/
public function __construct($word) {
$this->word = $word;
// Remove initial ', if present.
if ($this->length() > 2 && substr($this->word, 0, 1) === "'") {
$this->word = substr($this->word, 1, $this->length() - 1);
}
// Set initial y, or y after a vowel, to Y.
$inc = 0;
while ($inc <= $this->length()) {
if (substr($this->word, $inc, 1) === 'y' && ($inc == 0 || $this->isVowel($inc - 1))) {
$this->word = substr_replace($this->word, 'Y', $inc, 1);
}
$inc++;
}
// Establish the regions R1 and R2. See function R().
$this->r1 = $this->R(1);
$this->r2 = $this->R(2);
}
/**
* The only public-facing method. Call ->stem(WORD) to return stemmed word.
*/
public function stem() {
// Ignore exceptions & words that are two letters or less.
if ($this->exceptions() || $this->length() <= 2) {
return strtolower($this->word);
}
else {
$this->step0();
$this->step1a();
$this->step1b();
$this->step1c();
$this->step2();
$this->step3();
$this->step4();
$this->step5();
}
return strtolower($this->word);
}
/**
* Is the word in a given list of exceptions? If so, replace and end process.
*/
private function exceptions() {
$checks = array(
'skis' => 'ski',
'skies' => 'sky',
'dying' => 'die',
'lying' => 'lie',
'tying' => 'tie',
'idly' => 'idl',
'gently' => 'gentl',
'ugly' => 'ugli',
'early' => 'earli',
'only' => 'onli',
'singly' => 'singl',
'sky' => 'sky',
'news' => 'news',
'howe' => 'howe',
'atlas' => 'atlas',
'cosmos' => 'cosmos',
'bias' => 'bias',
'andes' => 'andes',
);
$checks = array_merge($checks, $this->custom_exceptions);
if (in_array($this->word, array_keys($checks))) {
foreach ($checks as $find => $replace) {
if ($find === $this->word) {
$this->word = $replace;
break;
}
}
return TRUE;
}
return FALSE;
}
/**
* Step 0: Search for the longest among the "s" suffixes & remove.
*/
private function step0() {
$found = FALSE;
$checks = array("'s'", "'s", "'");
foreach ($checks as $check) {
if (!$found && $this->hasEnding($check)) {
$this->removeEnding($check);
$found = TRUE;
}
}
}
/**
* Step 1a: Search for the longest among the following suffixes.
*/
private function step1a() {
$found = FALSE;
if ($this->hasEnding('sses')) {
$this->removeEnding('sses');
$this->addEnding('ss');
$found = TRUE;
}
$checks = array('ied', 'ies');
foreach ($checks as $check) {
if (!$found && $this->hasEnding($check)) {
if ($this->length() > 4) {
$this->removeEnding($check);
$this->addEnding('i');
}
else {
$this->removeEnding($check);
$this->addEnding('ie');
}
$found = TRUE;
}
}
if ($this->hasEnding('us') || $this->hasEnding('ss')) {
$found = TRUE;
}
// Delete if preceding word part has a vowel not immediately before the s.
if (!$found && $this->hasEnding('s') && $this->containsVowel(substr($this->word, 0, -2))) {
$this->removeEnding('s');
}
}
/**
* Step 1b.
*/
private function step1b() {
$exceptions = array(
'inning', 'outing', 'canning', 'herring', 'earring', 'proceed', 'exceed', 'succeed',
);
if (in_array($this->word, $exceptions)) {
return;
}
$checks = array('eedly', 'eed');
foreach ($checks as $check) {
if ($this->hasEnding($check)) {
if ($this->r1 !== $this->length()) {
$this->removeEnding($check);
$this->addEnding('ee');
}
return;
}
}
$checks = array('ingly', 'edly', 'ing', 'ed');
$second_endings = array('at', 'bl', 'iz');
foreach ($checks as $check) {
// If the ending is present and the previous part contains a vowel.
if ($this->hasEnding($check) && $this->containsVowel(substr($this->word, 0, $this->length() - strlen($check)))) {
$this->removeEnding($check);
foreach ($second_endings as $ending) {
if ($this->hasEnding($ending)) {
$this->addEnding('e');
return;
}
}
// If the word ends with a double, remove the last letter.
$found = $this->removeDoubles();
// If the word is short, add e (so hop -> hope).
if (!$found && ($this->isShort())) {
$this->addEnding('e');
}
return;
}
}
}
/**
* Step 1c. Replace suffix y or Y with i if after non-vowel not @ word begin.
*/
private function step1c() {
if (($this->hasEnding('y') || $this->hasEnding('Y')) && $this->length() > 2 && !($this->isVowel($this->length() - 2))) {
$this->removeEnding('y');
$this->removeEnding('Y');
$this->addEnding('i');
}
}
/**
* Step 2.
*/
private function step2() {
$checks = array(
"ization" => "ize",
"iveness" => "ive",
"fulness" => "ful",
"ational" => "ate",
"ousness" => "ous",
"biliti" => "ble",
"tional" => "tion",
"lessli" => "less",
"fulli" => "ful",
"entli" => "ent",
"ation" => "ate",
"aliti" => "al",
"iviti" => "ive",
"ousli" => "ous",
"alism" => "al",
"abli" => "able",
"anci" => "ance",
"alli" => "al",
"izer" => "ize",
"enci" => "ence",
"ator" => "ate",
"bli" => "ble",
"ogi" => "og",
);
foreach ($checks as $find => $replace) {
if ($this->hasEnding($find)) {
if ($this->inR1($find)) {
$this->removeEnding($find);
$this->addEnding($replace);
}
return;
}
}
if ($this->hasEnding('li')) {
if ($this->validli(substr($this->word, -3, 1)) && $this->length() > 4) {
$this->removeEnding('li');
}
}
}
/**
* Step 3.
*/
private function step3() {
$checks = array(
'ational' => 'ate',
'tional' => 'tion',
'alize' => 'al',
'icate' => 'ic',
'iciti' => 'ic',
'ical' => 'ic',
'ness' => '',
'ful' => '',
);
foreach ($checks as $find => $replace) {
if ($this->hasEnding($find)) {
if ($this->inR1($find)) {
$this->removeEnding($find);
$this->addEnding($replace);
}
return;
}
}
if ($this->hasEnding('ative')) {
if ($this->inR2('ative')) {
$this->removeEnding('ative');
}
}
}
/**
* Step 4.
*/
private function step4() {
$checks = array(
'ement',
'ment',
'ance',
'ence',
'able',
'ible',
'ant',
'ent',
'ion',
'ism',
'ate',
'iti',
'ous',
'ive',
'ize',
'al',
'er',
'ic',
);
foreach ($checks as $check) {
// Among the suffixes, if found and in R2, delete.
if ($this->hasEnding($check)) {
if ($check == 'ion') {
if ($this->inR2('ion') && in_array(substr($this->word, -4, 1), array('s', 't'))) {
$this->removeEnding('ion');
}
}
elseif ($this->inR2($check)) {
$this->removeEnding($check);
}
return;
}
}
}
/**
* Step 5.
*/
private function step5() {
if ($this->hasEnding('e')) {
// Delete if in R2, or in R1 and not preceded by a short syllable.
if ($this->inR2('e') || ($this->inR1('e') && !$this->isShortSyllable($this->length() - 3))) {
$this->removeEnding('e');
}
return;
}
if ($this->hasEnding('l')) {
// Delete if in R2 and preceded by l.
if ($this->inR2('l') && substr($this->word, -2, 1) == 'l') {
$this->removeEnding('l');
}
}
}
/**
* Remove doubles from the following list.
*/
private function removeDoubles() {
$found = FALSE;
$doubles = array('bb', 'dd', 'ff', 'gg', 'mm', 'nn', 'pp', 'rr', 'tt');
foreach ($doubles as $double) {
if (substr($this->word, -2) == $double) {
$this->word = substr($this->word, 0, $this->length() - 2) . substr($double, 1);
$found = TRUE;
break;
}
}
return $found;
}
/**
* Is the letter indicated by the string position a vowel?
*/
private function isVowel($position, $word = NULL, $additional = array()) {
if ($word == NULL) {
$word = $this->word;
}
$vowels = array_merge(array('a', 'e', 'i', 'o', 'u', 'y'), $additional);
if (in_array(substr($word, $position, 1), $vowels)) {
return TRUE;
}
return FALSE;
}
/**
* From a given ending $position in a word, does it end -vowel-consonant?
*
* If this position is not provided, assume end of word, and
* additionally check that the last letter is not w, x, or Y.
*/
private function isShortSyllable($position = NULL) {
if ($position === NULL) {
$position = $this->length() - 2;
}
// A vowel at the beginning of the word followed by a non-vowel.
if ($position === 0) {
if ($this->isVowel(0) && !$this->isVowel(1)) {
return TRUE;
}
}
else {
// Vowel followed by non-vowel other than w, x, Y & preceded by non-vowel.
$additional = array('w', 'x', 'Y');
return (!$this->isVowel($position - 1) && $this->isVowel($position) && !$this->isVowel($position + 1, NULL, $additional));
}
}
/**
* A word is called short if it ends in a short syllable and if R1 is null.
*/
private function isShort() {
return $this->isShortSyllable() && $this->r1 == $this->length();
}
/**
* R is a region after the first non-vowel following a vowel, or end of word.
*
* @param int $type
* 1 or 2. If 2, then calculate the R after the R1.
*/
private function R($type = 1) {
$inc = 1;
if ($type === 2) {
$inc = $this->r1;
}
else {
if ($this->length() > 5 && (strcmp('gener', substr($this->word, 0, 5)) === 0)) {
return 5;
}
if ($this->length() > 5 && (strcmp('arsen', substr($this->word, 0, 5)) === 0)) {
return 5;
}
if ($this->length() > 6 && (strcmp('commun', substr($this->word, 0, 6)) === 0)) {
return 6;
}
}
while ($inc <= $this->length()) {
if (!$this->isVowel($inc) && $this->isVowel($inc - 1)) {
$position = $inc;
break;
}
$inc++;
}
if (!isset($position)) {
$position = $this->length();
}
else {
// We add one, as this is the position AFTER the first non-vowel.
$position++;
}
return $position;
}
/**
* Is the given string in R1?
*/
private function inR1($string = '') {
$r1 = substr($this->word, $this->r1);
return strpos($r1, $string) !== FALSE;
}
/**
* Is the given string in R2?
*/
private function inR2($string = '') {
$r2 = substr($this->word, $this->r2);
return strpos($r2, $string) !== FALSE;
}
/**
* The string length of the current word.
*/
private function length() {
return strlen($this->word);
}
/**
* Does the word end with a given string?
*/
private function hasEnding($string) {
$length = strlen($string);
if ($length > $this->length()) {
return FALSE;
}
return (substr_compare($this->word, $string, -1 * $length, $length) === 0);
}
/**
* Append a given string to the current word state.
*/
private function addEnding($string = '') {
$this->word = $this->word . $string;
}
/**
* Remove a given string from the current word state.
*/
private function removeEnding($string = '') {
$length = strlen($string);
if (substr($this->word, -1 * $length, $length) == $string) {
$this->word = substr($this->word, 0, $this->length() - strlen($string));
}
}
/**
* Does a given string contain a vowel?
*/
private function containsVowel($string) {
$inc = 0;
$return = FALSE;
while ($inc < strlen($string)) {
if ($this->isVowel($inc, $string)) {
$return = TRUE;
break;
}
$inc++;
}
return $return;
}
/**
* Is a given string in the array of valid -li prefixes?
*/
private function validli($string = '') {
return in_array($string, array(
'c', 'd', 'e', 'g', 'h', 'k', 'm', 'n', 'r', 't',
));
}
}