]> git.localhorst.tv Git - alttp.git/blob - app/TwitchBot/TokenizedMessage.php
revamp chatlib tokenization
[alttp.git] / app / TwitchBot / TokenizedMessage.php
1 <?php
2
3 namespace App\TwitchBot;
4
5 use App\Models\ChatLog;
6 use Illuminate\Support\Arr;
7 use Illuminate\Support\Str;
8
9 class TokenizedMessage {
10
11         public function __construct($text, $tags = []) {
12                 $this->text = trim($text);
13                 $this->tags = $tags;
14                 if (isset($tags['reply-parent-display-name'])) {
15                         $this->text = mb_substr($text, mb_strlen($tags['reply-parent-display-name']) + 2);
16                 }
17                 $this->raw = strtolower(preg_replace('/[^\w]/u', '', $this->text));
18                 $this->tokens = array_values(array_map('trim', array_filter(preg_split('/\b/u', strtolower($this->text)))));
19
20                 $this->emoteless = $text;
21                 if (isset($this->tags['emotes']) && !empty($this->tags['emotes'])) {
22                         $emotes = explode('/', $this->tags['emotes']);
23                         foreach ($emotes as $emote) {
24                                 $set = explode(':', $emote);
25                                 $positions = explode(',', $set[1]);
26                                 foreach ($positions as $position) {
27                                         $coords = explode('-', $position);
28                                         $emote_text = mb_substr($this->text, $coords[0], $coords[1] - $coords[0] + 1);
29                                         $this->original_emotes[] = $emote_text;
30                                         $this->emotes[] = preg_replace('/\d+$/', '', strtolower($emote_text));
31                                         $this->emoteless = mb_substr($this->emoteless, 0, $coords[0]).str_repeat(' ', $coords[1] - $coords[0] + 1).mb_substr($this->emoteless, $coords[1] + 1);
32                                 }
33                         }
34                         $this->emoteless = trim(preg_replace('/\s+/u', ' ', $this->emoteless));
35                 }
36                 $this->emoteless_raw = strtolower(preg_replace('/[^\w]/u', '', $this->emoteless));
37                 $this->emoteless_tokens = array_values(array_map('trim', array_filter(preg_split('/\b/u', strtolower($this->emoteless)))));
38         }
39
40         public static function fromIRC(IRCMessage $msg) {
41                 return new self($msg->getText(), $msg->tags);
42         }
43
44         public static function fromLog(ChatLog $log) {
45                 return new self($log->params[1], $log->tags);
46         }
47
48         public static function fromString($text, $tags = []) {
49                 return new self($text, $tags);
50         }
51
52
53         public function contains($text) {
54                 return Str::contains($this->text, $text);
55         }
56
57         public function containsEmoteless($text) {
58                 return Str::contains($this->emoteless, $text);
59         }
60
61         public function containsRaw($text) {
62                 return Str::contains($this->raw, $text);
63         }
64
65         public function endsWith($text) {
66                 return Str::endsWith($this->text, $text);
67         }
68
69         public function endsWithEmoteless($text) {
70                 return Str::endsWith($this->emoteless, $text);
71         }
72
73         public function endsWithEmotelessToken($text) {
74                 return !empty($this->emoteless_tokens) && $this->emoteless_tokens[count($this->emoteless_tokens) - 1] == $text;
75         }
76
77         public function endsWithRaw($text) {
78                 return Str::endsWith($this->raw, $text);
79         }
80
81         public function endsWithToken($text) {
82                 return !empty($this->tokens) && $this->tokens[count($this->tokens) - 1] == $text;
83         }
84
85         public function getEmotes() {
86                 return $this->emotes;
87         }
88
89         public function getNumericValue() {
90                 return intval($this->text);
91         }
92
93         public function getOriginalEmotes() {
94                 return $this->original_emotes;
95         }
96
97         public function hasConsecutiveTokens($tokens) {
98                 for ($i = 0; $i < count($this->tokens) - count($tokens) + 1; ++$i) {
99                         for ($j = 0; $j < count($tokens); ++$j) {
100                                 if ($this->tokens[$i + $j] != $tokens[$j]) break;
101                         }
102                         if ($j == count($tokens)) return true;
103                 }
104                 return false;
105         }
106
107         public function hasEmote($text) {
108                 if (is_array($text)) {
109                         foreach ($text as $token) {
110                                 if (in_array($token, $this->emotes)) {
111                                         return true;
112                                 }
113                         }
114                         return false;
115                 }
116                 return in_array($text, $this->emotes);
117         }
118
119         public function hasEmoteThatContains($text) {
120                 foreach ($this->emotes as $emote) {
121                         if (Str::contains($emote, $text)) {
122                                 return true;
123                         }
124                 }
125                 return false;
126         }
127
128         public function hasEmoteThatEndsWith($text) {
129                 foreach ($this->emotes as $emote) {
130                         if (Str::endsWith($emote, $text)) {
131                                 return true;
132                         }
133                 }
134                 return false;
135         }
136
137         public function hasEmoteThatStartsOrEndsWith($text) {
138                 foreach ($this->emotes as $emote) {
139                         if (Str::startsWith($emote, $text) || Str::endsWith($emote, $text)) {
140                                 return true;
141                         }
142                 }
143                 return false;
144         }
145
146         public function hasEmoteThatStartsWith($text) {
147                 foreach ($this->emotes as $emote) {
148                         if (Str::startsWith($emote, $text)) {
149                                 return true;
150                         }
151                 }
152                 return false;
153         }
154
155         public function hasToken($text) {
156                 if (is_array($text)) {
157                         foreach ($text as $token) {
158                                 if (in_array($token, $this->tokens)) {
159                                         return true;
160                                 }
161                         }
162                         return false;
163                 }
164                 return in_array($text, $this->tokens);
165         }
166
167         public function hasTokenThatContains($text) {
168                 foreach ($this->tokens as $token) {
169                         if (Str::contains($token, $text)) {
170                                 return true;
171                         }
172                 }
173                 return false;
174         }
175
176         public function hasTokenThatEndsWith($text) {
177                 foreach ($this->tokens as $token) {
178                         if (Str::endsWith($token, $text)) {
179                                 return true;
180                         }
181                 }
182                 return false;
183         }
184
185         public function hasTokenThatStartsOrEndsWith($text) {
186                 foreach ($this->tokens as $token) {
187                         if (Str::startsWith($token, $text) || Str::endsWith($token, $text)) {
188                                 return true;
189                         }
190                 }
191                 return false;
192         }
193
194         public function hasTokenThatStartsWith($text) {
195                 foreach ($this->tokens as $token) {
196                         if (Str::startsWith($token, $text)) {
197                                 return true;
198                         }
199                 }
200                 return false;
201         }
202
203         public function isLong() {
204                 return strlen($this->emoteless_raw) > 20;
205         }
206
207         public function isShort() {
208                 return strlen($this->emoteless_raw) < 15;
209         }
210
211         public function isVeryLong() {
212                 return strlen($this->emoteless_raw) > 40;
213         }
214
215         public function startsOrEndsWith($text) {
216                 return $this->startsWith($text) || $this->endsWith($text);
217         }
218
219         public function startsOrEndsWithEmotelessToken($text) {
220                 return $this->startsWithEmotelessToken($text) || $this->endsWithEmotelessToken($text);
221         }
222
223         public function startsOrEndsWithRaw($text) {
224                 return $this->startsWithRaw($text) || $this->endsWithRaw($text);
225         }
226
227         public function startsOrEndsWithToken($text) {
228                 return $this->startsWithToken($text) || $this->endsWithToken($text);
229         }
230
231         public function startsWith($text) {
232                 return Str::startsWith($this->text, $text);
233         }
234
235         public function startsWithEmoteless($text) {
236                 return Str::startsWith($this->emoteless, $text);
237         }
238
239         public function startsWithEmotelessToken($text) {
240                 return isset($this->emoteless_tokens[0]) && $this->emoteless_tokens[0] == $text;
241         }
242
243         public function startsWithRaw($text) {
244                 return Str::startsWith($this->raw, $text);
245         }
246
247         public function startsWithToken($text) {
248                 return isset($this->tokens[0]) && $this->tokens[0] == $text;
249         }
250
251
252         public function isSpammy() {
253                 if ($this->startsWith('!')) {
254                         return true;
255                 }
256                 if ($this->contains(['€', '$', '@', '://'])) {
257                         return true;
258                 }
259                 if ($this->containsRaw(['follow', 'promotion', 'viewer'])) {
260                         return true;
261                 }
262                 if ($this->containsRaw('horsti')) {
263                         return true;
264                 }
265                 if ($this->containsRaw([
266                         'folgtjetzt',
267                         'hatdeinenkanalgeraided',
268                         'isnowlivestreaming',
269                         'stürmtdenladenmit',
270                         'thanksfortheraid',
271                         'verschwindetfürneweileindenlurk',
272                         'vielendankfürdenraid',
273                         'willkommenaufstarbase47',
274                 ])) {
275                         return true;
276                 }
277                 return false;
278         }
279
280
281         public function classify() {
282                 if (is_null($this->classification)) {
283                         if (empty($this->text) || $this->isVeryLong()) {
284                                 $this->classification = 'unclassified';
285                         } else if ($this->startsWith('!')) {
286                                 $this->classification = 'cmd';
287                         } else if ($this->isShort() && ($this->hasTokenThatStartsOrEndsWith(['gg']) || $this->hasEmoteThatEndsWith(['gg']))) {
288                                 $this->classification = 'gg';
289                         } else if ($this->isShort() && $this->containsRaw(['glgl', 'glhf', 'goodluck', 'hfgl', 'vielglück'])) {
290                                 $this->classification = 'gl';
291                         } else if ($this->hasToken(['danke', 'thanks', 'thx', 'ty']) && !$this->hasToken(['nah', 'nee', 'nein', 'no'])) {
292                                 $this->classification = 'thx';
293                         } else if (!$this->isLong() && ($this->startsWithRaw(['ahoi', 'hallo', 'hello', 'hey', 'huhu', 'moin']) || $this->hasEmoteThatEndsWith(['hello', 'heyguys', 'hi', 'vohiyo', 'wave']) || $this->hasToken(['hi', 'hey', 'yo']) || $this->containsRaw(['gutenmorgen', 'gutenabend']))) {
294                                 $this->classification = 'hi';
295                         } else if ($this->isShort() && $this->hasTokenThatStartsOrEndsWith(['pog', 'wow'])) {
296                                 $this->classification = 'pog';
297                         } else if ($this->containsRaw(['hype', 'letsgo']) || $this->hasEmoteThatEndsWith(['dance', 'jam', 'party', 'rave', 'troete'])) {
298                                 $this->classification = 'hype';
299                         } else if ($this->hasToken(['<3']) || $this->hasEmoteThatEndsWith(['heart', 'herz', 'hug', 'love'])) {
300                                 $this->classification = 'love';
301                         } else if ($this->hasToken(['nani', 'wat', 'wtf']) || $this->hasEmoteThatEndsWith(['wat', 'wtf'])) {
302                                 $this->classification = 'wtf';
303                         } else if ($this->hasConsecutiveTokens([':', 'eyes', ':']) || $this->hasEmoteThatEndsWith(['eyes'])) {
304                                 $this->classification = 'eyes';
305                         } else if ($this->hasEmoteThatEndsWith(['angry', 'rage', 'ree'])) {
306                                 $this->classification = 'rage';
307                         } else if ($this->hasToken([':(']) || $this->hasEmoteThatEndsWith(['cry', 'sad'])) {
308                                 $this->classification = 'sad';
309                         } else if ($this->hasToken(['monkas', 'sweat_smile']) || $this->hasEmoteThatEndsWith(['sweat'])) {
310                                 $this->classification = 'sweat';
311                         } else if ($this->endsWithEmoteless('?')) {
312                                 $this->classification = 'question';
313                         } else if ($this->hasToken(['jo', 'yep', 'yes']) || $this->startsOrEndsWithEmotelessToken('ja') || $this->containsRaw('nodders') || $this->hasEmoteThatEndsWith(['nod', 'nodders', 'yea'])) {
314                                 $this->classification = 'yes';
315                         } else if ($this->hasToken(['nah', 'nee', 'nein', 'no']) || $this->containsRaw('nopers') || $this->hasEmoteThatEndsWith(['nay', 'nope', 'nopers'])) {
316                                 $this->classification = 'no';
317                         } else if ($this->hasEmoteThatContains(['kappa', 'keepo'])) {
318                                 $this->classification = 'kappa';
319                         } else if ($this->startsOrEndsWithRaw(['o7']) || $this->hasEmoteThatContains('salut')) {
320                                 $this->classification = 'o7';
321                         } else if (!$this->isLong() && ($this->containsRaw(['haha', 'hehe', 'hihi', 'kekw', 'lol', 'lul']) || $this->hasTokenThatStartsWith(['xd']) || $this->hasConsecutiveTokens([':', 'd']))) {
322                                 $this->classification = 'lol';
323                         } else if (is_numeric($this->raw)) {
324                                 $this->classification = 'number';
325                         } else {
326                                 $this->classification = 'unclassified';
327                         }
328                 }
329                 return $this->classification;
330         }
331
332         public function getResponseCategory() {
333                 switch ($this->classify()) {
334                         case 'gg':
335                                 return ['love', 'eyes', 'thx', 'pog', 'kappa'];
336                         case 'gl':
337                                 return ['love', 'eyes', 'thx'];
338                         case 'hi':
339                                 return ['hi', 'love', 'eyes', 'hype', 'pog'];
340                         case 'kappa':
341                                 return ['kappa', 'lol', 'eyes'];
342                         case 'love':
343                                 return ['hi', 'love', 'eyes', 'thx'];
344                         case 'question':
345                                 if (
346                                         $this->hasToken(['number', 'nummer', 'wieviel', 'zahl']) ||
347                                         $this->hasConsecutiveTokens(['how', 'many']) ||
348                                         $this->hasConsecutiveTokens(['how', 'much']) ||
349                                         $this->hasConsecutiveTokens(['wie', 'viele'])
350                                 ) {
351                                         return ['yes', 'no', 'kappa', 'lol', 'wtf', 'number'];
352                                 }
353                                 return ['yes', 'no', 'kappa', 'lol', 'wtf'];
354                         case 'rage':
355                                 return ['kappa', 'lol', 'rage'];
356                         case 'wtf':
357                                 return ['kappa', 'lol', 'rage'];
358                 }
359                 return false;
360         }
361
362
363         private $text;
364         private $tags;
365         private $raw;
366         private $tokens;
367
368         private $emotes = [];
369         private $original_emotes = [];
370         private $emoteless = '';
371         private $emoteless_raw = '';
372         private $emoteless_tokens = [];
373
374         private $classification = null;
375
376 }