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 = []) {
14 $this->raw = strtolower(preg_replace('/[^\w]/', '', $text));
15 $this->tokens = preg_split('/\s+/', strtolower(trim($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 $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] = ' ';
31 $this->emoteless = trim(preg_replace('/\s+/', ' ', $this->emoteless));
33 $this->emoteless_tokens = preg_split('/\s+/', strtolower($this->emoteless));
36 public static function fromIRC(IRCMessage $msg) {
37 return new self($msg->getText(), $msg->tags);
40 public static function fromLog(ChatLog $log) {
41 return new self($log->params[1], $log->tags);
44 public static function fromString($text, $tags = []) {
45 return new self($text, $tags);
49 public function getNumericValue() {
50 return intval($this->raw);
53 public function isSpammy() {
54 if (substr($this->raw, 0, 1) == '!') {
57 if (strpos($this->raw, '$') !== false) {
60 if (strpos($this->raw, '€') !== false) {
63 if (strpos($this->raw, '@') !== false) {
66 if (strpos($this->raw, '://') !== false) {
69 if (strpos($this->raw, 'followers') !== false) {
72 if (strpos($this->raw, 'horstie') !== false) {
75 if (strpos($this->raw, 'promotion') !== false) {
78 if (strpos($this->raw, 'viewers') !== false) {
81 if (strpos($this->raw, 'view ers') !== false) {
84 if (strpos($this->raw, 'vielen dank für den raid') !== false) {
87 if (strpos($this->raw, 'willkommen auf starbase 47') !== false) {
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';
115 $this->classification = 'unclassified';
118 return $this->classification;
127 private $emotes = [];
128 private $emoteless = '';
129 private $emoteless_tokens = [];
131 private $classification = null;