1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Suilven\RandomEnglish; |
4
|
|
|
|
5
|
|
|
use Suilven\RandomEnglish\Helper\LanguageHelper; |
6
|
|
|
|
7
|
|
|
class RandomEnglishGenerator |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
private const POSSIBLE_WORD_TYPES = [ |
11
|
|
|
'noun', |
12
|
|
|
'countable_noun', |
13
|
|
|
|
14
|
|
|
'adjective', |
15
|
|
|
'preposition', |
16
|
|
|
'conjunction', |
17
|
|
|
'contraction', |
18
|
|
|
'control_verb', |
19
|
|
|
|
20
|
|
|
'sequence_adverb', |
21
|
|
|
'frequency_adverb', |
22
|
|
|
|
23
|
|
|
// pluralize and ing these? |
24
|
|
|
'intransitive_verb', |
25
|
|
|
'irregular_verb', |
26
|
|
|
'transitive_verb', |
27
|
|
|
'verb', |
28
|
|
|
'adverb', |
29
|
|
|
|
30
|
|
|
'intransitive_verb_ing', |
31
|
|
|
'irregular_verb_ing', |
32
|
|
|
'transitive_verb_ing', |
33
|
|
|
'verb_ing', |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
// positions |
37
|
|
|
'locative', |
38
|
|
|
|
39
|
|
|
'plural_noun', |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
// names |
43
|
|
|
'mens_name', |
44
|
|
|
'womens_name', |
45
|
|
|
'country', |
46
|
|
|
'colour', |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** @var string configuration file for sentences */ |
50
|
|
|
private $config = ''; |
51
|
|
|
|
52
|
|
|
public function __construct() |
53
|
|
|
{ |
54
|
|
|
$configFile = \dirname(__FILE__) . '/../config/sentence-structure.cfg'; |
55
|
|
|
$this->setConfig(\file_get_contents($configFile)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** @param string $newConfig configuration file of how sentence structures */ |
60
|
|
|
public function setConfig(string $newConfig): void |
61
|
|
|
{ |
62
|
|
|
$this->config = \trim($newConfig); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Generate a random sentence |
68
|
|
|
* |
69
|
|
|
* @param bool $titleCase true to generate a title, false (default) to generate a sentence |
70
|
|
|
* @return string a random sentence |
71
|
|
|
*/ |
72
|
|
|
public function sentence(bool $titleCase = false): string |
73
|
|
|
{ |
74
|
|
|
// choose a random sentence structure |
75
|
|
|
$structures = \explode(\PHP_EOL, $this->config); |
76
|
|
|
\shuffle($structures); |
77
|
|
|
$structure = $structures[0]; |
78
|
|
|
|
79
|
|
|
$expression='/[\s]+/'; |
80
|
|
|
$splits = \preg_split($expression, $structure, -1, \PREG_SPLIT_NO_EMPTY); |
81
|
|
|
|
82
|
|
|
$sentenceArray = []; |
83
|
|
|
|
84
|
|
|
/** @var string $possiblyRandomWord */ |
85
|
|
|
foreach ($splits as $possiblyRandomWord) { |
86
|
|
|
$randomized = false; |
87
|
|
|
|
88
|
|
|
foreach (self::POSSIBLE_WORD_TYPES as $wordType) { |
89
|
|
|
$pluralNoun = $this->pluralNounCheck($wordType, $possiblyRandomWord); |
90
|
|
|
$ing = $this->ingVerbCheck($wordType, $possiblyRandomWord); |
91
|
|
|
|
92
|
|
|
$start = '[' . $wordType . ']'; |
93
|
|
|
|
94
|
|
|
// check the start of the word as it may be suffixed by likes of a question of exclamation mark. |
95
|
|
|
// If no match for the possible word type continue until the next one |
96
|
|
|
if (\substr($possiblyRandomWord, 0, \strlen($start)) !== $start) { |
97
|
|
|
continue; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// ---- we are now getting a random word from a file ---- |
101
|
|
|
|
102
|
|
|
// note the suffix of the word, and append this to the random word |
103
|
|
|
$restOfWord = \str_replace($start, '', $possiblyRandomWord); |
104
|
|
|
|
105
|
|
|
// This avoids having to use 'countrie' in the sentence structure file : country -> countries |
106
|
|
|
$wordType = \str_replace('country', 'countrie', $wordType); |
107
|
|
|
|
108
|
|
|
/** @var string $randomWord */ |
109
|
|
|
$randomWord = $this->getRandomWord($wordType); |
110
|
|
|
|
111
|
|
|
// augment the random word if a plural noun or an ing verb |
112
|
|
|
$this->augmentIfPluralNoun($pluralNoun, $randomWord); |
113
|
|
|
$this->augmentIfIngVerb($ing, $randomWord); |
114
|
|
|
|
115
|
|
|
// add the random word, and then the suffix of the word such as ?! or !! |
116
|
|
|
$sentenceArray[] =$randomWord . $restOfWord; |
117
|
|
|
|
118
|
|
|
// flag as randomized |
119
|
|
|
$randomized = true; |
120
|
|
|
|
121
|
|
|
break; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// if we have found a randomized word, skill to the next word in the sentence |
125
|
|
|
if ($randomized) { |
126
|
|
|
continue; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// no randomized word has been found, aka no [verb] or [noun], thus append the possibly random word as is |
130
|
|
|
$sentenceArray[] = $possiblyRandomWord; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// ensure sentence starts with a capital |
134
|
|
|
$sentenceArray[0] = \ucfirst($sentenceArray[0]); |
135
|
|
|
|
136
|
|
|
$result = \implode(" ", $sentenceArray) . '.'; |
137
|
|
|
|
138
|
|
|
$this->augmentSentenceTitleCase($titleCase, $result); |
139
|
|
|
|
140
|
|
|
$result = \str_replace('?.', '?', $result); |
141
|
|
|
$result = \str_replace('!.', '?', $result); |
142
|
|
|
|
143
|
|
|
return $result; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Generate a random title |
149
|
|
|
* |
150
|
|
|
* @return string a random sentence, all in caps, no trailing full stop |
151
|
|
|
*/ |
152
|
|
|
public function title(): string |
153
|
|
|
{ |
154
|
|
|
return $this->sentence(true); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param int $maxSentences The maximum number of sentences |
160
|
|
|
* @return string a paragraph of random sentences |
161
|
|
|
*/ |
162
|
|
|
public function paragraph(int $maxSentences = 10): string |
163
|
|
|
{ |
164
|
|
|
$nParagraph = \rand(1, $maxSentences); |
165
|
|
|
$sentences = []; |
166
|
|
|
|
167
|
|
|
for ($i=0; $i< $nParagraph; $i++) { |
168
|
|
|
$sentences[] = $this->sentence(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return \implode(" ", $sentences); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
/** @return string a plural noun */ |
176
|
|
|
public function pluralNoun(): string |
177
|
|
|
{ |
178
|
|
|
$plurableNoun = $this->getRandomWord('countable_noun'); |
179
|
|
|
|
180
|
|
|
return $plurableNoun . 's'; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
/** @return string a plural verb */ |
185
|
|
|
public function pluralVerb(): string |
186
|
|
|
{ |
187
|
|
|
// @todo Choose a random verb source file |
188
|
|
|
$plurableNoun = $this->getRandomWord('verb'); |
189
|
|
|
|
190
|
|
|
return $plurableNoun . 's'; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
/** @return string doing version of a verb */ |
195
|
|
|
public function verbing(): string |
196
|
|
|
{ |
197
|
|
|
// @todo Choose a random verb source file |
198
|
|
|
$plurableNoun = $this->getRandomWord('verb'); |
199
|
|
|
|
200
|
|
|
return $plurableNoun . 'ing'; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param string $wordType The word type, e.g. 'noun' or 'verb' |
206
|
|
|
* @return bool true if this is a plural noun |
207
|
|
|
*/ |
208
|
|
|
public function pluralNounCheck(string &$wordType, string &$possiblyRandomWord): bool |
209
|
|
|
{ |
210
|
|
|
$pluralNoun = ($wordType === 'plural_noun'); |
211
|
|
|
if ($pluralNoun) { |
212
|
|
|
$wordType = 'noun'; |
213
|
|
|
$possiblyRandomWord = \str_replace('plural_', '', $possiblyRandomWord); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $pluralNoun; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
/** @return bool true if this is a verb with ing */ |
221
|
|
|
public function ingVerbCheck(string &$wordType, string &$possiblyRandomWord): bool |
222
|
|
|
{ |
223
|
|
|
$ing = \substr($wordType, -4) === '_ing'; |
224
|
|
|
if ($ing) { |
225
|
|
|
$wordType = \str_replace('_ing', '', $wordType); |
226
|
|
|
$possiblyRandomWord = \str_replace('_ing', '', $possiblyRandomWord); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $ing; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
public function augmentIfPluralNoun(bool $pluralNoun, string &$randomWord): void |
234
|
|
|
{ |
235
|
|
|
if (!$pluralNoun) { |
236
|
|
|
return; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$helper = new LanguageHelper(); |
240
|
|
|
$randomWord = $helper->pluralizeNoun($randomWord); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
|
244
|
|
|
public function augmentIfIngVerb(bool $ing, string &$randomWord): void |
245
|
|
|
{ |
246
|
|
|
if (!$ing) { |
247
|
|
|
return; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$helper = new LanguageHelper(); |
251
|
|
|
$randomWord = $helper->ingVerb($randomWord); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
|
255
|
|
|
public function augmentSentenceTitleCase(bool $titleCase, string &$result): void |
256
|
|
|
{ |
257
|
|
|
if (!$titleCase) { |
258
|
|
|
return; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$result = \ucwords($result); |
262
|
|
|
$result = \substr_replace($result, "", -1); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param string $wordType the type of word, e.g. noun, verb |
268
|
|
|
* @return string a random word for the given word type |
269
|
|
|
*/ |
270
|
|
|
private function getRandomWord(string $wordType): string |
271
|
|
|
{ |
272
|
|
|
$wordsFile = \dirname(__FILE__) . '/../words/english_' . $wordType . 's.txt'; |
273
|
|
|
$words = \explode(\PHP_EOL, \trim(\file_get_contents($wordsFile))); |
274
|
|
|
\shuffle($words); |
275
|
|
|
|
276
|
|
|
return $words[0]; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|