]> git.localhorst.tv Git - alttp.git/blob - app/TwitchBot/TokenizedMessage.php
259cfc6a33f20759f4f6a817e0f10df51232a290
[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 = $text;
13                 $this->tags = $tags;
14                 $this->raw = strtolower(preg_replace('/[^\w]/', '', $text));
15                 $this->tokens = preg_split('/\s+/', strtolower(trim($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[] = 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_tokens = preg_split('/\s+/', strtolower($this->emoteless));
34         }
35
36         public static function fromIRC(IRCMessage $msg) {
37                 return new self($msg->getText(), $msg->tags);
38         }
39
40         public static function fromLog(ChatLog $log) {
41                 return new self($log->params[1], $log->tags);
42         }
43
44         public static function fromString($text, $tags = []) {
45                 return new self($text, $tags);
46         }
47
48
49         public function getNumericValue() {
50                 return intval($this->raw);
51         }
52
53         public function isSpammy() {
54                 if (substr($this->raw, 0, 1) == '!') {
55                         return true;
56                 }
57                 if (strpos($this->raw, '$') !== false) {
58                         return true;
59                 }
60                 if (strpos($this->raw, '€') !== false) {
61                         return true;
62                 }
63                 if (strpos($this->raw, '@') !== false) {
64                         return true;
65                 }
66                 if (strpos($this->raw, '://') !== false) {
67                         return true;
68                 }
69                 if (strpos($this->raw, 'followers') !== false) {
70                         return true;
71                 }
72                 if (strpos($this->raw, 'horstie') !== false) {
73                         return true;
74                 }
75                 if (strpos($this->raw, 'promotion') !== false) {
76                         return true;
77                 }
78                 if (strpos($this->raw, 'viewers') !== false) {
79                         return true;
80                 }
81                 if (strpos($this->raw, 'view ers') !== false) {
82                         return true;
83                 }
84                 if (strpos($this->raw, 'vielen dank für den raid') !== false) {
85                         return true;
86                 }
87                 if (strpos($this->raw, 'willkommen auf starbase 47') !== false) {
88                         return true;
89                 }
90                 return false;
91         }
92
93
94         public function classify() {
95                 if (is_null($this->classification)) {
96                         if (empty($this->raw)) {
97                                 $this->classification = 'unclassified';
98                         } else if (is_numeric($this->raw)) {
99                                 $this->classification = 'number';
100                         } else if (Str::startsWith($this->raw, 'gg') || Str::endsWith($this->raw, 'gg')) {
101                                 $this->classification = 'gg';
102                         } else if (Str::contains($this->raw, ['glgl', 'glhf', 'hfgl'])) {
103                                 $this->classification = 'gl';
104                         } else if (Str::contains($this->raw, ['haha', 'hehe', 'hihi', 'kekw', 'lol', 'lul', 'xd'])) {
105                                 $this->classification = 'lol';
106                         } else if (Str::startsWith($this->raw, ['ahoi', 'hallo', 'hello', 'hi ', 'huhu']) || Str::endsWith($this->raw, ['hi', 'wave'])) {
107                                 $this->classification = 'hi';
108                         } else if (Str::contains($this->raw, ['pog', 'wow'])) {
109                                 $this->classification = 'pog';
110                         } else if (Str::contains($this->raw, ['hype'])) {
111                                 $this->classification = 'hype';
112                         } else if (Str::startsWith($this->raw, 'o7') || Str::endsWith($this->raw, 'o7') || Str::contains($this->raw, 'salut')) {
113                                 $this->classification = 'o7';
114                         } else {
115                                 $this->classification = 'unclassified';
116                         }
117                 }
118                 return $this->classification;
119         }
120
121
122         private $text;
123         private $tags;
124         private $raw;
125         private $tokens;
126
127         private $emotes = [];
128         private $emoteless = '';
129         private $emoteless_tokens = [];
130
131         private $classification = null;
132
133 }