]> git.localhorst.tv Git - alttp.git/blob - app/TwitchBot/TokenizedMessage.php
more classifications
[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 endsWithRaw($text) {
71                 return Str::endsWith($this->raw, $text);
72         }
73
74         public function getNumericValue() {
75                 return intval($this->text);
76         }
77
78         public function hasConsecutiveTokens($tokens) {
79                 for ($i = 0; $i < count($this->tokens) - count($tokens) + 1; ++$i) {
80                         for ($j = 0; $j < count($tokens); ++$j) {
81                                 if ($this->tokens[$i + $j] != $tokens[$j]) break;
82                         }
83                         if ($j == count($tokens)) return true;
84                 }
85                 return false;
86         }
87
88         public function hasEmote($text) {
89                 if (is_array($text)) {
90                         foreach ($text as $token) {
91                                 if (in_array($token, $this->emotes)) {
92                                         return true;
93                                 }
94                         }
95                         return false;
96                 }
97                 return in_array($text, $this->emotes);
98         }
99
100         public function hasEmoteThatContains($text) {
101                 foreach ($this->emotes as $emote) {
102                         if (Str::contains($emote, $text)) {
103                                 return true;
104                         }
105                 }
106                 return false;
107         }
108
109         public function hasEmoteThatEndsWith($text) {
110                 foreach ($this->emotes as $emote) {
111                         if (Str::endsWith($emote, $text)) {
112                                 return true;
113                         }
114                 }
115                 return false;
116         }
117
118         public function hasEmoteThatStartsOrEndsWith($text) {
119                 foreach ($this->emotes as $emote) {
120                         if (Str::startsWith($emote, $text) || Str::endsWith($emote, $text)) {
121                                 return true;
122                         }
123                 }
124                 return false;
125         }
126
127         public function hasEmoteThatStartsWith($text) {
128                 foreach ($this->emotes as $emote) {
129                         if (Str::startsWith($emote, $text)) {
130                                 return true;
131                         }
132                 }
133                 return false;
134         }
135
136         public function hasToken($text) {
137                 if (is_array($text)) {
138                         foreach ($text as $token) {
139                                 if (in_array($token, $this->tokens)) {
140                                         return true;
141                                 }
142                         }
143                         return false;
144                 }
145                 return in_array($text, $this->tokens);
146         }
147
148         public function hasTokenThatContains($text) {
149                 foreach ($this->tokens as $token) {
150                         if (Str::contains($token, $text)) {
151                                 return true;
152                         }
153                 }
154                 return false;
155         }
156
157         public function hasTokenThatEndsWith($text) {
158                 foreach ($this->tokens as $token) {
159                         if (Str::endsWith($token, $text)) {
160                                 return true;
161                         }
162                 }
163                 return false;
164         }
165
166         public function hasTokenThatStartsOrEndsWith($text) {
167                 foreach ($this->tokens as $token) {
168                         if (Str::startsWith($token, $text) || Str::endsWith($token, $text)) {
169                                 return true;
170                         }
171                 }
172                 return false;
173         }
174
175         public function hasTokenThatStartsWith($text) {
176                 foreach ($this->tokens as $token) {
177                         if (Str::startsWith($token, $text)) {
178                                 return true;
179                         }
180                 }
181                 return false;
182         }
183
184         public function startsOrEndsWith($text) {
185                 return $this->startsWith($text) || $this->endsWith($text);
186         }
187
188         public function startsOrEndsWithRaw($text) {
189                 return $this->startsWithRaw($text) || $this->endsWithRaw($text);
190         }
191
192         public function startsWith($text) {
193                 return Str::startsWith($this->text, $text);
194         }
195
196         public function startsWithEmoteless($text) {
197                 return Str::startsWith($this->emoteless, $text);
198         }
199
200         public function startsWithRaw($text) {
201                 return Str::startsWith($this->raw, $text);
202         }
203
204
205         public function isSpammy() {
206                 if ($this->startsWith('!')) {
207                         return true;
208                 }
209                 if ($this->contains(['€', '$', '@', '://'])) {
210                         return true;
211                 }
212                 if ($this->containsRaw(['followers', 'promotion', 'viewers'])) {
213                         return true;
214                 }
215                 if ($this->containsRaw('horstie')) {
216                         return true;
217                 }
218                 if ($this->containsRaw(['vielendankfürdenraid', 'thanksfortheraid', 'willkommenaufstarbase47'])) {
219                         return true;
220                 }
221                 return false;
222         }
223
224
225         public function classify() {
226                 if (is_null($this->classification)) {
227                         if (empty($this->text)) {
228                                 $this->classification = 'unclassified';
229                         } else if ($this->startsWith('!')) {
230                                 $this->classification = 'cmd';
231                         } else if (is_numeric($this->raw)) {
232                                 $this->classification = 'number';
233                         } else if ($this->hasTokenThatStartsOrEndsWith(['gg']) || $this->hasEmoteThatEndsWith(['gg'])) {
234                                 $this->classification = 'gg';
235                         } else if ($this->containsRaw(['glgl', 'glhf', 'goodluck', 'hfgl'])) {
236                                 $this->classification = 'gl';
237                         } 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'])) {
238                                 $this->classification = 'hi';
239                         } else if ($this->hasTokenThatStartsOrEndsWith(['pog', 'wow'])) {
240                                 $this->classification = 'pog';
241                         } else if ($this->containsRaw(['hype']) || $this->hasEmoteThatEndsWith(['dance', 'jam', 'party', 'rave', 'troete'])) {
242                                 $this->classification = 'hype';
243                         } else if ($this->hasToken(['danke', 'thanks', 'thx', 'ty'])) {
244                                 $this->classification = 'thx';
245                         } else if ($this->hasToken(['<3']) || $this->hasEmoteThatEndsWith(['heart', 'herz', 'hug', 'love'])) {
246                                 $this->classification = 'love';
247                         } else if ($this->hasToken(['nani', 'wat', 'wtf']) || $this->hasEmoteThatEndsWith(['wat', 'wtf'])) {
248                                 $this->classification = 'wtf';
249                         } else if ($this->hasConsecutiveTokens([':', 'eyes', ':']) || $this->hasEmoteThatEndsWith(['eyes'])) {
250                                 $this->classification = 'eyes';
251                         } else if ($this->hasEmoteThatEndsWith(['angry', 'rage', 'ree'])) {
252                                 $this->classification = 'rage';
253                         } else if ($this->hasToken([':(']) || $this->hasEmoteThatEndsWith(['cry', 'sad'])) {
254                                 $this->classification = 'sad';
255                         } else if ($this->hasToken(['monkas', 'sweat_smile']) || $this->hasEmoteThatEndsWith(['sweat'])) {
256                                 $this->classification = 'sweat';
257                         } else if ($this->endsWithEmoteless('?')) {
258                                 $this->classification = 'question';
259                         } else if ($this->startsOrEndsWithRaw(['o7']) || $this->hasEmoteThatContains('salut')) {
260                                 $this->classification = 'o7';
261                         } else if ($this->containsRaw(['haha', 'hehe', 'hihi', 'kekw', 'lol', 'lul']) || $this->hasTokenThatStartsWith(['xd']) || $this->hasConsecutiveTokens([':', 'd'])) {
262                                 $this->classification = 'lol';
263                         } else {
264                                 $this->classification = 'unclassified';
265                         }
266                 }
267                 return $this->classification;
268         }
269
270
271         private $text;
272         private $tags;
273         private $raw;
274         private $tokens;
275
276         private $emotes = [];
277         private $emoteless = '';
278         private $emoteless_raw = '';
279         private $emoteless_tokens = [];
280
281         private $classification = null;
282
283 }