public static function searchNGram($cuv) { $leng = mb_strlen($cuv); $hash = NGram::searchLexemIds($cuv); if (empty($hash)) { return array(); } arsort($hash); $max = current($hash); $lexIds = array_keys($hash, $max); $results = array(); foreach ($lexIds as $id) { $lexem = Model::factory('Lexem')->where('id', $id)->where_gte('charLength', $leng - self::$LENGTH_DIF)->where_lte('charLength', $leng + self::$LENGTH_DIF)->find_one(); if ($lexem) { $results[] = $lexem; if (count($results) == self::$MAX_RESULTS) { break; } } } // Sort the lexems by their Levenshtein distance from $cuv $distances = array(); foreach ($results as $lexem) { $distances[] = Levenshtein::dist($cuv, $lexem->formNoAccent); } array_multisort($distances, $results); return $results; }
$log->save(); } $count = Model::factory('DefinitionSimple')->count(); $chosenDef = rand(0, $count - 1); $answer = rand(1, 4); $maindef = Model::factory('DefinitionSimple')->limit(1)->offset($chosenDef)->find_one(); $word = getWordForDefitionId($maindef->definitionId); $options = array(); $options[$answer] = array(); $options[$answer]['term'] = getWordForDefitionId($maindef->definitionId); $options[$answer]['text'] = $maindef->getDisplayValue(); $used[$maindef->definitionId] = 1; $closestLexemsDefinitionsCount = null; $closestLexemsDefinitions = null; if ($difficulty > 1) { $nearLexemIds = NGram::searchLexemIds($word); arsort($nearLexemIds); $lexemPoolSize = 48 / $difficulty; $closestLexemIds = array_slice($nearLexemIds, 0, $lexemPoolSize, true); $closestLexemIds = array_keys($closestLexemIds); $closestLexemsDefinitions = getSimpleDefinitionsForLexemIds($closestLexemIds); $closestLexemsDefinitionsCount = count($closestLexemsDefinitions); //if there are no close lexem definitions to choose from //then use easier difficulty if ($closestLexemsDefinitionsCount == 0) { $difficulty = 1; } } for ($i = 1; $i <= 4; $i++) { $def = null; if ($i != $answer) {