Created
July 23, 2012 14:25
-
-
Save hikari-no-yume/3163900 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$beginMark = '[[begin]]'; | |
$endMark = '[[end]]'; | |
if (file_exists('relationships.json')) { | |
$relations = json_decode(file_get_contents('relationships.json'), true); | |
} else { | |
$relations = [ | |
$beginMark => [ | |
'Propagandist'/* => 1*/ | |
], | |
'Propagandist' => [ | |
$endMark/* => 1 */ | |
] | |
]; | |
} | |
function feed($text) { | |
global $relations, $beginMark, $endMark; | |
$bits = explode(' ', $text); | |
array_push($bits, $endMark); | |
$prev = $beginMark; | |
foreach ($bits as $word) { | |
if (!isset($relations[$prev])) { | |
$relations[$prev] = []; | |
} | |
/*if (!isset($relations[$prev][$word])) { | |
$relations[$prev][$word] = 0; | |
} | |
$relations[$prev][$word] += 1;*/ | |
array_push($relations[$prev], $word); | |
$prev = $word; | |
} | |
} | |
function spew($word=null, $limit='none') { | |
global $relations, $beginMark, $endMark; | |
$string = ''; | |
if ($word === null) { | |
$word = $beginMark; | |
} | |
while ($word !== $endMark && $limit !== 0) { | |
if ($limit === 'none') { | |
$limit -= 1; | |
} | |
if ($word !== $beginMark) { | |
$string .= $word . ' '; | |
} | |
/*$total = 0; | |
foreach ($relations[$word] as $freq) { | |
$total += $freq; | |
} | |
$rand = rand(1, $total); | |
$at = 0; | |
foreach ($relations[$word] as $nextWord => $freq) { | |
$at += $freq; | |
if ($rand >= $at) { | |
$word = $nextWord; | |
} | |
} | |
if ($rand < $at) { | |
error_log($total .','. $rand .','. $at); | |
}*/ | |
$word = $relations[$word][array_rand($relations[$word])]; | |
} | |
return $string; | |
} | |
if (isset($_POST['input'])) { | |
feed($_POST['input']); | |
} | |
file_put_contents('relationships.json', json_encode($relations)); | |
$images = ['emblem.png', 'flag.jpg', 'sung.jpg', 'jong.jpg', 'leaders.png', 'flag.gif']; | |
$img = $images[array_rand($images)]; | |
?><!doctype html> | |
<meta charset=utf-8> | |
<link rel="shortcut icon" href="<?=$img?>"> | |
<title>Propagandist</title> | |
<style> | |
html { | |
background-color: darkred; | |
color: gold; | |
font-family: 'Georgia', serif; | |
} | |
h1 { | |
clear: both; | |
} | |
div { | |
float: right; | |
margin: 2em; | |
} | |
img { | |
float: left; | |
margin: 1em; | |
} | |
</style> | |
<h1><?=spew('Propagandist', 10)?></h1> | |
<div> | |
<form action=/ method=GET> | |
<input type=submit value=Spew> | |
</form> | |
<form action=/ method=POST> | |
<textarea name=input></textarea><br><input type=submit value=Feed> | |
</form> | |
</div> | |
<?php | |
echo '<img src="' . $img . '" alt="DPRK" title="' . htmlspecialchars(spew()) . '">'; | |
?> | |
<p><?=nl2br(htmlspecialchars(spew()))?></p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment