-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7835af8
Showing
5 changed files
with
1,185 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
/* | ||
PHP Markov Chain text generator 1.0 | ||
Copyright (c) 2008-2010, Hay Kranen <http://www.haykranen.nl/projects/markov/> | ||
Fork on Github: < http://github.com/hay/markov > | ||
*/ | ||
|
||
require 'markov.php'; | ||
|
||
if (isset($_POST['submit'])) { | ||
// generate text with markov library | ||
$order = $_REQUEST['order']; | ||
$length = $_REQUEST['length']; | ||
$input = $_REQUEST['input']; | ||
$ptext = $_REQUEST['text']; | ||
|
||
if ($input) $text = $input; | ||
if ($ptext) $text = file_get_contents("text/".$ptext.".txt"); | ||
|
||
if(isset($text)) { | ||
$markov_table = generate_markov_table($text, $order); | ||
$markov = generate_markov_text($length, $markov_table, $order); | ||
|
||
if (get_magic_quotes_gpc()) $markov = stripslashes($markov); | ||
} | ||
} | ||
?> | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>PHP Markov chain text generator by Hay Kranen</title> | ||
<link rel="stylesheet" type="text/css" href="http://static.haykranen.nl/common/style.css" /> | ||
<?php echo file_get_contents('http://static.haykranen.nl/common/nav/html/head.php'); ?> | ||
</head> | ||
<body> | ||
<?php echo file_get_contents('http://static.haykranen.nl/common/nav/html/nav.php'); ?> | ||
<div id="wrapper"> | ||
<h1>PHP Markov chain text generator</h1> | ||
<p>This is a very simple Markov chain text generator. Try it below by entering some | ||
text or by selecting one of the pre-selected texts available. </p> | ||
<p>The source code of this generator is available under the terms of the <a href="http://www.opensource.org/licenses/mit-license.php">MIT license</a>.See the original posting on this generator <a href="http://www.haykranen.nl/projects/markov">here</a>.</p> | ||
|
||
<?php if (isset($markov)) : ?> | ||
<h2>Output text</h2> | ||
<textarea rows="20" cols="80" readonly="readonly"><?php echo $markov; ?></textarea> | ||
<?php endif; ?> | ||
|
||
<h2>Input text</h2> | ||
<form method="post" action="" name="markov"> | ||
<textarea rows="20" cols="80" name="input"></textarea> | ||
<br /> | ||
<select name="text"> | ||
<option value="">Or select one of the input texts here below</option> | ||
<option value="alice">Alice's Adventures in Wonderland, by Lewis Carroll</option> | ||
<option value="calvin">The Wikipedia article on Calvin and Hobbes</option> | ||
<option value="kant">The Critique of Pure Reason by Immanuel Kant</option> | ||
</select> | ||
<br /> | ||
<label for="order">Order</label> | ||
<input type="text" name="order" value="4" /> | ||
<label for="length">Text size of output</label> | ||
<input type="text" name="length" value="2500" /> | ||
<br /> | ||
<input type="submit" name="submit" value="GO" /> | ||
</form> | ||
</div> <!-- /wrapper --> | ||
</body> | ||
</html> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/* | ||
PHP Markov Chain text generator 1.0 | ||
Copyright (c) 2008-2010, Hay Kranen <http://www.haykranen.nl/projects/markov/> | ||
Fork on Github: < http://github.com/hay/markov > | ||
License (MIT / X11 license) | ||
Permission is hereby granted, free of charge, to any person | ||
obtaining a copy of this software and associated documentation | ||
files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, | ||
copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following | ||
conditions: | ||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
function generate_markov_table($text, $look_forward) { | ||
$table = array(); | ||
|
||
// now walk through the text and make the index table | ||
for ($i = 0; $i < strlen($text); $i++) { | ||
$char = substr($text, $i, $look_forward); | ||
if (!isset($table[$char])) $table[$char] = array(); | ||
} | ||
|
||
// walk the array again and count the numbers | ||
for ($i = 0; $i < (strlen($text) - $look_forward); $i++) { | ||
$char_index = substr($text, $i, $look_forward); | ||
$char_count = substr($text, $i+$look_forward, $look_forward); | ||
|
||
if (isset($table[$char_index][$char_count])) { | ||
$table[$char_index][$char_count]++; | ||
} else { | ||
$table[$char_index][$char_count] = 1; | ||
} | ||
} | ||
|
||
return $table; | ||
} | ||
|
||
function generate_markov_text($length, $table, $look_forward) { | ||
// get first character | ||
$char = array_rand($table); | ||
$o = $char; | ||
|
||
for ($i = 0; $i < ($length / $look_forward); $i++) { | ||
$newchar = return_weighted_char($table[$char]); | ||
|
||
if ($newchar) { | ||
$char = $newchar; | ||
$o .= $newchar; | ||
} else { | ||
$char = array_rand($table); | ||
} | ||
} | ||
|
||
return $o; | ||
} | ||
|
||
|
||
function return_weighted_char($array) { | ||
if (!$array) return false; | ||
|
||
$total = array_sum($array); | ||
$rand = mt_rand(1, $total); | ||
foreach ($array as $item => $weight) { | ||
if ($rand <= $weight) return $item; | ||
$rand -= $weight; | ||
} | ||
} | ||
?> |
Oops, something went wrong.