|
1
|
|
|
<?php |
|
2
|
|
|
namespace morphos\Russian; |
|
3
|
|
|
|
|
4
|
|
|
trait RussianLanguage { |
|
5
|
|
|
static public $vowels = array( |
|
6
|
|
|
'А', |
|
7
|
|
|
'Е', |
|
8
|
|
|
'Ё', |
|
9
|
|
|
'И', |
|
10
|
|
|
'О', |
|
11
|
|
|
'У', |
|
12
|
|
|
'Ы', |
|
13
|
|
|
'Э', |
|
14
|
|
|
'Ю', |
|
15
|
|
|
'Я', |
|
16
|
|
|
); |
|
17
|
|
|
|
|
18
|
|
|
static public $consonants = array( |
|
19
|
|
|
'Б', |
|
20
|
|
|
'В', |
|
21
|
|
|
'Г', |
|
22
|
|
|
'Д', |
|
23
|
|
|
'Ж', |
|
24
|
|
|
'З', |
|
25
|
|
|
'Й', |
|
26
|
|
|
'К', |
|
27
|
|
|
'Л', |
|
28
|
|
|
'М', |
|
29
|
|
|
'Н', |
|
30
|
|
|
'П', |
|
31
|
|
|
'Р', |
|
32
|
|
|
'С', |
|
33
|
|
|
'Т', |
|
34
|
|
|
'Ф', |
|
35
|
|
|
'Х', |
|
36
|
|
|
'Ц', |
|
37
|
|
|
'Ч', |
|
38
|
|
|
'Ш', |
|
39
|
|
|
'Щ', |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
static public $pairs = array( |
|
43
|
|
|
'Б' => 'П', |
|
44
|
|
|
'В' => 'Ф', |
|
45
|
|
|
'Г' => 'К', |
|
46
|
|
|
'Д' => 'Т', |
|
47
|
|
|
'Ж' => 'Ш', |
|
48
|
|
|
'З' => 'С', |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
static public function isHissingConsonant($consonant) { |
|
52
|
|
|
return in_array(lower($consonant), array('ж', 'ш', 'ч', 'щ')); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function isVelarConsonant($consonant) { |
|
56
|
|
|
return in_array(lower($consonant), array('г', 'к', 'х')); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
static private function isConsonant($consonant) { |
|
60
|
|
|
return in_array(upper($consonant), self::$consonants); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
static private function isVowel($char) { |
|
64
|
|
|
return in_array(upper($char), self::$vowels); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function countSyllables($string) { |
|
68
|
|
|
return chars_count($string, array_map(__NAMESPACE__.'\\lower', self::$vowels)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function isPaired($consonant) { |
|
72
|
|
|
$consonant = lower($consonant); |
|
73
|
|
|
return array_key_exists($consonant, self::$pairs) || (array_search($consonant, self::$pairs) !== false); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function checkLastConsonantSoftness($word) { |
|
77
|
|
|
if (($substring = last_position_for_one_of_chars(lower($word), array_map('lower', self::$consonants))) !== false) { |
|
78
|
|
|
if (in_array(slice($substring, 0, 1), ['й', 'ч', 'щ'])) // always soft consonants |
|
79
|
|
|
return true; |
|
80
|
|
|
else if (length($substring) > 1 && in_array(slice($substring, 1, 2), ['е', 'ё', 'и', 'ю', 'я', 'ь'])) // consonants are soft if they are trailed with these vowels |
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function choosePrepositionByFirstLetter($word, $prepositionWithVowel, $preposition) { |
|
87
|
|
|
if (in_array(upper(slice($word, 0, 1)), array('А', 'О', 'И', 'У', 'Э'))) |
|
88
|
|
|
return $prepositionWithVowel; |
|
89
|
|
|
else |
|
90
|
|
|
return $preposition; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function chooseVowelAfterConsonant($last, $soft_last, $after_soft, $after_hard) { |
|
94
|
|
|
if ((RussianLanguage::isHissingConsonant($last) && !in_array($last, array('ж', 'ч'))) || $this->isVelarConsonant($last) || $soft_last) { |
|
95
|
|
|
return $after_soft; |
|
96
|
|
|
} else { |
|
97
|
|
|
return $after_hard; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|