首先,这不是一个特定于语言的问题,下面的例子使用PHP,但更多的是关于方法(regex?)找到答案。
假设我有一个数组:
$array = ['The Bert and Ernie game', 'The Bert & Ernie game', 'Bert and Ernie game', 'Bert and Ernie game - english version', 'Bert & Ernie (game)', 'Bert and Ernie - game'] etc...我想获取一个显示最重要组合的组合。所以我想做:
$magicPattern = [something that renders most important occurrences];
preg_match($magicPattern, $array, $matches);
print_r($matches);作为一个输出,我想得到这样的东西:“伯特和厄尼游戏”。
PS:我没有必要去寻找一个真正的数组,这样做的概念也会很棒。
更新:
下面的代码中,如果这是找到最佳版本的好方法,有什么想法吗?很难从函数的来源中计算出来。
$array['The Bert and Ernie game'] =0; //lev distance
$array['The Bert & Ernie game'] =0; //lev distance
$array['Bert and Ernie game'] =0; //lev distance
$array['Bert and Ernie game - english version'] =0; //lev distance
$array['Bert & Ernie (game)'] =0; //lev distance
$array['Bert and Ernie - game'] =0; //lev distance
foreach($array as $currentKey => $currentVal){
foreach($array as $matchKey => $matchVal){
$array[$currentKey] += levenshtein($currentKey, $matchKey);
}
}
$array = array_flip($array);
ksort($array);
echo array_values($array)[0]; //Bert and Ernie game发布于 2015-06-17 09:44:11
对于这样的问题,有很多不同的解决方案,我个人不推荐这方面的正则表达式。这通常是使用全文搜索索引解决的问题(只需谷歌全文搜索许多方法即可)。
对于这个特殊情况,假设您没有太多的数据,您可以只计算Levenshtein距离:http://php.net/manual/en/function.levenshtein.php
或者使用similar_text()函数:http://php.net/manual/en/function.similar-text.php
发布于 2015-06-17 09:43:29
您需要一些东西来查看每个值并计算一个数字权重,然后根据权重对数组进行排序,并取最上面的项目。
权重是你的“重要性”,例如,你可以选择给你认为更重要的术语赋予更高的权重。
https://stackoverflow.com/questions/30887722
复制相似问题