From: Daniel Karbach Date: Fri, 10 May 2024 16:34:34 +0000 (+0200) Subject: universal examples X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=1a863c3ecfa04eacf7df3c5bce71c12244e4625b;p=alttp.git universal examples --- diff --git a/app/Models/ChatLib.php b/app/Models/ChatLib.php index a87c6a7..c7a19b6 100644 --- a/app/Models/ChatLib.php +++ b/app/Models/ChatLib.php @@ -6,7 +6,7 @@ use Illuminate\Support\Facades\Storage; class ChatLib { - public function __construct($size = 7) { + public function __construct($size = 6) { $this->size = $size; $converted = []; @@ -39,16 +39,27 @@ class ChatLib { if ($end - $i < 5) break; } } + $this->addExample(array_slice($tokens, 0, $num), $token); } } public function compile() { - foreach ($this->transitions as $key => $value) { - $this->transitions[$key] = $this->index($this->transitions[$key]); + foreach ($this->transitions as $key => $values) { + $this->transitions[$key] = $this->index($values, 2); if (empty($this->transitions[$key])) { unset($this->transitions[$key]); } } + foreach ($this->examples as $key => $values) { + if (in_array($key, ['', ' '])) { + unset($this->examples[$key]); + continue; + } + $this->examples[$key] = $this->index($values, 1); + if (empty($this->examples[$key]) || (count($this->examples[$key]) === 1 && $this->examples[$key][0][0] === $key)) { + unset($this->examples[$key]); + } + } } public function generate($limit = 100) { @@ -67,6 +78,7 @@ class ChatLib { $data = [ 'size' => $this->size, 'transitions' => $this->transitions, + 'examples' => $this->examples, ]; Storage::disk('chatlib')->put($name.'.json', json_encode($data)); } @@ -75,28 +87,17 @@ class ChatLib { $data = json_decode(Storage::disk('chatlib')->get($name.'.json'), true); $this->size = $data['size']; $this->transitions = $data['transitions']; + $this->examples = $data['examples']; } - private function index($arr) { + private function index($arr, $min_weight = 2) { $result = []; $sum = 0; - foreach ($arr as $key => $entry) { - $weight = $entry[0]; - if ($weight == 1) continue; + foreach ($arr as $key => $weight) { + if ($weight < $min_weight) continue; $lower = $sum; $sum += $weight; - $examples = []; - if ($key === ' ') { - $examples = [[' ', 0, 1]]; - } else { - $subsum = 0; - foreach ($entry[1] as $example => $subweight) { - $sublower = $subsum; - $subsum += $subweight; - $examples[] = [$example, $sublower, $subsum]; - } - } - $result[] = [$key, $lower, $sum, $examples]; + $result[] = [$key, $lower, $sum]; } return $result; } @@ -108,7 +109,7 @@ class ChatLib { if (isset($this->transitions[$cmb])) { $pick = $this->pick($this->transitions[$cmb]); if (!is_null($pick)) { - return $this->exampleOf($pick); + return $this->exampleOf($pick, $tokens); } } } @@ -142,28 +143,27 @@ class ChatLib { } private function addTransition($state, $next) { - $cmb = $this->generalize($state); - if (!isset($this->transitions[$cmb])) { - $this->transitions[$cmb] = []; + $ctx = $this->generalize($state); + $cmb = $this->generalize([$next]); + if (!isset($this->transitions[$ctx])) { + $this->transitions[$ctx] = []; + } + if (!isset($this->transitions[$ctx][$cmb])) { + $this->transitions[$ctx][$cmb] = 1; + } else { + ++$this->transitions[$ctx][$cmb]; } - $this->increment($this->transitions[$cmb], $next); } - private function increment(&$which, $token) { - $generalized = $this->generalize([$token]); - if (!isset($which[$generalized])) { - $which[$generalized] = [ - 1, - [], - ]; - $which[$generalized][1][$token] = 1; + private function addExample($context, $token) { + $cmb = $this->generalize([$token]); + if (!isset($this->examples[$cmb])) { + $this->examples[$cmb] = []; + } + if (!isset($this->examples[$cmb][$token])) { + $this->examples[$cmb][$token] = 1; } else { - ++$which[$generalized][0]; - if (!isset($which[$generalized][1][$token])) { - $which[$generalized][1][$token] = 1; - } else { - ++$which[$generalized][1][$token]; - } + ++$this->examples[$cmb][$token]; } } @@ -199,13 +199,20 @@ class ChatLib { return $str; } - private function exampleOf($pick) { - $example = $this->pick($pick[3]); - return $example[0]; + private function exampleOf($pick, $context) { + if (!isset($this->examples[$pick[0]])) { + return $pick[0]; + } + if (isset($this->examples[$pick[0]])) { + $example = $this->pick($this->examples[$pick[0]]); + return $example[0]; + } + return $pick[0]; } - private $size = 7; + private $size; private $transitions = []; + private $examples = []; private $aliases = [ 'chest' => ['kiste'], @@ -358,6 +365,7 @@ class ChatLib { 'wave' => [ 'dennsenhi', 'dergoawave', + 'falcnwavehi', 'heyguys', 'holysm0heyguys', 'muftaahey',