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() |
||
57 | |||
58 | |||
59 | /** @param string $newConfig configuration file of how sentence structures */ |
||
60 | public function setConfig(string $newConfig): void |
||
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 |
||
95 | |||
96 | |||
97 | /** |
||
98 | * Generate a random title |
||
99 | * |
||
100 | * @return string a random sentence, all in caps, no trailing full stop |
||
101 | */ |
||
102 | public function title(): string |
||
106 | |||
107 | |||
108 | /** |
||
109 | * @param int $maxSentences The maximum number of sentences |
||
110 | * @return string a paragraph of random sentences |
||
111 | */ |
||
112 | public function paragraph(int $maxSentences = 10): string |
||
123 | |||
124 | |||
125 | /** @return string a plural noun */ |
||
126 | public function pluralNoun(): string |
||
132 | |||
133 | |||
134 | /** @return string the ing version of a verb, e.g. run -> running */ |
||
135 | public function verbing(): string |
||
142 | |||
143 | |||
144 | /** |
||
145 | * @param string $wordType The word type, e.g. 'noun' or 'verb' |
||
146 | * @return bool true if this is a plural noun |
||
147 | */ |
||
148 | public function pluralNounCheck(string &$wordType, string &$possiblyRandomWord): bool |
||
158 | |||
159 | |||
160 | /** @return bool true if this is a verb with ing */ |
||
161 | public function ingVerbCheck(string &$wordType, string &$possiblyRandomWord): bool |
||
171 | |||
172 | |||
173 | public function augmentIfPluralNoun(bool $pluralNoun, string &$randomWord): void |
||
182 | |||
183 | |||
184 | public function augmentIfIngVerb(bool $ing, string &$randomWord): void |
||
193 | |||
194 | |||
195 | public function augmentSentenceTitleCase(bool $titleCase, string &$result): void |
||
204 | |||
205 | |||
206 | /** |
||
207 | * Convert an array of strings into a sentence |
||
208 | * 1) Capitalize first letter |
||
209 | * 2) Glue words together with a space |
||
210 | * 3) Add a full stop |
||
211 | * 4) Deal with diferent sentence endings such as ! or ? |
||
212 | * |
||
213 | * @param array<string> $sentenceArray a sentence whose words are in an array of strings |
||
214 | * @return string a sentence |
||
215 | */ |
||
216 | public function makeArrayIntoSentence(array $sentenceArray): string |
||
226 | |||
227 | |||
228 | /** |
||
229 | * If a sentence ended with a question or exclamation mark prior to having a full stop appended, deal with it |
||
230 | * |
||
231 | * @param string $result The sentence, by reference, prior to being fixed |
||
232 | */ |
||
233 | public function fixEndOfSentence(string &$result): void |
||
238 | |||
239 | |||
240 | /** |
||
241 | * @param string $wordType the type of word, e.g. noun, verb |
||
242 | * @return string a random word for the given word type |
||
243 | */ |
||
244 | private function getRandomWord(string $wordType): string |
||
252 | |||
253 | |||
254 | /** @return string the above string as is, or a possibly randomized version, e.g. a random noun */ |
||
255 | private function getWordOrRandomWord(string $possiblyRandomWord): string |
||
286 | |||
287 | |||
288 | /** |
||
289 | * @param string $wordType noun, verb, adjective etc |
||
290 | * @param bool $pluralizeNoun if true pluralize a noun |
||
291 | * @param bool $makeVerbIng if true make a verb an ing verb, e.g. run -> running |
||
292 | */ |
||
293 | private function chooseRandomWord( |
||
318 | } |
||
319 |