Completed
Push — master ( 055a89...5807a1 )
by Gordon
01:43 queued 43s
created

RandomEnglishGenerator::getWordOrRandomWord()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 8.5937
c 0
b 0
f 0
cc 6
nc 3
nop 1
1
<?php declare(strict_types = 1);
2
3
namespace Suilven\RandomEnglish;
4
5
use Faker\Factory;
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
    private const POSSIBLE_FAKER_TYPES=[
50
        'address',
51
        'name',
52
        'randomDigit',
53
        'randomLetter',
54
        'randomNumber',
55
        'title',
56
        'titleMale',
57
        'titleFemale',
58
        'firstNameMale',
59
        'firstNameFemale',
60
        'lastName',
61
62
        'catchPhrase',
63
        'bs',
64
        'company',
65
        'companySuffix',
66
        'jobTitle',
67
68
        'realText',
69
70
        'dayOfWeek',
71
        'monthName',
72
73
    ];
74
75
    /** @var string configuration file for sentences */
76
    private $config = '';
77
78
    public function __construct()
79
    {
80
        $configFile = \dirname(__FILE__) . '/../config/sentence-structure.cfg';
81
        $this->setConfig(\file_get_contents($configFile));
82
    }
83
84
85
    /** @param string $newConfig configuration file of how sentence structures */
86
    public function setConfig(string $newConfig): void
87
    {
88
        $this->config = \trim($newConfig);
89
    }
90
91
92
    /**
93
     * Generate a random sentence
94
     *
95
     * @param bool $titleCase true to generate a title, false (default) to generate a sentence
96
     * @return string a random sentence
97
     */
98
    public function sentence(bool $titleCase = false): string
99
    {
100
        // choose a random sentence structure
101
        $structures = \explode(\PHP_EOL, $this->config);
102
        \shuffle($structures);
103
        $structure = $structures[0];
104
105
        $expression = '/[\s]+/';
106
        $splits = \preg_split($expression, $structure, -1, \PREG_SPLIT_NO_EMPTY);
107
108
        $sentenceArray = [];
109
110
        /** @var string $possiblyRandomWord */
111
        foreach ($splits as $possiblyRandomWord) {
112
            $word = $this->getWordOrRandomWord($possiblyRandomWord);
113
            $sentenceArray[] = $word;
114
        }
115
116
        $result = $this->makeArrayIntoSentence($sentenceArray);
117
        $this->augmentSentenceTitleCase($titleCase, $result);
118
119
        return $result;
120
    }
121
122
123
    /**
124
     * Generate a random title
125
     *
126
     * @return string a random sentence, all in caps, no trailing full stop
127
     */
128
    public function title(): string
129
    {
130
        return $this->sentence(true);
131
    }
132
133
134
    /**
135
     * @param int $maxSentences The maximum number of sentences
136
     * @return string a paragraph of random sentences
137
     */
138
    public function paragraph(int $maxSentences = 10): string
139
    {
140
        $nParagraph = \rand(1, $maxSentences);
141
        $sentences = [];
142
143
        for ($i = 0; $i < $nParagraph; $i++) {
144
            $sentences[] = $this->sentence();
145
        }
146
147
        return \implode("  ", $sentences);
148
    }
149
150
151
    /** @return string a plural noun */
152
    public function pluralNoun(): string
153
    {
154
        $plurableNoun = $this->getRandomWord('countable_noun');
155
156
        return $plurableNoun . 's';
157
    }
158
159
160
    /** @return string the ing version of a verb, e.g. run -> running */
161
    public function verbing(): string
162
    {
163
        // @todo Choose a random verb source file
164
        $ingVerb = $this->getRandomWord('verb');
165
166
        return $ingVerb . 'ing';
167
    }
168
169
170
    /**
171
     * @param string $wordType The word type, e.g. 'noun' or 'verb'
172
     * @return bool true if this is a plural noun
173
     */
174
    public function pluralNounCheck(string &$wordType, string &$possiblyRandomWord): bool
175
    {
176
        $pluralNoun = ($wordType === 'plural_noun');
177
        if ($pluralNoun) {
178
            $wordType = 'noun';
179
            $possiblyRandomWord = \str_replace('plural_', '', $possiblyRandomWord);
180
        }
181
182
        return $pluralNoun;
183
    }
184
185
186
    /** @return bool true if this is a verb with ing */
187
    public function ingVerbCheck(string &$wordType, string &$possiblyRandomWord): bool
188
    {
189
        $ing = \substr($wordType, -4) === '_ing';
190
        if ($ing) {
191
            $wordType = \str_replace('_ing', '', $wordType);
192
            $possiblyRandomWord = \str_replace('_ing', '', $possiblyRandomWord);
193
        }
194
195
        return $ing;
196
    }
197
198
199
    public function augmentIfPluralNoun(bool $pluralNoun, string &$randomWord): void
200
    {
201
        if (!$pluralNoun) {
202
            return;
203
        }
204
205
        $randomWord = $this->pluralNoun();
206
    }
207
208
209
    public function augmentIfIngVerb(bool $ing, string &$randomWord): void
210
    {
211
        if (!$ing) {
212
            return;
213
        }
214
215
        $randomWord = $this->verbing();
216
    }
217
218
219
    public function augmentSentenceTitleCase(bool $titleCase, string &$result): void
220
    {
221
        if (!$titleCase) {
222
            return;
223
        }
224
225
        $result = \ucwords($result);
226
        $result = \substr_replace($result, "", -1);
227
    }
228
229
230
    /**
231
     * Convert an array of strings into a sentence
232
     * 1) Capitalize first letter
233
     * 2) Glue words together with a space
234
     * 3) Add a full stop
235
     * 4) Deal with diferent sentence endings such as ! or ?
236
     *
237
     * @param array<string> $sentenceArray a sentence whose words are in an array of strings
238
     * @return string a sentence
239
     */
240
    public function makeArrayIntoSentence(array $sentenceArray): string
241
    {
242
        // ensure sentence starts with a capital
243
        $sentenceArray[0] = \ucfirst($sentenceArray[0]);
244
245
        $sentence = \implode(" ", $sentenceArray) . '.';
246
        $this->fixEndOfSentence($sentence);
247
248
        return $sentence;
249
    }
250
251
252
    /**
253
     * If a sentence ended with a question or exclamation mark prior to having a full stop appended, deal with it
254
     *
255
     * @param string $result The sentence, by reference, prior to being fixed
256
     */
257
    public function fixEndOfSentence(string &$result): void
258
    {
259
        $result = \str_replace('?.', '?', $result);
260
        $result = \str_replace('!.', '?', $result);
261
        $result = \str_replace('..', '.', $result);
262
    }
263
264
265
    /**
266
     * @param string $wordType the type of word, e.g. noun, verb
267
     * @return string a random word for the given word type
268
     */
269
    private function getRandomWord(string $wordType): string
270
    {
271
        $wordsFile = \dirname(__FILE__) . '/../words/english_' . $wordType . 's.txt';
272
        $words = \explode(\PHP_EOL, \trim(\file_get_contents($wordsFile)));
273
        \shuffle($words);
274
275
        return $words[0];
276
    }
277
278
279
    /** @return string the above string as is, or a possibly randomized version, e.g. a random noun */
280
    private function getWordOrRandomWord(string $possiblyRandomWord): string
281
    {
282
        $result = $possiblyRandomWord;
283
284
        if (\strstr($possiblyRandomWord, '|')) {
285
            $splits = \explode('|', $possiblyRandomWord);
286
            \shuffle($splits);
287
            $result = $splits[0];
288
        } else {
289
            foreach (self::POSSIBLE_WORD_TYPES as $wordType) {
290
                $pluralNoun = $this->pluralNounCheck($wordType, $possiblyRandomWord);
291
                $ing = $this->ingVerbCheck($wordType, $possiblyRandomWord);
292
293
                $start = '[' . $wordType . ']';
294
295
                // check the start of the word as it may be suffixed by likes of a question of exclamation mark.
296
                // If no match for the possible word type continue until the next one
297
                if (\substr($possiblyRandomWord, 0, \strlen($start)) !== $start) {
298
                    continue;
299
                }
300
301
                // ---- we are now getting a random word from a file ----
302
                $result = $this->chooseRandomWord($possiblyRandomWord, $wordType, $pluralNoun, $ing);
303
304
                break;
305
            }
306
307
            $faker = Factory::create();
308
            foreach (self::POSSIBLE_FAKER_TYPES as $wordType) {
309
                $start = '[' . $wordType . ']';
310
311
                // check the start of the word as it may be suffixed by likes of a question of exclamation mark.
312
                // If no match for the possible word type continue until the next one
313
                if (\substr($possiblyRandomWord, 0, \strlen($start)) !== $start) {
314
                    continue;
315
                }
316
317
                $result = $faker->$wordType;
318
            }
319
        }
320
321
        // no randomized word has been found, aka no [verb] or [noun], thus append the possibly random word as is
322
        return $result;
323
    }
324
325
326
    /**
327
     * @param string $wordType noun, verb, adjective etc
328
     * @param bool $pluralizeNoun if true pluralize a noun
329
     * @param bool $makeVerbIng if true make a verb an ing verb, e.g. run -> running
330
     */
331
    private function chooseRandomWord(
332
        string $randomWordTypeWithSuffix,
333
        string $wordType,
334
        bool $pluralizeNoun,
335
        bool $makeVerbIng
336
    ): string {
337
        $start = '[' . $wordType . ']';
338
339
        // note the suffix of the word, and append this to the random word
340
        $restOfWord = \str_replace($start, '', $randomWordTypeWithSuffix);
341
342
        // This avoids having to use 'countrie' in the sentence structure file : country -> countries
343
        $wordType = \str_replace('country', 'countrie', $wordType);
344
345
        /** @var string $randomWord */
346
        $randomWord = $this->getRandomWord($wordType);
347
348
        // augment the random word if a plural noun or an ing verb
349
        $this->augmentIfPluralNoun($pluralizeNoun, $randomWord);
350
        $this->augmentIfIngVerb($makeVerbIng, $randomWord);
351
352
        // add the random word, and then the suffix of the word such as ?! or !!
353
        return $randomWord . $restOfWord;
354
    }
355
}
356