-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclasses.php
More file actions
2623 lines (2271 loc) · 82.6 KB
/
classes.php
File metadata and controls
2623 lines (2271 loc) · 82.6 KB
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
<?php
// ***********
// STEMMING CLASSES
// ***********
/**
* Implementation of the Porter Stemming algorithm.
*
* Usage:
* $stem = PorterStemmer::Stem($word);
*/
class PorterStemmer
{
/**
* Regex for matching a consonant
* @var string
*/
private static $regex_consonant = '(?:[bcdfghjklmnpqrstvwxz]|(?<=[aeiou])y|^y)';
/**
* Regex for matching a vowel
* @var string
*/
private static $regex_vowel = '(?:[aeiou]|(?<![aeiou])y)';
/**
* Stems a word.
* @param string $word Word to stem
* @return string Stemmed word
*/
public static function stem($word)
{
if (empty($word)) {
return false;
}
$result = '';
$word = strtolower($word);
// Strip punctuation, etc. Keep ' and . for URLs and contractions.
if (substr($word, -2) == "'s") {
$word = substr($word, 0, -2);
}
$word = preg_replace("/[^a-z0-9'.-]/", '', $word);
$first = '';
if (strpos($word, '-') !== false) {
$first = substr($word, 0, strrpos($word, '-') + 1); // Grabs hyphen too
$word = substr($word, strrpos($word, '-') + 1);
}
if (strlen($word) > 2) {
$word = self::step1ab($word);
$word = self::step1c($word);
$word = self::step2($word);
$word = self::step3($word);
$word = self::step4($word);
$word = self::step5($word);
}
$result = $first . $word;
return $result;
}
/**
* Step 1ab
*
* @param string $word Word to stem
* @return string stemmed word after applying step 1(a, b)
*/
private static function step1ab($word)
{
// Part a
if (substr($word, -1) == 's') {
self::replace($word, 'sses', 'ss')
OR self::replace($word, 'ies', 'i')
OR self::replace($word, 'ss', 'ss')
OR self::replace($word, 's', '');
}
// Part b
if (substr($word, -2, 1) != 'e' OR !self::replace($word, 'eed', 'ee', 0)) { // First rule
$v = self::$regex_vowel;
// ing and ed
if (preg_match("#$v+#", substr($word, 0, -3)) && self::replace($word, 'ing', '')
OR preg_match("#$v+#", substr($word, 0, -2)) && self::replace($word, 'ed', '')
) { // Note use of && and OR, for precedence reasons
// If one of above two test successful
if (!self::replace($word, 'at', 'ate')
AND !self::replace($word, 'bl', 'ble')
AND !self::replace($word, 'iz', 'ize')
) {
// Double consonant ending
if (self::doubleConsonant($word)
AND substr($word, -2) != 'll'
AND substr($word, -2) != 'ss'
AND substr($word, -2) != 'zz'
) {
$word = substr($word, 0, -1);
} else if (self::m($word) == 1 AND self::cvc($word)) {
$word .= 'e';
}
}
}
}
return $word;
}
/**
* Step 1c
*
* @param string $word Word to stem
* @return string stemmed word after applying step 1c
*/
private static function step1c($word)
{
$v = self::$regex_vowel;
if (substr($word, -1) == 'y' && preg_match("#$v+#", substr($word, 0, -1))) {
self::replace($word, 'y', 'i');
}
return $word;
}
/**
* Step 2
*
* @param string $word Word to stem
* @return string stemmed word after applying step 2
*/
private static function step2($word)
{
switch (substr($word, -2, 1)) {
case 'a':
self::replace($word, 'ational', 'ate', 0)
OR self::replace($word, 'tional', 'tion', 0);
break;
case 'c':
self::replace($word, 'enci', 'ence', 0)
OR self::replace($word, 'anci', 'ance', 0);
break;
case 'e':
self::replace($word, 'izer', 'ize', 0);
break;
case 'g':
self::replace($word, 'logi', 'log', 0);
break;
case 'l':
self::replace($word, 'entli', 'ent', 0)
OR self::replace($word, 'ousli', 'ous', 0)
OR self::replace($word, 'alli', 'al', 0)
OR self::replace($word, 'bli', 'ble', 0)
OR self::replace($word, 'eli', 'e', 0);
break;
case 'o':
self::replace($word, 'ization', 'ize', 0)
OR self::replace($word, 'ation', 'ate', 0)
OR self::replace($word, 'ator', 'ate', 0);
break;
case 's':
self::replace($word, 'iveness', 'ive', 0)
OR self::replace($word, 'fulness', 'ful', 0)
OR self::replace($word, 'ousness', 'ous', 0)
OR self::replace($word, 'alism', 'al', 0);
break;
case 't':
self::replace($word, 'biliti', 'ble', 0)
OR self::replace($word, 'aliti', 'al', 0)
OR self::replace($word, 'iviti', 'ive', 0);
break;
}
return $word;
}
/**
* Step 3
*
* @param string $word String to stem
* @return string stemmed word after applying step 3
*/
private static function step3($word)
{
switch (substr($word, -2, 1)) {
case 'a':
self::replace($word, 'ical', 'ic', 0);
break;
case 's':
self::replace($word, 'ness', '', 0);
break;
case 't':
self::replace($word, 'icate', 'ic', 0)
OR self::replace($word, 'iciti', 'ic', 0);
break;
case 'u':
self::replace($word, 'ful', '', 0);
break;
case 'v':
self::replace($word, 'ative', '', 0);
break;
case 'z':
self::replace($word, 'alize', 'al', 0);
break;
}
return $word;
}
/**
* Step 4
*
* @param string $word Word to stem
* @return string stemmed word after applying step 4
*/
private static function step4($word)
{
switch (substr($word, -2, 1)) {
case 'a':
self::replace($word, 'al', '', 1);
break;
case 'c':
self::replace($word, 'ance', '', 1)
OR self::replace($word, 'ence', '', 1);
break;
case 'e':
self::replace($word, 'er', '', 1);
break;
case 'i':
self::replace($word, 'ic', '', 1);
break;
case 'l':
self::replace($word, 'able', '', 1)
OR self::replace($word, 'ible', '', 1);
break;
case 'n':
self::replace($word, 'ant', '', 1)
OR self::replace($word, 'ement', '', 1)
OR self::replace($word, 'ment', '', 1)
OR self::replace($word, 'ent', '', 1);
break;
case 'o':
if (substr($word, -4) == 'tion' OR substr($word, -4) == 'sion') {
self::replace($word, 'ion', '', 1);
} else {
self::replace($word, 'ou', '', 1);
}
break;
case 's':
self::replace($word, 'ism', '', 1);
break;
case 't':
self::replace($word, 'ate', '', 1)
OR self::replace($word, 'iti', '', 1);
break;
case 'u':
self::replace($word, 'ous', '', 1);
break;
case 'v':
self::replace($word, 'ive', '', 1);
break;
case 'z':
self::replace($word, 'ize', '', 1);
break;
}
return $word;
}
/**
* Step 5
*
* @param string $word Word to stem
* @return string stemmed word after applying step 5
*/
private static function step5($word)
{
// Part a
if (substr($word, -1) == 'e') {
if (self::m(substr($word, 0, -1)) > 1) {
self::replace($word, 'e', '');
} else if (self::m(substr($word, 0, -1)) == 1) {
if (!self::cvc(substr($word, 0, -1))) {
self::replace($word, 'e', '');
}
}
}
// Part b
if (self::m($word) > 1 AND self::doubleConsonant($word) AND substr($word, -1) == 'l') {
$word = substr($word, 0, -1);
}
return $word;
}
/**
* Replaces the first string with the second, at the end of the string. If third
* arg is given, then the preceding string must match that m count at least.
*
* @param string $str String to check
* @param string $check Ending to check for
* @param string $repl Replacement string
* @param int $m Optional minimum number of m() to meet
* @return bool Whether the $check string was at the end
* of the $str string. True does not necessarily mean
* that it was replaced.
*/
private static function replace(&$str, $check, $repl, $m = null)
{
$len = 0 - strlen($check);
if (substr($str, $len) == $check) {
$substr = substr($str, 0, $len);
if (is_null($m) OR self::m($substr) > $m) {
$str = $substr . $repl;
}
return true;
}
return false;
}
/**
* Measures the number of consonant sequences in $str. if c is
* a consonant sequence and v a vowel sequence, and <..> indicates arbitrary
* presence,
*
* <c><v> gives 0
* <c>vc<v> gives 1
* <c>vcvc<v> gives 2
* <c>vcvcvc<v> gives 3
*
* @param string $str The string to return the m count for
* @return int The m count
*/
private static function m($str)
{
$c = self::$regex_consonant;
$v = self::$regex_vowel;
$str = preg_replace("#^$c+#", '', $str);
$str = preg_replace("#$v+$#", '', $str);
preg_match_all("#($v+$c+)#", $str, $matches);
return count($matches[1]);
}
/**
* Returns true/false as to whether the given string contains two
* of the same consonant next to each other at the end of the string.
*
* @param string $str String to check
* @return bool Result
*/
private static function doubleConsonant($str)
{
$c = self::$regex_consonant;
return preg_match("#$c{2}$#", $str, $matches) AND $matches[0]{0} == $matches[0]{1};
}
/**
* Checks for ending CVC sequence where second C is not W, X or Y
*
* @param string $str String to check
* @return bool Result
*/
private static function cvc($str)
{
$c = self::$regex_consonant;
$v = self::$regex_vowel;
return preg_match("#($c$v$c)$#", $str, $matches)
AND strlen($matches[1]) == 3
AND $matches[1]{2} != 'w'
AND $matches[1]{2} != 'x'
AND $matches[1]{2} != 'y';
}
}
/**
* Implementation of the Porter2 Stemming Algorithm.
*
* Usage:
* $stem = Porter2::stem($word);
*/
class Porter2Stemmer
{
/**
* Computes the stem of the word.
*
* @param string $word word to be stemmed
* @return string
* The word's stem.
*/
public static function stem($word)
{
$exceptions = 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',
);
// Process exceptions.
if (isset($exceptions[$word])) {
$word = $exceptions[$word];
} elseif (strlen($word) > 2) {
// Only execute algorithm on words that are longer than two letters.
$word = self::prepare($word);
$word = self::step0($word);
$word = self::step1a($word);
$word = self::step1b($word);
$word = self::step1c($word);
$word = self::step2($word);
$word = self::step3($word);
$word = self::step4($word);
$word = self::step5($word);
}
return strtolower($word);
}
/**
* Set initial y, or y after a vowel, to Y.
*
* @param string $word
* The word to be stemmed.
*
* @return string $word
* The prepared word.
*/
private static function prepare($word)
{
$inc = 0;
if (strpos($word, "'") === 0) {
$word = substr($word, 1);
}
while ($inc <= strlen($word)) {
if (substr($word, $inc, 1) === 'y' && ($inc == 0 || self::isVowel($inc - 1, $word))) {
$word = substr_replace($word, 'Y', $inc, 1);
}
$inc++;
}
return $word;
}
/**
* Search for the longest among the "s" suffixes and removes it.
*
* @param string $word
* The word to stemmed
*
* @return string $word
* The modified word.
*/
private static function step0($word)
{
$found = FALSE;
$checks = array("'s'", "'s", "'");
foreach ($checks as $check) {
if (!$found && self::hasEnding($word, $check)) {
$word = self::removeEnding($word, $check);
$found = TRUE;
}
}
return $word;
}
/**
* Handles various suffixes, of which the longest is replaced.
*
* @param string $word
* The word to be stemmed
*
* @return string $word
* The modified word.
*/
private static function step1a($word)
{
$found = FALSE;
if (self::hasEnding($word, 'sses')) {
$word = self::removeEnding($word, 'sses') . 'ss';
$found = TRUE;
}
$checks = array('ied', 'ies');
foreach ($checks as $check) {
if (!$found && self::hasEnding($word, $check)) {
// @todo: check order here.
$length = strlen($word);
$word = self::removeEnding($word, $check);
if ($length > 4) {
$word .= 'i';
} else {
$word .= 'ie';
}
$found = TRUE;
}
}
if (self::hasEnding($word, 'us') || self::hasEnding($word, 'ss')) {
$found = TRUE;
}
// Delete if preceding word part has a vowel not immediately before the s.
if (!$found && self::hasEnding($word, 's') && self::containsVowel(substr($word, 0, -2))) {
$word = self::removeEnding($word, 's');
}
return $word;
}
/**
* Handles various suffixes, of which the longest is replaced.
*
* @param string $word
* The word to be stemmed
*
* @return string $word
* The modified word.
*/
private static function step1b($word)
{
$exceptions = array(
'inning', 'outing', 'canning',
'herring', 'earring', 'proceed', 'exceed', 'succeed',
);
if (in_array($word, $exceptions)) {
return $word;
}
$checks = array('eedly', 'eed');
foreach ($checks as $check) {
if (self::hasEnding($word, $check)) {
if (self::r($word, 1) !== strlen($word)) {
$word = self::removeEnding($word, $check) . 'ee';
}
return $word;
}
}
$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 (self::hasEnding($word, $check) && self::containsVowel(substr($word, 0, -strlen($check)))) {
$word = self::removeEnding($word, $check);
foreach ($second_endings as $ending) {
if (self::hasEnding($word, $ending)) {
return $word . 'e';
}
}
// If the word ends with a double, remove the last letter.
$double_removed = self::removeDoubles($word);
if ($double_removed != $word) {
$word = $double_removed;
} elseif (self::isShort($word)) {
// If the word is short, add e (so hop -> hope).
$word .= 'e';
}
return $word;
}
}
return $word;
}
/**
* Replaces suffix y or Y with i if after non-vowel not @ word begin.
*
* @param string $word
* The word to be stemmed
*
* @return string $word
* The modified word.
*/
private static function step1c($word)
{
if ((self::hasEnding($word, 'y') || self::hasEnding($word, 'Y')) && strlen($word) > 2 && !(self::isVowel(strlen($word) - 2, $word))) {
$word = self::removeEnding($word, 'y');
$word .= 'i';
}
return $word;
}
/**
* Implements step 2 of the Porter2 algorithm.
*
* @param string $word
* The word to be stemmed
*
* @return string $word
* The modified word.
*/
private static function step2($word)
{
$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 (self::hasEnding($word, $find)) {
if (self::inR1($word, $find)) {
$word = self::removeEnding($word, $find) . $replace;
}
return $word;
}
}
if (self::hasEnding($word, 'li')) {
if (strlen($word) > 4 && self::validLi(self::charAt(-3, $word))) {
$word = self::removeEnding($word, 'li');
}
}
return $word;
}
/**
* Implements step 3 of the Porter2 algorithm.
*
* @param string $word
* The word to be stemmed
*
* @return string $word
* The modified word.
*/
private static function step3($word)
{
$checks = array(
'ational' => 'ate', 'tional' => 'tion', 'alize' => 'al',
'icate' => 'ic', 'iciti' => 'ic', 'ical' => 'ic',
'ness' => '', 'ful' => '',
);
foreach ($checks as $find => $replace) {
if (self::hasEnding($word, $find)) {
if (self::inR1($word, $find)) {
$word = self::removeEnding($word, $find) . $replace;
}
return $word;
}
}
if (self::hasEnding($word, 'ative')) {
if (self::inR2($word, 'ative')) {
$word = self::removeEnding($word, 'ative');
}
}
return $word;
}
/**
* Implements step 4 of the Porter2 algorithm.
*
* @param string $word
* The word to be stemmed
*
* @return string $word
* The modified word.
*/
private static function step4($word)
{
$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 (self::hasEnding($word, $check)) {
if (self::inR2($word, $check)) {
if ($check !== 'ion' || in_array(self::charAt(-4, $word), array('s', 't'))) {
$word = self::removeEnding($word, $check);
}
}
return $word;
}
}
return $word;
}
/**
* Implements step 5 of the Porter2 algorithm.
*
* @param string $word
* The word to be stemmed
*
* @return string $word
* The modified word.
*/
private static function step5($word)
{
if (self::hasEnding($word, 'e')) {
// Delete if in R2, or in R1 and not preceded by a short syllable.
if (self::inR2($word, 'e') || (self::inR1($word, 'e') && !self::isShortSyllable($word, strlen($word) - 3))) {
$word = self::removeEnding($word, 'e');
}
return $word;
}
if (self::hasEnding($word, 'l')) {
// Delete if in R2 and preceded by l.
if (self::inR2($word, 'l') && self::charAt(-2, $word) == 'l') {
$word = self::removeEnding($word, 'l');
}
}
return $word;
}
/**
* Removes certain double consonants from the word's end.
*
* @param string $word
* The word to be stemmed
*
* @return string $word
* The modified word.
*/
private static function removeDoubles($word)
{
$doubles = array('bb', 'dd', 'ff', 'gg', 'mm', 'nn', 'pp', 'rr', 'tt');
foreach ($doubles as $double) {
if (substr($word, -2) == $double) {
$word = substr($word, 0, -1);
break;
}
}
return $word;
}
/**
* Checks whether a character is a vowel.
*
* @param int $position
* The character's position.
* @param string $word
* The word in which to check.
* @param string[] $additional
* (optional) Additional characters that should count as vowels.
*
* @return bool
* TRUE if the character is a vowel, FALSE otherwise.
*/
private static function isVowel($position, $word, array $additional = array())
{
$vowels = array_merge(array('a', 'e', 'i', 'o', 'u', 'y'), $additional);
return in_array(self::charAt($position, $word), $vowels);
}
/**
* Retrieves the character at the given position.
*
* @param int $position
* The 0-based index of the character. If a negative number is given, the
* position is counted from the end of the string.
* @param string $word
* The word from which to retrieve the character.
*
* @return string
* The character at the given position, or an empty string if the given
* position was illegal.
*/
private static function charAt($position, $word)
{
$length = strlen($word);
if (abs($position) >= $length) {
return '';
}
if ($position < 0) {
$position += $length;
}
return $word[$position];
}
/**
* Determines whether the word ends in a "vowel-consonant" suffix.
*
* Unless the word is only two characters long, it also checks that the
* third-last character is neither "w", "x" nor "Y".
*
* @param string $word
* The word in which to check.
* @param int|null $position
* (optional) If given, do not check the end of the word, but the character
* at the given position, and the next one.
*
* @return bool
* TRUE if the word has the described suffix, FALSE otherwise.
*/
private static function isShortSyllable($word, $position = NULL)
{
if ($position === NULL) {
$position = strlen($word) - 2;
}
// A vowel at the beginning of the word followed by a non-vowel.
if ($position === 0) {
return self::isVowel(0, $word) && !self::isVowel(1, $word);
}
// Vowel followed by non-vowel other than w, x, Y and preceded by
// non-vowel.
$additional = array('w', 'x', 'Y');
return !self::isVowel($position - 1, $word) && self::isVowel($position, $word) && !self::isVowel($position + 1, $word, $additional);
}
/**
* Determines whether the word is short.
* A word is called short if it ends in a short syllable and if R1 is null.
*
* @param string $word
* The word in which to check.
* @return bool
* TRUE if the word is short, FALSE otherwise.
*/
private static function isShort($word)
{
return self::isShortSyllable($word) && self::r($word, 1) == strlen($word);
}
/**
* Determines the start of a certain "R" region.
*
* R is a region after the first non-vowel following a vowel, or end of word.
*
* @param string $word
* The word in which to check.
* @param int $type
* (optional) 1 or 2. If 2, then calculate the R after the R1.
*
* @return int
* The R position.
*/
private static function r($word, $type = 1)
{
$inc = 1;
if ($type === 2) {
$inc = self::r($word, 1);
} elseif (strlen($word) > 5) {
$prefix_5 = substr($word, 0, 5);
if ($prefix_5 === 'gener' || $prefix_5 === 'arsen') {
return 5;
}
if (strlen($word) > 5 && substr($word, 0, 6) === 'commun') {
return 6;
}
}
while ($inc <= strlen($word)) {
if (!self::isVowel($inc, $word) && self::isVowel($inc - 1, $word)) {
$position = $inc;
break;
}
$inc++;
}
if (!isset($position)) {
$position = strlen($word);
} else {
// We add one, as this is the position AFTER the first non-vowel.
$position++;
}
return $position;
}
/**
* Checks whether the given string is contained in R1.
*
* @param string $word
* The word in which to check.
* @param string $string
* The string.
*
* @return bool
* TRUE if the string is in R1, FALSE otherwise.
*/
private static function inR1($word, $string)
{
$r1 = substr($word, self::r($word, 1));
return strpos($r1, $string) !== FALSE;
}
/**
* Checks whether the given string is contained in R2.
*
* @param string $word
* The word in which to check.
* @param string $string
* The string.
*
* @return bool
* TRUE if the string is in R2, FALSE otherwise.
*/
private static function inR2($word, $string)
{
$r2 = substr($word, self::r($word, 2));
return strpos($r2, $string) !== FALSE;
}
/**
* Checks whether the word ends with the given string.
*
* @param string $word
* The word in which to check.
* @param string $string
* The string.
*
* @return bool
* TRUE if the word ends with the given string, FALSE otherwise.
*/
private static function hasEnding($word, $string)
{
$length = strlen($string);
if ($length > strlen($word)) {
return FALSE;
}
return (substr_compare($word, $string, -1 * $length, $length) === 0);
}
/**
* Removes a given string from the end of the current word.
*
* Does not check whether the ending is actually there.
*
* @param string $word
* The word in which to check.
* @param string $string
* The ending to remove.
* @return string $word after removing ending
*/
private static function removeEnding($word, $string)
{
return substr($word, 0, -strlen($string));
}
/**
* Checks whether the given string contains a vowel.
*
* @param string $string
* The string to check.
*
* @return bool
* TRUE if the string contains a vowel, FALSE otherwise.
*/
private static function containsVowel($string)
{
$inc = 0;
$return = FALSE;
while ($inc < strlen($string)) {
if (self::isVowel($inc, $string)) {
$return = TRUE;
break;
}
$inc++;
}
return $return;
}
/**
* Checks whether the given string is a valid -li prefix.
*
* @param string $string
* The string to check.
*
* @return bool