use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
+use LanguageDetector\LanguageDetector;
class ChatLog extends Model {
$this->type = 'dm';
}
$this->text_content = $this->params[1];
+ $this->detectLanguage();
if ($this->scanForSpam()) {
$this->banned = true;
}
$this->channel()->associate($channel);
}
+ protected function detectLanguage() {
+ $languages = ['de', 'en', 'es', 'fr'];
+ if (!is_null($this->channel)) {
+ $languages = array_values($this->channel->languages);
+ if (!in_array('en', $languages)) {
+ $languages[] = 'en';
+ }
+ }
+ $detector = LanguageDetector::detect($this->text_content, $languages);
+ $scores = $detector->getScores();
+ $lang = strval($detector->getLanguage());
+ //var_dump($scores, $lang, $this->text_content);
+ if (is_array($scores) && isset($scores[$lang]) && $scores[$lang] > 0.35) {
+ $this->detected_language = $lang;
+ }
+ }
+
protected function scanForSpam() {
if (substr($this->text_content, 0, 1) == '!') {
return true;
$line = ChatLog::where('type', '=', 'chat')
->where('banned', '=', false)
->where('created_at', '<', now()->sub(1, 'day'))
+ ->where(function ($query) use ($channel) {
+ $query->whereNull('detected_language');
+ $query->orWhereIn('detected_language', $channel->languages);
+ })
->inRandomOrder()
->first();
return $line->text_content;
}
private function randomWaitTime(Channel $channel) {
- return random_int(1, 1800);
+ return random_int(1, 900);
}
private function tagChannelRead(Channel $channel) {
"doctrine/dbal": "^3.3",
"guzzlehttp/guzzle": "^7.2",
"jakyeru/larascord": "^3.0",
+ "landrok/language-detector": "^1.4",
"laravel/breeze": "^1.4",
"laravel/framework": "^9.2",
"laravel/sanctum": "^2.14.1",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
- "content-hash": "a370e7a3f5ba178c836caec3a7ac4879",
+ "content-hash": "ffe863b64722a40de9d8de5597d8c9b8",
"packages": [
{
"name": "beyondcode/laravel-websockets",
},
"time": "2022-06-26T11:09:59+00:00"
},
+ {
+ "name": "landrok/language-detector",
+ "version": "1.4.0",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/landrok/language-detector.git",
+ "reference": "91511a4f93700bd1c4c576b0e3b42173334a3cab"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/landrok/language-detector/zipball/91511a4f93700bd1c4c576b0e3b42173334a3cab",
+ "reference": "91511a4f93700bd1c4c576b0e3b42173334a3cab",
+ "shasum": ""
+ },
+ "require": {
+ "ext-mbstring": "*",
+ "php": ">=7.4",
+ "webmozart/assert": "^1.2"
+ },
+ "require-dev": {
+ "phpunit/phpunit": ">=6"
+ },
+ "type": "library",
+ "autoload": {
+ "psr-4": {
+ "LanguageDetector\\": "src/LanguageDetector/"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "description": "A fast and reliable PHP library for detecting languages",
+ "homepage": "https://github.com/landrok/language-detector",
+ "keywords": [
+ "detector",
+ "language",
+ "n-grams"
+ ],
+ "support": {
+ "issues": "https://github.com/landrok/language-detector/issues",
+ "source": "https://github.com/landrok/language-detector/tree/1.4.0"
+ },
+ "time": "2023-12-18T21:52:42+00:00"
+ },
{
"name": "laravel/breeze",
"version": "v1.19.1",
"php": "^8.0.2"
},
"platform-dev": [],
- "plugin-api-version": "2.0.0"
+ "plugin-api-version": "2.3.0"
}
--- /dev/null
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+ /**
+ * Run the migrations.
+ *
+ * @return void
+ */
+ public function up()
+ {
+ Schema::table('chat_logs', function (Blueprint $table) {
+ $table->string('detected_language')->nullable()->default(null);
+ $table->index(['detected_language']);
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::table('chat_logs', function (Blueprint $table) {
+ $table->dropColumn('detected_language');
+ });
+ }
+};