-
Notifications
You must be signed in to change notification settings - Fork 0
/
nonsentences.php
executable file
·232 lines (190 loc) · 7.46 KB
/
nonsentences.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
* Nonsentences
*
* V1.4 - 2018-Jul-30
*
* A nonsense sentence and title generator.
* Copyright 2015-2018 Chris Mospaw
*
* Original code based on Nonsense Generator 2.0.3
* http://www.jholman.com/scripts/nonsense/ (URL now defunct)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* See LICENSE.md for the terms of the GNU GPL.
*
* See README.md / example.php for installation and usage instructions.
*
*/
// Add WP-CLI support if needed.
if (defined('WP_CLI') && WP_CLI) {
include_once __DIR__ . '/wp-cli.php';
}
class Nonsentences
{
protected $lists = array( "interjections", "determiners", "adjectives", "nouns", "adverbs", "verbs", "prepositions", "conjunctions", "comparatives" );
protected $vowels = array( 'a', 'e', 'i', 'o', 'u' );
protected $wordlists = array();
protected $punctuation = array( ',', '.', '?', '!' );
protected $sentence_structures;
protected $title_structures;
public $min_sentences = 3;
public $max_sentences;
public $min_paragraphs = 3;
public $max_paragraphs;
public $paragraph_wrapper = array( '<p>', '</p>' );
public $taxonomy;
public $taxonomy_term;
// Used by WP-CLI
public $number_of_posts;
public $post_type;
public function __construct($args)
{
if (isset($args['min_sentences'])) {
$this->min_sentences = $args['min_sentences'];
}
if (isset($args['max_sentences'])) {
$this->max_sentences = $args['max_sentences'];
}
if (isset($args['min_paragraphs'])) {
$this->min_paragraphs = $args['min_paragraphs'];
}
if (isset($args['max_paragraphs'])) {
$this->max_paragraphs = $args['max_paragraphs'];
}
if (isset($args['number_of_posts'])) {
$this->number_of_posts = $args['number_of_posts'];
}
if (isset($args['post_type'])) {
$this->post_type = $args['post_type'];
}
if (isset($args['paragraph_wrapper'])) {
$this->paragraph_wrapper = $args['paragraph_wrapper'];
}
if (isset($args['taxonomy'])) {
$this->taxonomy = $args['taxonomy'];
}
if (isset($args['taxonomy_term'])) {
$this->taxonomy_term = $args['taxonomy_term'];
}
foreach ($this->lists as $part) {
$this->wordlists[$part] = file(realpath(dirname(__FILE__)) . '/db/' . $part . '.txt');
}
// Sentence structures ... each is randomly selected. The first two are weighted since they're a bit more "normal",
$this->sentence_structures = array(
explode(' ', '[determiners] [adjectives] [nouns] [verbs] [prepositions] [determiners] [nouns] .'),
explode(' ', '[determiners] [adjectives] [nouns] [verbs] [prepositions] [determiners] [nouns] .'),
explode(' ', '[determiners] [adjectives] [nouns] [verbs] [prepositions] [determiners] [nouns] .'),
explode(' ', '[determiners] [adjectives] [nouns] [adverbs] [verbs] [prepositions] [determiners] [adjectives] [nouns] .'),
explode(' ', '[determiners] [adjectives] [nouns] [adverbs] [verbs] [prepositions] [determiners] [adjectives] [nouns] .'),
explode(' ', '[determiners] [adjectives] [nouns] [adverbs] [verbs] [prepositions] [determiners] [adjectives] [nouns] .'),
explode(' ', '[interjections] , [determiners] [nouns] is [comparatives] [adjectives] than [determiners] [adjectives] [nouns] .'),
explode(' ', '[adjectives] [plural_nouns] [verbs] [prepositions] [plural_nouns] [adverbs] .'),
explode(' ', '[determiners] [nouns] , [adverbs] [verbs] the [nouns] .'),
explode(' ', '[interjections] ! The [nouns] [verbs] the [nouns] .'),
);
// Title structures ... each is randomly selected.
$this->title_structures = array(
explode(' ', '[determiners] [nouns] [adverbs] [verbs] [determiners] [nouns]'),
explode(' ', '[determiners] [nouns] [verbs]'),
explode(' ', '[adverbs] [verbs] [nouns]'),
explode(' ', '[interjections] , [determiners] [nouns] [verbs]'),
explode(' ', '[adjectives] [nouns]'),
);
}
/**
* Get a number of nonsense sentences
*/
public function sentences()
{
if ($this->max_sentences === null) {
$this->max_sentences = $this->min_sentences;
}
$count = rand($this->min_sentences, $this->max_sentences);
$output = '';
for ($x=0; $x < $count; $x++) {
$output .= $this->sentence() . ' ';
}
return $output;
}
/**
* Return a number of paragraphs of assembled from random sentences, wrapped in the
* configured wrapper.
*/
public function paragraphs()
{
if ($this->max_paragraphs === null) {
$this->max_paragraphs = $this->min_paragraphs;
}
$count = rand($this->min_paragraphs, $this->max_paragraphs);
$output = '';
for ($x=0; $x < $count; $x++) {
$output .= $this->paragraph_wrapper[0];
$output .= $this->sentences() . ' ';
$output .= $this->paragraph_wrapper[1];
}
return $output . PHP_EOL;
}
/**
* Generate one nonsense sentence
*/
public function sentence()
{
return $this->get_words('sentence_structures');
}
/**
* Generate one nonsense title
*/
public function title()
{
return $this->get_words('title_structures');
}
/**
* Get a list of words assembled from ramdom words in the lists as determined by the
* passed structure type.
*/
public function get_words($structure_type)
{
$wordcount = array();
$type = rand(0, (count($this->{$structure_type}) - 1));
for ($i=0; $i < 2; $i++) {
foreach ($this->lists as $part) {
${$part}[$i] = trim($this->wordlists[$part][rand(0, count($this->wordlists[$part]) - 1)]);
$wordcount[$part] = 0;
}
}
$sentence_structure = $this->{$structure_type}[$type];
$word_list = '';
foreach ($sentence_structure as $position => $word) {
switch (1) {
case ($position == 0):
$word = str_replace(array( '[' , ']' ), array( '', ''), $word);
$word_list .= ucfirst(${$word}[ $wordcount[$word] ]);
$wordcount[ $word ]++;
break;
case ($word == '[plural_nouns]'):
$word_list .= $nouns[ $wordcount['nouns'] ] . 's';
$wordcount['nouns']++;
break;
case (substr($word, 0, 1) == '['):
$word = str_replace(array( '[' , ']' ), array( '', ''), $word);
$word_list .= ${$word}[ $wordcount[$word] ];
$wordcount[ $word ]++;
break;
case (! in_array($word, $this->punctuation)):
$word_list .= $word;
break;
}
if (in_array($word, $this->punctuation)) {
$word_list = trim($word_list) . $word . ' ';
} else {
$word_list .= ' ';
}
}
return $word_list;
}
}