Markov chain text generator: Difference between revisions

Line 956:
tears seemed to Alice a good dinner."
</pre>
 
=={{header|PHP}}==
 
<lang PHP><?php
 
// Automated text generator using markov chain
function markovChainTextGenerator($text, $keySize, $maxWords) {
 
// Create list of tokens
$token = array();
$position = 0;
$maxPosition = strlen($text);
while ($position < $maxPosition) {
if (preg_match('/^(\S+)/', substr($text, $position, 25), $matches)) {
$token[] = $matches[1];
$position += strlen($matches[1]);
}
elseif (preg_match('/^(\s+)/', substr($text, $position, 25), $matches)) {
$position += strlen($matches[1]);
}
else {
die(
'Unknown token found at position ' . $position . ' : ' .
substr($text, $position, 25) . '...' . PHP_EOL
);
}
}
 
// Create Dictionary
$dictionary = array();
for ($i = 0 ; $i < count($token) - $keySize ; $i++) {
$prefix = '';
$separator = '';
for ($c = 0 ; $c < $keySize ; $c++) {
$prefix .= $separator . $token[$i + $c];
$separator = '.';
}
$dictionary[$prefix][] = $token[$i + $keySize];
}
 
// Starting token
$rand = rand(0, count($token) - $keySize);
$startToken = array();
for ($c = 0 ; $c < $keySize ; $c++) {
array_push($startToken, $token[$rand + $c]);
}
 
// Create Text
$text = implode(' ', $startToken);
$words = $keySize;
do {
$tokenKey = implode('.', $startToken);
$rand = rand(0, count($dictionary[$tokenKey]) - 1);
$newToken = $dictionary[$tokenKey][$rand];
$text .= ' ' . $newToken;
$words++;
array_shift($startToken);
array_push($startToken, $newToken);
} while($words < $maxWords);
return $text;
 
}
 
srand(5678);
 
$text = markovChainTextGenerator(
file_get_contents(__DIR__ . '/inc/alice_oz.txt'),
3,
308
);
 
echo wordwrap($text, 100, PHP_EOL) . PHP_EOL;</lang>
 
{{out}}
<pre>As soon as they heard her orders they ran away in every direction as fast as they could, Dorothy
only stopping once to pick a beautiful flower; and after a time the ladder was ready. The Scarecrow
climbed up the ladder first, but he was so anxious to get the new house and my wife as soon as
possible. The Lion hesitated no longer, but drank till the dish was empty. How do you do? I'm pretty
well, thank you, replied Dorothy politely. How do you do? I'm not feeling well, said the wolf, and
he dashed away at full speed, followed by the others. It was lucky the Scarecrow and the Tin
Woodman, for we certainly must climb over the wall. When they were on, Dorothy could not take them
off had she wished, but of course she did not wish to leave her little dog behind. Toto had run into
the crowd to bark at the birds sitting there. Dorothy went to the Witch's castle, where he was
placed in a small yard with a high arched room, the walls of which glistened with countless
emeralds. Before them stood a little man about the same height as herself; and when she had made out
the proper way of nursing it, (which was to twist it up into a sort of lullaby to it as she did not
notice when the Scarecrow stumbled into a hole and rolled over to the other side of the Tin Woodman,
sadly; for he is much too heavy to carry I shall have to think about that, replied the little old
woman as her only friend. No, I cannot do that, she replied, but I will give you some supper and a
place to pass the night with you, if you will only hold fast to the tip of my tail.</pre>
 
=={{header|Python}}==
Anonymous user