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