Completed
Branch RANDOM_SELECTION (2a0258)
by Gordon
01:12
created

RandomEnglishGenerator::fixEndOfSentence()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
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
            $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
        $result = $this->makeArrayIntoSentence($sentenceArray);
134
        $this->augmentSentenceTitleCase($titleCase, $result);
135
136
        $this->fixEndOfSentence($result);
137
138
        return $result;
139
    }
140
141
142
    /**
143
     * Generate a random title
144
     *
145
     * @return string a random sentence, all in caps, no trailing full stop
146
     */
147
    public function title(): string
148
    {
149
        return $this->sentence(true);
150
    }
151
152
153
    /**
154
     * @param int $maxSentences The maximum number of sentences
155
     * @return string a paragraph of random sentences
156
     */
157
    public function paragraph(int $maxSentences = 10): string
158
    {
159
        $nParagraph = \rand(1, $maxSentences);
160
        $sentences = [];
161
162
        for ($i=0; $i< $nParagraph; $i++) {
163
            $sentences[] = $this->sentence();
164
        }
165
166
        return \implode("  ", $sentences);
167
    }
168
169
170
    /** @return string a plural noun */
171
    public function pluralNoun(): string
172
    {
173
        $plurableNoun = $this->getRandomWord('countable_noun');
174
175
        return $plurableNoun . 's';
176
    }
177
178
179
    /** @return string a plural verb */
180
    public function pluralVerb(): string
181
    {
182
        // @todo Choose a random verb source file
183
        $plurableNoun = $this->getRandomWord('verb');
184
185
        return $plurableNoun . 's';
186
    }
187
188
189
    /** @return string doing version of a verb */
190
    public function verbing(): string
191
    {
192
        // @todo Choose a random verb source file
193
        $plurableNoun = $this->getRandomWord('verb');
194
195
        return $plurableNoun . 'ing';
196
    }
197
198
199
    /**
200
     * @param string $wordType The word type, e.g. 'noun' or 'verb'
201
     * @return bool true if this is a plural noun
202
     */
203
    public function pluralNounCheck(string &$wordType, string &$possiblyRandomWord): bool
204
    {
205
        $pluralNoun = ($wordType === 'plural_noun');
206
        if ($pluralNoun) {
207
            $wordType = 'noun';
208
            $possiblyRandomWord = \str_replace('plural_', '', $possiblyRandomWord);
209
        }
210
211
        return $pluralNoun;
212
    }
213
214
215
    /** @return bool true if this is a verb with ing */
216
    public function ingVerbCheck(string &$wordType, string &$possiblyRandomWord): bool
217
    {
218
        $ing = \substr($wordType, -4) === '_ing';
219
        if ($ing) {
220
            $wordType = \str_replace('_ing', '', $wordType);
221
            $possiblyRandomWord = \str_replace('_ing', '', $possiblyRandomWord);
222
        }
223
224
        return $ing;
225
    }
226
227
228
    public function augmentIfPluralNoun(bool $pluralNoun, string &$randomWord): void
229
    {
230
        if (!$pluralNoun) {
231
            return;
232
        }
233
234
        $helper = new LanguageHelper();
235
        $randomWord = $helper->pluralizeNoun($randomWord);
236
    }
237
238
239
    public function augmentIfIngVerb(bool $ing, string &$randomWord): void
240
    {
241
        if (!$ing) {
242
            return;
243
        }
244
245
        $helper = new LanguageHelper();
246
        $randomWord = $helper->ingVerb($randomWord);
247
    }
248
249
250
    public function augmentSentenceTitleCase(bool $titleCase, string &$result): void
251
    {
252
        if (!$titleCase) {
253
            return;
254
        }
255
256
        $result = \ucwords($result);
257
        $result = \substr_replace($result, "", -1);
258
    }
259
260
261
    /** @param array<string> $sentenceArray */
262
    public function makeArrayIntoSentence(array $sentenceArray): string
263
    {
264
// ensure sentence starts with a capital
265
        $sentenceArray[0] = \ucfirst($sentenceArray[0]);
266
267
        return \implode(" ", $sentenceArray) . '.';
268
    }
269
270
271
    public function fixEndOfSentence(string &$result): void
272
    {
273
        $result = \str_replace('?.', '?', $result);
274
        $result = \str_replace('!.', '?', $result);
275
    }
276
277
278
    /**
279
     * @param string $wordType the type of word, e.g. noun, verb
280
     * @return string a random word for the given word type
281
     */
282
    private function getRandomWord(string $wordType): string
283
    {
284
        $wordsFile = \dirname(__FILE__) . '/../words/english_' . $wordType . 's.txt';
285
        $words = \explode(\PHP_EOL, \trim(\file_get_contents($wordsFile)));
286
        \shuffle($words);
287
288
        return $words[0];
289
    }
290
}
291