Completed
Push — master ( 4511fa...c3d0d4 )
by Gordon
10:20 queued 09:20
created

RandomEnglishGenerator::pluralNoun()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php declare(strict_types = 1);
2
3
namespace Suilven\RandomEnglish;
4
5
class RandomEnglishGenerator
6
{
7
8
    private const POSSIBLE_WORD_TYPES = [
9
        'noun',
10
        'countable_noun',
11
12
        'adjective',
13
        'preposition',
14
        'conjunction',
15
        'contraction',
16
        'control_verb',
17
18
        'sequence_adverb',
19
        'frequency_adverb',
20
21
        // pluralize and ing these?
22
        'intransitive_verb',
23
        'irregular_verb',
24
        'transitive_verb',
25
        'verb',
26
27
28
        // positions
29
        'locative',
30
31
32
    ];
33
34
    /** @var string configuration file for sentences */
35
    private $config = '';
36
37
    public function __construct()
38
    {
39
        $configFile = \dirname(__FILE__) . '/../config/sentence-structure.cfg';
40
        $this->setConfig(\file_get_contents($configFile));
41
    }
42
43
    /** @param string $newConfig configuration file of how sentence structures */
44
    public function setConfig(string $newConfig): void
45
    {
46
        $this->config = \trim($newConfig);
47
    }
48
49
    /**
50
     * Generate a random sentence
51
     *
52
     * @param bool $titleCase true to generate a title, false (default) to generate a sentence
53
     * @return string a random sentence
54
     */
55
    public function sentence(bool $titleCase = false): string
56
    {
57
        $structures = \explode(\PHP_EOL, $this->config);
58
        \shuffle($structures);
59
        $structure = $structures[0];
60
        
61
        $expression='/[\s]+/';
62
        $splits = \preg_split($expression, $structure, -1, \PREG_SPLIT_NO_EMPTY);
63
64
        $sentenceArray = [];
65
66
        foreach ($splits as $possiblyRandomWord) {
67
            $randomized = false;
68
69
            foreach (self::POSSIBLE_WORD_TYPES as $wordType) {
70
                $start = '[' . $wordType . ']';
71
72
                if (\substr($possiblyRandomWord, 0, \strlen($start)) === $start) {
73
                    $restOfWord = \str_replace($start, '', $possiblyRandomWord);
74
                    $sentenceArray[] = $this->getRandomWord($wordType) . $restOfWord;
75
                    $randomized = true;
76
77
                    break;
78
                }
79
            }
80
81
            if ($randomized) {
82
                continue;
83
            }
84
85
            $sentenceArray[] = $possiblyRandomWord;
86
        }
87
88
        // ensure sentence starts with a capital
89
        $sentenceArray[0] = \ucfirst($sentenceArray[0]);
90
91
        $result = \implode(" ", $sentenceArray) . '.';
92
93
        if ($titleCase) {
94
            $result = \ucwords($result);
95
            $result = \substr_replace($result, "", -1);
96
        }
97
98
        return $result;
99
    }
100
101
    /**
102
     * Generate a random title
103
     *
104
     * @return string a random sentence, all in caps, no trailing full stop
105
     */
106
    public function title(): string
107
    {
108
        return $this->sentence(true);
109
    }
110
111
    /**
112
     * @param int $maxSentences The maximum number of sentences
113
     * @return string a paragraph of random sentences
114
     */
115
    public function paragraph(int $maxSentences = 10): string
116
    {
117
        $nParagraph = \rand(1, $maxSentences);
118
        $sentences = [];
119
120
        for ($i=0; $i< $nParagraph; $i++) {
121
            $sentences[] = $this->sentence();
122
        }
123
124
        return \implode("  ", $sentences);
125
    }
126
127
    /** @return string a plural noun */
128
    public function pluralNoun(): string
129
    {
130
        $plurableNoun = $this->getRandomWord('countable_noun');
131
132
        return $plurableNoun . 's';
133
    }
134
135
    /** @return string a plural verb */
136
    public function pluralVerb(): string
137
    {
138
        // @todo Choose a random verb source file
139
        $plurableNoun = $this->getRandomWord('verb');
140
141
        return $plurableNoun . 's';
142
    }
143
144
    /** @return string doing version of a verb */
145
    public function verbing(): string
146
    {
147
        // @todo Choose a random verb source file
148
        $plurableNoun = $this->getRandomWord('verb');
149
150
        return $plurableNoun . 'ing';
151
    }
152
153
    /**
154
     * @param string $wordType the type of word, e.g. noun, verb
155
     * @return string a random word for the given word type
156
     */
157
    private function getRandomWord(string $wordType): string
158
    {
159
        $wordsFile = \dirname(__FILE__) . '/../words/english_' . $wordType . 's.txt';
160
        $words = \explode(\PHP_EOL, \trim(\file_get_contents($wordsFile)));
161
        \shuffle($words);
162
163
        return $words[0];
164
    }
165
166
}
167