2010-11-03 89 views
2

我需要創建一個簡單的基於文本的搜索引擎asap(使用PHP)!基本上它必須讀取目錄中的文件,刪除停止和無用的單詞,將每個剩餘的有用單詞與它在每個文檔中出現的次數進行索引。創建一個簡單的基於文本文件的搜索引擎

我想這樣做的僞代碼爲:

for each file in directory: 
    read in contents, 
    compare to stop words, 
    add each remaining word to array, 
    count how many times that word appears in document, 
    add that number to the array, 
    add the id/name of the file to the array,

還需要計算單詞的總量(無用取出後我猜的)在整個文件中,即時猜測可以事後只要完成因爲我可以從該數組中獲取文件ID,然後計算內部的單詞....?

任何人都可以幫忙,也許提供一個準系統結構?我認爲主要的一點,我需要與越來越每個詞在文檔中出現的次數,並將其添加到索引陣列幫助...

感謝

回答

1
$words=array(); 
foreach (glob('*') as $file) { 
    $contents=file_get_contents($file); 
    $words[$file]=array(); 
    preg_match_all('/\S+/',$contents,$matches,PREG_SET_ORDER); 
    foreach ($matches as $match) { 
     if (!isset($words[$file][$match[0])) 
      $words[$file][$match[0]]=0; 
     $words[$file][$match[0]]++; 
    } 
    foreach ($useless as $value) 
     if (isset($words[$file][$value])) 
      unset($words[$file][$value]); 
    $count=count($words[$file]); 
    var_dump($words[$file]); 
    echo 'Number of words: '.$count; 
} 
+0

謝謝!我給了這個嘗試,我只是得到「ArrayArrayArrayArray」等等打印如果我呼應單詞數組......計數工作正常,但我需要將每個單詞存儲在數組中,並能夠檢索它... – 2010-11-04 13:02:41

+0

謝謝,想我即將到達的地方! (忽略以前的評論,我回應,而不是打印......)唯一的事情是,我如何將文件名旁邊的每個單詞和數量存儲在數組中? – 2010-11-04 13:09:52

+0

也即時消除停止/無用的單詞。我有一個單詞陣列即時比較使用array_diff,並且當我將它與末尾的單詞數組進行比較時,似乎沒有任何事情發生,沒有單詞被刪除... – 2010-11-04 13:35:00

1

看看str_word_count。它計算單詞,但也可以將它們提取到數組(數組中的每個值都是一個單詞)。然後,您可以後處理該陣列以去除停止詞,計數出現等

+0

+ 1,再加上像'array_count_values'這樣的東西可以非常小而且快。 – Wrikken 2010-11-03 22:42:40

1

好讓目錄中的每個文件應該是簡單的使用glob
然後讀取文件可以用 file_get_contents

完成
/** 
* This is how you will add extra rows 
* 
* $index[] = array(
* 'filename' => 'airlines.txt', 
* 'word' => 'JFK', 
* 'count' => 3, 
* 'all_words_count' => 42 
*); 
*/ 
$index = array(); 

$words = array('jfk', 'car'); 

foreach($words as $word) { 

    // All files with a .txt extension 
    // Alternate way would be "/path/to/dir/*" 
    foreach (glob("test_files/*.txt") as $filename) { 

    // Includes the file based on the include_path 
    $content = file_get_contents($filename, true); 

    $count = 0; 

    $totalCount = str_word_count($content); 

    if(preg_match_all('/' . $word . '/i', $content, $matches)) { 
     $count = count($matches[0]); 
    } 

    // And another item to the list 
    $index[] = array(
     'filename' => $filename, 
     'word' => $word, 
     'count' => $count, 
     'all_words_count' => $totalCount 
    ); 

    } 

} 

// Debug and look at the index array, 
// make sure it looks the way you want it. 
echo '<pre>'; 
print_r($index); 
echo '</pre>'; 

當我測試上面的代碼,這就是我得到的。

Array 
(
    [0] => Array 
     (
      [filename] => test_files/airlines.txt 
      [word] => jfk 
      [count] => 2 
      [all_words_count] => 38 
     ) 

    [1] => Array 
     (
      [filename] => test_files/rentals.txt 
      [word] => jfk 
      [count] => 0 
      [all_words_count] => 47 
     ) 

    [2] => Array 
     (
      [filename] => test_files/airlines.txt 
      [word] => car 
      [count] => 0 
      [all_words_count] => 38 
     ) 

    [3] => Array 
     (
      [filename] => test_files/rentals.txt 
      [word] => car 
      [count] => 3 
      [all_words_count] => 47 
     ) 

) 

我想我已經解決了你的問題:上面的腳本後d此添加到您應該能夠排序計數,從零開始與$sorted和最高配$sorted_desc

function sorter($a, $b) { 
    if($a['count'] == $b['count']) 
    return 0; 

    return ($a['count'] < $b['count']) ? -1 : 1; 
} 

// Clone the original list 
$sorted = $index; 

// Run a custom sort function 
uasort($sorted, 'sorter'); 

// Reverse the array to find the highest first 
$sorted_desc = array_reverse($sorted); 

// Debug and look at the index array, 
// make sure it looks the way you want it. 
echo '<h1>Ascending</h1><pre>'; 
print_r($sorted); 
echo '</pre>'; 

echo '<h1>Descending</h1><pre>'; 
print_r($sorted_desc); 
echo '</pre>'; 
+0

非常感謝,這適用於查找搜索詞,但是我需要它來計算每個文檔中的所有單詞並將其記錄下來! – 2010-11-03 21:38:42

+0

太棒了!我一直在玩這個,我很難得到每個單詞進入一個數組.... 基本上我想說在$索引數組保持: 文件名| word |每個單詞出現的次數。 即airline.txt |航空公司| 5 – 2010-11-04 12:57:19

+0

好吧,這很好,但我需要每個文件中的每個單詞,所以而不是你有$ words = array('jfk','car');我需要它來獲取文檔中的每個單詞來創建一個巨大的索引,讓我?然後我必須能夠搜索某個POST'ed字... – 2010-11-04 16:50:37

0

這是一個基本結構:

  1. 創建$index陣列
  2. 使用scandir(或glob,如果您只需要獲取某種類型的文件)來獲取目錄中的文件。
  3. 對於每個文件:
    1. file_get_contents
    2. 使用str_word_count獲取內容獲取字流
    3. 的陣列$word_stream創建一個數組$word_array持有字數
    4. 對於$word_stream每個字:
      1. 如果它在$ignored_words陣列中,則跳過它
      2. 如果它不在$word_array作爲重點,增加$word_array[$word] = 1
      3. 如果它已經在$word_array,增加$word_array[$word]++
    5. 獲取的$word_array總和與array_sum,或獨特的單詞,count的總和;你可以將其與鍵"_unique""_count"添加到$word_array(該不會是的話),如果你喜歡
    6. 添加的文件名作爲關鍵$index陣列,取值爲$word_array