]> git.localhorst.tv Git - alttp.git/commitdiff
refine chat gen
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 8 May 2024 14:42:50 +0000 (16:42 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 8 May 2024 14:42:50 +0000 (16:42 +0200)
app/Models/ChatLib.php

index cf5d7dd6c825f6d8f37451dbfe7136bd0c91b787..c89ef941be5f4c2802eb89c92cc2be6dd7bcfd9d 100644 (file)
@@ -6,19 +6,23 @@ use Illuminate\Support\Facades\Storage;
 
 class ChatLib {
 
-       public function addMessage($msg) {
-               $tokens = $this->tokenize($msg->text_content);
+       public function addMessage(ChatLog $msg) {
+               $this->addText($msg->text_content);
+       }
+
+       public function addText($text) {
+               $tokens = $this->tokenize($text);
                if (empty($tokens)) return;
                $tokens[] = '';
                foreach ($tokens as $num => $token) {
                        if ($num === 0) {
                                $this->addTransition([], $token);
                        } else {
-                               $start = max(0, $num - $this->size);
+                               $start = max(0, $num - $this->size - 1);
                                $end = $num;
                                for ($i = $start; $i < $end; ++$i) {
                                        $this->addTransition(array_slice($tokens, $i, $end - $i), $token);
-                                       if ($end - $i < 3) break;
+                                       if ($end - $i < 4) break;
                                }
                        }
                }
@@ -34,11 +38,11 @@ class ChatLib {
        }
 
        public function generate($limit = 100) {
-               $tokens = [];
+               $tokens = [''];
                $generated = '';
                while (strlen($generated) < $limit) {
                        $next = $this->randomNext($tokens);
-                       if (empty($next)) break;
+                       if ($next === '') break;
                        $tokens[] = $next;
                        $generated .= $next;
                }
@@ -75,7 +79,7 @@ class ChatLib {
                                $subsum = 0;
                                foreach ($entry['examples'] as $example => $subweight) {
                                        $sublower = $subsum;
-                                       $subsum += $subweight * $subweight;
+                                       $subsum += $subweight;
                                        $examples[] = [$example, $sublower, $subsum];
                                }
                        }
@@ -86,8 +90,8 @@ class ChatLib {
 
        private function randomNext($tokens) {
                $cnt = count($tokens);
-               for ($size = min($this->size, $cnt); $size >= 0; --$size) {
-                       $cmb = $this->generalize(array_slice($tokens, $cnt - $size, $size));
+               for ($size = min($this->size, $cnt); $size > 0; --$size) {
+                       $cmb = $this->generalize(array_slice($tokens, -$size));
                        if (isset($this->transitions[$cmb])) {
                                $pick = $this->pick($this->transitions[$cmb]);
                                if (!is_null($pick)) {
@@ -159,7 +163,7 @@ class ChatLib {
                foreach ($tokens as $token) {
                        $replaced = preg_replace('/\d+/', '0', $token);
                        $replaced = strtolower($replaced);
-                       $str .= empty($replaced) ? $token : $replaced;
+                       $str .= $replaced;
                }
                return $str;
        }