Last active
January 2, 2016 06:41
-
-
Save evert/779f43cd4e41ba1e6726 to your computer and use it in GitHub Desktop.
A PHP script to generate a random blogpost.
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 | |
// Where to find the blog posts | |
$sources = '_posts/2015/**.md'; | |
$files = glob($sources); | |
$startWords = []; | |
$allWords = []; | |
echo "Loading " . count($files) . " files\n"; | |
foreach($files as $file) { | |
loadFile($file); | |
} | |
echo "Loaded " . count($allWords) . " words\n"; | |
$count = 20; | |
echo "Generating post...\n"; | |
echo generatePost(); | |
function loadFile($filename) { | |
// Yuck | |
global $startWords, $allWords; | |
$source = file_get_contents($filename); | |
// Stripping frontmatter. This is a markdown/jekyll thing | |
list(,,$source) = explode("---\n", $source, 3); | |
$lastWord = null; | |
foreach(explode(' ', $source) as $word) { | |
//$word = strtolower($word); | |
//$word = trim($word,'.?!,;'); | |
//if (!preg_match('/^[a-z]+$/', $word)) continue; | |
if ($lastWord === null) { | |
// Counting starting words for blog posts | |
if (isset($startWords[$word])) { | |
$startWords[$word]++; | |
} else { | |
$startWords[$word] = 1; | |
} | |
} else { | |
// We count every word based on which word came | |
// before it. | |
if (!isset($allWords[$lastWord][$word])) { | |
$allWords[$lastWord][$word] = 1; | |
} else { | |
$allWords[$lastWord][$word]++; | |
} | |
} | |
if (!isset($allWords[$word])) { | |
$allWords[$word] = []; | |
} | |
$lastWord = $word; | |
} | |
} | |
function generatePost() { | |
global $startWords, $allWords; | |
// Get the first word | |
$word = getWeightedRandom($startWords); | |
$post = $word; | |
while(true) { | |
// All the potential next words | |
$nextWords = $allWords[$word]; | |
if (!$nextWords) { | |
break; | |
} | |
$word = getWeightedRandom($nextWords); | |
$post.=' ' . $word; | |
} | |
return $post; | |
} | |
function getWeightedRandom(array $array) { | |
$no = mt_rand(1, array_sum($array)); | |
foreach($array as $value => $weight) { | |
$no -= $weight; | |
if ($no < 1) { | |
return $value; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/mdwheele/marky ! 😄 Does character-level, which would probably be hilarious for this... would take many more pre-flight adjustments, I'm sure haha.