Repository navigation
Expand file tree
/
Copy pathInflect.php
More file actions
254 lines (234 loc) · 8.4 KB
/
Copy pathInflect.php
File metadata and controls
254 lines (234 loc) · 8.4 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
<?php
/**
* Russian names inflection library
*
* @author Igor Gavrilov <igor.gavrilov@softline.ru>
* @link git://github.com/mytholog/Inflect.git
* @version 0.0.1
*/
class Inflect {
const MALE = 'male';
const FEMALE = 'female';
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('я', 'ю', 'я', 'ем', 'е'),
)
);
public function __construct() {
mb_internal_encoding("UTF-8");
}
/**
* @param $name
* @return string ru|en
*/
public function getNameLang($name) {
return preg_match('/[a-zA-Z]/', $name) ? 'en' : 'ru';
}
/**
* Возвращает просклоненное имя в выбранном падеже
*
* @param string $fullName Фамилия Имя Отчество
* @param int $case Падеж (
* 0 - genitive,
* 1 - dative,
* 2 - accusative,
* 3 - instrumentative,
* 4 - prepositional)
* @param $name_order string [fn | nf] Фамилия-Имя или Имя-Фамилия
* @return string
*/
public function getInflectName($fullName, $case, $name_order = 'fn') {
if (empty($fullName)) {
return;
}
if ($this->getNameLang($fullName) != 'ru')
return $fullName;
$this->explodeName($fullName, $name_order);
$this->gender = $this->getGender();
$this->case = $case;
$this->processingMiddleName();
$this->processingFirstName();
$this->processingLastName();
return $name_order == 'fn'
? sprintf(
'%s%s%s',
$this->lastName,
empty($this->firstName) ? '' : ' ' .$this->firstName,
empty($this->middleName) ? '' : ' ' .$this->middleName
)
: sprintf(
'%s%s%s',
$this->firstName,
empty($this->lastName) ? '' : ' ' .$this->lastName,
empty($this->middleName) ? '' : ' ' .$this->middleName
);
}
/**
* Определение пола
*
* @param string $fullName OPTIONAL Фамилия Имя Отчество
* @return string|null
*/
public function getGender($fullName = null) {
if (!is_null($fullName)) {
$this->explodeName($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;
}
/**
* @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, $name_order = 'fn') {
$expl = explode(' ', ucwords(trim($fullName)));
if ($name_order == 'fn')
list($this->lastName, $this->firstName, $this->middleName) = $expl;
else
list($this->firstName, $this->lastName, $this->middleName) = $expl;
}
}