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;
11 class ChatLog extends Model {
15 public function channel() {
16 return $this->belongsTo(Channel::class);
19 public function user() {
20 return $this->belongsTo(User::class);
23 public function evaluate() {
24 $this->evaluateUser();
25 $this->evaluateChannel();
27 if (is_null($this->nick)) {
28 $this->type = 'system';
31 if (in_array($this->nick, ['horstiebot', 'localhorsttv'])) {
36 if ($this->command == 'PRIVMSG') {
37 if ($this->isKnownBot()) {
39 } else if (substr($this->params[0], 0, 1) == '#') {
44 $this->text_content = $this->params[1];
45 $this->detectLanguage();
46 if ($this->scanForSpam()) {
49 $this->classification = static::classify($this->text_content);
53 throw new \Exception('unidentified message');
56 public function isKnownBot() {
57 return in_array(strtolower($this->nick), [
62 'pokemoncommunitygame',
71 public static function classify($text) {
73 return 'unclassified';
75 if (is_numeric(trim($text))) {
78 $rawText = strtolower(preg_replace('/[^\w]/', '', $text));
79 $tokenizedText = preg_split('/\s+/', strtolower(trim($text)));
80 if (Str::startsWith($rawText, 'gg') || Str::endsWith($rawText, 'gg')) {
83 if (Str::contains($rawText, ['glgl', 'glhf', 'hfgl'])) {
86 if (Str::contains($rawText, ['haha', 'hehe', 'hihi', 'kekw', 'lol', 'lul', 'xd'])) {
89 if (Str::startsWith($rawText, ['ahoi', 'hallo', 'hello', 'hi', 'huhu']) || Str::endsWith($rawText, ['hi', 'wave'])) {
92 if (Str::contains($rawText, ['pog', 'wow'])) {
95 if (Str::contains($rawText, ['hype'])) {
98 if (Str::startsWith($rawText, 'o7') || Str::endsWith($rawText, 'o7') || Str::contains($rawText, 'salut')) {
101 return 'unclassified';
104 protected function evaluateUser() {
107 protected function evaluateChannel() {
108 if (empty($this->params)) {
109 $this->channel()->associate(null);
112 $cname = $this->params[0];
113 if (substr($cname, 0, 1) != '#') {
116 $channel = Channel::firstWhere('twitch_chat', '=', $cname);
117 $this->channel()->associate($channel);
120 protected function detectLanguage() {
121 $languages = ['de', 'en', 'es', 'fr'];
122 if (!is_null($this->channel)) {
123 $languages = array_values($this->channel->languages);
124 if (!in_array('en', $languages)) {
128 $detector = LanguageDetector::detect($this->text_content, $languages);
129 $scores = $detector->getScores();
130 $lang = strval($detector->getLanguage());
131 //var_dump($scores, $lang, $this->text_content);
132 if (is_array($scores) && isset($scores[$lang]) && $scores[$lang] > 0.35) {
133 $this->detected_language = $lang;
137 protected function scanForSpam() {
138 if (substr($this->text_content, 0, 1) == '!') {
141 if (strpos($this->text_content, '$') !== false) {
144 if (strpos($this->text_content, '€') !== false) {
147 if (strpos($this->text_content, '@') !== false) {
150 if (strpos($this->text_content, '://') !== false) {
153 if (is_numeric($this->text_content)) {
156 if (strpos($this->text_content, 'followers') !== false) {
159 if (strpos($this->text_content, 'promotion') !== false) {
162 if (strpos($this->text_content, 'viewers') !== false) {
165 if (strpos($this->text_content, 'view ers') !== false) {
172 'banned' => 'boolean',
175 'user_id' => 'string',
178 protected $fillable = [