我有两个varchar,我试图根据varchar中单词的相似性进行比较和显示。例如,如果我有字符串:“ hello mellow guy”,并将其与包含以下内容的表进行比较:
“你好海洋人”
“再见电脑用品”
“你好仙人掌仙人掌”
然后它将基于它们共有多少个单词来显示结果,例如:
“你好仙人掌”
“你好海洋人”
“再见的电脑东西”
预先感谢
答案 0 :(得分:0)
通常,可以通过标记原始语句和其他每个语句来做到这一点,对两个结果数组之间的交点进行计数并相应地排序,这是示例代码:
<?php
$myPhrase = "hello mellow fellow";
$otherPhrases = ["hello ocean man", "goodbye computer thing", "hello fellow cactus"];
$resultsOrdered = [];
$myWords = explode(" ", $myPhrase);
foreach($otherPhrases as $otherPhrase) {
$otherWords = explode(" ", $otherPhrase);
$intersectWords = array_intersect($myWords, $otherWords);
$resultsOrdered[$otherPhrase] = count($intersectWords);
}
arsort($resultsOrdered);
foreach($resultsOrdered as $phrase => $wordCountInCommon) {
echo $phrase." (".$wordCountInCommon. " words in common)".PHP_EOL;
}