2012-11-21 87 views
0

我需要比較下面兩個句子和輸出應該是匹配PHP:比較兩個句子

A的百分比:高級經理,生產

B:高級經理,prodcution銷售

我通過比較句子的每個單詞來嘗試(代碼如下)。它工作,但我需要非常有效的方式。

public function contact_title_match($old_title,$new_title) 
{ 
    $new_title=preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $new_title); 
    $new_title_arr=explode(" ",$new_title); 
    $count=count($new_title_arr); 
    $index=0; 
    if($count>0) 
    { 
     foreach($new_title_arr as $title_word) 
     { 
     if(stripos($old_title,$title_word)!==false) 
      $index++; 
     } 
     if(($index/$count)>= 0.5) return 1; 
     else      return 0; 
    } 
} 
+0

你能至少表明你已經嘗試呢? – Phorce

+0

添加了我試過的代碼 – Muzaffer

回答

6

聽起來像是不錯的工作爲similar_text

$sentence_a = "senior manager, production"; 
$sentence_b = "senior manager, prodcution-sales"; 
$percentage = 0; 

similar_text($sentence_a, $sentence_b, $percentage); 

// The strings are 86 percent similar. 
printf("The strings are %d percent similar.", $percentage); 

演示:http://codepad.org/9Vx797uB

+1

這太棒了。 –

+0

similar_text比較理論上,但我的代碼邏輯比較。以下是示例:A =「數據庫管理員」B =「數據庫管理員」similar_text_output:77%My_code_output:100%。 – Muzaffer

+0

@Muzaffer但「管理員」和「管理員」是不一樣的單詞。那麼他們的基礎是什麼?除非你持有同義詞數據庫。但是再次,你可能會在那裏發現一些「管理員」可能在「管理員」的地方出現的誤報,但被混淆爲「管理員」甚至「管理員」的同義詞。 – Sampson