· 8 years ago · Jul 22, 2018, 10:14 PM
1<?php
2
3class phpIBModule extends ModuleCore
4{
5 private $db, $wordIdCache;
6 public function getWordId($word)
7 {
8 $db = $this->db;
9 $word = trim($word);
10 if(!$word) return 0;
11 if(!isset($this->wordIdCache[$word]))
12 {
13 $obj = $db ->prepare('SELECT id FROM words WHERE word = :word');
14 $obj->execute(array('word' => $word));
15 $id = $obj->fetchColumn();
16 if(!$id)
17 {
18 $obj = $db->prepare('
19 INSERT INTO
20 words
21 (`word`)
22 VALUES
23 (:word)'
24 );
25 $obj->execute(array('word' => $word));
26 $id = $db->lastInsertId();
27 }
28 if(isset($this->wordIdCache[$word]))
29 {
30 unset($this->wordIdCache[$word]);
31 }
32 $this->wordIdCache[$word] = $id;
33 if(count($this->wordIdCache) > 100) array_shift($this->wordIdCache);
34 }else{
35 $id = $this->wordIdCache[$word];
36 }
37 return $id;
38 }
39 public function ModuleInit()
40 {
41 $db = new PDO('sqlite:ai.db');
42 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
43 $db->query('
44 CREATE TABLE IF NOT EXISTS
45 phrases
46 (
47 `word` INTEGER,
48 `left` INTEGER,
49 `right` INTEGER,
50 PRIMARY KEY (`word`, `left`, `right`)
51 );
52 ');
53 $db->query('
54 CREATE TABLE IF NOT EXISTS
55 words
56 (
57 `id` INTEGER PRIMARY KEY ASC,
58 `word` VARCHAR(45) NOT NULL
59 );
60 ');
61 $db->query('
62 CREATE INDEX IF NOT EXISTS
63 idx_words_word
64 ON `words` (`word`)
65 ');
66 $this->db = $db;
67 }
68 public function OnChannelText(ircChannel $channel, ircUser $user, $message)
69 {
70 $db = $this->db;
71 $chan = $channel->channel;
72 if($user->nick == "namelessbot" || $chan == '#opers' || stripos($user->mask, 'services.ffochat.com')) return;
73 $left = null;
74 $leftId = 0;
75 list($cmd, $data) = explode(' ', strtolower($message));
76 $message = preg_replace("/[^a-zA-Z0-9 ]/", "", $message);
77
78 $words = explode(' ', strtolower($message));
79 $words = array_map('trim', $words);
80 $words = array_filter($words);
81
82
83 //if($chan == '#vanadiel') $channel->sendMessage("$cmd - $data");
84 //if($chan == '#vanadiel') $channel->sendMessage('got words:'.implode(',', $words));
85 if($cmd == '!wordcount')
86 {
87 if($data)
88 {
89 $stmt = $db->prepare('
90 select count(*) from words w
91 join phrases p1 on (p1.word = w.id)
92 where w.word = :word
93 ');
94 $stmt->execute(array('word' => trim($data)));
95
96 $count = (int) $stmt->fetchColumn();
97 $channel->sendMessage('The word ' . trim($data) . ' appears in db ' . $count . ' times.');
98 }else{
99 $stmt = $db->prepare('
100 select count(*) from words
101 ');
102 $stmt->execute();
103
104 $count1 = (int) $stmt->fetchColumn();
105 $stmt = $db->prepare('
106 select count(*) from phrases
107 ');
108 $stmt->execute();
109
110 $count2 = (int) $stmt->fetchColumn();
111 clearstatcache();
112 $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');
113 }
114 return;
115 }
116 $isRakia = array_search('rakia', $words);
117 if($isRakia !== false && count($words) > 1)
118 {
119 unset($words[$isRakia]);
120 $count = mt_rand(2,5);
121 if($count > count($words)) $count = count($words);
122 $keys = (array) array_rand($words, $count);
123
124 $string = array();
125 $subsetids = array();
126 $getWordId = $db->prepare('SELECT id FROM words WHERE word = :word');
127 $getPhrase = $db->prepare('SELECT
128 left, word, right
129 FROM phrases
130 WHERE
131 left = :word
132 OR
133 word = :word
134 OR
135 right = :word
136 ORDER BY RANDOM() LIMIT 1');
137 $getRandomNext = $db->prepare('
138 SELECT
139 word, right
140 FROM phrases
141 WHERE
142 left = :word
143 ORDER BY RANDOM() LIMIT 1
144 ');
145 if($keys)
146 {
147 foreach($keys as $key)
148 {
149 //error_log('word:'.$words[$key]);
150 $getWordId->execute(array('word' => $words[$key]));
151 $wordId = $getWordId->fetchColumn();
152 $getPhrase->execute(array('word' => $wordId));
153 $res = $getPhrase->fetch(PDO::FETCH_ASSOC);
154 if($res)
155 {
156 //if($chan == '#vanadiel') $channel->sendMessage('orig res:'.str_replace("\n",'',print_r($res,true)));
157 foreach($res as $sid)
158 {
159 //error_log('got row'.print_r($sid,true));
160 if($sid) $subsetids[] = $sid;
161 }
162 $next = $res['right'];
163 if($next)
164 {
165 $getRandomNext->execute(array('word' => $next));
166 $res = $getRandomNext->fetch(PDO::FETCH_ASSOC);
167 if($res)
168 {
169 //if($chan == '#vanadiel') $channel->sendMessage('added res:'.str_replace("\n",'',print_r($res,true)));
170 foreach($res as $sid)
171 {
172 //error_log('got row'.print_r($sid,true));
173 if($sid) $subsetids[] = $sid;
174 }
175 }else{
176 //$channel->sendMessage('failed res on next'.$next);
177 }
178 }else{
179 //$channel->sendMessage('next was empty ' . $next);
180 }
181 }
182 }
183 $getWord = $db->prepare('
184 SELECT
185 word
186 FROM
187 words
188 WHERE id = :id
189 ');
190 foreach($subsetids as $sid )
191 {
192
193 $getWord->execute(array('id' => $sid));
194 $word = $getWord->fetchColumn();
195 $word = $word[0].ircCode('<:b><:b>').substr($word,1);
196 $string[] = $word;
197 }
198
199 $string = implode(' ', $string);
200 $nick = $user->nick[0] .ircCode('<:b><:b>'). substr($user->nick,1);
201 $channel->sendMessage($nick . ': '.$string);
202 }
203 return;
204 }
205
206 reset($words);
207 if(count($words) > 2)
208 {
209 do
210 {
211 $word = current($words);
212 $next = next($words);
213 if($next === FALSE) $next = null;
214 if($left)
215 {
216 $leftId = $this->getWordId($left);
217 }else{
218 $leftId = 0;
219 }
220 $curId = $this->getWordId($word);
221 $left = $word;
222 if($next)
223 {
224 $rightId = $this->getWordId($next);
225 $word = $next;
226 }else{
227 $rightId = 0;
228 $word = null;
229 }
230 $db->prepare('
231 INSERT OR IGNORE INTO
232 phrases
233 (`word`, `left`, `right`)
234 VALUES
235 (:cur, :left, :right)
236 ')->execute(array(
237 'left' => $leftId,
238 'cur' => $curId,
239 'right' => $rightId)
240 );
241 } while($word);
242 }
243 }
244}