]> git.localhorst.tv Git - alttp.git/blobdiff - app/TwitchBot/TokenizedMessage.php
respond to whispers
[alttp.git] / app / TwitchBot / TokenizedMessage.php
index 0e241d5cd804c490e72f761d01cba7ae70357e5d..f218e7ae9de89e8482bb0a2b6d0b1c92ff8eaab3 100644 (file)
@@ -9,12 +9,15 @@ use Illuminate\Support\Str;
 class TokenizedMessage {
 
        public function __construct($text, $tags = []) {
-               $this->text = $text;
+               $this->text = trim($text);
                $this->tags = $tags;
-               $this->raw = strtolower(preg_replace('/[^\w]/u', '', $text));
-               $this->tokens = preg_split('/\s+/', strtolower(trim($text)));
+               if (isset($tags['reply-parent-display-name'])) {
+                       $this->text = mb_substr($text, mb_strlen($tags['reply-parent-display-name']) + 2);
+               }
+               $this->raw = strtolower(preg_replace('/[^\w]/u', '', $this->text));
+               $this->tokens = array_values(array_map('trim', array_filter(preg_split('/\b/u', strtolower($this->text)))));
 
-               $this->emoteless = $this->text;
+               $this->emoteless = $text;
                if (isset($this->tags['emotes']) && !empty($this->tags['emotes'])) {
                        $emotes = explode('/', $this->tags['emotes']);
                        foreach ($emotes as $emote) {
@@ -22,16 +25,16 @@ class TokenizedMessage {
                                $positions = explode(',', $set[1]);
                                foreach ($positions as $position) {
                                        $coords = explode('-', $position);
-                                       $this->emotes[] = preg_replace('/\d+$/', '', strtolower(substr($this->text, $coords[0], $coords[1] - $coords[0] + 1)));
-                                       for ($i = intval($coords[0]); $i <= intval($coords[1]); ++$i) {
-                                               $this->emoteless[$i] = ' ';
-                                       }
+                                       $emote_text = mb_substr($this->text, $coords[0], $coords[1] - $coords[0] + 1);
+                                       $this->original_emotes[] = $emote_text;
+                                       $this->emotes[] = preg_replace('/\d+$/', '', strtolower($emote_text));
+                                       $this->emoteless = mb_substr($this->emoteless, 0, $coords[0]).str_repeat(' ', $coords[1] - $coords[0] + 1).mb_substr($this->emoteless, $coords[1] + 1);
                                }
                        }
-                       $this->emoteless = trim(preg_replace('/\s+/', ' ', $this->emoteless));
+                       $this->emoteless = trim(preg_replace('/\s+/u', ' ', $this->emoteless));
                }
-               $this->emoteless_raw = strtolower(preg_replace('/[^\w]/', '', $this->emoteless));
-               $this->emoteless_tokens = preg_split('/\s+/', strtolower($this->emoteless));
+               $this->emoteless_raw = strtolower(preg_replace('/[^\w]/u', '', $this->emoteless));
+               $this->emoteless_tokens = array_values(array_map('trim', array_filter(preg_split('/\b/u', strtolower($this->emoteless)))));
        }
 
        public static function fromIRC(IRCMessage $msg) {
@@ -47,6 +50,11 @@ class TokenizedMessage {
        }
 
 
+       public function getText() {
+               return $this->text;
+       }
+
+
        public function contains($text) {
                return Str::contains($this->text, $text);
        }
@@ -67,14 +75,40 @@ class TokenizedMessage {
                return Str::endsWith($this->emoteless, $text);
        }
 
+       public function endsWithEmotelessToken($text) {
+               return !empty($this->emoteless_tokens) && $this->emoteless_tokens[count($this->emoteless_tokens) - 1] == $text;
+       }
+
        public function endsWithRaw($text) {
                return Str::endsWith($this->raw, $text);
        }
 
+       public function endsWithToken($text) {
+               return !empty($this->tokens) && $this->tokens[count($this->tokens) - 1] == $text;
+       }
+
+       public function getEmotes() {
+               return $this->emotes;
+       }
+
        public function getNumericValue() {
                return intval($this->text);
        }
 
+       public function getOriginalEmotes() {
+               return $this->original_emotes;
+       }
+
+       public function hasConsecutiveTokens($tokens) {
+               for ($i = 0; $i < count($this->tokens) - count($tokens) + 1; ++$i) {
+                       for ($j = 0; $j < count($tokens); ++$j) {
+                               if ($this->tokens[$i + $j] != $tokens[$j]) break;
+                       }
+                       if ($j == count($tokens)) return true;
+               }
+               return false;
+       }
+
        public function hasEmote($text) {
                if (is_array($text)) {
                        foreach ($text as $token) {
@@ -171,14 +205,34 @@ class TokenizedMessage {
                return false;
        }
 
+       public function isLong() {
+               return strlen($this->emoteless_raw) > 20;
+       }
+
+       public function isShort() {
+               return strlen($this->emoteless_raw) < 15;
+       }
+
+       public function isVeryLong() {
+               return strlen($this->emoteless_raw) > 40;
+       }
+
        public function startsOrEndsWith($text) {
                return $this->startsWith($text) || $this->endsWith($text);
        }
 
+       public function startsOrEndsWithEmotelessToken($text) {
+               return $this->startsWithEmotelessToken($text) || $this->endsWithEmotelessToken($text);
+       }
+
        public function startsOrEndsWithRaw($text) {
                return $this->startsWithRaw($text) || $this->endsWithRaw($text);
        }
 
+       public function startsOrEndsWithToken($text) {
+               return $this->startsWithToken($text) || $this->endsWithToken($text);
+       }
+
        public function startsWith($text) {
                return Str::startsWith($this->text, $text);
        }
@@ -187,10 +241,18 @@ class TokenizedMessage {
                return Str::startsWith($this->emoteless, $text);
        }
 
+       public function startsWithEmotelessToken($text) {
+               return isset($this->emoteless_tokens[0]) && $this->emoteless_tokens[0] == $text;
+       }
+
        public function startsWithRaw($text) {
                return Str::startsWith($this->raw, $text);
        }
 
+       public function startsWithToken($text) {
+               return isset($this->tokens[0]) && $this->tokens[0] == $text;
+       }
+
 
        public function isSpammy() {
                if ($this->startsWith('!')) {
@@ -199,13 +261,22 @@ class TokenizedMessage {
                if ($this->contains(['€', '$', '@', '://'])) {
                        return true;
                }
-               if ($this->containsRaw(['followers', 'promotion', 'viewers'])) {
+               if ($this->containsRaw(['follow', 'promotion', 'viewer'])) {
                        return true;
                }
-               if ($this->containsRaw('horstie')) {
+               if ($this->containsRaw('horsti')) {
                        return true;
                }
-               if ($this->containsRaw(['vielendankfürdenraid', 'thanksfortheraid', 'willkommenaufstarbase47'])) {
+               if ($this->containsRaw([
+                       'folgtjetzt',
+                       'hatdeinenkanalgeraided',
+                       'isnowlivestreaming',
+                       'stürmtdenladenmit',
+                       'thanksfortheraid',
+                       'verschwindetfürneweileindenlurk',
+                       'vielendankfürdenraid',
+                       'willkommenaufstarbase47',
+               ])) {
                        return true;
                }
                return false;
@@ -214,34 +285,48 @@ class TokenizedMessage {
 
        public function classify() {
                if (is_null($this->classification)) {
-                       if (empty($this->raw)) {
+                       if (empty($this->text) || $this->isVeryLong()) {
                                $this->classification = 'unclassified';
                        } else if ($this->startsWith('!')) {
                                $this->classification = 'cmd';
-                       } else if (is_numeric($this->raw)) {
-                               $this->classification = 'number';
-                       } else if ($this->hasTokenThatStartsOrEndsWith(['gg']) || $this->hasEmoteThatEndsWith(['gg'])) {
+                       } else if ($this->isShort() && ($this->hasTokenThatStartsOrEndsWith(['gg']) || $this->hasEmoteThatEndsWith(['gg']))) {
                                $this->classification = 'gg';
-                       } else if ($this->containsRaw(['glgl', 'glhf', 'goodluck', 'hfgl'])) {
+                       } else if ($this->isShort() && ($this->containsRaw(['glgl', 'glhf', 'goodluck', 'hfgl', 'vielglück']) || $this->hasToken('gl'))) {
                                $this->classification = 'gl';
-                       } else if ($this->startsWithRaw(['ahoi', 'hallo', 'hello', 'hey', 'huhu', 'moin']) || $this->hasEmoteThatEndsWith(['hello', 'heyguys', 'hi', 'wave']) || $this->hasToken(['hi', 'hey']) || $this->containsRaw(['gutenmorgen', 'gutenabend'])) {
+                       } else if ($this->hasToken(['danke', 'thanks', 'thx', 'ty']) && !$this->hasToken(['nah', 'nee', 'nein', 'no'])) {
+                               $this->classification = 'thx';
+                       } 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']))) {
                                $this->classification = 'hi';
-                       } else if ($this->hasTokenThatStartsOrEndsWith(['pog', 'wow'])) {
+                       } else if ($this->isShort() && $this->hasTokenThatStartsOrEndsWith(['pog', 'wow'])) {
                                $this->classification = 'pog';
-                       } else if ($this->containsRaw(['hype'])) {
+                       } else if ($this->containsRaw(['hype', 'letsgo']) || $this->hasEmoteThatEndsWith(['dance', 'jam', 'party', 'rave', 'troete'])) {
                                $this->classification = 'hype';
-                       } else if ($this->hasToken(['danke', 'thanks', 'thx', 'ty'])) {
-                               $this->classification = 'thx';
-                       } else if ($this->hasToken(['<3']) || $this->hasEmoteThatEndsWith(['herz', 'hug', 'love'])) {
+                       } else if ($this->hasToken(['<3']) || $this->hasEmoteThatEndsWith(['heart', 'herz', 'hug', 'love'])) {
                                $this->classification = 'love';
-                       } else if ($this->hasTokenThatStartsWith(['wat', 'wtf']) || $this->hasEmoteThatStartsWith(['wat', 'wtf'])) {
+                       } else if ($this->hasToken(['nani', 'wat', 'wtf']) || $this->hasEmoteThatEndsWith(['wat', 'wtf'])) {
                                $this->classification = 'wtf';
+                       } else if ($this->hasConsecutiveTokens([':', 'eyes', ':']) || $this->hasEmoteThatEndsWith(['eyes'])) {
+                               $this->classification = 'eyes';
+                       } else if ($this->hasEmoteThatEndsWith(['angry', 'rage', 'ree'])) {
+                               $this->classification = 'rage';
+                       } else if ($this->hasToken([':(']) || $this->hasEmoteThatEndsWith(['cry', 'sad'])) {
+                               $this->classification = 'sad';
+                       } else if ($this->hasToken(['monkas', 'sweat_smile']) || $this->hasEmoteThatEndsWith(['sweat'])) {
+                               $this->classification = 'sweat';
                        } else if ($this->endsWithEmoteless('?')) {
                                $this->classification = 'question';
+                       } else if ($this->hasToken(['jo', 'yep', 'yes']) || $this->startsOrEndsWithEmotelessToken('ja') || $this->containsRaw('nodders') || $this->hasEmoteThatEndsWith(['nod', 'nodders', 'yea'])) {
+                               $this->classification = 'yes';
+                       } else if ($this->hasToken(['nah', 'nee', 'nein', 'no']) || $this->containsRaw('nopers') || $this->hasEmoteThatEndsWith(['nay', 'nope', 'nopers'])) {
+                               $this->classification = 'no';
+                       } else if ($this->hasEmoteThatContains(['kappa', 'keepo'])) {
+                               $this->classification = 'kappa';
                        } else if ($this->startsOrEndsWithRaw(['o7']) || $this->hasEmoteThatContains('salut')) {
                                $this->classification = 'o7';
-                       } else if ($this->containsRaw(['haha', 'hehe', 'hihi', 'kekw', 'lol', 'lul']) || $this->hasTokenThatStartsWith([':d', 'xd'])) {
+                       } else if (!$this->isLong() && ($this->containsRaw(['haha', 'hehe', 'hihi', 'kekw', 'lol', 'lul']) || $this->hasTokenThatStartsWith(['xd']) || $this->hasConsecutiveTokens([':', 'd']))) {
                                $this->classification = 'lol';
+                       } else if (is_numeric($this->raw)) {
+                               $this->classification = 'number';
                        } else {
                                $this->classification = 'unclassified';
                        }
@@ -249,6 +334,36 @@ class TokenizedMessage {
                return $this->classification;
        }
 
+       public function getResponseCategory() {
+               switch ($this->classify()) {
+                       case 'gg':
+                               return ['love', 'eyes', 'thx', 'pog', 'kappa'];
+                       case 'gl':
+                               return ['love', 'eyes', 'thx'];
+                       case 'hi':
+                               return ['hi', 'love', 'eyes', 'hype', 'pog'];
+                       case 'kappa':
+                               return ['kappa', 'lol', 'eyes'];
+                       case 'love':
+                               return ['hi', 'love', 'eyes', 'thx'];
+                       case 'question':
+                               if (
+                                       $this->hasToken(['number', 'nummer', 'wieviel', 'zahl']) ||
+                                       $this->hasConsecutiveTokens(['how', 'many']) ||
+                                       $this->hasConsecutiveTokens(['how', 'much']) ||
+                                       $this->hasConsecutiveTokens(['wie', 'viele'])
+                               ) {
+                                       return ['yes', 'no', 'kappa', 'wtf', 'number'];
+                               }
+                               return ['yes', 'no', 'kappa', 'wtf'];
+                       case 'rage':
+                               return ['kappa', 'lol', 'rage'];
+                       case 'wtf':
+                               return ['kappa', 'lol', 'rage'];
+               }
+               return false;
+       }
+
 
        private $text;
        private $tags;
@@ -256,6 +371,7 @@ class TokenizedMessage {
        private $tokens;
 
        private $emotes = [];
+       private $original_emotes = [];
        private $emoteless = '';
        private $emoteless_raw = '';
        private $emoteless_tokens = [];