1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Suilven\RandomEnglish\Helper; |
4
|
|
|
|
5
|
|
|
use Doctrine\Inflector\InflectorFactory; |
6
|
|
|
|
7
|
|
|
class LanguageHelper |
8
|
|
|
{ |
9
|
|
|
private const DOUBLE_LAST_CHARS = ['b', 'd', 'g', 'l', 'm', 'n', 'p', 't']; |
10
|
|
|
|
11
|
|
|
private const DO_NOT_DOUBLE_LAST_CHAR_TWO_CHARS = [ |
12
|
|
|
'mb', |
13
|
|
|
'ad', |
14
|
|
|
'ld', |
15
|
|
|
'dd', |
16
|
|
|
'ed', |
17
|
|
|
'nd', |
18
|
|
|
'rd', |
19
|
|
|
'gg', |
20
|
|
|
'ng', |
21
|
|
|
'nt', |
22
|
|
|
'en', |
23
|
|
|
'wn', |
24
|
|
|
'rn', |
25
|
|
|
'am', |
26
|
|
|
'om', |
27
|
|
|
'rm', |
28
|
|
|
'rn', |
29
|
|
|
'lp', |
30
|
|
|
'ep', |
31
|
|
|
'mp', |
32
|
|
|
'op', |
33
|
|
|
'rp', |
34
|
|
|
'up', |
35
|
|
|
'ct', |
36
|
|
|
'at', |
37
|
|
|
'et', |
38
|
|
|
'ht', |
39
|
|
|
'lm', |
40
|
|
|
'lt', |
41
|
|
|
'ut', |
42
|
|
|
'ot', |
43
|
|
|
'pt', |
44
|
|
|
'rt', |
45
|
|
|
'st', |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
private const DO_NOT_DOUBLE_LAST_CHAR_THREE_CHARS = [ |
49
|
|
|
'ain', |
50
|
|
|
'ait', |
51
|
|
|
'ion', |
52
|
|
|
'oin', |
53
|
|
|
'ool', |
54
|
|
|
'son', |
55
|
|
|
'oap', |
56
|
|
|
'oon', |
57
|
|
|
'ron', |
58
|
|
|
// visit |
59
|
|
|
'sit', |
60
|
|
|
'tim', |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** @var \Doctrine\Inflector\Inflector */ |
65
|
|
|
private $inflector; |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
public function __construct() |
69
|
|
|
{ |
70
|
|
|
// English only |
71
|
|
|
$this->inflector = InflectorFactory::create()->build(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $noun A noun |
77
|
|
|
* @return string the plurarl of that noun |
78
|
|
|
*/ |
79
|
|
|
public function pluralizeNoun(string $noun): string |
80
|
|
|
{ |
81
|
|
|
return $this->inflector->pluralize($noun); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Attempt to convert a verb into a verb with ing. |
87
|
|
|
* |
88
|
|
|
* @param string $verb a verb |
89
|
|
|
* @return string the ing version of the verb |
90
|
|
|
*/ |
91
|
|
|
public function ingVerb(string $verb): string |
92
|
|
|
{ |
93
|
|
|
$lastChar = \substr($verb, -1); |
94
|
|
|
$lastTwoChars = \substr($verb, -2); |
95
|
|
|
$lastThreeChars = \substr($verb, -3); |
96
|
|
|
|
97
|
|
|
$verbPart = $verb; |
98
|
|
|
if (\substr($verb, -1) === 'e' && \substr($verb, -2) !== 'ee') { |
99
|
|
|
$verbPart = \rtrim($verb, 'e'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (\in_array($lastChar, self::DOUBLE_LAST_CHARS, true) |
103
|
|
|
&& !\in_array($lastTwoChars, self::DO_NOT_DOUBLE_LAST_CHAR_TWO_CHARS, true) |
104
|
|
|
&& !\in_array($lastThreeChars, self::DO_NOT_DOUBLE_LAST_CHAR_THREE_CHARS, true) |
105
|
|
|
) { |
106
|
|
|
$verbPart .= $lastChar; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$result = $verbPart . 'ing'; |
110
|
|
|
|
111
|
|
|
// deal with lie ->lying |
112
|
|
|
$result = \str_replace('iing', 'ying', $result); |
113
|
|
|
|
114
|
|
|
// deal with special cases |
115
|
|
|
switch ($verb) { |
116
|
|
|
case 'sit': |
117
|
|
|
$result = 'sitting'; |
118
|
|
|
|
119
|
|
|
break; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->fixSuffix($result); |
123
|
|
|
|
124
|
|
|
return $result; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** @param string $word the word whose suffix needs fixed */ |
129
|
|
|
public function fixSuffix(string &$word): void |
130
|
|
|
{ |
131
|
|
|
$suffixes = [ |
132
|
|
|
'eeing' => 'eeing', |
133
|
|
|
'ieing' => 'ying', |
134
|
|
|
'eing' => 'ing', |
135
|
|
|
]; |
136
|
|
|
|
137
|
|
|
$incorrectSuffixes = \array_keys($suffixes); |
138
|
|
|
$wordLength = \strlen($word); |
139
|
|
|
|
140
|
|
|
foreach ($incorrectSuffixes as $incorrectSuffix) { |
141
|
|
|
$suffixLength = \strlen($incorrectSuffix); |
142
|
|
|
$actualSuffix = \substr($word, -$suffixLength); |
143
|
|
|
if ($actualSuffix === $incorrectSuffix) { |
144
|
|
|
$word = \substr($word, 0, $wordLength-$suffixLength) . $suffixes[$incorrectSuffix]; |
145
|
|
|
|
146
|
|
|
break; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|