Repository navigation
Expand file tree
/
Copy pathInflect.php
More file actions
406 lines (366 loc) · 13.7 KB
/
Copy pathInflect.php
File metadata and controls
406 lines (366 loc) · 13.7 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
<?php
/**
* Russian names inflection library
*
* @author Igor Gavrilov <mytholog@yandex.ru>
* @link git://github.com/mytholog/Inflect.git
* @version 0.0.1
*/
class Inflect {
const MALE = 'male';
const FEMALE = 'female';
const NEUTER = 'neuter';
private $firstName;
private $lastName;
private $middleName;
private $gender;
/**
* Выбранный падеж
* @var string
*/
private $case;
/*
* [part of the name] => [suffix] => [genitive, dative, accusative, instrumentative, prepositional]
* [часть имени] => [окончание] => [родительный, дательный, винительный, творительный, предложный]
*/
private $map = array(
'middle' => array(
'на' => array('ны', 'не', 'ну', 'ной', 'не'),
'ич' => array('ича', 'ичу', 'ича', 'ичем', 'иче'),
'ыч' => array('ыча', 'ычу', 'ыча', 'ычем', 'ыче')
),
'first' => array(
'ия' => array('ии', 'ии', 'ию', 'ией', 'ие'),
'([гжйкхчшщ])а' => array('$1и', '$1е', '$1у', '$1ой', '$1е'),
'а' => array('ы', 'е', 'у', 'ой', 'е'),
'мя' => array('мени', 'мени', 'мя', 'менем', 'мени'),
'я' => array('и', 'е', 'ю', 'ей', 'е'),
'й' => array('я', 'ю', 'я', 'ем', 'е'),
),
'first_exp' => array(
'Лев' => array('Льва', 'Льву', 'Льва', 'Львом', 'Льве'),
'Зоя' => array('Зои', 'Зое', 'Зою', 'Зоей', 'Зое'),
),
'last' => array(
'(ин|ын|ев|ёв|ов)а' => array('$1ой', '$1ой', '$1у', '$1ой', '$1ой'),
'(ин|ын|ев|ёв|ов)' => array('$1а', '$1у', '$1а', '$1ым', '$1е'),
'ая' => array('ой', 'ой', 'ую', 'ой', 'ой'),
'яя' => array('ей', 'ей', 'юю', 'ей', 'ей'),
'кий' => array('кого', 'кому', 'кого', 'ким', 'ком'),
'ий' => array('его', 'ему', 'его', 'им', 'ем'),
'ый' => array('ого', 'ому', 'ого', 'ым', 'ом'),
'ой' => array('ого', 'ому', 'ого', 'ым', 'ом'),
),
'last_m' => array(
'а' => array('ы', 'е', 'у', 'ой', 'е'),
'мя' => array('мени', 'мени', 'мя', 'менем', 'мени'),
'я' => array('и', 'е', 'ю', 'ёй', 'е'),
'й' => array('я', 'ю', 'й', 'ем', 'е'),
'ь' => array('я', 'ю', 'я', 'ем', 'е'),
)
);
private $abjectiveEndingsMap = array(
self::MALE => array(
'(ый)' => array('ого', 'ому', 'ого', 'ым', 'ом'), // красный
'(кий)' => array('кого','кому', 'кого', 'ким', 'ком'), // технологический
'(ий)' => array('его', 'ему', 'его', 'им', 'ем'), // синий
'(ой)' => array('ого', 'ому', 'ого', 'ом', 'ом'), // большой
),
self::FEMALE => array(
'(шая)' => array('шей', 'шей', 'шей', 'шей', 'шей'), // высшая
'(ая)' => array('ой', 'ой', 'ой', 'ой', 'ой'), // красная
'(яя)' => array('ей', 'ей', 'ей', 'ей', 'ей'), // синяя
),
self::NEUTER => array(
'(ое)' => array('ого', 'ому', 'ого', 'ым', 'ом'), // красное
'(ее)' => array('его', 'ему', 'его', 'им', 'ем'), // синее
),
);
private $nounEndingsMap = array(
self::MALE => array(
'(к|т|д)' => array('$1а', '$1у', '$1а', '$1ом', '$1е'), // техник, документовед
'(ч|он|р|им|л)' => array('$1а', '$1у', '$1', '$1ом', '$1е'), // топор
'(ый)' => array('ого', 'ому', 'ого', 'ым', 'ом'), // учёный
'(ец)' => array('ца', 'цу', 'ца', 'цом', 'це'), // певец
'(н|р|л)ь' => array('$1я', '$1ю', '$1я', '$1ем', '$1е'), // конь
'тр' => array('а', 'у', '', 'ом', 'е'), // центр
'(ай)' => array('ая', 'аю', 'ай', 'аем', 'ае'), // май
),
self::FEMALE => array(
'(а)' => array('ы', 'е', 'у', 'ой', 'е'), // машина
'(я)' => array('и', 'е', 'ю', 'ей', 'и'), // станция
'(сть)' => array('сти', 'сте', 'сть', 'стью', 'сти'), // пряность
'(ь)' => array('и', 'е', 'ь', 'ью', 'е'), // мелочь
),
self::NEUTER => array(
'(o)' => array('а', 'у', 'о', 'ом', 'е'), // молоко
'(ще)' => array('ща', 'щу', 'ще', 'щем', 'ще'), // чудовище
'(ё)' => array('я', 'ю', 'ё', 'ём', 'е'), // копьё
'(ие)' => array('ия', 'ию', 'ие', 'ием', 'ии'), // смирение
'(я)' => array('яни', 'яне', 'я', 'енем', 'мени'),// вымя
),
);
public function __construct() {
mb_internal_encoding("UTF-8");
}
/**
* Возвращает просклоненное имя в выбранном падеже
*
* @param string $fullName Фамилия Имя Отчество
* @param int $case Падеж (0 - genitive, 1 - dative, 2 - accusative, 3 - instrumentative, 4 - prepositional)
* @return string
*/
public function getInflectName($fullName, $case) {
if (empty($fullName)) {
return;
}
if ($this->explodeName($fullName) === FALSE) {
return $fullname;
}
$this->gender = $this->getGender();
$this->case = $case;
$this->processingMiddleName();
$this->processingFirstName();
$this->processingLastName();
return sprintf(
'%s%s%s',
$this->lastName,
empty($this->firstName) ? '' : ' ' .$this->firstName,
empty($this->middleName) ? '' : ' ' .$this->middleName
);
}
/**
* Возвращает просклоненное прилагательное в выбранном падеже
*
* @param string $abjective Существительное
* @param int $case Падеж (0 - genitive, 1 - dative, 2 - accusative, 3 - instrumentative, 4 - prepositional)
* @return string
*/
public function getInflectAbjective($abjective, $case) {
if (empty($abjective)) {
return;
}
$this->case = $case;
$words = explode(' ', $abjective);
foreach ($words as $id => &$word) {
$word = $this->processAbjective($word);
}
return join(' ', $words);
}
/**
* Возвращает просклоненное существительное в выбранном падеже
*
* @param string $noun Существительное
* @param int $case Падеж (0 - genitive, 1 - dative, 2 - accusative, 3 - instrumentative, 4 - prepositional)
* @return string
*/
public function getInflectNoun($noun, $case) {
if (empty($noun)) {
return;
}
$this->case = $case;
$words = explode(' ', $noun);
foreach ($words as &$word) {
if (!$this->isAdjective($word)) {
$subwords = explode('-', $word);
foreach ($subwords as &$subword)
$subword = $this->processNoun($subword);
$word = join('-', $subwords);
break;
}
$word = $this->getInflectAbjective($word, $case);
}
return join(' ', $words);
}
public function isAdjective($string) {
return ($this->getAbjectiveGender($string) != null);
}
/**
* Определение пола
*
* @return string|null
*/
public function getAbjectiveGender($abjective) {
switch (true) {
case preg_match('/(ый|ий|ой)$/u', $abjective):
return self::MALE;
break;
case preg_match('/(ая|яя)$/u', $abjective):
return self::FEMALE;
break;
case preg_match('/(ое|ее)$/u', $abjective):
return self::NEUTER;
break;
default:
return null;
}
return null;
}
/**
* Определение пола
*
* @return string|null
*/
public function getNounGender($noun) {
switch (true) {
case preg_match('/(к|ч|он|ый|ст|р|ец|нь|рь|рт|им|тр)$/u', $noun):
return self::MALE;
break;
case preg_match('/(а|я|сть|чь)$/u', $noun):
return self::FEMALE;
break;
case preg_match('/(о|ще|ё|ие|мя|е)$/u', $noun):
return self::NEUTER;
break;
default:
return self::MALE;
}
return null;
}
/**
* Определение пола
*
* @param string $fullName OPTIONAL Фамилия Имя Отчество
* @return string|null
*/
public function getGender($fullName = null) {
if (!is_null($fullName)) {
if ($this->explodeName($fullName) === FALSE) {
return $fullName;
}
}
//by MiddleName
if (isset($this->middleName)) {
return mb_substr($this->middleName, -2) == 'на' ? self::FEMALE : self::MALE;
}
switch (true) {
//by LastName
case preg_match('/(ев|ин|ын|ёв|ов)а$/u', $this->lastName):
case preg_match('/(ая|яя)$/u', $this->lastName):
return self::FEMALE;
break;
case preg_match('/(ев|ин|ын|ёв|ов)$/u', $this->lastName):
case preg_match('/(ий|ый)$/u', $this->lastName):
return self::MALE;
break;
//by FirstName
case preg_match('/[ая]$/u', $this->firstName):
return self::FEMALE;
break;
case preg_match('/[^аеёиоуыэюя]$/u', $this->firstName):
return self::MALE;
break;
}
return null;
}
/**
* Обработка множеств (склонение существительных после числительных)
*
* @param array $titles Варианты слова (час, часа, часов)
* @param int $number Число, которое нужно перевести
* @param bool $full Если true, то возвращать вместе с цифрой
* @return string
*/
public function getPlural(array $titles, $number, $full = false) {
$result = $titles[(($number % 10 == 1) && ($number % 100 != 11)) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2)];
return $full ? $number . ' ' . $result : $result;
}
protected function processingLastName() {
if (!is_null($this->lastName) && !is_null($this->gender)) {
switch (true) {
case preg_match('/[еёиоуыэю]$/u', $this->lastName):
case preg_match('/[аеёиоуыэюя]а$/u', $this->lastName):
case preg_match('/[ёоуыэю]я$/u', $this->lastName):
case preg_match('/[иы]х$/u', $this->lastName):
case $this->replaceProcessing('last', 'lastName'):
case $this->gender == self::MALE && $this->replaceProcessing('last_m', 'lastName'):
break;
case $this->gender == self::MALE:
$value = array('а', 'у', 'а', 'ом', 'е');
$this->lastName .= $value[$this->case];
break;
}
}
return $this;
}
protected function processingFirstName() {
if (!is_null($this->firstName)) {
$this->firstName = preg_replace('/Пётр$/u', 'Петр', $this->firstName);
switch (true) {
case $this->replaceProcessing('first_exp', 'firstName'):
break;
case preg_match('/[еёиоуыэю]$/u', $this->firstName):
case preg_match('/[аеёиоуыэюя]а$/u', $this->firstName):
case preg_match('/[аёоуыэюя]я$/u', $this->firstName):
case $this->gender == self::FEMALE && preg_match('/[бвгджзклмнйпрстфхцчшщ]$/u', $this->firstName):
break;
case $this->gender == self::MALE && preg_match('/ь$/u', $this->firstName):
$value = array('я', 'ю', 'я', 'ем', 'е');
$this->firstName = preg_replace('/ь$/u', $value[$this->case], $this->firstName);
break;
case $this->gender == self::FEMALE && preg_match('/ь$/u', $this->firstName):
$value = array('и', 'и', 'ь', 'ью', 'и');
$this->firstName = preg_replace('/ь$/u', $value[$this->case], $this->firstName);
break;
case $this->replaceProcessing('first', 'firstName'):
break;
default:
$value = array('а', 'у', 'а', 'ом', 'е');
$this->firstName .= $value[$this->case];
break;
}
}
return $this;
}
protected function processingMiddleName() {
if (!is_null($this->middleName)) {
if($this->replaceProcessing('middle', 'middleName')) {
return $this;
}
$this->middleName = preg_replace('/(Иль|Кузьм|Фом)ичем$/u', '$1ичом', $this->middleName);
}
return $this;
}
protected function processAbjective($abjective) {
$abjectiveEndingsMap = $this->abjectiveEndingsMap[$this->getAbjectiveGender($abjective)];
foreach($abjectiveEndingsMap as $pattern => $replacement_array) {
$count = 0;
$abjective = preg_replace('/'.$pattern.'$/u', $replacement_array[$this->case], $abjective, 1, $count);
if ($count)
break;
}
return $abjective;
}
protected function processNoun($noun) {
$nounEndingsMap = $this->nounEndingsMap[$this->getNounGender($noun)];
foreach($nounEndingsMap as $pattern => $replacement_array) {
$count = 0;
$noun = preg_replace('/'.$pattern.'$/u', $replacement_array[$this->case], $noun, 1, $count);
if ($count)
break;
}
return $noun;
}
/**
* @param string $ruleGroup
* @param string $field
* @return boolean
*/
protected function replaceProcessing($ruleGroup, $field) {
foreach ($this->map[$ruleGroup] as $pattern => $value) {
$pattern = '/'.$pattern.'$/u';
if (preg_match($pattern, $this->{$field})) {
$this->{$field} = preg_replace($pattern, $value[$this->case], $this->{$field});
return true;
}
}
return false;
}
private function explodeName($fullName) {
$array = explode(' ', ucwords(trim($fullName)));
if (!isset($array[2]))
return FALSE;
list($this->lastName, $this->firstName, $this->middleName) = $array;
return TRUE;
}
}