php function Similar_text () principle Analysis _php Tutorial

Source: Internet
Author: User
PHP has a function similar_text (), which calculates two string similarity, and can draw a percentage to represent the similarity of two strings. The effect is as follows:

Similar_text (' aaaa ', ' aaaa ', $percent);
Var_dump ($percent);
Float (100)
Similar_text (' aaaa ', ' aaaabbbb ', $percent);
Var_dump ($percent);
Float (66.666666666667)
Similar_text (' abcdef ', ' AABCDEFG ', $percent);
Var_dump ($percent);
Float (85.714285714286)
Using this function, it can be used to do fuzzy search functions, or other functions that require fuzzy matching. I have recently involved this function in the feature matching step in the verification Code recognition study.

But what kind of algorithm does this function specifically use? I studied his bottom-up implementation, summed up in three steps:

(1) Find the longest paragraph of the same part in two strings;
(2) Use the same method to find the longest section of the same part in the remaining two paragraphs, and so on, until there is no identical part;
(3) Similarity = sum of the sum of the length of all the same parts * 2/two strings;

I studied the source code version is PHP 5.4.6, the relevant code is located in the No. 2951 to No. 3031 line of the file PHP-5.4.6/EXT/STANDARD/STRING.C. Here is the source code after I added the comment.

Find the longest segment of the same part in two strings
static void Php_similar_str (const char *TXT1, int len1, const char *txt2, int len2, int *pos1, int *pos2, int *max)
{
Char *p, *q;
Char *end1 = (char *) TXT1 + len1;
Char *end2 = (char *) TXT2 + len2;
int l;

*max = 0;
Begins a traversal with the first string as a baseline
for (p = (char *) txt1; p < end1; p++) {
Traverse the second string
for (q = (char *) txt2; q < end2; q++) {
Find the same character, continue to loop, L is the same part of the length
for (l = 0; (P + L < end1) && (q + L < End2) && (p[l] = = Q[l]); l++);
The bubbling method finds the longest one l, and remembers where the same part starts
if (L > *max) {
*max = l;
*POS1 = P-txt1;
*pos2 = q-txt2;
}
}
}
}

Calculates the total length of the same part of a two string
static int Php_similar_char (const char *TXT1, int len1, const char *txt2, int len2)
{
int sum;
int pos1, POS2, Max;

Find two strings in the same part of the longest paragraph
Php_similar_str (Txt1, Len1, Txt2, Len2, &AMP;POS1, &pos2, &max);
This is the initial assignment of sum and the judgment of the max value.
If Max is zero, indicating that two strings do not have any of the same characters, they will jump out of the IF
if ((sum = max)) {
Recursive to the first half, same length cumulative
if (pos1 && pos2) {
Sum + = Php_similar_char (Txt1, POS1,
TXT2, Pos2);
}
Recursive to the second half, the same length is accumulated
if ((POS1 + Max < len1) && (Pos2 + Max < len2)) {
Sum + = Php_similar_char (txt1 + pos1 + max, Len1-pos1-max,
TXT2 + Pos2 + max, Len2-pos2-max);
}
}

return sum;
}

PHP function definition
Php_function (Similar_text)
{
Char *t1, *T2;
Zval **percent = NULL;
int ac = Zend_num_args ();
int sim;
int T1_len, T2_len;

Check parameter legitimacy
if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "ss| Z ", &t1, &t1_len, &t2, &t2_len, &percent) = = FAILURE) {
Return
}

If there is a third parameter
if (AC > 2) {
CONVERT_TO_DOUBLE_EX (percent);
}

If two string lengths are 0, return 0
if (T1_len + T2_len = = 0) {
if (AC > 2) {
Z_DVAL_PP (percent) = 0;
}

Return_long (0);
}

Call the above function to calculate a similar library of two strings
SIM = Php_similar_char (T1, T1_len, T2, T2_len);

Can see the calculation formula of the third parameter percent
if (AC > 2) {
Z_DVAL_PP (percent) = sim * 200.0/(T1_len + T2_len);
}

Return_long (SIM);
}

In addition, PHP provides another function Levenshtein () to calculate the similarity of strings, which is a common algorithm by calculating the editing distance of two strings to represent the similarity of strings. The performance of Levenshtein () is better than Similar_text (), as you can see from the previous code analysis that the complexity of Similar_text () is O (n^3), that n represents the length of the longest string, and that Levenshtein () The complexity is O (m*n), and M and N are two string lengths respectively.

http://www.bkjia.com/PHPjc/363811.html www.bkjia.com true http://www.bkjia.com/PHPjc/363811.html techarticle PHP has a function similar_text (), which calculates two string similarity, and can draw a percentage to represent the similarity of two strings. The effect is as follows: Similar_text (' aaaa ', ' AAAA ', $ ...

  • 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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.