]> git.localhorst.tv Git - alttp.git/blob - app/TwitchBot/TokenizedMessage.php
e35299803c4753f369c5942c3d3c914b05f5208e
[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                 $this->raw = strtolower(preg_replace('/[^\w]/u', '', $this->text));
15                 $this->tokens = array_values(array_map('trim', array_filter(preg_split('/\b/', strtolower($this->text)))));
16
17                 $this->emoteless = $this->text;
18                 if (isset($this->tags['emotes']) && !empty($this->tags['emotes'])) {
19                         $emotes = explode('/', $this->tags['emotes']);
20                         foreach ($emotes as $emote) {
21                                 $set = explode(':', $emote);
22                                 $positions = explode(',', $set[1]);
23                                 foreach ($positions as $position) {
24                                         $coords = explode('-', $position);
25                                         $this->emotes[] = preg_replace('/\d+$/', '', strtolower(substr($this->text, $coords[0], $coords[1] - $coords[0] + 1)));
26                                         for ($i = intval($coords[0]); $i <= intval($coords[1]); ++$i) {
27                                                 $this->emoteless[$i] = ' ';
28                                         }
29                                 }
30                         }
31                         $this->emoteless = trim(preg_replace('/\s+/', ' ', $this->emoteless));
32                 }
33                 $this->emoteless_raw = strtolower(preg_replace('/[^\w]/', '', $this->emoteless));
34                 $this->emoteless_tokens = array_values(array_map('trim', array_filter(preg_split('/\b/', strtolower($this->emoteless)))));
35         }
36
37         public static function fromIRC(IRCMessage $msg) {
38                 return new self($msg->getText(), $msg->tags);
39         }
40
41         public static function fromLog(ChatLog $log) {
42                 return new self($log->params[1], $log->tags);
43         }
44
45         public static function fromString($text, $tags = []) {
46                 return new self($text, $tags);
47         }
48
49
50         public function contains($text) {
51                 return Str::contains($this->text, $text);
52         }
53
54         public function containsEmoteless($text) {
55                 return Str::contains($this->emoteless, $text);
56         }
57
58         public function containsRaw($text) {
59                 return Str::contains($this->raw, $text);
60         }
61
62         public function endsWith($text) {
63                 return Str::endsWith($this->text, $text);
64         }
65
66         public function endsWithEmoteless($text) {
67                 return Str::endsWith($this->emoteless, $text);
68         }
69
70         public function endsWithEmotelessToken($text) {
71                 return !empty($this->emoteless_tokens) && $this->emoteless_tokens[count($this->emoteless_tokens) - 1] == $text;
72         }
73
74         public function endsWithRaw($text) {
75                 return Str::endsWith($this->raw, $text);
76         }
77
78         public function endsWithToken($text) {
79                 return !empty($this->tokens) && $this->tokens[count($this->tokens) - 1] == $text;
80         }
81
82         public function getNumericValue() {
83                 return intval($this->text);
84         }
85
86         public function hasConsecutiveTokens($tokens) {
87                 for ($i = 0; $i < count($this->tokens) - count($tokens) + 1; ++$i) {
88                         for ($j = 0; $j < count($tokens); ++$j) {
89                                 if ($this->tokens[$i + $j] != $tokens[$j]) break;
90                         }
91                         if ($j == count($tokens)) return true;
92                 }
93                 return false;
94         }
95
96         public function hasEmote($text) {
97                 if (is_array($text)) {
98                         foreach ($text as $token) {
99                                 if (in_array($token, $this->emotes)) {
100                                         return true;
101                                 }
102                         }
103                         return false;
104                 }
105                 return in_array($text, $this->emotes);
106         }
107
108         public function hasEmoteThatContains($text) {
109                 foreach ($this->emotes as $emote) {
110                         if (Str::contains($emote, $text)) {
111                                 return true;
112                         }
113                 }
114                 return false;
115         }
116
117         public function hasEmoteThatEndsWith($text) {
118                 foreach ($this->emotes as $emote) {
119                         if (Str::endsWith($emote, $text)) {
120                                 return true;
121                         }
122                 }
123                 return false;
124         }
125
126         public function hasEmoteThatStartsOrEndsWith($text) {
127                 foreach ($this->emotes as $emote) {
128                         if (Str::startsWith($emote, $text) || Str::endsWith($emote, $text)) {
129                                 return true;
130                         }
131                 }
132                 return false;
133         }
134
135         public function hasEmoteThatStartsWith($text) {
136                 foreach ($this->emotes as $emote) {
137                         if (Str::startsWith($emote, $text)) {
138                                 return true;
139                         }
140                 }
141                 return false;
142         }
143
144         public function hasToken($text) {
145                 if (is_array($text)) {
146                         foreach ($text as $token) {
147                                 if (in_array($token, $this->tokens)) {
148                                         return true;
149                                 }
150                         }
151                         return false;
152                 }
153                 return in_array($text, $this->tokens);
154         }
155
156         public function hasTokenThatContains($text) {
157                 foreach ($this->tokens as $token) {
158                         if (Str::contains($token, $text)) {
159                                 return true;
160                         }
161                 }
162                 return false;
163         }
164
165         public function hasTokenThatEndsWith($text) {
166                 foreach ($this->tokens as $token) {
167                         if (Str::endsWith($token, $text)) {
168                                 return true;
169                         }
170                 }
171                 return false;
172         }
173
174         public function hasTokenThatStartsOrEndsWith($text) {
175                 foreach ($this->tokens as $token) {
176                         if (Str::startsWith($token, $text) || Str::endsWith($token, $text)) {
177                                 return true;
178                         }
179                 }
180                 return false;
181         }
182
183         public function hasTokenThatStartsWith($text) {
184                 foreach ($this->tokens as $token) {
185                         if (Str::startsWith($token, $text)) {
186                                 return true;
187                         }
188                 }
189                 return false;
190         }
191
192         public function startsOrEndsWith($text) {
193                 return $this->startsWith($text) || $this->endsWith($text);
194         }
195
196         public function startsOrEndsWithEmotelessToken($text) {
197                 return $this->startsWithEmotelessToken($text) || $this->endsWithEmotelessToken($text);
198         }
199
200         public function startsOrEndsWithRaw($text) {
201                 return $this->startsWithRaw($text) || $this->endsWithRaw($text);
202         }
203
204         public function startsOrEndsWithToken($text) {
205                 return $this->startsWithToken($text) || $this->endsWithToken($text);
206         }
207
208         public function startsWith($text) {
209                 return Str::startsWith($this->text, $text);
210         }
211
212         public function startsWithEmoteless($text) {
213                 return Str::startsWith($this->emoteless, $text);
214         }
215
216         public function startsWithEmotelessToken($text) {
217                 return isset($this->emoteless_tokens[0]) && $this->emoteless_tokens[0] == $text;
218         }
219
220         public function startsWithRaw($text) {
221                 return Str::startsWith($this->raw, $text);
222         }
223
224         public function startsWithToken($text) {
225                 return isset($this->tokens[0]) && $this->tokens[0] == $text;
226         }
227
228
229         public function isSpammy() {
230                 if ($this->startsWith('!')) {
231                         return true;
232                 }
233                 if ($this->contains(['€', '$', '@', '://'])) {
234                         return true;
235                 }
236                 if ($this->containsRaw(['followers', 'promotion', 'viewers'])) {
237                         return true;
238                 }
239                 if ($this->containsRaw('horstie')) {
240                         return true;
241                 }
242                 if ($this->containsRaw(['folgtjetzt', 'vielendankfürdenraid', 'thanksfortheraid', 'willkommenaufstarbase47'])) {
243                         return true;
244                 }
245                 return false;
246         }
247
248
249         public function classify() {
250                 if (is_null($this->classification)) {
251                         if (empty($this->text)) {
252                                 $this->classification = 'unclassified';
253                         } else if ($this->startsWith('!')) {
254                                 $this->classification = 'cmd';
255                         } else if ($this->hasTokenThatStartsOrEndsWith(['gg']) || $this->hasEmoteThatEndsWith(['gg'])) {
256                                 $this->classification = 'gg';
257                         } else if ($this->containsRaw(['glgl', 'glhf', 'goodluck', 'hfgl'])) {
258                                 $this->classification = 'gl';
259                         } else if ($this->hasToken(['danke', 'thanks', 'thx', 'ty']) && !$this->hasToken(['nah', 'nee', 'nein', 'no'])) {
260                                 $this->classification = 'thx';
261                         } else if ($this->startsWithRaw(['ahoi', 'hallo', 'hello', 'hey', 'huhu', 'moin']) || $this->hasEmoteThatEndsWith(['hello', 'heyguys', 'hi', 'vohiyo', 'wave']) || $this->hasToken(['hi', 'hey', 'yo']) || $this->containsRaw(['gutenmorgen', 'gutenabend'])) {
262                                 $this->classification = 'hi';
263                         } else if ($this->hasTokenThatStartsOrEndsWith(['pog', 'wow'])) {
264                                 $this->classification = 'pog';
265                         } else if ($this->containsRaw(['hype']) || $this->hasEmoteThatEndsWith(['dance', 'jam', 'party', 'rave', 'troete'])) {
266                                 $this->classification = 'hype';
267                         } else if ($this->hasToken(['<3']) || $this->hasEmoteThatEndsWith(['heart', 'herz', 'hug', 'love'])) {
268                                 $this->classification = 'love';
269                         } else if ($this->hasToken(['nani', 'wat', 'wtf']) || $this->hasEmoteThatEndsWith(['wat', 'wtf'])) {
270                                 $this->classification = 'wtf';
271                         } else if ($this->hasConsecutiveTokens([':', 'eyes', ':']) || $this->hasEmoteThatEndsWith(['eyes'])) {
272                                 $this->classification = 'eyes';
273                         } else if ($this->hasEmoteThatEndsWith(['angry', 'rage', 'ree'])) {
274                                 $this->classification = 'rage';
275                         } else if ($this->hasToken([':(']) || $this->hasEmoteThatEndsWith(['cry', 'sad'])) {
276                                 $this->classification = 'sad';
277                         } else if ($this->hasToken(['monkas', 'sweat_smile']) || $this->hasEmoteThatEndsWith(['sweat'])) {
278                                 $this->classification = 'sweat';
279                         } else if ($this->endsWithEmoteless('?')) {
280                                 $this->classification = 'question';
281                         } else if ($this->hasToken(['jo', 'yep', 'yes']) || $this->startsOrEndsWithEmotelessToken('ja') || $this->containsRaw('nodders') || $this->hasEmoteThatEndsWith(['nod', 'nodders', 'yea'])) {
282                                 $this->classification = 'yes';
283                         } else if ($this->hasToken(['nah', 'nee', 'nein', 'no']) || $this->containsRaw('nopers') || $this->hasEmoteThatEndsWith(['nay', 'nope', 'nopers'])) {
284                                 $this->classification = 'no';
285                         } else if ($this->hasEmoteThatContains(['kappa', 'keepo'])) {
286                                 $this->classification = 'kappa';
287                         } else if ($this->startsOrEndsWithRaw(['o7']) || $this->hasEmoteThatContains('salut')) {
288                                 $this->classification = 'o7';
289                         } else if ($this->containsRaw(['haha', 'hehe', 'hihi', 'kekw', 'lol', 'lul']) || $this->hasTokenThatStartsWith(['xd']) || $this->hasConsecutiveTokens([':', 'd'])) {
290                                 $this->classification = 'lol';
291                         } else if (is_numeric($this->raw)) {
292                                 $this->classification = 'number';
293                         } else {
294                                 $this->classification = 'unclassified';
295                         }
296                 }
297                 return $this->classification;
298         }
299
300
301         private $text;
302         private $tags;
303         private $raw;
304         private $tokens;
305
306         private $emotes = [];
307         private $emoteless = '';
308         private $emoteless_raw = '';
309         private $emoteless_tokens = [];
310
311         private $classification = null;
312
313 }