2010-03-22 42 views
2

如何提取php5中兩段或多段之間的常用詞? 我想它可能會工作來總結每個文本創建一個高度排名的單詞列表 然後比較它們。提取兩段之間的常用詞?

+0

你能給輸入和預期輸出的例子嗎? – Gumbo 2010-03-22 17:14:00

回答

5

我想最基本的方法是:

  • 分裂每個段落成單詞的陣列,使用任一explodepreg_split
    • 第一個可能有點快
    • 第二個可能提供更多的選擇
  • 也許,做一些過濾單詞的列表:
    • 乾淨的每一個字
      • 刪除特殊字符,如重音字母
      • 將所有內容都轉換爲小寫/大寫,以幫助稍後進行比較
    • 刪除太常見
    • 刪除太短
    • array_filter,在這裏,或許可以幫助
  • ,然後碰到一些詞的名單在兩個陣列,使用類似array_intersect
+2

這是一個很好的方法,對於過濾,更準確(但更復雜)的方法是根據大型語料庫中的頻率來減少單詞的權重。例如 這個詞有很高的頻率,所以它的排名會大大降低。那麼排名較高的詞則更具代表性。 – Mathieu 2010-03-22 17:21:59

+0

@mathroc:true;並且稍微調整一下,這也可以讓一個人爲某些特定詞語注入高分量 – 2010-03-22 17:51:32

+0

另一個轉折可能是使用http://tartarus.org/~martin/PorterStemmer/作爲其中的一部分。 – goat 2010-03-22 19:12:09

4

有可能是一種更快的方法,但你可以像!! - 。/ \ @#$%^ & *這樣標點符號,然後將兩個段落分解成一個數組,然後在兩個數組上嘗試array_intersect()。數組2中的數組1中的任何數據都應作爲匹配返回。

http://php.net/manual/en/function.array-intersect.php

理論上應該接收回來的匹配單詞的數組。從那裏,排名取決於你以及你如何選擇這樣做。

+1

+1打敗我的回答,雖然我可能已經使用'str_replace'來處理標點符號。 – Isaac 2010-03-22 17:18:44

-1
  1. 分割空間上的每一段
  2. 選擇從A段記號。如果它在段落B中,則將它放在'匹配'數組中。
  3. 重複步驟2,直到在A段中沒有更多標記
+1

這似乎是一個奇怪的答案,已得到downvote沒有評論。這個理論沒有任何明顯的錯誤。 – Beska 2010-03-22 17:20:12

+0

我沒有失望,但我期望這是因爲以這種方式做這件事似乎如此笨拙。它會工作,但使用數組函數更有意義。此外,沒有提及任何關於標點符號的問題,如果它沒有被剝離,會導致問題。 – 2010-03-22 17:55:31

2

像這樣的東西可能會工作...

<?php 
    $paragraph = "hello this is some sample text. Sample text is usually used to test a program. For example, this sample text will be used to test the script below."; 
    $words = array(); 
    preg_match_all('/\w+/', $paragraph, $matches); 
    foreach($matches[0] as $w){ 
    $w = strtolower($w); 
    if(!array_key_exists($w, $words)){ 
     $words[$w] = 0; 
    } 
    $words[$w]++; 
    } 
    asort($words); 
    echo print_r($words, true); 

    /* Output 
    Array (
     [hello] => 1 
     [will] => 1 
     [example] => 1 
     [a] => 1 
     [program] => 1 
     [usually] => 1 
     [Sample] => 1 
     [script] => 1 
     [below] => 1 
     [some] => 1 
     [the] => 1 
     [be] => 1 
     [for] => 1 
     [to] => 2 
     [is] => 2 
     [sample] => 2 
     [test] => 2 
     [used] => 2 
     [this] => 2 
     [text] => 3 
) */ 

?> 
2
<?php 
/** 
* Gets all the words as an array for a given text blob 
* 
* @param string $paragraph The pragraph in question 
* @return string[] Words found 
*/ 
function getWords($paragraph) { 
    //only lowercase 
    $paragraph = strtolower($paragraph); 
    //replace all non alpha num characters with spaces (this way periods won't screw 
    //with our words) 
    $paragraph = preg_replace("/[^a-z]/", " ", $paragraph); 
    $paragraph = explode(" ", $paragraph); 
    //get rid of empty words 
    $paragraph = array_flip($paragraph); 
    unset($paragraph[""]); 
    $paragraph = array_flip($paragraph); 
    return $paragraph; 
} 

$paragraph1 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sit amet ante 
nisl. Morbi tempor varius semper. Suspendisse vel nisi dui. Sed tristique consectetur imperdiet. 
Morbi nulla diam, lobortis non eleifend eget, ullamcorper nec tortor. Duis quis lectus felis. 
In vulputate varius luctus. Maecenas gravida laoreet massa quis faucibus. Duis dictum, dui sit 
amet pharetra laoreet, tortor nisi mattis tortor, et ornare purus dolor vitae ligula. Sed id 
orci ut dolor fermentum imperdiet. Nulla non justo urna, in suscipit nunc. Donec ut nibh risus, 
ut tempus mi. Proin fringilla pretium urna sed faucibus. Proin et porttitor sem. Nulla eros 
arcu, sodales et aliquam in, pharetra et mauris. Duis placerat blandit justo at tincidunt. 
Etiam eu rutrum arcu."; 

$paragraph2 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sit amet leo id 
arcu feugiat tempus quis a risus. Proin non nisi augue. Cras ultricies dignissim augue vel gravida. 
Vivamus sed orci sed leo sollicitudin aliquet non at dui. Nulla facilisi. Suspendisse nunc nibh, 
sollicitudin vitae tincidunt eget, aliquet vitae magna. Aliquam vehicula cursus ante, vitae rhoncus 
orci egestas et. Fusce condimentum metus at metus auctor pellentesque. Suspendisse potenti. Morbi 
blandit, leo sed eleifend pretium, augue dui interdum eros, vel faucibus felis dolor id elit. Nam 
condimentum, odio at mattis consequat, sem eros molestie risus, a tempus dolor arcu sit amet justo."; 

$common = array_intersect(getWords($paragraph1), getWords($paragraph2)); 
sort($common); 
var_dump($common); 
?>