Completed
Push — master ( dc9884...dd4443 )
by Gordon
01:01
created

RandomEnglishGenerator::sentence()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 2
nc 2
nop 1
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
            $word = $this->getWordOrRandomWord($possiblyRandomWord);
87
            $sentenceArray[] = $word;
88
        }
89
90
        $result = $this->makeArrayIntoSentence($sentenceArray);
91
        $this->augmentSentenceTitleCase($titleCase, $result);
92
93
        return $result;
94
    }
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
103
    {
104
        return $this->sentence(true);
105
    }
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
113
    {
114
        $nParagraph = \rand(1, $maxSentences);
115
        $sentences = [];
116
117
        for ($i = 0; $i < $nParagraph; $i++) {
118
            $sentences[] = $this->sentence();
119
        }
120
121
        return \implode("  ", $sentences);
122
    }
123
124
125
    /** @return string a plural noun */
126
    public function pluralNoun(): string
127
    {
128
        $plurableNoun = $this->getRandomWord('countable_noun');
129
130
        return $plurableNoun . 's';
131
    }
132
133
134
    /** @return string the ing version of a verb, e.g. run -> running */
135
    public function verbing(): string
136
    {
137
        // @todo Choose a random verb source file
138
        $ingVerb = $this->getRandomWord('verb');
139
140
        return $ingVerb . 'ing';
141
    }
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
149
    {
150
        $pluralNoun = ($wordType === 'plural_noun');
151
        if ($pluralNoun) {
152
            $wordType = 'noun';
153
            $possiblyRandomWord = \str_replace('plural_', '', $possiblyRandomWord);
154
        }
155
156
        return $pluralNoun;
157
    }
158
159
160
    /** @return bool true if this is a verb with ing */
161
    public function ingVerbCheck(string &$wordType, string &$possiblyRandomWord): bool
162
    {
163
        $ing = \substr($wordType, -4) === '_ing';
164
        if ($ing) {
165
            $wordType = \str_replace('_ing', '', $wordType);
166
            $possiblyRandomWord = \str_replace('_ing', '', $possiblyRandomWord);
167
        }
168
169
        return $ing;
170
    }
171
172
173
    public function augmentIfPluralNoun(bool $pluralNoun, string &$randomWord): void
174
    {
175
        if (!$pluralNoun) {
176
            return;
177
        }
178
179
        $helper = new LanguageHelper();
180
        $randomWord = $helper->pluralizeNoun($randomWord);
181
    }
182
183
184
    public function augmentIfIngVerb(bool $ing, string &$randomWord): void
185
    {
186
        if (!$ing) {
187
            return;
188
        }
189
190
        $helper = new LanguageHelper();
191
        $randomWord = $helper->ingVerb($randomWord);
192
    }
193
194
195
    public function augmentSentenceTitleCase(bool $titleCase, string &$result): void
196
    {
197
        if (!$titleCase) {
198
            return;
199
        }
200
201
        $result = \ucwords($result);
202
        $result = \substr_replace($result, "", -1);
203
    }
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
217
    {
218
        // ensure sentence starts with a capital
219
        $sentenceArray[0] = \ucfirst($sentenceArray[0]);
220
221
        $sentence = \implode(" ", $sentenceArray) . '.';
222
        $this->fixEndOfSentence($sentence);
223
224
        return $sentence;
225
    }
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
234
    {
235
        $result = \str_replace('?.', '?', $result);
236
        $result = \str_replace('!.', '?', $result);
237
    }
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
245
    {
246
        $wordsFile = \dirname(__FILE__) . '/../words/english_' . $wordType . 's.txt';
247
        $words = \explode(\PHP_EOL, \trim(\file_get_contents($wordsFile)));
248
        \shuffle($words);
249
250
        return $words[0];
251
    }
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
256
    {
257
        $result = $possiblyRandomWord;
258
259
        if (\strstr($possiblyRandomWord, '|')) {
260
            $splits = \explode('|', $possiblyRandomWord);
261
            \shuffle($splits);
262
            $result = $splits[0];
263
        } else {
264
            foreach (self::POSSIBLE_WORD_TYPES as $wordType) {
265
                $pluralNoun = $this->pluralNounCheck($wordType, $possiblyRandomWord);
266
                $ing = $this->ingVerbCheck($wordType, $possiblyRandomWord);
267
268
                $start = '[' . $wordType . ']';
269
270
                // check the start of the word as it may be suffixed by likes of a question of exclamation mark.
271
                // If no match for the possible word type continue until the next one
272
                if (\substr($possiblyRandomWord, 0, \strlen($start)) !== $start) {
273
                    continue;
274
                }
275
276
                // ---- we are now getting a random word from a file ----
277
                $result = $this->chooseRandomWord($possiblyRandomWord, $wordType, $pluralNoun, $ing);
278
279
                break;
280
            }
281
        }
282
283
        // no randomized word has been found, aka no [verb] or [noun], thus append the possibly random word as is
284
        return $result;
285
    }
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(
294
        string $randomWordTypeWithSuffix,
295
        string $wordType,
296
        bool $pluralizeNoun,
297
        bool $makeVerbIng
298
    ): string {
299
        $start = '[' . $wordType . ']';
300
       // error_log('WT:' . $randomWordTypeWithSuffix);
301
302
        // note the suffix of the word, and append this to the random word
303
        $restOfWord = \str_replace($start, '', $randomWordTypeWithSuffix);
304
305
        // This avoids having to use 'countrie' in the sentence structure file : country -> countries
306
        $wordType = \str_replace('country', 'countrie', $wordType);
307
308
        /** @var string $randomWord */
309
        $randomWord = $this->getRandomWord($wordType);
310
311
        // augment the random word if a plural noun or an ing verb
312
        $this->augmentIfPluralNoun($pluralizeNoun, $randomWord);
313
        $this->augmentIfIngVerb($makeVerbIng, $randomWord);
314
315
        // add the random word, and then the suffix of the word such as ?! or !!
316
        return $randomWord . $restOfWord;
317
    }
318
}
319