Skip to content

Instantly share code, notes, and snippets.

@aikar
Created August 25, 2010 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aikar/548902 to your computer and use it in GitHub Desktop.
Save aikar/548902 to your computer and use it in GitHub Desktop.
<?php
class phpIBModule extends ModuleCore
{
private $db, $wordIdCache;
public function getWordId($word)
{
$db = $this->db;
$word = trim($word);
if(!$word) return 0;
if(!isset($this->wordIdCache[$word]))
{
$obj = $db ->prepare('SELECT id FROM words WHERE word = :word');
$obj->execute(array('word' => $word));
$id = $obj->fetchColumn();
if(!$id)
{
$obj = $db->prepare('
INSERT INTO
words
(`word`)
VALUES
(:word)'
);
$obj->execute(array('word' => $word));
$id = $db->lastInsertId();
}
if(isset($this->wordIdCache[$word]))
{
unset($this->wordIdCache[$word]);
}
$this->wordIdCache[$word] = $id;
if(count($this->wordIdCache) > 100) array_shift($this->wordIdCache);
}else{
$id = $this->wordIdCache[$word];
}
return $id;
}
public function ModuleInit()
{
$db = new PDO('sqlite:ai.db');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->query('
CREATE TABLE IF NOT EXISTS
phrases
(
`word` INTEGER,
`left` INTEGER,
`right` INTEGER,
PRIMARY KEY (`word`, `left`, `right`)
);
');
$db->query('
CREATE TABLE IF NOT EXISTS
words
(
`id` INTEGER PRIMARY KEY ASC,
`word` VARCHAR(45) NOT NULL
);
');
$db->query('
CREATE INDEX IF NOT EXISTS
idx_words_word
ON `words` (`word`)
');
$this->db = $db;
}
public function OnChannelText(ircChannel $channel, ircUser $user, $message)
{
$db = $this->db;
$chan = $channel->channel;
if($user->nick == "namelessbot" || $chan == '#opers') return;
$left = null;
$leftId = 0;
$message = preg_replace("/[^a-zA-Z0-9\s]/", "", $message);
$words = explode(' ', strtolower($message));
$words = array_map('trim', $words);
$words = array_filter($words);
//if($chan == '#vanadiel') $channel->sendMessage('got words:'.implode(',', $words));
$isRakia = array_search('rakia', $words);
if($isRakia !== false && count($words) > 1)
{
unset($words[$isRakia]);
$count = mt_rand(2,5);
if($count > count($words)) $count = count($words);
$keys = (array) array_rand($words, $count);
$string = array();
$subsetids = array();
$getWordId = $db->prepare('SELECT id FROM words WHERE word = :word');
$getPhrase = $db->prepare('SELECT
left, word, right
FROM phrases
WHERE
left = :word
OR
word = :word
OR
right = :word
ORDER BY RANDOM() LIMIT 1');
$getRandomNext = $db->prepare('
SELECT
word, right
FROM phrases
WHERE
left = :word
ORDER BY RANDOM() LIMIT 1
');
if($keys)
{
foreach($keys as $key)
{
//error_log('word:'.$words[$key]);
$getWordId->execute(array('word' => $words[$key]));
$wordId = $getWordId->fetchColumn();
$getPhrase->execute(array('word' => $wordId));
$res = $getPhrase->fetch(PDO::FETCH_ASSOC);
if($res)
{
//if($chan == '#vanadiel') $channel->sendMessage('orig res:'.str_replace("\n",'',print_r($res,true)));
foreach($res as $sid)
{
//error_log('got row'.print_r($sid,true));
if($sid) $subsetids[] = $sid;
}
$next = $res['right'];
if($next)
{
$getRandomNext->execute(array('word' => $next));
$res = $getRandomNext->fetch(PDO::FETCH_ASSOC);
if($res)
{
//if($chan == '#vanadiel') $channel->sendMessage('added res:'.str_replace("\n",'',print_r($res,true)));
foreach($res as $sid)
{
//error_log('got row'.print_r($sid,true));
if($sid) $subsetids[] = $sid;
}
}else{
//$channel->sendMessage('failed res on next'.$next);
}
}else{
//$channel->sendMessage('next was empty ' . $next);
}
}
}
$getWord = $db->prepare('
SELECT
word
FROM
words
WHERE id = :id
');
foreach($subsetids as $sid )
{
$getWord->execute(array('id' => $sid));
$string[] = $getWord->fetchColumn();
}
$string = implode(' ', $string);
$channel->sendMessage($string);
}
return;
}
reset($words);
if(count($words) > 2)
{
do
{
$word = current($words);
$next = next($words);
if($next === FALSE) $next = null;
if($left)
{
$leftId = $this->getWordId($left);
}else{
$leftId = 0;
}
$curId = $this->getWordId($word);
$left = $word;
if($next)
{
$rightId = $this->getWordId($next);
$word = $next;
}else{
$rightId = 0;
$word = null;
}
$db->prepare('
INSERT OR IGNORE INTO
phrases
(`word`, `left`, `right`)
VALUES
(:cur, :left, :right)
')->execute(array(
'left' => $leftId,
'cur' => $curId,
'right' => $rightId)
);
} while($word);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment