2016-10-27 95 views
3

我想創建查找在用戶輸入關鍵詞的一個簡單的搜索引擎。我知道我可以使用strpos來檢查字符串中是否存在單詞。但是,我希望用戶能夠將錯誤的單詞拼寫出來。例如,使用similar_text和strpos一起

$userInput = "What year did George Washingtin become president?"; 
$key_word = "Washington"; 
someFuntion($userInput, $key_word, $percent); 
if($percent > .95){ 
$user_searched_washington = True; 
} 

有沒有這樣做的PHP函數,或者你有建議如何創建一個函數呢?

+1

我將通過拼寫檢查第一 – Jaime

回答

3

你可以嘗試利用在PHP的標準庫中的levenshtein功能。看到這裏說明文檔中的一些例子:http://php.net/manual/en/function.levenshtein.php

然而,當你可能的關鍵字列表的增長,這可能成爲一個非常昂貴的計算。

編輯:最低可行的例子:

<?php 

$myInput = 'persident'; 
$possibleKeywords = ['tyrant', 'president', 'king', 'royal']; 
$scores = []; 

foreach ($possibleKeywords as $keyword) { 
    $scores[] = levenshtein($myInput, $keyword); 
} 

echo $possibleKeywords[array_search(min($scores), $scores)]; 
// prints: "president" 
+0

由於運行它!這就是我一直在尋找的! – John

2

這裏是我想出了基於你的標題(同時使用strpossimilar_text),它應該有希望足以讓你開始。這使得除了單詞搜索短語(詞組)和忽略標點符號:

function search($haystack, $needle) { 
    // remove punctuation 
    $haystack = preg_replace('/[^a-zA-Z 0-9]+/', '', $haystack); 

    // look for exact match 
    if (stripos($haystack, $needle)) { 
     return true; 
    } 

    // look for similar match 
    $words = explode(' ', $haystack); 
    $total_words = count($words); 
    $total_search_words = count(explode(' ', $needle)); 
    for ($i = 0; $i < $total_words; $i++) { 
     // make sure the number of words we're searching for 
     // don't exceed the number of words remaining 
     if (($total_words - $i) < $total_search_words) { 
      break; 
     } 

     // compare x-number of words at a time 
     $temp = implode(' ', array_slice($words, $i, $total_search_words)); 
     $percent = 0; 
     similar_text($needle, $temp, $percent); 
     if ($percent >= 80) { 
      return true; 
     } 
    } 

    return false; 
} 

$text = "What year did George Washingtin become president?"; 
$keyword = "Washington"; 

if (search($text, $keyword)) { 
    echo 'looks like a match!'; 
}