]> git.localhorst.tv Git - alttp.git/blob - app/Models/ChatLog.php
simple chat classification
[alttp.git] / app / Models / ChatLog.php
1 <?php
2
3 namespace App\Models;
4
5 use Illuminate\Database\Eloquent\Factories\HasFactory;
6 use Illuminate\Database\Eloquent\Model;
7 use Illuminate\Support\Arr;
8 use Illuminate\Support\Str;
9 use LanguageDetector\LanguageDetector;
10
11 class ChatLog extends Model {
12
13         use HasFactory;
14
15         public function channel() {
16                 return $this->belongsTo(Channel::class);
17         }
18
19         public function user() {
20                 return $this->belongsTo(User::class);
21         }
22
23         public function evaluate() {
24                 $this->evaluateUser();
25                 $this->evaluateChannel();
26
27                 if (is_null($this->nick)) {
28                         $this->type = 'system';
29                         return;
30                 }
31                 if (in_array($this->nick, ['horstiebot', 'localhorsttv'])) {
32                         $this->type = 'self';
33                         return;
34                 }
35
36                 if ($this->command == 'PRIVMSG') {
37                         if ($this->isKnownBot()) {
38                                 $this->type = 'bot';
39                         } else if (substr($this->params[0], 0, 1) == '#') {
40                                 $this->type = 'chat';
41                         } else {
42                                 $this->type = 'dm';
43                         }
44                         $this->text_content = $this->params[1];
45                         $this->detectLanguage();
46                         if ($this->scanForSpam()) {
47                                 $this->banned = true;
48                         }
49                         $this->classification = static::classify($this->text_content);
50                         return;
51                 }
52
53                 throw new \Exception('unidentified message');
54         }
55
56         public function isKnownBot() {
57                 return in_array(strtolower($this->nick), [
58                         'birrellthesquirrel',
59                         'funtoon',
60                         'nidbot2000',
61                         'nightbot',
62                         'pokemoncommunitygame',
63                         'speedgaming',
64                         'streamelements',
65                         'wizebot',
66                         'zockerstuebchen',
67                 ]);
68         }
69
70         public static function classify($text) {
71                 if (empty($text)) {
72                         return 'unclassified';
73                 }
74                 if (is_numeric(trim($text))) {
75                         return 'number';
76                 }
77                 $rawText = strtolower(preg_replace('/[^\w]/', '', $text));
78                 $tokenizedText = preg_split('/\s+/', strtolower(trim($text)));
79                 if (Str::startsWith($rawText, 'gg') || Str::endsWith($rawText, 'gg')) {
80                         return 'gg';
81                 }
82                 if (Str::contains($rawText, ['glgl', 'glhf', 'hfgl'])) {
83                         return 'gl';
84                 }
85                 if (Str::contains($rawText, ['haha', 'hehe', 'hihi', 'kekw', 'lol', 'lul', 'xd'])) {
86                         return 'lol';
87                 }
88                 if (Str::startsWith($rawText, ['ahoi', 'hallo', 'hello', 'hi', 'huhu']) || Str::endsWith($rawText, ['hi', 'wave'])) {
89                         return 'hi';
90                 }
91                 if (Str::contains($rawText, ['pog', 'wow'])) {
92                         return 'pog';
93                 }
94                 if (Str::contains($rawText, ['hype'])) {
95                         return 'hype';
96                 }
97                 return 'unclassified';
98         }
99
100         protected function evaluateUser() {
101         }
102
103         protected function evaluateChannel() {
104                 if (empty($this->params)) {
105                         $this->channel()->associate(null);
106                         return;
107                 }
108                 $cname = $this->params[0];
109                 if (substr($cname, 0, 1) != '#') {
110                         $cname = '#'.$cname;
111                 }
112                 $channel = Channel::firstWhere('twitch_chat', '=', $cname);
113                 $this->channel()->associate($channel);
114         }
115
116         protected function detectLanguage() {
117                 $languages = ['de', 'en', 'es', 'fr'];
118                 if (!is_null($this->channel)) {
119                         $languages = array_values($this->channel->languages);
120                         if (!in_array('en', $languages)) {
121                                 $languages[] = 'en';
122                         }
123                 }
124                 $detector = LanguageDetector::detect($this->text_content, $languages);
125                 $scores = $detector->getScores();
126                 $lang = strval($detector->getLanguage());
127                 //var_dump($scores, $lang, $this->text_content);
128                 if (is_array($scores) && isset($scores[$lang]) && $scores[$lang] > 0.35) {
129                         $this->detected_language = $lang;
130                 }
131         }
132
133         protected function scanForSpam() {
134                 if (substr($this->text_content, 0, 1) == '!') {
135                         return true;
136                 }
137                 if (strpos($this->text_content, '$') !== false) {
138                         return true;
139                 }
140                 if (strpos($this->text_content, '€') !== false) {
141                         return true;
142                 }
143                 if (strpos($this->text_content, '@') !== false) {
144                         return true;
145                 }
146                 if (strpos($this->text_content, '://') !== false) {
147                         return true;
148                 }
149                 if (is_numeric($this->text_content)) {
150                         return true;
151                 }
152                 if (strpos($this->text_content, 'followers') !== false) {
153                         return true;
154                 }
155                 if (strpos($this->text_content, 'promotion') !== false) {
156                         return true;
157                 }
158                 if (strpos($this->text_content, 'viewers') !== false) {
159                         return true;
160                 }
161                 if (strpos($this->text_content, 'view ers') !== false) {
162                         return true;
163                 }
164                 return false;
165         }
166
167         protected $casts = [
168                 'banned' => 'boolean',
169                 'params' => 'array',
170                 'tags' => 'array',
171                 'user_id' => 'string',
172         ];
173
174         protected $fillable = [
175                 'command',
176                 'nick',
177                 'params',
178                 'tags',
179         ];
180
181 }