Php document similarity calculation similar_text () function upgrade

Source: Internet
Author: User
Tags php code strlen

By default, php has a function similar_text () used to calculate the similarity between strings. This function can also calculate the similarity between two strings (expressed in percentages ). However, this function is not accurate in Chinese computing, for example:

The code is as follows: Copy code
Echo similar_text ("112 people were killed in a fire in Jilin poultry company", "112 people were killed in a fire in Jilin Baoyuanfeng Poultry Company ");

The two news titles are actually the same. If similar_text () is used, the result is:42, which is similar to 42%So this feeling is very unreliable. Today, we just collected a piece of PHP code to compare the similarity between two strings and paste the code directly:

The code is as follows: Copy code

<? Php
Class LCS {
Var $ str1;
Var $ str2;
Var $ c = array ();
/* Returns the longest common subsequence of string 1 and string 2.
*/
Function getLCS ($ str1, $ str2, $ len1 = 0, $ len2 = 0 ){
$ This-> 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 );
    }
/* Returns the similarity between two strings.
*/
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 ();
// Returns the longest common subsequence.
$ Lcs-> getLCS ("hello word", "hello china ");
// Return similarity
Echo $ lcs-> getSimilar ("112 people have been killed in a fire in Jilin poultry company", "112 people have been killed in a fire in Jilin Baoyuanfeng Poultry Company"); the same output is: 0.90322580645161, it is much more accurate.


Another way is to use the word segmentation system to split the title, and then enter the row more accurately.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.