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 for ($i = intval($coords[0]); $i <= intval($coords[1]); ++$i) {
43 return trim(preg_replace('/\s+/', ' ', $text));
46 public function evaluate() {
47 $this->evaluateUser();
48 $this->evaluateChannel();
50 if (is_null($this->nick)) {
51 $this->type = 'system';
54 if (in_array($this->nick, ['horstiebot', 'localhorsttv'])) {
59 if ($this->command == 'PRIVMSG') {
60 if (static::isKnownBot($this->nick)) {
62 } else if (substr($this->params[0], 0, 1) == '#') {
67 $this->text_content = $this->params[1];
68 $this->detectLanguage();
69 $tokenized = $this->tokenize();
70 if ($tokenized->isSpammy()) {
73 $this->classification = $tokenized->classify();
77 throw new \Exception('unidentified message');
80 public static function isKnownBot($nick) {
81 return in_array(strtolower($nick), [
87 'pokemoncommunitygame',
97 protected function evaluateUser() {
100 protected function evaluateChannel() {
101 if (empty($this->params)) {
104 $cname = $this->params[0];
105 if (substr($cname, 0, 1) != '#') {
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;
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)) {
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;
135 'banned' => 'boolean',
138 'user_id' => 'string',
141 protected $fillable = [