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