Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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' || stripos($user->mask, 'services.ffochat.com')) return;
- $left = null;
- $leftId = 0;
- list($cmd, $data) = explode(' ', strtolower($message));
- $message = preg_replace("/[^a-zA-Z0-9 ]/", "", $message);
- $words = explode(' ', strtolower($message));
- $words = array_map('trim', $words);
- $words = array_filter($words);
- //if($chan == '#vanadiel') $channel->sendMessage("$cmd - $data");
- //if($chan == '#vanadiel') $channel->sendMessage('got words:'.implode(',', $words));
- if($cmd == '!wordcount')
- {
- if($data)
- {
- $stmt = $db->prepare('
- select count(*) from words w
- join phrases p1 on (p1.word = w.id)
- where w.word = :word
- ');
- $stmt->execute(array('word' => trim($data)));
- $count = (int) $stmt->fetchColumn();
- $channel->sendMessage('The word ' . trim($data) . ' appears in db ' . $count . ' times.');
- }else{
- $stmt = $db->prepare('
- select count(*) from words
- ');
- $stmt->execute();
- $count1 = (int) $stmt->fetchColumn();
- $stmt = $db->prepare('
- select count(*) from phrases
- ');
- $stmt->execute();
- $count2 = (int) $stmt->fetchColumn();
- clearstatcache();
- $channel->sendMessage('There are ' . $count1 . ' words in my DB and a total of ' . $count2 . ' triplet phrases for a filesize of ' . round(filesize('ai.db') / 1024,2) . 'kb');
- }
- return;
- }
- $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));
- $word = $getWord->fetchColumn();
- $word = $word[0].ircCode('<:b><:b>').substr($word,1);
- $string[] = $word;
- }
- $string = implode(' ', $string);
- $nick = $user->nick[0] .ircCode('<:b><:b>'). substr($user->nick,1);
- $channel->sendMessage($nick . ': '.$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);
- }
- }
- }
Add Comment
Please, Sign In to add comment