1 | <?php declare(strict_types = 1); |
||
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 |
||
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 |
||
173 | |||
174 | |||
175 | /** @return string a plural noun */ |
||
176 | public function pluralNoun(): string |
||
182 | |||
183 | |||
184 | /** @return string a plural verb */ |
||
185 | public function pluralVerb(): string |
||
192 | |||
193 | |||
194 | /** @return string doing version of a verb */ |
||
195 | public function verbing(): string |
||
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 |
||
218 | |||
219 | |||
220 | /** @return bool true if this is a verb with ing */ |
||
221 | public function ingVerbCheck(string &$wordType, string &$possiblyRandomWord): bool |
||
231 | |||
232 | |||
233 | public function augmentIfPluralNoun(bool $pluralNoun, string &$randomWord): void |
||
242 | |||
243 | |||
244 | public function augmentIfIngVerb(bool $ing, string &$randomWord): void |
||
253 | |||
254 | |||
255 | public function augmentSentenceTitleCase(bool $titleCase, string &$result): void |
||
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 |
||
278 | } |
||
279 |