Php 比较两个句子

Php 比较两个句子,php,comparison,Php,Comparison,我需要比较下面两个句子,输出应该是匹配的百分比 A:生产部高级经理 B:生产销售部高级经理 我试着比较下面句子代码中的每个单词。这是可行的,但我需要更有效的方法 public function contact_title_match($old_title,$new_title) { $new_title=preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $new_title); $new_title_arr=explode

我需要比较下面两个句子,输出应该是匹配的百分比

A:生产部高级经理

B:生产销售部高级经理

我试着比较下面句子代码中的每个单词。这是可行的,但我需要更有效的方法

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;
   }
}

听起来是一份不错的工作:


演示:

你至少能证明你已经尝试过了吗?添加了我尝试过的代码,相似的文本理论上比较,但我的代码逻辑上比较。下面是一个例子:A=数据库管理员B=数据库管理员相似的\u文本\u输出:77%我的\u代码\u输出:100%。@Muzaffer但Admin和Administrator不是同一个词。那么,它们在什么基础上是相同的呢?除非你还有一个同义词数据库。但是,同样,你可能会有一些误报,在这里可以找到Admin而不是Administrate,但是会被混淆为Administrator甚至Administration的同义词。这里的想法是比较联系人的标题。我希望标题中不要出现“管理”一词。
$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);