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);
}
$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];
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) {
$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];
}
}
}
$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;
}
'chest' => ['kiste'],
'einen' => ['n', 'nen'],
'musik' => ['mukke'],
- 'schade' => ['schad'],
+ 'schade' => ['schad', 'schaade'],
];
private $categories = [
'wuschlwave',
],
- 'zelda_boss' => [
+ 'zb' => [
'aga(hnim)?',
'armos( knights)?',
'arrghus',
'vit(reous|ty)',
],
- 'zelda_dungeon' => [
+ 'zd' => [
'eastern',
'desert( palace)?',
'gt',
'tt',
],
- 'zelda_item' => [
+ 'zi' => [
'(big|small|retro|generic) ?keys?',
'b[oö]gen',
'bombos',
'sword',
],
- 'zelda_location' => [
+ 'zl' => [
'big chest',
'bumper( cave)?( ledge)?',
'(hyrule)? ?castle ?(tower)?',