-
Notifications
You must be signed in to change notification settings - Fork 7
/
Lingua_Stem_Ru.class.php
128 lines (105 loc) · 3.35 KB
/
Lingua_Stem_Ru.class.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
<?php
/**
* @file
* The class from
* This class was initially get from http://forum.dklab.ru/php/advises/HeuristicWithoutTheDictionaryExtractionOfARootFromRussianWord.html
*
* @author andyceo http://andyceo.ruware.com/
*/
class Lingua_Stem_Ru {
const VERSION = "0.01";
const VOWEL = '/аеиоуыэюя/';
const PERFECTIVEGROUND = '/((ив|ивши|ившись|ыв|ывши|ывшись)|((?<=[ая])(в|вши|вшись)))$/';
const REFLEXIVE = '/(с[яь])$/';
const ADJECTIVE = '/(ее|ие|ые|ое|ими|ыми|ей|ий|их|ый|ой|ем|им|ым|ом|ому|его|ого|еых|ую|юю|ая|яя|ою|ею)$/';
const PARTICIPLE = '/((ивш|ывш|ующ)|((?<=[ая])(ем|нн|вш|ющ|щ)))$/';
const VERB = '/((ила|ыла|ена|ейте|уйте|ите|или|ыли|ей|уй|ил|ыл|им|ым|ены|ить|ыть|ишь|ую|ю)|((?<=[ая])(ла|на|ете|йте|ли|й|л|ем|н|ло|но|ет|ют|ны|ть|ешь|нно)))$/';
const NOUN = '/(а|ев|ов|ие|ье|е|иями|ями|у|ами|еи|ии|и|ией|ей|ой|ий|й|и|ы|ь|ию|ью|ю|ия|ья|я)$/';
const RVRE = '/^(.*?[аеиоуыэюя])(.*)$/';
const DERIVATIONAL = '/[^аеиоуыэюя][аеиоуыэюя]+[^аеиоуыэюя]+[аеиоуыэюя].*(?<=о)сть?$/';
private $Stem_Caching = 0;
private $Stem_Cache = array();
function __construct() {
}
function __destruct() {
}
/**
*
*/
private function stem_caching($parm_ref) {
$caching_level = @$parm_ref['-level'];
if ($caching_level) {
if (!$this->m($caching_level, '/^[012]$/')) {
die(__CLASS__ . "::stem_caching() - Legal values are '0','1' or '2'. '$caching_level' is not a legal value");
}
$this->Stem_Caching = $caching_level;
}
return $this->Stem_Caching;
}
/**
*
*/
private function clear_stem_cache() {
$this->Stem_Cache = array();
}
/**
*
*/
private function s(&$s, $re, $to) {
$orig = $s;
$s = preg_replace($re, $to, $s);
return $orig !== $s;
}
/**
*
*/
private function m($s, $re) {
return preg_match($re, $s);
}
/**
*
*/
public function stem_word($word) {
$word = mb_strtolower($word, 'UTF-8');
$word = str_replace('ё', 'е', $word);
// Check against cache of stemmed words
if ($this->Stem_Caching && isset($this->Stem_Cache[$word])) {
return $this->Stem_Cache[$word];
}
$stem = $word;
do {
if (!preg_match(self::RVRE, $word, $p)) break;
$start = $p[1];
$RV = $p[2];
if (!$RV) break;
// Step 1
if (!$this->s($RV, self::PERFECTIVEGROUND, '')) {
$this->s($RV, self::REFLEXIVE, '');
if ($this->s($RV, self::ADJECTIVE, '')) {
$this->s($RV, self::PARTICIPLE, '');
}
else {
if (!$this->s($RV, self::VERB, '')) {
$this->s($RV, self::NOUN, '');
}
}
}
// Step 2
$this->s($RV, '/и$/', '');
// Step 3
if ($this->m($RV, self::DERIVATIONAL)) {
$this->s($RV, '/ость?$/', '');
}
// Step 4
if (!$this->s($RV, '/ь$/', '')) {
$this->s($RV, '/ейше?/', '');
$this->s($RV, '/нн$/', 'н');
}
$stem = $start.$RV;
} while(false);
if ($this->Stem_Caching) {
$this->Stem_Cache[$word] = $stem;
}
return $stem;
}
}