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;
12 class ChatLog extends Model {
16 public function channel() {
17 return $this->belongsTo(Channel::class);
20 public function user() {
21 return $this->belongsTo(User::class);
24 public function tokenize() {
25 return TokenizedMessage::fromLog($this);
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 $text = mb_substr($text, 0, $coords[0]).str_repeat(' ', $coords[1] - $coords[0] + 1).mb_substr($text, $coords[1] + 1);
41 return trim(preg_replace('/\s+/', ' ', $text));
44 public function evaluate() {
45 $this->evaluateUser();
46 $this->evaluateChannel();
48 if (is_null($this->nick)) {
49 $this->type = 'system';
52 if (in_array($this->nick, ['horstiebot', 'localhorsttv'])) {
57 if ($this->command == 'PRIVMSG') {
58 if (static::isKnownBot($this->nick)) {
60 } else if (substr($this->params[0], 0, 1) == '#') {
65 $this->text_content = $this->params[1];
66 $this->detectLanguage();
67 $tokenized = $this->tokenize();
68 if ($tokenized->isSpammy()) {
71 $this->classification = $tokenized->classify();
75 throw new \Exception('unidentified message');
78 public static function isKnownBot($nick) {
79 return in_array(strtolower($nick), [
85 'pokemoncommunitygame',
95 protected function evaluateUser() {
98 protected function evaluateChannel() {
99 if (empty($this->params)) {
102 $cname = $this->params[0];
103 if (substr($cname, 0, 1) != '#') {
106 $channel = Channel::firstWhere('twitch_chat', '=', $cname);
107 if (!is_null($channel)) {
108 $this->channel()->associate($channel);
109 if (empty($this->twitch_category) && now()->sub(15, 'minute')->isBefore($this->created_at)) {
110 $this->twitch_category = $channel->twitch_category;
115 protected function detectLanguage() {
116 $languages = ['de', 'en', 'es', 'fr'];
117 if (!is_null($this->channel)) {
118 $languages = array_values($this->channel->languages);
119 if (!in_array('en', $languages)) {
123 $detector = (new Language($languages))->detect($this->getTextWithoutEmotes());
124 $scores = $detector->close();
125 $lang = strval($detector);
126 //var_dump($scores, $lang, $this->text_content);
127 if (!empty($lang) && $scores[$lang] > 0.4) {
128 $this->detected_language = $lang;
133 'banned' => 'boolean',
136 'user_id' => 'string',
139 protected $fillable = [