我正在尝试检查字符串的大致相似性。
这是我使用的标准。
1) 单词的顺序很重要 2) 单词可以有 80% 的相似度。
例子:
$string1 = "How much will it cost to me" //string in vocabulary (all "right" words is here)
$string2 = "How much does costs it " //"costs" instead "cost" -is a deliberate mistake (user input);
算法: 1)检查单词的相似性并用“正确”的单词创建干净的字符串(根据它在词汇表中出现的顺序)。输出:“它要花多少钱” 2)用“正确”的词创建干净的字符串,以便它出现在用户输入中。输出:“花费多少” 3)比较两个输出 - 如果不同 - 返回否,否则如果相同返回是。
有什么建议么?
我开始写代码,但是我不熟悉PHP中的工具,所以我不知道如何合理有效地完成它。
它看起来更像 javascript/php
$string1="how much will it cost for me" ;
$string2= "how much does costs it";
function compareStrings($string1, $string2) {
if (strlen($s1)==0 || strlen($s2)==0) {
return 0;
}
while (strpos($s1, " ")!==false) {
$s1 = str_replace(" ", " ", $s1);
}
while (strpos($s2, " ")!==false) {
$s2 = str_replace(" ", " ", $s2);
}
$ar1 = explode(" ",$s1);
$ar2 = explode(" ",$s2);
$array1 = array_flip($ar1);
$array2 = array_flip($ar2);
$l1 = count($ar1);
$l2 = count($ar2);
$meaning="";
$rightorder=""
for ($i=0;$i<=$l1;$i++) {
for ($j=0;$j<=$l2;$j++) {
$k= similar_text($array1[i], $array2[j], $perc).PHP_EOL;
if ($perc>=85) {
$meaning=$meaning." ".$array1[j]; //generating a string of the first output
$rightorder[i]= array1[i]; //generating the array with second output
}
}
}
}
$meaning 的想法将得到“成本多少”,而 $rightorder 将得到
$rightorder[0]='how'
$rightorder[1]='much'
$rightorder[2]=''
$rightorder[3]='cost'
$rightorder[4]='it'
之后我会以某种方式将其转换回字符串“它的成本是多少”
并比较这两者。
if ("how much cost it"=="how much it cost") return true; else return false.