Added sentence generation

This commit is contained in:
Joshua Sherman 2014-05-13 19:04:13 -04:00
parent 6239233483
commit ac5eff1575
2 changed files with 56 additions and 12 deletions

View file

@ -4,11 +4,11 @@ namespace joshtronic;
class LoremIpsum class LoremIpsum
{ {
private $first = true; private $first = true;
private $sentence_mean = 24.46; private $sentence_mean = 24.46;
private $sentence_stdev = 5.08; private $sentence_std_dev = 5.08;
private $paragraph_mean = 5.8; private $paragraph_mean = 5.8;
private $paragraph_stdev = 1.93; private $paragraph_std_dev = 1.93;
public $words = array( public $words = array(
// Lorem ipsum... // Lorem ipsum...
@ -22,7 +22,7 @@ class LoremIpsum
'commodo', 'condimentum', 'congue', 'consequat', 'commodo', 'condimentum', 'congue', 'consequat',
'conubia', 'convallis', 'cras', 'cubilia', 'conubia', 'convallis', 'cras', 'cubilia',
'cum', 'curabitur', 'curae', 'cursus', 'cum', 'curabitur', 'curae', 'cursus',
'dapibus', 'diam', 'dictum', 'dictumst', 'dapibus', 'diam', 'dictum', 'dictumst',
'dignissim', 'dis', 'donec', 'dui', 'dignissim', 'dis', 'donec', 'dui',
'duis', 'egestas', 'eget', 'eleifend', 'duis', 'egestas', 'eget', 'eleifend',
'elementum', 'enim', 'erat', 'eros', 'elementum', 'enim', 'erat', 'eros',
@ -71,6 +71,7 @@ class LoremIpsum
return $this->words($count, $tags, true); return $this->words($count, $tags, true);
} }
// TODO Need to refactor to allow for counts larger than array of words
public function words($count = 1, $tags = false, $array = false) public function words($count = 1, $tags = false, $array = false)
{ {
$this->shuffle(); $this->shuffle();
@ -90,9 +91,18 @@ class LoremIpsum
return $this->sentences($count, $tags, true); return $this->sentences($count, $tags, true);
} }
public function sentences() public function sentences($count = 1, $tags = false, $array = false)
{ {
$sentences = array();
for ($i = 0; $i < $count; $i++)
{
$sentences[] = $this->wordsArray($this->gauss($this->sentence_mean, $this->sentence_std_dev));
}
$this->punctuate($sentences);
return $this->output($sentences, $tags, $array);
} }
public function paragraph($tags = false) public function paragraph($tags = false)
@ -105,11 +115,20 @@ class LoremIpsum
return $this->paragraphs($count, $tags, true); return $this->paragraphs($count, $tags, true);
} }
public function paragraphs() public function paragraphs($count = 1, $tags = false, $array = false)
{ {
} }
private function gauss($mean, $std_dev)
{
$x = mt_rand() / mt_getrandmax();
$y = mt_rand() / mt_getrandmax();
$z = sqrt(-2 * log($x)) * cos(2 * pi() * $y);
return $z * $std_dev + $mean;
}
private function shuffle() private function shuffle()
{ {
if ($this->first) if ($this->first)
@ -129,6 +148,33 @@ class LoremIpsum
} }
} }
private function punctuate(&$sentences)
{
foreach ($sentences as $key => $sentence)
{
$words = count($sentence);
if ($words > 4)
{
$mean = log($words, 6);
$std_dev = $mean / 6;
$commas = round($this->gauss($mean, $std_dev));
for ($i = 1; $i <= $commas; $i++)
{
$word = round($i * $words / ($commas + 1));
if ($word < ($words - 1) && $word > 0)
{
$sentence[$word] .= ',';
}
}
$sentences[$key] = ucfirst(implode(' ', $sentence) . '.');
}
}
}
private function output($strings, $tags, $array) private function output($strings, $tags, $array)
{ {
if ($tags) if ($tags)

View file

@ -36,7 +36,6 @@ class LoremIpsumTest extends PHPUnit_Framework_TestCase
} }
} }
/*
public function testSentence() public function testSentence()
{ {
$this->assertRegExp('/^[a-z, ]+\.$/i', $this->lipsum->sentence()); $this->assertRegExp('/^[a-z, ]+\.$/i', $this->lipsum->sentence());
@ -53,12 +52,13 @@ class LoremIpsumTest extends PHPUnit_Framework_TestCase
$this->assertTrue(is_array($sentences)); $this->assertTrue(is_array($sentences));
$this->assertCount(3, $sentences); $this->assertCount(3, $sentences);
foreach ($words as $word) foreach ($sentences as $sentence)
{ {
$this->assertRegExp('/^[a-z, ]+\.$/i', $sentence); $this->assertRegExp('/^[a-z, ]+\.$/i', $sentence);
} }
} }
/*
public function testParagraph() public function testParagraph()
{ {
@ -102,7 +102,6 @@ class LoremIpsumTest extends PHPUnit_Framework_TestCase
); );
} }
/*
public function testMarkupArrayReturn() public function testMarkupArrayReturn()
{ {
$words = $this->lipsum->wordsArray(3, 'li'); $words = $this->lipsum->wordsArray(3, 'li');
@ -114,7 +113,6 @@ class LoremIpsumTest extends PHPUnit_Framework_TestCase
$this->assertRegExp('/^<li>[a-z]+<\/li>$/i', $word); $this->assertRegExp('/^<li>[a-z]+<\/li>$/i', $word);
} }
} }
*/
} }
?> ?>