forked from RobertAKARobin/ineedaprompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
300 lines (266 loc) · 10.1 KB
/
index.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?
function _array_column($array,$key){
foreach($array as $row) $return[] = $row[$key];
return $return;
}
function sentenceOrder($a,$b){
global $typeValues;
return $typeValues[$a] - $typeValues[$b];
}
function oxfordComma($array,$key,$field){
if($array[$key][$field] == $array[$key + 1][$field]){
if($array[$key][$field] == $array[$key + 2][$field]){
return ",";
}else if($array[$key][$field] == $array[$key - 1][$field]){
return ", and";
}else{
return " and";
}
}
}
$typeOptions = array("adjective","noun","adverb","verb","location");
$default = array("adjective","noun","verb","noun");
$vowels = array("a","e","i","o","u");
$prepositions = "/\s(aboard$|about$|above$|across$|after$|against$|along$|alongside$|amid$|among$|anti$|around$|as$|at$|before$|behind$|below$|beneath$|beside$|besides$|between$|beyond$|but$|by$|concerning$|considering$|despite$|down$|during$|except$|excepting$|excluding$|following$|for$|from$|in$|inside$|into$|like$|minus$|near$|of$|off$|on$|onto$|opposite$|outside$|over$|past$|per$|plus$|regarding$|round$|save$|since$|than$|through$|to$|toward$|towards$|under$|underneath$|unlike$|until$|up$|upon$|versus$|via$|with$|within$|without)/i";
$x = 0;
foreach($typeOptions as $type){
$typeValues[$type] = $x;
$x++;
}
$dictionary = json_decode(file_get_contents("dictionary.json"),true);
//Get params from URL. If a number, return that many random word types.
$order = @array_slice($_GET["prompt"],0,20);
if(empty($_GET) || empty($_GET["prompt"])) $order = $default;
if(!is_array($_GET["prompt"])) $order = array($_GET["prompt"]);
foreach($order as $key => $val){
if(is_numeric($val)){
$order = null;
if($val > 20) $val = 20;
for($x = 0; $x < $val; $x++){
$order[] = $typeOptions[array_rand(array_keys($typeOptions))];
}
break;
}
$val = preg_replace("/[0-9]+/","",$val);
if(!in_array($val,$typeOptions) || empty($_GET)){
$order = $default;
break;
}
$order[$key] = $val;
}
//$order = json_decode('["verb","noun","adjective","adverb","adverb","verb","adjective","verb","location","location","noun","noun","adjective","location","verb"]');
//Split into objects by type (noun or verb).
$objectNum = 0;
foreach($order as $type){
$objects[$objectNum]["words"][] = $type;
$objects[$objectNum]["objectType"] = $type;
if(in_array($type,array("noun","verb"))) $objectNum++;
}
foreach($objects as $objectNum => $object){
//Sort and do any necessary replacing within objects to make grammatic sense.
foreach($object["words"] as $key => $type){
if(count($order) > 1){
switch($object["objectType"]){
case "noun": if($type == "adverb") $type = "adjective"; break;
case "verb": if($type == "adjective") $type = "adverb"; break;
case "location":
if(in_array($type,array("adverb","adjective"))) $type = "location";
if($objectNum == count($objects) - 1) break;
default: $type = "noun"; $object["objectType"] = $type; break;
}
}
$object["words"][$key] = $type;
}
usort($object["words"],"sentenceOrder");
foreach($object["words"] as $key => $type){
$newOrder[] = $type;
//Get words from dictionary. No duplicates.
do{
$word = $dictionary[$type][array_rand($dictionary[$type])];
}while(@in_array($word,$wordsUsed));
$wordsUsed[] = $word;
$object["words"][$key] = array($type,$word);
}
$objects[$objectNum] = array(
"words" => $object["words"],
"objectType" => $object["objectType"]
);
}
foreach($objects as $objectNum => $object){
$objectType = $object["objectType"];
//Before object
if($objectType == "noun"){
$sentence[$objectNum] .= (
in_array(
strtolower(
substr($object["words"][0][1],0,1)
)
,$vowels) ? " an" : " a");
}else if($objectType == "verb"){
if(
$objects[$objectNum - 1]["objectType"] == "noun"
&&count($objects) > 3
){
$sentence[$objectNum] .= " who is";
}
}
//In object
$types = _array_column($object["words"],"0");
$typeTotal = array_count_values($types);
$typeCount = array();
foreach($object["words"] as $key => $pair){
$type = $pair[0];
$word = $pair[1];
//In word
if($type == "verb"){
if(
$objects[$objectNum + 1]["objectType"] == "verb"
||$objects[$objectNum + 1]["objectType"] == "location"
||empty($objects[$objectNum + 1])
||$objects[$objectNum]["words"][$key + 1][0] == "location"
){
$word = preg_replace($prepositions,"",$word);
}
}
$sentence[$objectNum] .= " $word";
//After word
$typeCount[$pair[0]]++;
if($type == "adjective"){
if($typeTotal[$type] > 1 && $typeCount[$type] < $typeTotal[$type]){
$sentence[$objectNum] .= ",";
}
}
if($type == "adverb"){
$sentence[$objectNum] .= oxfordComma($object["words"],$key,0);
}
if(
$type == "location"
&&$key == count($object["words"]) - 1
&&$objectType == "verb"
&&$objects[$objectNum + 1]["objectType"] == "noun"
){
$sentence[$objectNum] .= " with";
}
}
//After object
if(in_array($objectType,array("noun","verb"))){
$sentence[$objectNum] .= oxfordComma($objects,$objectNum,"objectType");
}
}
foreach($sentence as $word){
$output .= $word;
}
$result = ucfirst(trim($output)) . ".";
if($counter = file_get_contents("counter.txt")){
$counter++;
file_put_contents("counter.txt", $counter);
}
if(isset($_GET["api"])) die($result);
$twitter = "https://twitter.com/intent/tweet?text=%23ineedaprompt%20" . urlencode($result);
$reddit = "http://reddit.com/r/ineedaprompt/submit?selftext=true&title=" . urlencode($result);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="description" content="A prompt generator for drawing or writing." />
<meta name="viewport" content="width=800" />
<link rel="stylesheet" type="text/css" href="indexcss.css" />
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<title>I Need A Prompt</title>
<base target="_blank" />
</head>
<body>
<header>
<form method="get" action="." target="_top" enctype="multipart/form-data">
<h1><span><?php
echo $result;
?></span></h1>
<ul>
<li><a href="<?php echo $twitter; ?>">Tweet it: #ineedaprompt</a></li>
<li><a href="<?php echo $reddit; ?>">Post it to /r/ineedaprompt</a></li>
</ul>
<ul id="options">
<?php
$sentenceOrder = array(
"adjective",
"adjective",
"noun",
"adverb",
"verb",
"adjective",
"adjective",
"noun",
"location"
);
$used = array();
do{
foreach($sentenceOrder as $key => $type){
reset($newOrder);
$isChecked = "";
$used[$type][] = 1;
$label = $type . count($used[$type]);
if($type == current($newOrder)){
$isChecked = "checked=\"checked\"";
unset($newOrder[key($newOrder)]);
}
echo <<<EOF
<li><input name="prompt[]" value="$label" type="checkbox" id="$label" $isChecked><label for="$label">$type</label></li>
EOF;
}
$sentenceOrder = $newOrder;
}while(count($newOrder) > 0);
?>
</ul>
<button type="submit"><span class="button">I need a prompt!</span><small><?php echo $counter; ?></small></button>
</form>
</header>
<main>
<nav>
<ul>
<li><label for="toggleDic">Dictionary</label></li>
<li><label for="toggleApi">API</label></li>
<li><a href="https://github.com/robertgfthomas/ineedaprompt">GitHub</a></li>
<li><a href="http://www.robertakarobin.com">RobertAKARobin</a></li>
<li><a href="mailto:[email protected]">Contact</a></li>
<li><a href="https://twitter.com/search?f=realtime&q=ineedaprompt">#ineedaprompt</a></li>
</ul>
</nav>
<input type="radio" name="toggle" id="toggleDic" />
<article id="dictionary">
<h2><label for="toggleDic">The INAP Dictionary</label></h2>
<p>All words and phrases are (I think) PG and suitable for all ages. Please <a href="mailto:[email protected]">e-mail me</a> or <a href="http://www.reddit.com/r/ineedaprompt/comments/1uh2fy/have_ideas_for_new_words_or_phrases/">post on Reddit</a> to suggest new ones!</p>
<dl>
<?php
$total = 0;
foreach($dictionary as $type => $words){
natcasesort($words);
$dictionary[$type] = $words;
}
foreach($dictionary as $type => $words){
echo "<dt>" . ucfirst($type) . "s (" . count($words) . ")</dt>";
foreach($words as $word){
$total++;
echo "<dd>" . ucfirst($word) . "</dd>";
}
}
?>
</dl>
</article>
<input type="radio" name="toggle" id="toggleApi" />
<article id="api">
<h2><label for="toggleApi">The INAP API</label></h2>
<p>The INAP dictionary is available as JSON <a href="dictionary.json">here</a>.</p>
<p>To get a prompt, just make a GET request to <code><a href="http://ineedaprompt.com/index.php?api">ineedaprompt.com?api</a></code>. Omitting the <code>api</code> will return the full webpage (what you're looking at now).</p>
<p>Specify your prompt's terms and their order by adding a <code>prompt[]</code> parameter for each term. Accepted values are <code>adjective</code>, <code>noun</code>, <code>adverb</code>, <code>verb</code> and <code>location</code>.</p>
<p>For example:</p>
<p><code><a href="index.php?api&prompt%5B%5D=noun&prompt%5B%5D=verb&prompt%5B%5D=adjective&prompt%5B%5D=adverb&prompt%5B%5D=location&prompt%5B%5D=noun">http://ineedaprompt.com?api&prompt[]=noun&prompt[]=verb&prompt[]=adjective&prompt[]=adverb&prompt[]=location&prompt[]=noun</a></code></p>
<p>If you instead specify a number, the API will return a prompt with that many terms of randomly-selected types.</p>
<p>For example:</p>
<p><code><a href="index.php?api&prompt=15">http://ineedaprompt.com?api&prompt=15</a></code></p>
<p>The API will return up to 20 terms.</p>
</article>
</main>
</body>
</html>