Skip to content

Instantly share code, notes, and snippets.

@evert
Last active January 2, 2016 06:41
Show Gist options
  • Save evert/779f43cd4e41ba1e6726 to your computer and use it in GitHub Desktop.
Save evert/779f43cd4e41ba1e6726 to your computer and use it in GitHub Desktop.
A PHP script to generate a random blogpost.
<?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;
}
}
}
@mdwheele
Copy link

mdwheele commented Jan 2, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment