]> git.localhorst.tv Git - alttp.git/blob - app/Models/ChatLog.php
more classification
[alttp.git] / app / Models / ChatLog.php
1 <?php
2
3 namespace App\Models;
4
5 use App\TwitchBot\TokenizedMessage;
6 use Illuminate\Database\Eloquent\Factories\HasFactory;
7 use Illuminate\Database\Eloquent\Model;
8 use Illuminate\Support\Arr;
9 use Illuminate\Support\Str;
10 use LanguageDetection\Language;
11
12 class ChatLog extends Model {
13
14         use HasFactory;
15
16         public function channel() {
17                 return $this->belongsTo(Channel::class);
18         }
19
20         public function user() {
21                 return $this->belongsTo(User::class);
22         }
23
24         public function tokenize() {
25                 return TokenizedMessage::fromLog($this);
26         }
27
28         public function getTextWithoutEmotes() {
29                 $text = $this->text_content;
30                 if (isset($this->tags['emotes']) && !empty($this->tags['emotes'])) {
31                         $emotes = explode('/', $this->tags['emotes']);
32                         foreach ($emotes as $emote) {
33                                 $set = explode(':', $emote);
34                                 $positions = explode(',', $set[1]);
35                                 foreach ($positions as $position) {
36                                         $coords = explode('-', $position);
37                                         for ($i = intval($coords[0]); $i <= intval($coords[1]); ++$i) {
38                                                 $text[$i] = ' ';
39                                         }
40                                 }
41                         }
42                 }
43                 return trim(preg_replace('/\s+/', ' ', $text));
44         }
45
46         public function evaluate() {
47                 $this->evaluateUser();
48                 $this->evaluateChannel();
49
50                 if (is_null($this->nick)) {
51                         $this->type = 'system';
52                         return;
53                 }
54                 if (in_array($this->nick, ['horstiebot', 'localhorsttv'])) {
55                         $this->type = 'self';
56                         return;
57                 }
58
59                 if ($this->command == 'PRIVMSG') {
60                         if (static::isKnownBot($this->nick)) {
61                                 $this->type = 'bot';
62                         } else if (substr($this->params[0], 0, 1) == '#') {
63                                 $this->type = 'chat';
64                         } else {
65                                 $this->type = 'dm';
66                         }
67                         $this->text_content = $this->params[1];
68                         $this->detectLanguage();
69                         $tokenized = $this->tokenize();
70                         if ($tokenized->isSpammy()) {
71                                 $this->banned = true;
72                         }
73                         $this->classification = $tokenized->classify();
74                         return;
75                 }
76
77                 throw new \Exception('unidentified message');
78         }
79
80         public static function isKnownBot($nick) {
81                 return in_array(strtolower($nick), [
82                         'a_n_i_v',
83                         'birrellthesquirrel',
84                         'funtoon',
85                         'nidbot2000',
86                         'nightbot',
87                         'pokemoncommunitygame',
88                         'sery_bot',
89                         'speedgaming',
90                         'starbase47',
91                         'streamelements',
92                         'wizebot',
93                         'zockerstuebchen',
94                 ]);
95         }
96
97         protected function evaluateUser() {
98         }
99
100         protected function evaluateChannel() {
101                 if (empty($this->params)) {
102                         return;
103                 }
104                 $cname = $this->params[0];
105                 if (substr($cname, 0, 1) != '#') {
106                         $cname = '#'.$cname;
107                 }
108                 $channel = Channel::firstWhere('twitch_chat', '=', $cname);
109                 if (!is_null($channel)) {
110                         $this->channel()->associate($channel);
111                         if (empty($this->twitch_category) && now()->sub(15, 'minute')->isBefore($this->created_at)) {
112                                 $this->twitch_category = $channel->twitch_category;
113                         }
114                 }
115         }
116
117         protected function detectLanguage() {
118                 $languages = ['de', 'en', 'es', 'fr'];
119                 if (!is_null($this->channel)) {
120                         $languages = array_values($this->channel->languages);
121                         if (!in_array('en', $languages)) {
122                                 $languages[] = 'en';
123                         }
124                 }
125                 $detector = (new Language($languages))->detect($this->getTextWithoutEmotes());
126                 $scores = $detector->close();
127                 $lang = strval($detector);
128                 //var_dump($scores, $lang, $this->text_content);
129                 if (!empty($lang) && $scores[$lang] > 0.4) {
130                         $this->detected_language = $lang;
131                 }
132         }
133
134         protected $casts = [
135                 'banned' => 'boolean',
136                 'params' => 'array',
137                 'tags' => 'array',
138                 'user_id' => 'string',
139         ];
140
141         protected $fillable = [
142                 'command',
143                 'nick',
144                 'params',
145                 'tags',
146         ];
147
148 }