Levenshtein距离是用于测量两个序列之间差异的字符串度量。通俗地说,两个单词之间的 Levenshtein 距离是将一个单词更改为另一个单词所需的最小单字符编辑(插入、删除、替换)次数。
这是一个简单的分析
$input = 'htc corporation';
// array of words to check against
$words = array(
'htc',
'Sprint Nextel',
'Sprint',
'banana',
'orange',
'radish',
'carrot',
'pea',
'bean'
);
foreach ( $words as $word ) {
// Check for Intercept
$ic = array_intersect(str_split($input), str_split($word));
printf("%s \t l= %s , s = %s , c = %d \n",$word ,
levenshtein($input, $word),
similar_text($input, $word),
count($ic));
}
输出
htc l= 12 , s = 3 , c = 5
Sprint Nextel l= 14 , s = 3 , c = 8
Sprint l= 12 , s = 1 , c = 7
banana l= 14 , s = 2 , c = 2
orange l= 12 , s = 4 , c = 7
radish l= 12 , s = 3 , c = 5
carrot l= 11 , s = 1 , c = 10
pea l= 13 , s = 2 , c = 2
bean l= 13 , s = 2 , c = 2
很明显,如果你想要 htc ,htc 有一段距离,12
而胡萝卜有一段距离,那么单独是不够的.. 你需要比较确切的单词然后设置优先级11
Levenshtein