Romanian.php
12.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
<?php
namespace SearchWP\Dependencies\Wamania\Snowball;
/**
*
* @link http://snowball.tartarus.org/algorithms/romanian/stemmer.html
* @author wamania
*
*/
class Romanian extends Stem
{
/**
* All Romanian vowels
*/
protected static $vowels = array('a', 'ă', 'â', 'e', 'i', 'î', 'o', 'u');
/**
* {@inheritdoc}
*/
public function stem($word)
{
// we do ALL in UTF-8
if (!Utf8::check($word)) {
throw new \Exception('Word must be in UTF-8');
}
$this->word = Utf8::strtolower($word);
$this->plainVowels = \implode('', self::$vowels);
// First, i and u between vowels are put into upper case (so that they are treated as consonants).
$this->word = \preg_replace('#([' . $this->plainVowels . '])u([' . $this->plainVowels . '])#u', '$1U$2', $this->word);
$this->word = \preg_replace('#([' . $this->plainVowels . '])i([' . $this->plainVowels . '])#u', '$1I$2', $this->word);
$this->rv();
$this->r1();
$this->r2();
$this->step0();
$word1 = $this->word;
$word2 = $this->word;
do {
$word1 = $this->word;
$this->step1();
} while ($this->word != $word1);
$this->step2();
// Do step 3 if no suffix was removed either by step 1 or step 2.
if ($word2 == $this->word) {
$this->step3();
}
$this->step4();
$this->finish();
return $this->word;
}
/**
* Step 0: Removal of plurals (and other simplifications)
* Search for the longest among the following suffixes, and, if it is in R1, perform the action indicated.
* @return boolean
*/
public function step0()
{
// ul ului
// delete
if (($position = $this->search(array('ul', 'ului'))) !== \false) {
if ($this->inR1($position)) {
$this->word = Utf8::substr($this->word, 0, $position);
}
return \true;
}
// aua
// replace with a
if (($position = $this->search(array('aua'))) !== \false) {
if ($this->inR1($position)) {
$this->word = \preg_replace('#(aua)$#u', 'a', $this->word);
}
return \true;
}
// ea ele elor
// replace with e
if (($position = $this->search(array('ea', 'ele', 'elor'))) !== \false) {
if ($this->inR1($position)) {
$this->word = \preg_replace('#(ea|ele|elor)$#u', 'e', $this->word);
}
return \true;
}
// ii iua iei iile iilor ilor
// replace with i
if (($position = $this->search(array('ii', 'iua', 'iei', 'iile', 'iilor', 'ilor'))) !== \false) {
if ($this->inR1($position)) {
$this->word = \preg_replace('#(ii|iua|iei|iile|iilor|ilor)$#u', 'i', $this->word);
}
return \true;
}
// ile
// replace with i if not preceded by ab
if (($position = $this->search(array('ile'))) !== \false) {
if ($this->inR1($position)) {
$before = Utf8::substr($this->word, $position - 2, 2);
if ($before != 'ab') {
$this->word = \preg_replace('#(ile)$#u', 'i', $this->word);
}
}
return \true;
}
// atei
// replace with at
if (($position = $this->search(array('atei'))) != \false) {
if ($this->inR1($position)) {
$this->word = \preg_replace('#(atei)$#u', 'at', $this->word);
}
return \true;
}
// aţie aţia
// replace with aţi
if (($position = $this->search(array('aţie', 'aţia'))) !== \false) {
if ($this->inR1($position)) {
$this->word = \preg_replace('#(aţie|aţia)$#u', 'aţi', $this->word);
}
return \true;
}
return \false;
}
/**
* Step 1: Reduction of combining suffixes
* Search for the longest among the following suffixes, and, if it is in R1, preform the replacement action indicated.
* Then repeat this step until no replacement occurs.
* @return boolean
*/
public function step1()
{
// abilitate abilitati abilităi abilităţi
// replace with abil
if (($position = $this->search(array('abilitate', 'abilitati', 'abilităi', 'abilităţi'))) !== \false) {
if ($this->inR1($position)) {
$this->word = \preg_replace('#(abilitate|abilitati|abilităi|abilităţi)$#u', 'abil', $this->word);
}
return \true;
}
// ibilitate
// replace with ibil
if (($position = $this->search(array('ibilitate'))) !== \false) {
if ($this->inR1($position)) {
$this->word = \preg_replace('#(ibilitate)$#u', 'ibil', $this->word);
}
return \true;
}
// ivitate ivitati ivităi ivităţi
// replace with iv
if (($position = $this->search(array('ivitate', 'ivitati', 'ivităi', 'ivităţi'))) !== \false) {
if ($this->inR1($position)) {
$this->word = \preg_replace('#(ivitate|ivitati|ivităi|ivităţi)$#u', 'iv', $this->word);
}
return \true;
}
// icitate icitati icităi icităţi icator icatori iciv iciva icive icivi icivă ical icala icale icali icală
// replace with ic
if (($position = $this->search(array('icitate', 'icitati', 'icităi', 'icităţi', 'icatori', 'icator', 'iciva', 'icive', 'icivi', 'icivă', 'icala', 'icale', 'icali', 'icală', 'iciv', 'ical'))) !== \false) {
if ($this->inR1($position)) {
$this->word = \preg_replace('#(icitate|icitati|icităi|icităţi|cator|icatori|iciva|icive|icivi|icivă|icala|icale|icali|icală|ical|iciv)$#u', 'ic', $this->word);
}
return \true;
}
// ativ ativa ative ativi ativă aţiune atoare ator atori ătoare ător ători
// replace with at
if (($position = $this->search(array('ativa', 'ative', 'ativi', 'ativă', 'ativ', 'aţiune', 'atoare', 'atori', 'ătoare', 'ători', 'ător', 'ator'))) !== \false) {
if ($this->inR1($position)) {
$this->word = \preg_replace('#(ativa|ative|ativi|ativă|ativ|aţiune|atoare|atori|ătoare|ători|ător|ator)$#u', 'at', $this->word);
}
return \true;
}
// itiv itiva itive itivi itivă iţiune itoare itor itori
// replace with it
if (($position = $this->search(array('itiva', 'itive', 'itivi', 'itivă', 'itiv', 'iţiune', 'itoare', 'itori', 'itor'))) !== \false) {
if ($this->inR1($position)) {
$this->word = \preg_replace('#(itiva|itive|itivi|itivă|itiv|iţiune|itoare|itori|itor)$#u', 'it', $this->word);
}
return \true;
}
return \false;
}
/**
* Step 2: Removal of 'standard' suffixes
* Search for the longest among the following suffixes, and, if it is in R2, perform the action indicated.
* @return boolean
*/
public function step2()
{
// atori itate itati, ităţi, abila abile abili abilă, ibila ibile ibili ibilă
// anta, ante, anti, antă, ator, ibil, oasa oasă oase, ităi, abil
// osi oşi ant ici ică iva ive ivi ivă ata ată ati ate, ata ată ati ate uta ută uti ute, ita ită iti ite ica ice
// at, os, iv, ut, it, ic
// delete
if (($position = $this->search(array('atori', 'itate', 'itati', 'ităţi', 'abila', 'abile', 'abili', 'abilă', 'ibila', 'ibile', 'ibili', 'ibilă', 'anta', 'ante', 'anti', 'antă', 'ator', 'ibil', 'oasa', 'oasă', 'oase', 'ităi', 'abil', 'osi', 'oşi', 'ant', 'ici', 'ică', 'iva', 'ive', 'ivi', 'ivă', 'ata', 'ată', 'ati', 'ate', 'ata', 'ată', 'ati', 'ate', 'uta', 'ută', 'uti', 'ute', 'ita', 'ită', 'iti', 'ite', 'ica', 'ice', 'at', 'os', 'iv', 'ut', 'it', 'ic'))) !== \false) {
if ($this->inR2($position)) {
$this->word = Utf8::substr($this->word, 0, $position);
}
return \true;
}
// iune iuni
// delete if preceded by ţ, and replace the ţ by t.
if (($position = $this->search(array('iune', 'iuni'))) !== \false) {
if ($this->inR2($position)) {
$before = $position - 1;
$letter = Utf8::substr($this->word, $before, 1);
if ($letter == 'ţ') {
$this->word = Utf8::substr($this->word, 0, $position);
$this->word = \preg_replace('#(ţ)$#u', 't', $this->word);
}
}
return \true;
}
// ism isme ist ista iste isti istă işti
// replace with ist
if (($position = $this->search(array('isme', 'ism', 'ista', 'iste', 'isti', 'istă', 'işti', 'ist'))) !== \false) {
if ($this->inR2($position)) {
$this->word = \preg_replace('#(isme|ism|ista|iste|isti|istă|işti|ist)$#u', 'ist', $this->word);
}
return \true;
}
return \false;
}
/**
* Step 3: Removal of verb suffixes
* Do step 3 if no suffix was removed either by step 1 or step 2.
* @return boolean
*/
public function step3()
{
// are ere ire âre ind ând indu ându eze ească ez ezi ează esc eşti
// eşte ăsc ăşti ăşte am ai au eam eai ea eaţi eau iam iai ia iaţi
// iau ui aşi arăm arăţi ară uşi urăm urăţi ură işi irăm irăţi iră âi
// âşi ârăm ârăţi âră asem aseşi ase aserăm aserăţi aseră isem iseşi ise
// iserăm iserăţi iseră âsem âseşi âse âserăm âserăţi âseră usem useşi use userăm userăţi useră
// delete if preceded in RV by a consonant or u
if (($position = $this->searchIfInRv(array('userăţi', 'iserăţi', 'âserăţi', 'aserăţi', 'userăm', 'iserăm', 'âserăm', 'aserăm', 'iseră', 'âseşi', 'useră', 'âseră', 'useşi', 'iseşi', 'aseră', 'aseşi', 'ârăţi', 'irăţi', 'urăţi', 'arăţi', 'ească', 'usem', 'âsem', 'isem', 'asem', 'ârăm', 'urăm', 'irăm', 'arăm', 'iaţi', 'eaţi', 'ăşte', 'ăşti', 'eşte', 'eşti', 'ează', 'ându', 'indu', 'âse', 'use', 'ise', 'ase', 'âră', 'iră', 'işi', 'ură', 'uşi', 'ară', 'aşi', 'âşi', 'iau', 'iai', 'iam', 'eau', 'eai', 'eam', 'ăsc', 'are', 'ere', 'ire', 'âre', 'ind', 'ând', 'eze', 'ezi', 'esc', 'âi', 'ui', 'ia', 'ea', 'au', 'ai', 'am', 'ez'))) !== \false) {
if ($this->inRv($position)) {
$before = $position - 1;
if ($this->inRv($before)) {
$letter = Utf8::substr($this->word, $before, 1);
if (!\in_array($letter, self::$vowels) || $letter == 'u') {
$this->word = Utf8::substr($this->word, 0, $position);
}
}
}
return \true;
}
// ăm aţi em eţi im iţi âm âţi seşi serăm serăţi seră sei se sesem seseşi sese seserăm seserăţi seseră
// delete
if (($position = $this->searchIfInRv(array('seserăm', 'seserăţi', 'seseră', 'seseşi', 'sesem', 'serăţi', 'serăm', 'seşi', 'sese', 'seră', 'aţi', 'eţi', 'iţi', 'âţi', 'sei', 'se', 'ăm', 'âm', 'em', 'im'))) !== \false) {
if ($this->inRv($position)) {
$this->word = Utf8::substr($this->word, 0, $position);
}
return \true;
}
}
/**
* Step 4: Removal of final vowel
*/
public function step4()
{
// Search for the longest among the suffixes "a e i ie ă " and, if it is in RV, delete it.
if (($position = $this->search(array('a', 'ie', 'e', 'i', 'ă'))) !== \false) {
if ($this->inRv($position)) {
$this->word = Utf8::substr($this->word, 0, $position);
}
}
return \true;
}
/**
* Finally
* Turn I, U back into i, u
*/
public function finish()
{
// Turn I, U back into i, u
$this->word = Utf8::str_replace(array('I', 'U'), array('i', 'u'), $this->word);
}
}