3 namespace App\TwitchBot;
5 use App\Models\ChatLog;
6 use Illuminate\Support\Arr;
7 use Illuminate\Support\Str;
9 class TokenizedMessage {
11 public function __construct($text, $tags = []) {
12 $this->text = trim($text);
14 if (isset($tags['reply-parent-display-name'])) {
15 $this->text = mb_substr($text, mb_strlen($tags['reply-parent-display-name']) + 2);
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)))));
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);
34 $this->emoteless = trim(preg_replace('/\s+/u', ' ', $this->emoteless));
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)))));
40 public static function fromIRC(IRCMessage $msg) {
41 return new self($msg->getText(), $msg->tags);
44 public static function fromLog(ChatLog $log) {
45 return new self($log->params[1], $log->tags);
48 public static function fromString($text, $tags = []) {
49 return new self($text, $tags);
53 public function getText() {
58 public function contains($text) {
59 return Str::contains($this->text, $text);
62 public function containsEmoteless($text) {
63 return Str::contains($this->emoteless, $text);
66 public function containsRaw($text) {
67 return Str::contains($this->raw, $text);
70 public function endsWith($text) {
71 return Str::endsWith($this->text, $text);
74 public function endsWithEmoteless($text) {
75 return Str::endsWith($this->emoteless, $text);
78 public function endsWithEmotelessToken($text) {
79 return !empty($this->emoteless_tokens) && $this->emoteless_tokens[count($this->emoteless_tokens) - 1] == $text;
82 public function endsWithRaw($text) {
83 return Str::endsWith($this->raw, $text);
86 public function endsWithToken($text) {
87 return !empty($this->tokens) && $this->tokens[count($this->tokens) - 1] == $text;
90 public function getEmotes() {
94 public function getNumericValue() {
95 return intval($this->text);
98 public function getOriginalEmotes() {
99 return $this->original_emotes;
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;
107 if ($j == count($tokens)) return true;
112 public function hasEmote($text) {
113 if (is_array($text)) {
114 foreach ($text as $token) {
115 if (in_array($token, $this->emotes)) {
121 return in_array($text, $this->emotes);
124 public function hasEmoteThatContains($text) {
125 foreach ($this->emotes as $emote) {
126 if (Str::contains($emote, $text)) {
133 public function hasEmoteThatEndsWith($text) {
134 foreach ($this->emotes as $emote) {
135 if (Str::endsWith($emote, $text)) {
142 public function hasEmoteThatStartsOrEndsWith($text) {
143 foreach ($this->emotes as $emote) {
144 if (Str::startsWith($emote, $text) || Str::endsWith($emote, $text)) {
151 public function hasEmoteThatStartsWith($text) {
152 foreach ($this->emotes as $emote) {
153 if (Str::startsWith($emote, $text)) {
160 public function hasToken($text) {
161 if (is_array($text)) {
162 foreach ($text as $token) {
163 if (in_array($token, $this->tokens)) {
169 return in_array($text, $this->tokens);
172 public function hasTokenThatContains($text) {
173 foreach ($this->tokens as $token) {
174 if (Str::contains($token, $text)) {
181 public function hasTokenThatEndsWith($text) {
182 foreach ($this->tokens as $token) {
183 if (Str::endsWith($token, $text)) {
190 public function hasTokenThatStartsOrEndsWith($text) {
191 foreach ($this->tokens as $token) {
192 if (Str::startsWith($token, $text) || Str::endsWith($token, $text)) {
199 public function hasTokenThatStartsWith($text) {
200 foreach ($this->tokens as $token) {
201 if (Str::startsWith($token, $text)) {
208 public function isLong() {
209 return strlen($this->emoteless_raw) > 20;
212 public function isShort() {
213 return strlen($this->emoteless_raw) < 15;
216 public function isVeryLong() {
217 return strlen($this->emoteless_raw) > 40;
220 public function startsOrEndsWith($text) {
221 return $this->startsWith($text) || $this->endsWith($text);
224 public function startsOrEndsWithEmotelessToken($text) {
225 return $this->startsWithEmotelessToken($text) || $this->endsWithEmotelessToken($text);
228 public function startsOrEndsWithRaw($text) {
229 return $this->startsWithRaw($text) || $this->endsWithRaw($text);
232 public function startsOrEndsWithToken($text) {
233 return $this->startsWithToken($text) || $this->endsWithToken($text);
236 public function startsWith($text) {
237 return Str::startsWith($this->text, $text);
240 public function startsWithEmoteless($text) {
241 return Str::startsWith($this->emoteless, $text);
244 public function startsWithEmotelessToken($text) {
245 return isset($this->emoteless_tokens[0]) && $this->emoteless_tokens[0] == $text;
248 public function startsWithRaw($text) {
249 return Str::startsWith($this->raw, $text);
252 public function startsWithToken($text) {
253 return isset($this->tokens[0]) && $this->tokens[0] == $text;
257 public function isSpammy() {
258 if ($this->startsWith('!')) {
261 if ($this->contains(['€', '$', '@', '://'])) {
264 if ($this->containsRaw(['follow', 'promotion', 'viewer'])) {
267 if ($this->containsRaw('horsti')) {
270 if ($this->containsRaw([
272 'hatdeinenkanalgeraided',
273 'isnowlivestreaming',
276 'verschwindetfürneweileindenlurk',
277 'vielendankfürdenraid',
278 'willkommenaufstarbase47',
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';
331 $this->classification = 'unclassified';
334 return $this->classification;
337 public function getResponseCategory() {
338 switch ($this->classify()) {
340 return ['love', 'eyes', 'thx', 'pog', 'kappa'];
342 return ['love', 'eyes', 'thx'];
344 return ['hi', 'love', 'eyes', 'hype', 'pog'];
346 return ['kappa', 'lol', 'eyes'];
348 return ['hi', 'love', 'eyes', 'thx'];
351 $this->hasToken(['number', 'nummer', 'wieviel', 'zahl']) ||
352 $this->hasConsecutiveTokens(['how', 'many']) ||
353 $this->hasConsecutiveTokens(['how', 'much']) ||
354 $this->hasConsecutiveTokens(['wie', 'viele'])
356 return ['yes', 'no', 'kappa', 'wtf', 'number'];
358 return ['yes', 'no', 'kappa', 'wtf'];
360 return ['kappa', 'lol', 'rage'];
362 return ['kappa', 'lol', 'rage'];
373 private $emotes = [];
374 private $original_emotes = [];
375 private $emoteless = '';
376 private $emoteless_raw = '';
377 private $emoteless_tokens = [];
379 private $classification = null;