2011-05-05 137 views
0

我使用PHP做一個簡單的搜索引擎,在布爾檢索 基地我已預定義文件 例如: - doc0:我的名字叫凱撒 - DOC1:凱撒字符。 .. -doc2 .....布爾檢索PHP

我已經構造術語文檔矩陣如下:enter image description here

因此,例如,「我」存在於第一個文件,而不是在第二次和「凱撒「是在這兩個文件 當我搜索一個單一的術語,我得到的布爾值因此,如果我在搜索引擎中鍵入「名稱」,我會得到1 0。

我的問題是,我想搜索鐵道部不是一個短期如:「愷撒字符」 我想打一個布爾值和它們的布爾值之間。因此搜索的結果將是0 1至1 1 & 0 1 = 01 所以我需要的PHP函數,它讓我使用上述方法尋找多個單詞

這是代碼 $查詢= $ _REQUEST [「關鍵字」]; $ stoplists = array(「我」,「。」,「a」,「」); $ words = array(); $ wordsdoc = array(); $ matrix = array(); $ docs = array(「我的名字是凱撒」,「凱撒是一個字符」); $ k = 0;

//looping the docs array 
for ($i=0;$i<sizeof($docs);$i++) 
{ 
    //splitting doc[i] on " " (space) 
    $words_temp=explode(" ",$docs[$i]); 

    //looping the splitted words 
    for ($j=0;$j<sizeof($words_temp);$j++) 
    { 
     //checking if the word is not in stop dictionnary and does not already added in words array 
     if (!in_array($words_temp[$j],$stoplists) && !in_array($words_temp[$j],$words)) 
     { 
      //adding word to words array 
      $words[$k]=$words_temp[$j]; 
      //incrementing counter 
      $k++;  
     } 

    } 
    //print_r($words[1]); 
} 

echo "<b>Words:</b> "; 
for ($j=0;$j<sizeof($words);$j++) 
{ 
    echo $words[$j]." "; 
} 

echo "<br><br>"; 

//looping the docs array 
for($i=0;$i<sizeof($docs);$i++) 
{ 
    //splitting doc[i] on " " (space) 
    $words_temp=explode(" ",$docs[$i]); 
    //initialize counter 
    $l=0; 

    //looping the splitted words 
    for ($j=0;$j<sizeof($words_temp);$j++) 
    { 
     //checking if the word is not in stop dictionnary 
     if (!in_array($words_temp[$j],$stoplists)) 
     { 
      //adding word to 2d array 
      $wordsdoc[$i][$l]=$words_temp[$j]; 
      //incrementing counter 
      $l++;  
     } 

    } 
} 

echo "<b><u>Docs:</u></b><br>"; 
for($i=0;$i<sizeof($wordsdoc);$i++) 
{ 
    echo "doc".$i.": "; 
    for($j=0;$j<sizeof($wordsdoc[$i]);$j++) 
    { 
     echo $wordsdoc[$i][$j]." "; 
    } 
    echo "<br>"; 
} 
echo "<br>"; 

echo "<b>Res Matrix First Col:</b><br>"; 
for($i=0;$i<sizeof($words);$i++) 
{ 
    $matrix[$i][0]=$words[$i]; 
    echo $matrix[$i][0]."<br>"; 
} 

$i1=0; 
$i2=0; 

foreach($wordsdoc as $items) 
{ 
     for($i=0;$i<sizeof($words);$i++) 
     { 
      if(in_array($matrix[$i][0],$items)) 
       $matrix[$i][$i1+1] = 1; 
      else 
       $matrix[$i][$i1+1] =0; 
     } 
    $i1++; 
} 

echo "<table border=1><br>"; 
echo "<tr><td></td>"; 

for($i=0;$i<sizeof($docs);$i++) 
{ 
    echo "<td>doc".($i+1)."</td>"; 
} 
echo "</tr><br>"; 

foreach($matrix as $items) 
{ 
    echo "<tr>"; 
    foreach($items as $item) 
    { 
     echo "<td>".$item."</td>"; 
    } 
    echo "</tr><br>"; 
} 
echo "</table><br>";  

*我張貼這麼長的問題,很抱歉,但我真的需要幫助:S * 謝謝你們提前:)

+0

@ Vidor's是唯一可能的建議,在沒有提供更多關於如何實現'矩陣'以及如何查詢它的信息的情況下。 – 2011-05-05 15:33:41

+0

我要添加code – Mariya 2011-05-05 16:20:03

+0

我們可以把你的'search($ word)'函數當作一個*黑盒子*;然後將所有結果數組合併爲一個整體數組。我在下面提出了一種方法。 – 2011-05-05 16:39:22

回答

1
function search($word) { 
    ... code to query the matrix ... 
    return $result_array; 
} 

與多個單詞使用

$search_terms = array('my', 'caesar'); 
$overall_result = array(true, true); 
foreach($search_terms as $st) { 
    $this_result = search($st); 
    $overal_index = 0; 
    foreach($this_result as $b) { 
    $overall_result[$overal_index] = $b && $overall_result[$overal_index]; 
    $overal_index++; 
    } 
} 
+0

非常感謝((: – Mariya 2011-05-05 16:59:48

2

也許你應該嘗試爆炸搜索查詢和做尋找數組中的每個部分,然後合併結果,只要你喜歡

+0

多數民衆贊成是對的,但假設我有一個搜索($單詞)函數,返回一個數組或布爾值,我爆炸查詢,使搜索foreach單詞,但是如何庫存他們,做布爾和他們之間的方式? – Mariya 2011-05-05 15:56:19