-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.php
57 lines (55 loc) · 1.77 KB
/
generate.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
<?php
$numParagraphs = intval($_POST["p"]);
$commaOrPeriod = array(".", ",");
function generateIpsum($numParagraphs) {
// Prints paragraphs of filler text.
// if ($numParagraphs==null) {
// $numParagraphs = 3;
// }
$paragraphs = array();
for ($i=1; $i<=$numParagraphs; $i++) {
$paragraph = generateParagraph();
$paragraphs[] = $paragraph;
}
return $paragraphs;
}
function generateParagraph() {
// Returns paragraph.
global $allWords;
$numPhrases = mt_rand(6,7);
shuffle($allWords);
$paragraph = generatePhrases($allWords, $numPhrases, "", ".");
// echo "<p class='paragraph'>" . $paragraph . "</p>";
return $paragraph;
}
function generatePhrases($words, $numPhrases, $phrasesSoFar, $currDelimiter) {
// Returns phrases specified; iterates recursively
global $commaOrPeriod;
if ($numPhrases==0) {
// base case, replace last character with "." no matter what
$phrases = substr_replace($phrasesSoFar, ".", -2);
return $phrases;
} else {
$numWords = mt_rand(4,6);
if ($currDelimiter==".") {
// start of new sentence, capitalize first word
$phrasesSoFar .= ucfirst(next($words)) . " ";
for ($i=2; $i<$numWords; $i++) {
$phrasesSoFar .= next($words) . " ";
}
shuffle($commaOrPeriod);
$currDelimiter = $commaOrPeriod[0];
$phrasesSoFar .= next($words) . $currDelimiter . " ";
return generatePhrases($words, $numPhrases-1, $phrasesSoFar, $currDelimiter);
} else {
// delimiter was comma, do not capitalize first word
for ($i=1; $i<$numWords; $i++) {
$phrasesSoFar .= next($words) . " ";
}
$phrasesSoFar .= next($words) . ". ";
return generatePhrases($words, $numPhrases-1, $phrasesSoFar, ".");
}
}
}
$paragraphs = generateIpsum($numParagraphs);
?>