按最高相似度百分比匹配来自两组数据的项目
Match items from two sets of data by highest % of similarities
任务:
我有两列产品名称。
我需要从 B 列中为单元格 A1 找到最相似的单元格,然后为 A2、A3 等等。
输入:
Col A | Col B
-------------
Red | Blackwell
Black | Purple
White | Whitewater
Green | Reddit
输出:
红色 = Reddit / 66% 相似
Black = Blackwell / 71% 相似度
白色 = 白水 / 66% 相似度
绿色 = Reddit / 30% 相似度
我认为列文斯坦距离可以帮助排序,但我不知道如何应用它。
提前致谢,任何信息都有帮助。
使用嵌套循环
<?php
// Arrays of words
$colA = ['Red', 'Black', 'White', 'Green'];
$colB = ['Blackwell', 'Purple', 'Whitewater', 'Reddit'];
// loop through words to find the closest
foreach ($colA as $a) {
// Current max number of matches
$maxMatches = -1;
$bestMatch = '';
foreach ($colB as $b) {
// Calculate the number of matches
$matches = similar_text($a, $b, $percent);
if ($matches > $maxMatches) {
// Found a better match, update
$maxMatches = $matches;
$bestMatch = $b;
$matchPercentage = $percent;
}
}
echo "$a = $bestMatch / " .
number_format($matchPercentage, 2) .
"% similar\n";
}
第一个循环遍历第一个数组的元素,为每个元素初始化找到的最佳匹配项以及该匹配项中匹配字符的数量。
内层循环遍历可能的匹配项数组以寻找最佳匹配项,它会为每个候选项检查相似性(您可以在此处使用 levenshtein
而不是 similar_text
,但后者更方便因为它会为您计算百分比),如果当前单词比当前最佳匹配更好,则该变量会更新。
对于外循环中的每个单词,我们回显找到的最佳匹配项和百分比。根据需要设置格式。
我不确定您从哪里得出这些所需的百分比,所以我将只使用 php 函数产生的值,您可以决定是否要对它们执行任何计算。
levenshtein()
根本没有提供您在问题中请求的所需匹配项。我认为您使用 similar_text()
.
会更明智
代码:(Demo)
$arrayA=['Red','Black','White','Green'];
$arrayB=['Blackwell','Purple','Whitewater','Reddit'];
// similar text
foreach($arrayA as $a){
$temp=array_combine($arrayB,array_map(function($v)use($a){similar_text($v,$a,$percent); return $percent;},$arrayB)); // generate assoc array of assessments
arsort($temp); // sort descending
$result[]="$a is most similar to ".key($temp)." (sim-score:".number_format(current($temp))."%)"; // access first key and value
}
var_export($result);
echo "\n--\n";
// levenstein doesn't offer the desired matching
foreach($arrayA as $a){
$temp=array_combine($arrayB,array_map(function($v)use($a){return levenshtein($v,$a);},$arrayB)); // generate assoc array of assessments
arsort($temp); // sort descending
$result2[]="$a is most similar to ".key($temp)." (lev-score:".current($temp).")"; // access first key and value
}
var_export($result2);
输出:
array (
0 => 'Red is most similar to Reddit (sim-score:67%)',
1 => 'Black is most similar to Blackwell (sim-score:71%)',
2 => 'White is most similar to Whitewater (sim-score:67%)',
3 => 'Green is most similar to Purple (sim-score:36%)',
)
--
array (
0 => 'Red is most similar to Whitewater (lev-score:9)',
1 => 'Black is most similar to Whitewater (lev-score:9)',
2 => 'White is most similar to Blackwell (lev-score:8)',
3 => 'Green is most similar to Blackwell (lev-score:8)',
)
任务: 我有两列产品名称。 我需要从 B 列中为单元格 A1 找到最相似的单元格,然后为 A2、A3 等等。
输入:
Col A | Col B
-------------
Red | Blackwell
Black | Purple
White | Whitewater
Green | Reddit
输出:
红色 = Reddit / 66% 相似
Black = Blackwell / 71% 相似度
白色 = 白水 / 66% 相似度
绿色 = Reddit / 30% 相似度
我认为列文斯坦距离可以帮助排序,但我不知道如何应用它。
提前致谢,任何信息都有帮助。
使用嵌套循环
<?php
// Arrays of words
$colA = ['Red', 'Black', 'White', 'Green'];
$colB = ['Blackwell', 'Purple', 'Whitewater', 'Reddit'];
// loop through words to find the closest
foreach ($colA as $a) {
// Current max number of matches
$maxMatches = -1;
$bestMatch = '';
foreach ($colB as $b) {
// Calculate the number of matches
$matches = similar_text($a, $b, $percent);
if ($matches > $maxMatches) {
// Found a better match, update
$maxMatches = $matches;
$bestMatch = $b;
$matchPercentage = $percent;
}
}
echo "$a = $bestMatch / " .
number_format($matchPercentage, 2) .
"% similar\n";
}
第一个循环遍历第一个数组的元素,为每个元素初始化找到的最佳匹配项以及该匹配项中匹配字符的数量。
内层循环遍历可能的匹配项数组以寻找最佳匹配项,它会为每个候选项检查相似性(您可以在此处使用 levenshtein
而不是 similar_text
,但后者更方便因为它会为您计算百分比),如果当前单词比当前最佳匹配更好,则该变量会更新。
对于外循环中的每个单词,我们回显找到的最佳匹配项和百分比。根据需要设置格式。
我不确定您从哪里得出这些所需的百分比,所以我将只使用 php 函数产生的值,您可以决定是否要对它们执行任何计算。
levenshtein()
根本没有提供您在问题中请求的所需匹配项。我认为您使用 similar_text()
.
代码:(Demo)
$arrayA=['Red','Black','White','Green'];
$arrayB=['Blackwell','Purple','Whitewater','Reddit'];
// similar text
foreach($arrayA as $a){
$temp=array_combine($arrayB,array_map(function($v)use($a){similar_text($v,$a,$percent); return $percent;},$arrayB)); // generate assoc array of assessments
arsort($temp); // sort descending
$result[]="$a is most similar to ".key($temp)." (sim-score:".number_format(current($temp))."%)"; // access first key and value
}
var_export($result);
echo "\n--\n";
// levenstein doesn't offer the desired matching
foreach($arrayA as $a){
$temp=array_combine($arrayB,array_map(function($v)use($a){return levenshtein($v,$a);},$arrayB)); // generate assoc array of assessments
arsort($temp); // sort descending
$result2[]="$a is most similar to ".key($temp)." (lev-score:".current($temp).")"; // access first key and value
}
var_export($result2);
输出:
array (
0 => 'Red is most similar to Reddit (sim-score:67%)',
1 => 'Black is most similar to Blackwell (sim-score:71%)',
2 => 'White is most similar to Whitewater (sim-score:67%)',
3 => 'Green is most similar to Purple (sim-score:36%)',
)
--
array (
0 => 'Red is most similar to Whitewater (lev-score:9)',
1 => 'Black is most similar to Whitewater (lev-score:9)',
2 => 'White is most similar to Blackwell (lev-score:8)',
3 => 'Green is most similar to Blackwell (lev-score:8)',
)