]> git.localhorst.tv Git - alttp.git/blobdiff - app/Models/ChatLib.php
separate chatlib database generation
[alttp.git] / app / Models / ChatLib.php
index ea16a2bd740efb4916a54881bceda3ff3b388f35..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,16 +153,16 @@ 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];
                        }
                }
        }
@@ -164,8 +179,8 @@ class ChatLib {
                $str = '';
                foreach ($tokens as $token) {
                        $replaced = preg_replace('/\d+/u', '0', $token);
-                       $replaced = preg_replace('/\s+/u', ' ', $token);
-                       $replaced = preg_replace('/(.)\1{2,}/u', '$1$1', $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)) {
@@ -179,9 +194,7 @@ class ChatLib {
                        $str .= $replaced;
                }
                foreach ($this->categories as $category => $patterns) {
-                       foreach ($patterns as $pattern) {
-                               $str = preg_replace('/\b'.$pattern.'\b/u', '%'.strtoupper($category).'%', $str);
-                       }
+                       $str = preg_replace($patterns, $category, $str);
                }
                return $str;
        }
@@ -198,7 +211,7 @@ class ChatLib {
                'chest' => ['kiste'],
                'einen' => ['n', 'nen'],
                'musik' => ['mukke'],
-               'schade' => ['schad'],
+               'schade' => ['schad', 'schaade'],
        ];
 
        private $categories = [
@@ -355,7 +368,7 @@ class ChatLib {
                        'wuschlwave',
                ],
 
-               'zelda_boss' => [
+               'zb' => [
                        'aga(hnim)?',
                        'armos( knights)?',
                        'arrghus',
@@ -371,7 +384,7 @@ class ChatLib {
                        'vit(reous|ty)',
                ],
 
-               'zelda_dungeon' => [
+               'zd' => [
                        'eastern',
                        'desert( palace)?',
                        'gt',
@@ -386,7 +399,7 @@ class ChatLib {
                        'tt',
                ],
 
-               'zelda_item' => [
+               'zi' => [
                        '(big|small|retro|generic) ?keys?',
                        'b[oö]gen',
                        'bombos',
@@ -431,7 +444,7 @@ class ChatLib {
                        'sword',
                ],
 
-               'zelda_location' => [
+               'zl' => [
                        'big chest',
                        'bumper( cave)?( ledge)?',
                        '(hyrule)? ?castle ?(tower)?',