From MWWiki
Back to Main Project Page
<?php
include_once('update.php');
include_once('markov.php');
include_once('logger.php');
// create the error logger
$errors = new Logger('post.log');
// parse the aggregated timeline and construct a post
$retval = @file('timeline.txt');
$fp = @fopen('timeline.txt', 'w+');
if (!$fp || !$retval) {
$errors->write('Unable to read timeline file.', Logger::$ERROR);
exit;
}
if (flock($fp, LOCK_EX)) {
ftruncate($fp, 0); // wipe out the file
flock($fp, LOCK_UN);
} else {
$errors->write('Unable to lock timeline file.', Logger::$ERROR);
exit;
}
fclose($fp);
// time to get to work
$m = new MarkovFirstOrder();
foreach ($retval as $line) {
$words = explode(' ', trim($line));
// go through each word
$numWords = count($words);
for ($i = 1; $i < $numWords; $i++) {
$m->add($words[$i - 1], $words[$i]);
}
}
// ok, let's build a post!
// start with the _START_ token
$currentWord = '_START_';
$nextWord = '';
$post = '';
while (($nextWord = $m->get($currentWord)) != '_STOP_') {
$temp = ($post . $nextWord);
if (strlen($temp) >= 140) {
// whoops
$errors->write('Had to truncate post to 140 characters.', Logger::$INFO);
break;
}
// reaching this point means we're good
$post = $temp . ' ';
$currentWord = $nextWord;
}
// that's it! post it
$update = new Update();
$update->postUpdate($post);
?>