]> git.localhorst.tv Git - alttp.git/blobdiff - app/Models/ChatLib.php
separate chatlib database generation
[alttp.git] / app / Models / ChatLib.php
index 3f1530b396b9b752652918c45d9f0a0fdea3d596..a87c6a7e0b04978e93b78b06ff028af782f9d052 100644 (file)
@@ -6,6 +6,20 @@ use Illuminate\Support\Facades\Storage;
 
 class ChatLib {
 
+       public function __construct($size = 7) {
+               $this->size = $size;
+
+               $converted = [];
+               foreach ($this->categories as $category => $patterns) {
+                       $converted_patterns = [];
+                       foreach ($patterns as $pattern) {
+                               $converted_patterns[] = '/\b'.$pattern.'\b/u';
+                       }
+                       $converted['%'.strtoupper($category).'%'] = $converted_patterns;
+               }
+               $this->categories = $converted;
+       }
+
        public function addMessage(ChatLog $msg) {
                $this->addText($msg->text_content);
        }
@@ -67,19 +81,16 @@ class ChatLib {
                $result = [];
                $sum = 0;
                foreach ($arr as $key => $entry) {
-                       $weight = $entry['count'];
+                       $weight = $entry[0];
                        if ($weight == 1) continue;
                        $lower = $sum;
                        $sum += $weight;
                        $examples = [];
-                       if (is_array(end($entry['examples']))) {
-                               // already processed
-                               $examples = $entry['examples'];
-                       } else if ($key === ' ') {
+                       if ($key === ' ') {
                                $examples = [[' ', 0, 1]];
                        } else {
                                $subsum = 0;
-                               foreach ($entry['examples'] as $example => $subweight) {
+                               foreach ($entry[1] as $example => $subweight) {
                                        $sublower = $subsum;
                                        $subsum += $subweight;
                                        $examples[] = [$example, $sublower, $subsum];
@@ -106,14 +117,18 @@ class ChatLib {
 
        private function pick($options) {
                if (empty($options)) return null;
-               $max = end($options)[2];
+               $max = end($options)[2] - 1;
                $num = random_int(0, $max);
+               return static::search($options, $num);
+       }
+
+       public static function search($options, $num) {
                $min_index = 0;
                $max_index = count($options) - 1;
                while ($min_index < $max_index) {
                        $cur_index = intval(($min_index + $max_index) / 2);
                        $cur_low = $options[$cur_index][1];
-                       $cur_high = $options[$cur_index][2];
+                       $cur_high = $options[$cur_index][2] - 1;
                        if ($cur_low > $num) {
                                $max_index = $cur_index;
                        } else if ($cur_high < $num) {
@@ -138,23 +153,23 @@ class ChatLib {
                $generalized = $this->generalize([$token]);
                if (!isset($which[$generalized])) {
                        $which[$generalized] = [
-                               'count' => 1,
-                               'examples' => [],
+                               1,
+                               [],
                        ];
-                       $which[$generalized]['examples'][$token] = 1;
+                       $which[$generalized][1][$token] = 1;
                } else {
-                       ++$which[$generalized]['count'];
-                       if (!isset($which[$generalized]['examples'][$token])) {
-                               $which[$generalized]['examples'][$token] = 1;
+                       ++$which[$generalized][0];
+                       if (!isset($which[$generalized][1][$token])) {
+                               $which[$generalized][1][$token] = 1;
                        } else {
-                               ++$which[$generalized]['examples'][$token];
+                               ++$which[$generalized][1][$token];
                        }
                }
        }
 
        private function tokenize($str) {
                return array_values(array_filter(preg_split('/\b/u', $str), function($token) {
-                       if (empty($token)) return false;
+                       if ($token === '') return false;
                        if (preg_match('/cheer\d+/u', strtolower($token))) return false;
                        return true;
                }));
@@ -163,12 +178,24 @@ class ChatLib {
        private function generalize($tokens) {
                $str = '';
                foreach ($tokens as $token) {
-                       $replaced = preg_replace('/\d+/', '0', $token);
-                       $replaced = preg_replace('/\s+/', ' ', $token);
-                       $replaced = preg_replace('/(.)\1{2,}/', '$1', $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);
+                       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) {
+                       $str = preg_replace($patterns, $category, $str);
+               }
                return $str;
        }
 
@@ -180,4 +207,274 @@ class ChatLib {
        private $size = 7;
        private $transitions = [];
 
+       private $aliases = [
+               'chest' => ['kiste'],
+               'einen' => ['n', 'nen'],
+               'musik' => ['mukke'],
+               'schade' => ['schad', 'schaade'],
+       ];
+
+       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',
+               ],
+
+               'zb' => [
+                       'aga(hnim)?',
+                       'armos( knights)?',
+                       'arrghus',
+                       'blind',
+                       'ganon(dorf)?',
+                       'helma',
+                       'kholdstare',
+                       'lanmo(las)?',
+                       'moldorm',
+                       'mothula',
+                       'mott[ei]',
+                       'trinexx',
+                       'vit(reous|ty)',
+               ],
+
+               'zd' => [
+                       'eastern',
+                       'desert( palace)?',
+                       'gt',
+                       'hera',
+                       'ice ?(palace)?',
+                       '(misery )?mire',
+                       'pod',
+                       'skull ?woods',
+                       'swamp',
+                       'thieve\'?s\'? ?town',
+                       'tr',
+                       'tt',
+               ],
+
+               'zi' => [
+                       '(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',
+               ],
+
+               'zl' => [
+                       '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)?',
+               ],
+       ];
+
 }