]> git.localhorst.tv Git - alttp.git/blobdiff - app/Models/ChatLib.php
cache chatlib databases
[alttp.git] / app / Models / ChatLib.php
index 417b18f3dd6b01f1cbeb5b923d71148ebb995cd3..cf5d7dd6c825f6d8f37451dbfe7136bd0c91b787 100644 (file)
@@ -2,6 +2,8 @@
 
 namespace App\Models;
 
+use Illuminate\Support\Facades\Storage;
+
 class ChatLib {
 
        public function addMessage($msg) {
@@ -29,7 +31,6 @@ class ChatLib {
                                unset($this->transitions[$key]);
                        }
                }
-               echo 'size: ', number_format(strlen(json_encode($this->transitions)), 0), PHP_EOL;
        }
 
        public function generate($limit = 100) {
@@ -44,6 +45,20 @@ class ChatLib {
                return $generated;
        }
 
+       public function saveAs($name) {
+               $data = [
+                       'size' => $this->size,
+                       'transitions' => $this->transitions,
+               ];
+               Storage::disk('chatlib')->put($name.'.json', json_encode($data));
+       }
+
+       public function loadFrom($name) {
+               $data = json_decode(Storage::disk('chatlib')->get($name.'.json'), true);
+               $this->size = $data['size'];
+               $this->transitions = $data['transitions'];
+       }
+
        private function index($arr) {
                $result = [];
                $sum = 0;
@@ -132,7 +147,11 @@ class ChatLib {
        }
 
        private function tokenize($str) {
-               return array_values(array_filter(preg_split('/\b/u', $str)));
+               return array_values(array_filter(preg_split('/\b/u', $str), function($token) {
+                       if (empty($token)) return false;
+                       if (preg_match('/cheer\d+/u', strtolower($token))) return false;
+                       return true;
+               }));
        }
 
        private function generalize($tokens) {