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 $this->raw = strtolower(preg_replace('/[^\w]/u', '', $this->text));
15 $this->tokens = array_values(array_map('trim', array_filter(preg_split('/\b/u', strtolower($this->text)))));
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 $emote_text = mb_substr($this->text, $coords[0], $coords[1] - $coords[0] + 1);
26 $this->original_emotes[] = $emote_text;
27 $this->emotes[] = preg_replace('/\d+$/', '', strtolower($emote_text));
28 $this->emoteless = mb_substr($this->emoteless, 0, $coords[0]).str_repeat(' ', $coords[1] - $coords[0] + 1).mb_substr($this->emoteless, $coords[1] + 1);
31 $this->emoteless = trim(preg_replace('/\s+/u', ' ', $this->emoteless));
33 $this->emoteless_raw = strtolower(preg_replace('/[^\w]/u', '', $this->emoteless));
34 $this->emoteless_tokens = array_values(array_map('trim', array_filter(preg_split('/\b/u', strtolower($this->emoteless)))));
37 public static function fromIRC(IRCMessage $msg) {
38 return new self($msg->getText(), $msg->tags);
41 public static function fromLog(ChatLog $log) {
42 return new self($log->params[1], $log->tags);
45 public static function fromString($text, $tags = []) {
46 return new self($text, $tags);
50 public function contains($text) {
51 return Str::contains($this->text, $text);
54 public function containsEmoteless($text) {
55 return Str::contains($this->emoteless, $text);
58 public function containsRaw($text) {
59 return Str::contains($this->raw, $text);
62 public function endsWith($text) {
63 return Str::endsWith($this->text, $text);
66 public function endsWithEmoteless($text) {
67 return Str::endsWith($this->emoteless, $text);
70 public function endsWithEmotelessToken($text) {
71 return !empty($this->emoteless_tokens) && $this->emoteless_tokens[count($this->emoteless_tokens) - 1] == $text;
74 public function endsWithRaw($text) {
75 return Str::endsWith($this->raw, $text);
78 public function endsWithToken($text) {
79 return !empty($this->tokens) && $this->tokens[count($this->tokens) - 1] == $text;
82 public function getEmotes() {
86 public function getNumericValue() {
87 return intval($this->text);
90 public function getOriginalEmotes() {
91 return $this->original_emotes;
94 public function hasConsecutiveTokens($tokens) {
95 for ($i = 0; $i < count($this->tokens) - count($tokens) + 1; ++$i) {
96 for ($j = 0; $j < count($tokens); ++$j) {
97 if ($this->tokens[$i + $j] != $tokens[$j]) break;
99 if ($j == count($tokens)) return true;
104 public function hasEmote($text) {
105 if (is_array($text)) {
106 foreach ($text as $token) {
107 if (in_array($token, $this->emotes)) {
113 return in_array($text, $this->emotes);
116 public function hasEmoteThatContains($text) {
117 foreach ($this->emotes as $emote) {
118 if (Str::contains($emote, $text)) {
125 public function hasEmoteThatEndsWith($text) {
126 foreach ($this->emotes as $emote) {
127 if (Str::endsWith($emote, $text)) {
134 public function hasEmoteThatStartsOrEndsWith($text) {
135 foreach ($this->emotes as $emote) {
136 if (Str::startsWith($emote, $text) || Str::endsWith($emote, $text)) {
143 public function hasEmoteThatStartsWith($text) {
144 foreach ($this->emotes as $emote) {
145 if (Str::startsWith($emote, $text)) {
152 public function hasToken($text) {
153 if (is_array($text)) {
154 foreach ($text as $token) {
155 if (in_array($token, $this->tokens)) {
161 return in_array($text, $this->tokens);
164 public function hasTokenThatContains($text) {
165 foreach ($this->tokens as $token) {
166 if (Str::contains($token, $text)) {
173 public function hasTokenThatEndsWith($text) {
174 foreach ($this->tokens as $token) {
175 if (Str::endsWith($token, $text)) {
182 public function hasTokenThatStartsOrEndsWith($text) {
183 foreach ($this->tokens as $token) {
184 if (Str::startsWith($token, $text) || Str::endsWith($token, $text)) {
191 public function hasTokenThatStartsWith($text) {
192 foreach ($this->tokens as $token) {
193 if (Str::startsWith($token, $text)) {
200 public function isLong() {
201 return strlen($this->emoteless_raw) > 20;
204 public function isShort() {
205 return strlen($this->emoteless_raw) < 15;
208 public function isVeryLong() {
209 return strlen($this->emoteless_raw) > 40;
212 public function startsOrEndsWith($text) {
213 return $this->startsWith($text) || $this->endsWith($text);
216 public function startsOrEndsWithEmotelessToken($text) {
217 return $this->startsWithEmotelessToken($text) || $this->endsWithEmotelessToken($text);
220 public function startsOrEndsWithRaw($text) {
221 return $this->startsWithRaw($text) || $this->endsWithRaw($text);
224 public function startsOrEndsWithToken($text) {
225 return $this->startsWithToken($text) || $this->endsWithToken($text);
228 public function startsWith($text) {
229 return Str::startsWith($this->text, $text);
232 public function startsWithEmoteless($text) {
233 return Str::startsWith($this->emoteless, $text);
236 public function startsWithEmotelessToken($text) {
237 return isset($this->emoteless_tokens[0]) && $this->emoteless_tokens[0] == $text;
240 public function startsWithRaw($text) {
241 return Str::startsWith($this->raw, $text);
244 public function startsWithToken($text) {
245 return isset($this->tokens[0]) && $this->tokens[0] == $text;
249 public function isSpammy() {
250 if ($this->startsWith('!')) {
253 if ($this->contains(['€', '$', '@', '://'])) {
256 if ($this->containsRaw(['followers', 'promotion', 'viewers'])) {
259 if ($this->containsRaw('horsti')) {
262 if ($this->containsRaw(['folgtjetzt', 'vielendankfürdenraid', 'thanksfortheraid', 'willkommenaufstarbase47'])) {
269 public function classify() {
270 if (is_null($this->classification)) {
271 if (empty($this->text) || $this->isVeryLong()) {
272 $this->classification = 'unclassified';
273 } else if ($this->startsWith('!')) {
274 $this->classification = 'cmd';
275 } else if ($this->isShort() && ($this->hasTokenThatStartsOrEndsWith(['gg']) || $this->hasEmoteThatEndsWith(['gg']))) {
276 $this->classification = 'gg';
277 } else if ($this->isShort() && $this->containsRaw(['glgl', 'glhf', 'goodluck', 'hfgl', 'vielglück'])) {
278 $this->classification = 'gl';
279 } else if ($this->hasToken(['danke', 'thanks', 'thx', 'ty']) && !$this->hasToken(['nah', 'nee', 'nein', 'no'])) {
280 $this->classification = 'thx';
281 } 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']))) {
282 $this->classification = 'hi';
283 } else if ($this->isShort() && $this->hasTokenThatStartsOrEndsWith(['pog', 'wow'])) {
284 $this->classification = 'pog';
285 } else if ($this->containsRaw(['hype', 'letsgo']) || $this->hasEmoteThatEndsWith(['dance', 'jam', 'party', 'rave', 'troete'])) {
286 $this->classification = 'hype';
287 } else if ($this->hasToken(['<3']) || $this->hasEmoteThatEndsWith(['heart', 'herz', 'hug', 'love'])) {
288 $this->classification = 'love';
289 } else if ($this->hasToken(['nani', 'wat', 'wtf']) || $this->hasEmoteThatEndsWith(['wat', 'wtf'])) {
290 $this->classification = 'wtf';
291 } else if ($this->hasConsecutiveTokens([':', 'eyes', ':']) || $this->hasEmoteThatEndsWith(['eyes'])) {
292 $this->classification = 'eyes';
293 } else if ($this->hasEmoteThatEndsWith(['angry', 'rage', 'ree'])) {
294 $this->classification = 'rage';
295 } else if ($this->hasToken([':(']) || $this->hasEmoteThatEndsWith(['cry', 'sad'])) {
296 $this->classification = 'sad';
297 } else if ($this->hasToken(['monkas', 'sweat_smile']) || $this->hasEmoteThatEndsWith(['sweat'])) {
298 $this->classification = 'sweat';
299 } else if ($this->endsWithEmoteless('?')) {
300 $this->classification = 'question';
301 } else if ($this->hasToken(['jo', 'yep', 'yes']) || $this->startsOrEndsWithEmotelessToken('ja') || $this->containsRaw('nodders') || $this->hasEmoteThatEndsWith(['nod', 'nodders', 'yea'])) {
302 $this->classification = 'yes';
303 } else if ($this->hasToken(['nah', 'nee', 'nein', 'no']) || $this->containsRaw('nopers') || $this->hasEmoteThatEndsWith(['nay', 'nope', 'nopers'])) {
304 $this->classification = 'no';
305 } else if ($this->hasEmoteThatContains(['kappa', 'keepo'])) {
306 $this->classification = 'kappa';
307 } else if ($this->startsOrEndsWithRaw(['o7']) || $this->hasEmoteThatContains('salut')) {
308 $this->classification = 'o7';
309 } else if (!$this->isLong() && ($this->containsRaw(['haha', 'hehe', 'hihi', 'kekw', 'lol', 'lul']) || $this->hasTokenThatStartsWith(['xd']) || $this->hasConsecutiveTokens([':', 'd']))) {
310 $this->classification = 'lol';
311 } else if (is_numeric($this->raw)) {
312 $this->classification = 'number';
314 $this->classification = 'unclassified';
317 return $this->classification;
320 public function getResponseCategory() {
321 switch ($this->classify()) {
323 return ['love', 'eyes', 'thx', 'pog', 'kappa'];
325 return ['love', 'eyes', 'thx'];
327 return ['hi', 'love', 'eyes', 'hype', 'pog'];
329 return ['kappa', 'lol', 'eyes'];
331 return ['hi', 'love', 'eyes', 'thx'];
334 $this->hasToken(['number', 'nummer', 'wieviel', 'zahl']) ||
335 $this->hasConsecutiveTokens(['how', 'many']) ||
336 $this->hasConsecutiveTokens(['how', 'much']) ||
337 $this->hasConsecutiveTokens(['wie', 'viele'])
339 return ['yes', 'no', 'kappa', 'lol', 'wtf', 'number'];
341 return ['yes', 'no', 'kappa', 'lol', 'wtf'];
343 return ['kappa', 'lol', 'rage'];
345 return ['kappa', 'lol', 'rage'];
356 private $emotes = [];
357 private $original_emotes = [];
358 private $emoteless = '';
359 private $emoteless_raw = '';
360 private $emoteless_tokens = [];
362 private $classification = null;