簡體   English   中英

相似文字搜索

[英]Similar Text search

我使用以下代碼搜索相似的文本。 但是由於某種原因,無論我搜索什么,它都只會返回相同的值。 有人可以看到問題所在嗎?

    $result = mysql_query("SELECT keyword FROM search");
$storeArray = Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$storeArray[] =  $row['keyword'];  
      }
foreach ($storeArray as $key){
    //echo "'".$key."'";
}  



$my_word = $_POST['value'];

$all_words = array(
"'".$key."'"
);

$bestMatch = array('word' => $my_word, 'match' => 2);

foreach($all_words as $word) {
similar_text($word, $my_word, $percent);
if($percent > $bestMatch['match']) $bestMatch = array('word' => $word, 'match' =>     $percent);
}

if($bestMatch['match'] < 100) echo 'Did you mean: <strong>' . $bestMatch['word'] .   '</strong>';

我編輯了一下,告訴我這是否是您想要的。

$my_word = $_POST['value'];
$bestMatch = array('word' => $my_word, 'match' => 2);
$result = mysql_query("SELECT keyword FROM search");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            //run similar text on each row
        similar_text($row['keyword'], $my_word, $percent);
            //if row has a better score, overwrite $bestMatch
        if ($percent > $bestMatch['match'])
            $bestMatch = array('word' => $row['keyword'], 'match' => $percent);
}
if ($bestMatch['match'] < 100)
    echo 'Did you mean: <strong>' . $bestMatch['word'] .   '</strong>';

編輯:別忘了, similar_text已經具有很大相似度的單詞(主要是在長度方面), similar_text才顯示出良好的結果,我認為這是80%的相似度(結合了字符數和長度)。 雖然不是100%肯定...

看來您在代碼中正在執行此操作,並且只能解釋得到一個結果。

$all_words = array(
    "'".$key."'"
);

foreach($all_words as $word) {
    // ...
}

您正在做的是在$key上運行一個foreach() ,它不存在,因此獲得與'' ..最接近的匹配項,這將始終是同一件事。

嘗試將foreach()更改為foreach($storeArray as $word)或類似的內容。

這是一些調試策略-將此代碼放在if($bestMatch['match'] < 100)之上。

echo '<pre>';
echo $my_word;
print_r($storeArray);
print_r($all_words);
echo '/<pre>';

您要查找的是$all_words包含您要在其上運行similar_text()所有單詞。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM