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; }
<?php /** * * @authors Your Name (you@example.org) * @date 2014-09-07 11:58:28 * @version $Id$ */ require_once "./levenshtein.php"; $l = new Levenshtein(); echo "距离:" . $l->contrast('quite', 'quite');