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 contains($text) {
54 return Str::contains($this->text, $text);
57 public function containsEmoteless($text) {
58 return Str::contains($this->emoteless, $text);
61 public function containsRaw($text) {
62 return Str::contains($this->raw, $text);
65 public function endsWith($text) {
66 return Str::endsWith($this->text, $text);
69 public function endsWithEmoteless($text) {
70 return Str::endsWith($this->emoteless, $text);
73 public function endsWithEmotelessToken($text) {
74 return !empty($this->emoteless_tokens) && $this->emoteless_tokens[count($this->emoteless_tokens) - 1] == $text;
77 public function endsWithRaw($text) {
78 return Str::endsWith($this->raw, $text);
81 public function endsWithToken($text) {
82 return !empty($this->tokens) && $this->tokens[count($this->tokens) - 1] == $text;
85 public function getEmotes() {
89 public function getNumericValue() {
90 return intval($this->text);
93 public function getOriginalEmotes() {
94 return $this->original_emotes;
97 public function hasConsecutiveTokens($tokens) {
98 for ($i = 0; $i < count($this->tokens) - count($tokens) + 1; ++$i) {
99 for ($j = 0; $j < count($tokens); ++$j) {
100 if ($this->tokens[$i + $j] != $tokens[$j]) break;
102 if ($j == count($tokens)) return true;
107 public function hasEmote($text) {
108 if (is_array($text)) {
109 foreach ($text as $token) {
110 if (in_array($token, $this->emotes)) {
116 return in_array($text, $this->emotes);
119 public function hasEmoteThatContains($text) {
120 foreach ($this->emotes as $emote) {
121 if (Str::contains($emote, $text)) {
128 public function hasEmoteThatEndsWith($text) {
129 foreach ($this->emotes as $emote) {
130 if (Str::endsWith($emote, $text)) {
137 public function hasEmoteThatStartsOrEndsWith($text) {
138 foreach ($this->emotes as $emote) {
139 if (Str::startsWith($emote, $text) || Str::endsWith($emote, $text)) {
146 public function hasEmoteThatStartsWith($text) {
147 foreach ($this->emotes as $emote) {
148 if (Str::startsWith($emote, $text)) {
155 public function hasToken($text) {
156 if (is_array($text)) {
157 foreach ($text as $token) {
158 if (in_array($token, $this->tokens)) {
164 return in_array($text, $this->tokens);
167 public function hasTokenThatContains($text) {
168 foreach ($this->tokens as $token) {
169 if (Str::contains($token, $text)) {
176 public function hasTokenThatEndsWith($text) {
177 foreach ($this->tokens as $token) {
178 if (Str::endsWith($token, $text)) {
185 public function hasTokenThatStartsOrEndsWith($text) {
186 foreach ($this->tokens as $token) {
187 if (Str::startsWith($token, $text) || Str::endsWith($token, $text)) {
194 public function hasTokenThatStartsWith($text) {
195 foreach ($this->tokens as $token) {
196 if (Str::startsWith($token, $text)) {
203 public function isLong() {
204 return strlen($this->emoteless_raw) > 20;
207 public function isShort() {
208 return strlen($this->emoteless_raw) < 15;
211 public function isVeryLong() {
212 return strlen($this->emoteless_raw) > 40;
215 public function startsOrEndsWith($text) {
216 return $this->startsWith($text) || $this->endsWith($text);
219 public function startsOrEndsWithEmotelessToken($text) {
220 return $this->startsWithEmotelessToken($text) || $this->endsWithEmotelessToken($text);
223 public function startsOrEndsWithRaw($text) {
224 return $this->startsWithRaw($text) || $this->endsWithRaw($text);
227 public function startsOrEndsWithToken($text) {
228 return $this->startsWithToken($text) || $this->endsWithToken($text);
231 public function startsWith($text) {
232 return Str::startsWith($this->text, $text);
235 public function startsWithEmoteless($text) {
236 return Str::startsWith($this->emoteless, $text);
239 public function startsWithEmotelessToken($text) {
240 return isset($this->emoteless_tokens[0]) && $this->emoteless_tokens[0] == $text;
243 public function startsWithRaw($text) {
244 return Str::startsWith($this->raw, $text);
247 public function startsWithToken($text) {
248 return isset($this->tokens[0]) && $this->tokens[0] == $text;
252 public function isSpammy() {
253 if ($this->startsWith('!')) {
256 if ($this->contains(['€', '$', '@', '://'])) {
259 if ($this->containsRaw(['follow', 'promotion', 'viewer'])) {
262 if ($this->containsRaw('horsti')) {
265 if ($this->containsRaw([
267 'hatdeinenkanalgeraided',
268 'isnowlivestreaming',
271 'verschwindetfürneweileindenlurk',
272 'vielendankfürdenraid',
273 'willkommenaufstarbase47',
281 public function classify() {
282 if (is_null($this->classification)) {
283 if (empty($this->text) || $this->isVeryLong()) {
284 $this->classification = 'unclassified';
285 } else if ($this->startsWith('!')) {
286 $this->classification = 'cmd';
287 } else if ($this->isShort() && ($this->hasTokenThatStartsOrEndsWith(['gg']) || $this->hasEmoteThatEndsWith(['gg']))) {
288 $this->classification = 'gg';
289 } else if ($this->isShort() && $this->containsRaw(['glgl', 'glhf', 'goodluck', 'hfgl', 'vielglück'])) {
290 $this->classification = 'gl';
291 } else if ($this->hasToken(['danke', 'thanks', 'thx', 'ty']) && !$this->hasToken(['nah', 'nee', 'nein', 'no'])) {
292 $this->classification = 'thx';
293 } 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']))) {
294 $this->classification = 'hi';
295 } else if ($this->isShort() && $this->hasTokenThatStartsOrEndsWith(['pog', 'wow'])) {
296 $this->classification = 'pog';
297 } else if ($this->containsRaw(['hype', 'letsgo']) || $this->hasEmoteThatEndsWith(['dance', 'jam', 'party', 'rave', 'troete'])) {
298 $this->classification = 'hype';
299 } else if ($this->hasToken(['<3']) || $this->hasEmoteThatEndsWith(['heart', 'herz', 'hug', 'love'])) {
300 $this->classification = 'love';
301 } else if ($this->hasToken(['nani', 'wat', 'wtf']) || $this->hasEmoteThatEndsWith(['wat', 'wtf'])) {
302 $this->classification = 'wtf';
303 } else if ($this->hasConsecutiveTokens([':', 'eyes', ':']) || $this->hasEmoteThatEndsWith(['eyes'])) {
304 $this->classification = 'eyes';
305 } else if ($this->hasEmoteThatEndsWith(['angry', 'rage', 'ree'])) {
306 $this->classification = 'rage';
307 } else if ($this->hasToken([':(']) || $this->hasEmoteThatEndsWith(['cry', 'sad'])) {
308 $this->classification = 'sad';
309 } else if ($this->hasToken(['monkas', 'sweat_smile']) || $this->hasEmoteThatEndsWith(['sweat'])) {
310 $this->classification = 'sweat';
311 } else if ($this->endsWithEmoteless('?')) {
312 $this->classification = 'question';
313 } else if ($this->hasToken(['jo', 'yep', 'yes']) || $this->startsOrEndsWithEmotelessToken('ja') || $this->containsRaw('nodders') || $this->hasEmoteThatEndsWith(['nod', 'nodders', 'yea'])) {
314 $this->classification = 'yes';
315 } else if ($this->hasToken(['nah', 'nee', 'nein', 'no']) || $this->containsRaw('nopers') || $this->hasEmoteThatEndsWith(['nay', 'nope', 'nopers'])) {
316 $this->classification = 'no';
317 } else if ($this->hasEmoteThatContains(['kappa', 'keepo'])) {
318 $this->classification = 'kappa';
319 } else if ($this->startsOrEndsWithRaw(['o7']) || $this->hasEmoteThatContains('salut')) {
320 $this->classification = 'o7';
321 } else if (!$this->isLong() && ($this->containsRaw(['haha', 'hehe', 'hihi', 'kekw', 'lol', 'lul']) || $this->hasTokenThatStartsWith(['xd']) || $this->hasConsecutiveTokens([':', 'd']))) {
322 $this->classification = 'lol';
323 } else if (is_numeric($this->raw)) {
324 $this->classification = 'number';
326 $this->classification = 'unclassified';
329 return $this->classification;
332 public function getResponseCategory() {
333 switch ($this->classify()) {
335 return ['love', 'eyes', 'thx', 'pog', 'kappa'];
337 return ['love', 'eyes', 'thx'];
339 return ['hi', 'love', 'eyes', 'hype', 'pog'];
341 return ['kappa', 'lol', 'eyes'];
343 return ['hi', 'love', 'eyes', 'thx'];
346 $this->hasToken(['number', 'nummer', 'wieviel', 'zahl']) ||
347 $this->hasConsecutiveTokens(['how', 'many']) ||
348 $this->hasConsecutiveTokens(['how', 'much']) ||
349 $this->hasConsecutiveTokens(['wie', 'viele'])
351 return ['yes', 'no', 'kappa', 'lol', 'wtf', 'number'];
353 return ['yes', 'no', 'kappa', 'lol', 'wtf'];
355 return ['kappa', 'lol', 'rage'];
357 return ['kappa', 'lol', 'rage'];
368 private $emotes = [];
369 private $original_emotes = [];
370 private $emoteless = '';
371 private $emoteless_raw = '';
372 private $emoteless_tokens = [];
374 private $classification = null;