From: Daniel Karbach Date: Wed, 8 May 2024 14:42:50 +0000 (+0200) Subject: refine chat gen X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=64f5be008ef28c812cb7ed87149f4eb86f6021eb;p=alttp.git refine chat gen --- diff --git a/app/Models/ChatLib.php b/app/Models/ChatLib.php index cf5d7dd..c89ef94 100644 --- a/app/Models/ChatLib.php +++ b/app/Models/ChatLib.php @@ -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; }