Есть скрипт
$questions_arr=json_decode(file_get_contents("base.json"),1);
array_change_key_case($questions_arr);
$input_text = $_GET['text'];
$input_text = mb_strtolower($input_text);
function utf8_to_extended_ascii($str, &$map)
{
$matches = array();
if (!preg_match_all('/[\xC0-\xF7][\x80-\xBF]+/', $str, $matches))
return $str;
foreach ($matches[0] as $mbc)
if (!isset($map[$mbc]))
$map[$mbc] = chr(128 + count($map));
return strtr($str, $map);
}
function levenshtein_utf8($s1, $s2)
{
$charMap = array();
$s1 = utf8_to_extended_ascii($s1, $charMap);
$s2 = utf8_to_extended_ascii($s2, $charMap);
return levenshtein($s1, $s2);
}
$shortest = -1;
foreach ($questions_arr as $q =>$reply) {
$lev = levenshtein_utf8($input_text, $q);
if ($lev == 0) {
$closest = $q;
$shortest = 0;
break;
}
if ($lev <= $shortest || $shortest < 0) {
$closest = $q;
$shortest = $lev;
}
}
$answer = $questions_arr[$closest][rand(0, count($questions_arr[$input_text])-1)];
$sys = array(
'Creator name' => '%creator_name%',
);
$answer = str_ireplace("%sys%", json_encode($sys), $answer);
if($answer == ""){
$answer = "Шо?";
}
echo $answer;
База 6 мб, все работает очень долго, как ускорить работу ?