當前位置:才華齋>設計>網頁設計>

PHP中計算字串相似度函式程式碼

網頁設計 閱讀(1.36W)

similar_text — 計算兩個字串的相似度

PHP中計算字串相似度函式程式碼

int similar_text ( string $first , string $second [, float &$percent ] )

$first 必需。規定要比較的第一個字串。

$second 必需。規定要比較的第二個字串。

$percent 可選。規定供儲存百分比相似度的變數名。

兩個字串的相似程度計算依據 Oliver [1993] 的描述進行。注意該實現沒有使用 Oliver 虛擬碼中的堆疊,但是卻進行了遞迴呼叫,這個做法可能會導致整個過程變慢或變快。也請注意,該演算法的複雜度是 O(N**3),N 是最長字串的長度。

比如我們想找字串abcdefg和字串aeg的相似度:

複製程式碼 程式碼如下:

$first = "abcdefg";

$second = "aeg";

echo similar_text($first, $second);結果輸出3.如果想以百分比顯示,則可使用它的第三個引數,如下:

$first = "abcdefg";

$second = "aeg";

similar_text($first, $second, $percent);

echo $percent;

similar_text函式的使用及實現過程。similar_text() 函式主要是用來計算兩個字串的`匹配字元的數目,也可以計算兩個字串的相似度(以百分比計)。與 similar_text() 函式相比,我們今天要介紹的 levenshtein() 函式更快。不過,similar_text() 函式能通過更少的必需修改次數提供更精確的結果。在追求速度而少精確度,並且字串長度有限時可以考慮使用 levenshtein() 函式。

使用說明

先看手冊上 levenshtein() 函式的說明:

levenshtein() 函式返回兩個字串之間的 Levenshtein 距離。

Levenshtein 距離,又稱編輯距離,指的是兩個字串之間,由一個轉換成另一個所需的最少編輯操作次數。許可的編輯操作包括將一個字元替換成另一個字元,插入一個字元,刪除一個字元。

例如把 kitten 轉換為 sitting:

sitten (k→s)

sittin (e→i)

sitting (→g)levenshtein() 函式給每個操作(替換、插入和刪除)相同的權重。不過,您可以通過設定可選的 、replace、 引數,來定義每個操作的代價。

語法:

levenshtein(string1,string2,,replace,)

引數 描述

string1 必需。要對比的第一個字串。

string2 必需。要對比的第二個字串。

可選。插入一個字元的代價。預設是 1。

replace 可選。替換一個字元的代價。預設是 1。

可選。刪除一個字元的代價。預設是 1。

提示和註釋

如果其中一個字串超過 255 個字元,levenshtein() 函式返回 -1。

levenshtein() 函式對大小寫不敏感。

levenshtein() 函式比 similar_text() 函式更快。不過,similar_text() 函式提供需要更少修改的更精確的結果。

例子

複製程式碼 程式碼如下:

<?php

echo levenshtein("Hello World","ello World");

echo "
";

echo levenshtein("Hello World","ello World",10,20,30);

?>

輸出: 1 30

以下是補充:

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

複製程式碼 程式碼如下:

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

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

str1 = $str1; $this->str2 = $str2; if ($len1 == 0) $len1 = strlen($str1); if ($len2 == 0) $len2 = strlen($str2); $this->initC($len1, $len2); return $this->printLCS($this->c, $len1 - 1, $len2 - 1); } /*返回兩個串的相似度*/ function getSimilar($str1, $str2) { $len1 = strlen($str1); $len2 = strlen($str2); $len = strlen($this->getLCS($str1, $str2, $len1, $len2)); return $len * 2 / ($len1 + $len2); } function initC($len1, $len2) { for ($i = 0; $i < $len1; $i++) $this->c[$i][0] = 0; for ($j = 0; $j < $len2; $j++) $this->c[0][$j] = 0; for ($i = 1; $i < $len1; $i++) { for ($j = 1; $j < $len2; $j++) { if ($this->str1[$i] == $this->str2[$j]) { $this->c[$i][$j] = $this->c[$i - 1][$j - 1] + 1; } else if ($this->c[$i - 1][$j] >= $this->c[$i][$j - 1]) { $this->c[$i][$j] = $this->c[$i - 1][$j]; } else { $this->c[$i][$j] = $this->c[$i][$j - 1]; } } } } function printLCS($c, $i, $j) { if ($i == 0 || $j == 0) { if ($this->str1[$i] == $this->str2[$j]) return $this->str2[$j]; else return ""; } if ($this->str1[$i] == $this->str2[$j]) { return $this->printLCS($this->c, $i - 1, $j - 1).$this->str2[$j]; } else if ($this->c[$i - 1][$j] >= $this->c[$i][$j - 1]) { return $this->printLCS($this->c, $i - 1, $j); } else { return $this->printLCS($this->c, $i, $j - 1); } }} $lcs = new LCS();//返回最長公共子序列$lcs->getLCS("hello word","hello china");//返回相似度echo $lcs->getSimilar("吉林禽業公司火災已致112人遇難","吉林寶源豐禽業公司火災已致112人遇難");

同樣輸出結果為:0.90322580645161,明顯準確的多。