]> git.localhorst.tv Git - alttp.git/blobdiff - app/Models/ChatLib.php
fix number and space normalization
[alttp.git] / app / Models / ChatLib.php
index 417b18f3dd6b01f1cbeb5b923d71148ebb995cd3..f4ab93f4798b28ebc87383b94f68e75728bbc7c9 100644 (file)
@@ -2,21 +2,27 @@
 
 namespace App\Models;
 
+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 < 5) break;
                                }
                        }
                }
@@ -29,21 +35,34 @@ class ChatLib {
                                unset($this->transitions[$key]);
                        }
                }
-               echo 'size: ', number_format(strlen(json_encode($this->transitions)), 0), PHP_EOL;
        }
 
        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;
                }
                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;
@@ -56,11 +75,13 @@ class ChatLib {
                        if (is_array(end($entry['examples']))) {
                                // already processed
                                $examples = $entry['examples'];
+                       } else if ($key === ' ') {
+                               $examples = [[' ', 0, 1]];
                        } else {
                                $subsum = 0;
                                foreach ($entry['examples'] as $example => $subweight) {
                                        $sublower = $subsum;
-                                       $subsum += $subweight * $subweight;
+                                       $subsum += $subweight;
                                        $examples[] = [$example, $sublower, $subsum];
                                }
                        }
@@ -71,8 +92,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)) {
@@ -132,15 +153,35 @@ 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 ($token === '') return false;
+                       if (preg_match('/cheer\d+/u', strtolower($token))) return false;
+                       return true;
+               }));
        }
 
        private function generalize($tokens) {
                $str = '';
                foreach ($tokens as $token) {
-                       $replaced = preg_replace('/\d+/', '0', $token);
+                       $replaced = preg_replace('/\d+/u', '0', $token);
+                       $replaced = preg_replace('/\s+/u', ' ', $replaced);
+                       $replaced = preg_replace('/(.)\1{2,}/u', '$1$1', $replaced);
                        $replaced = strtolower($replaced);
-                       $str .= empty($replaced) ? $token : $replaced;
+                       foreach ($this->aliases as $canonical => $variants) {
+                               if (in_array($replaced, $variants)) {
+                                       $replaced = $canonical;
+                                       break;
+                               }
+                               if ($replaced === $canonical) {
+                                       break;
+                               }
+                       }
+                       $str .= $replaced;
+               }
+               foreach ($this->categories as $category => $patterns) {
+                       foreach ($patterns as $pattern) {
+                               $str = preg_replace('/\b'.$pattern.'\b/u', '%'.strtoupper($category).'%', $str);
+                       }
                }
                return $str;
        }
@@ -150,7 +191,277 @@ class ChatLib {
                return $example[0];
        }
 
-       private $size = 5;
+       private $size = 7;
        private $transitions = [];
 
+       private $aliases = [
+               'chest' => ['kiste'],
+               'einen' => ['n', 'nen'],
+               'musik' => ['mukke'],
+               'schade' => ['schad'],
+       ];
+
+       private $categories = [
+               'fail' => [
+                       'failfish',
+                       'holysm0notlikethis',
+                       'notlikethis',
+                       'tetobridge0',
+                       'vinter0clown',
+               ],
+
+               'hype' => [
+                       'dergoaparty',
+                       'dinodance',
+                       'elemen0party',
+                       'muftaahype',
+                       'luckwuhype',
+                       'olliwahype',
+                       'osora0umbrihype',
+                       'partyhat',
+                       'peepocheer',
+                       'rei0hype',
+                       'sakayahype',
+                       'tetotroete',
+                       'ticknaboargeil0',
+                       'ticknahype0',
+               ],
+
+               'kappa' => [
+                       'kappa(claus|hd)?',
+               ],
+
+               'jam' => [
+                       '(cat|dog|rat)jam',
+                       'kanash0jam',
+                       'rei0jamers',
+                       'samusdance',
+               ],
+
+               'lol' => [
+                       ':d',
+                       'boothi0lul',
+                       'kekw',
+                       'lol',
+                       'lul',
+                       'rei0lul',
+                       'samusgrin',
+                       'ticknaauslachen',
+                       'xd',
+               ],
+
+               'love' => [
+                       '<3',
+                       'duden0love',
+                       'exec0love',
+                       'krawal0heart',
+                       'lodanzhug',
+                       'luckwulove',
+                       'luvsign',
+                       'muftaal',
+                       'osora0love',
+                       'peepoexcitedhug',
+                       'spirit0love',
+                       'svenkalove',
+                       'ticknaherz',
+               ],
+
+               'name' => [
+                       'baba',
+                       'baka',
+                       'bobe?r',
+                       'brog(i|or)',
+                       'cfate',
+                       'danny',
+                       'danzi+',
+                       'daruck',
+                       'dennsen',
+                       'dimez',
+                       'divi',
+                       'dud(en|i+)',
+                       'ele',
+                       'eri(ror)?',
+                       '(name)?faker',
+                       'fetti+',
+                       'gamma(chuu)?',
+                       'goat(buster|ie?|y)?',
+                       'hitsu(yan)?',
+                       'holy',
+                       'jem',
+                       'kala(marino)?',
+                       'kromb',
+                       'koval',
+                       'kum(i|o|p)',
+                       'lanux',
+                       'len(esha|chen)',
+                       'leya+',
+                       'magno',
+                       'malmo',
+                       'markam',
+                       'micha',
+                       'mimsy',
+                       'muf(fy|taay)',
+                       'murd(elizer|i+)',
+                       'nami',
+                       'nula',
+                       'onio',
+                       'paulinche',
+                       'phaaze',
+                       'ralen',
+                       'ramond',
+                       'ray(vis)?',
+                       'schulzer',
+                       'skunk(ner)?',
+                       'skipsy',
+                       'soli+',
+                       'sven(ka+)?',
+                       'tantalus',
+                       'teto',
+                       'thalanee?',
+                       'tick(i+|naldo|y+)',
+                       'tofu',
+                       'tr[i0]x+',
+                       'vin(nie?|ny|ter)',
+                       'xall',
+                       'yasi',
+               ],
+
+               'pog' => [
+                       'bumble0Pog',
+                       'komodohype',
+                       'pog',
+                       'pogchamp',
+                       'poggers',
+                       'satono0pog',
+               ],
+
+               'run' => [
+                       'dennsenboots',
+                       'lodanzrun',
+                       'ticknaldosprint',
+                       'vinter0run',
+               ],
+
+               'wave' => [
+                       'dennsenhi',
+                       'dergoawave',
+                       'heyguys',
+                       'holysm0heyguys',
+                       'muftaahey',
+                       'rei0wave',
+                       'sayuri0wave',
+                       'shindi0wave',
+                       'svenkawave',
+                       'wuschlwave',
+               ],
+
+               'zelda_boss' => [
+                       'aga(hnim)?',
+                       'armos( knights)?',
+                       'arrghus',
+                       'blind',
+                       'ganon(dorf)?',
+                       'helma',
+                       'kholdstare',
+                       'lanmo(las)?',
+                       'moldorm',
+                       'mothula',
+                       'mott[ei]',
+                       'trinexx',
+                       'vit(reous|ty)',
+               ],
+
+               'zelda_dungeon' => [
+                       'eastern',
+                       'desert( palace)?',
+                       'gt',
+                       'hera',
+                       'ice ?(palace)?',
+                       '(misery )?mire',
+                       'pod',
+                       'skull ?woods',
+                       'swamp',
+                       'thieve\'?s\'? ?town',
+                       'tr',
+                       'tt',
+               ],
+
+               'zelda_item' => [
+                       '(big|small|retro|generic) ?keys?',
+                       'b[oö]gen',
+                       'bombos',
+                       'boots',
+                       'bottle',
+                       'bows?',
+                       'bugnet',
+                       'byrna',
+                       'cape',
+                       'ether',
+                       'flasche',
+                       'flippers',
+                       'fl[uö]te',
+                       'frod',
+                       '(gloves?|mitts|handschuhe?)',
+                       '(half|quarter) ?magic',
+                       'hammer',
+                       'hookshot',
+                       '(ice|fire) ?rod',
+                       'lampe?',
+                       'laser ?bridge',
+                       'mearl',
+                       'mirror',
+                       'moon ?pearl',
+                       'mushroom',
+                       'ocarina',
+                       'pilz',
+                       'powder',
+                       'puder',
+                       'quake',
+                       '(red|blue) ?cane',
+                       '(red|green|blue) ?(goo|potion)',
+                       '(red|green|blue|baby) ?mail',
+                       '(red|blue|bu|boo|good|bad|both)merang',
+                       'schaufel',
+                       '(gro(ss|ß)er? |kleiner? )?schlüssel',
+                       'schwert',
+                       'shovel',
+                       'silvers',
+                       'somaria',
+                       'spiegel',
+                       'sword',
+               ],
+
+               'zelda_location' => [
+                       'big chest',
+                       'bumper( cave)?( ledge)?',
+                       '(hyrule)? ?castle ?(tower)?',
+                       'catfish',
+                       'cave 0?',
+                       'chest ?game',
+                       'cutscene ?chest',
+                       'damm',
+                       'desert( ledge)?',
+                       'dig(ging)? ?game',
+                       '((back|front) of )?escape',
+                       'gyl',
+                       'hobo',
+                       'hook ?(shot) cave',
+                       'lava ?chest',
+                       '(light|dark) ?world',
+                       'lss',
+                       'magic bat',
+                       '(dark )?(death )?mountain',
+                       'ped(estal)?',
+                       'pyramid( fairy)?( ledge)?',
+                       'red bomb',
+                       'sahasrahla',
+                       'sasha',
+                       'sick kid',
+                       'stumpy',
+                       'tile ?room',
+                       'torch',
+                       'zora( ledge)?',
+               ],
+       ];
+
 }