我試圖檢查字符串的大致相似性。在PHP中檢查兩個字符串的近似匹配
這是我使用的標準。
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);
Algoritm: 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
}
}
}
}
的想法泰德的$意義將得到 「多少它的成本」 美元再經過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.
查看[levenshtein()](http://php.net/manual/en/function.levenshtein.php)和[similar_text()](http://www.php.net/manual/en /function.similar-text。PHP)提供的功能,它們可能適合賬單。 – hexblot 2013-05-14 13:20:43
不確定... – 2013-05-14 13:23:20
另外[soundex](http://php.net/manual/en/function.soundex.php) – 2013-05-14 13:23:51