国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php文章相似度計算similar_text()函數升級

2024-05-04 21:58:09
字體:
來源:轉載
供稿:網友

有時我們希望調用相關文章時肯定調用相似度高的我先是使用了php的similar_text()函數,但是測試兩個相同的標題得出結果只有40%左右啊,下面看實例.

php默認有個函數similar_text()用于計算字符串之間的相似度,該函數也可以計算兩個字符串的相似度(以百分比計),不過這個函數感覺對中文計算很不準確比如:

echo similar_text("吉林禽業公司火災已致112人遇難","吉林寶源豐禽業公司火災已致112人遇難"); 

這兩個新聞標題其實都是一樣的,如果使用similar_text()相似對結果為:42,即只相似42%,所以這個感覺很不靠譜,今天剛好收集到一段PHP代碼也是用于比較兩個字符串的相似度,直接貼出代碼:

  1. <?php  
  2. class LCS { 
  3.     var $str1
  4.     var $str2
  5.     var $c = array(); 
  6.     /*返回串一和串二的最長公共子序列 
  7. */ 
  8.     function getLCS($str1$str2$len1 = 0, $len2 = 0) { 
  9.         $this->str1 = $str1
  10.         $this->str2 = $str2
  11.         if ($len1 == 0) $len1 = strlen($str1); 
  12.         if ($len2 == 0) $len2 = strlen($str2); 
  13.         $this->initC($len1$len2); 
  14.         return $this->printLCS($this->c, $len1 - 1, $len2 - 1); 
  15.     } 
  16.     /*返回兩個串的相似度 
  17. */ 
  18.     function getSimilar($str1$str2) { 
  19.         $len1 = strlen($str1); 
  20.         $len2 = strlen($str2); 
  21.         $len = strlen($this->getLCS($str1$str2$len1$len2)); 
  22.         return $len * 2 / ($len1 + $len2); 
  23.     } 
  24.     function initC($len1$len2) { 
  25.         for ($i = 0; $i < $len1$i++) $this->c[$i][0] = 0; 
  26.         for ($j = 0; $j < $len2$j++) $this->c[0][$j] = 0; 
  27.         for ($i = 1; $i < $len1$i++) { 
  28.             for ($j = 1; $j < $len2$j++) { 
  29.                 if ($this->str1[$i] == $this->str2[$j]) { 
  30.                     $this->c[$i][$j] = $this->c[$i - 1][$j - 1] + 1; 
  31.                 } else if ($this->c[$i - 1][$j] >= $this->c[$i][$j - 1]) { 
  32.                     $this->c[$i][$j] = $this->c[$i - 1][$j]; 
  33.                 } else { 
  34.                     $this->c[$i][$j] = $this->c[$i][$j - 1]; 
  35.                 } 
  36.             } 
  37.         } 
  38.     } 
  39.     function printLCS($c$i$j) { 
  40.         if ($i == 0 || $j == 0) { 
  41.             if ($this->str1[$i] == $this->str2[$j]) return $this->str2[$j]; 
  42.             else return ""
  43.         } 
  44.         if ($this->str1[$i] == $this->str2[$j]) { 
  45.             return $this->printLCS($this->c, $i - 1, $j - 1).$this->str2[$j]; 
  46.         } else if ($this->c[$i - 1][$j] >= $this->c[$i][$j - 1]) { 
  47.             return $this->printLCS($this->c, $i - 1, $j); 
  48.         } else { 
  49.             return $this->printLCS($this->c, $i$j - 1); 
  50.         } 
  51.     } 
  52.  
  53. $lcs = new LCS(); 
  54. //返回最長公共子序列 
  55. $lcs->getLCS("hello word","hello china"); 
  56. //返回相似度 
  57. echo $lcs->getSimilar("吉林禽業公司火災已致112人遇難","吉林寶源豐禽業公司火災已致112人遇難"); 

同樣輸出結果為:0.90322580645161,明顯準確的多,還有一種辦法就是利用分詞系統我們把標題分詞,然后再進行會更準確一些.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长葛市| 乐东| 林口县| 重庆市| 巴彦淖尔市| 公主岭市| 红原县| 新乡县| 类乌齐县| 大港区| 安吉县| 郎溪县| 临邑县| 龙口市| 新乐市| 北碚区| 绍兴县| 志丹县| 商水县| 宁国市| 余庆县| 丹阳市| 浠水县| 竹北市| 巴林右旗| 元氏县| 托克托县| 新昌县| 随州市| 瑞昌市| 大悟县| 洛扎县| 武城县| 固始县| 盐山县| 大田县| 鹤山市| 和静县| 论坛| 正定县| 塔城市|