Questions about the normalization of similar_text when calculating similarity

Source: Internet
Author: User
When similar_text is used to calculate similarity normalization, when I calculate the length of two strings, it seems that the normalization function adopts different methods.
For the first time, I tried two strings of different lengths to calculate the editing distance:
Echo "levenshtein calculation: \ n"; echo levenshtein ("seller_id", "selr_id"); echo "\ n ";
The result is: 2.

Use the same two strings and the similar_text function of PHP to calculate the similarity.
Echo "similar_text calculation: \ n"; similar_text ("seller_id", "selr_id", $ percent );
Echo $ percent;
The similarity is as follows: 87.5
When the distance 2 is normalized, the formula is exactly as follows: 1-(edit distance/(the sum of the two strings ))

The second time, I tried two strings of the same length to calculate the editing distance and similarity respectively.
Similar_text ("abcd", "1234", $ percent); echo $ percent; echo "\ n ";
Echo levenshtein ("abcd", "1234 ");
The obtained values are 4 and 0, respectively.
Exactly match the formula: 1-(edit distance/(length of any string ))

My question is: why is the denominator the sum of the lengths of two strings when similarity is obtained for two strings of different lengths?
I found some pdf documents on the internet. when the editing distance is normalized, the denominator is the longest length of the string.


Reply to discussion (solution)

The second result is. you have reversed it and misled the masses.

The second result is. you have reversed it and misled the masses.


Oh, yes. I reversed it. thank you for pointing it out.

Incorrect Description. Questions about normalization are still found ~~

Are your conclusions universal?

Not long

$str1= "esca";$str2= "bca";echolevenshtein($str1,$str2), "\n";similar_text($str1,$str2,$percent);echo$percent;/*257.142857142857*/

Same length
$str1= "esca";$str2= "sbca";echolevenshtein($str1,$str2), "\n";similar_text($str1,$str2,$percent);echo$percent;/*275*/

Well, the similar_text algorithm is different from the theory and seems to be weighted.
I don't know which one can bother finding out the source code
In the manual, it is implemented using recursion, and the code should not be too long.

#define LEVENSHTEIN_MAX_LENGTH 255/* {{{ reference_levdist * reference implementation, only optimized for memory usage, not speed */static int reference_levdist(const char *s1, int l1, const char *s2, int l2, int cost_ins, int cost_rep, int cost_del ){int *p1, *p2, *tmp;int i1, i2, c0, c1, c2;if (l1 == 0) {return l2 * cost_ins;}if (l2 == 0) {return l1 * cost_del;}if ((l1 > LEVENSHTEIN_MAX_LENGTH) || (l2 > LEVENSHTEIN_MAX_LENGTH)) {return -1;}p1 = safe_emalloc((l2 + 1), sizeof(int), 0);p2 = safe_emalloc((l2 + 1), sizeof(int), 0);for (i2 = 0; i2 <= l2; i2++) {p1[i2] = i2 * cost_ins;}for (i1 = 0; i1 < l1 ; i1++) {p2[0] = p1[0] + cost_del;for (i2 = 0; i2 < l2; i2++) {c0 = p1[i2] + ((s1[i1] == s2[i2]) ? 0 : cost_rep);c1 = p1[i2 + 1] + cost_del;if (c1 < c0) {c0 = c1;}c2 = p2[i2] + cost_ins;if (c2 < c0) {c0 = c2;}p2[i2 + 1] = c0;}tmp = p1;p1 = p2;p2 = tmp;}c0 = p1[l2];efree(p1);efree(p2);return c0;}/* }}} *//* {{{ custom_levdist */static int custom_levdist(char *str1, char *str2, char *callback_name TSRMLS_DC){php_error_docref(NULL TSRMLS_CC, E_WARNING, "The general Levenshtein support is not there yet");/* not there yet */return -1;}/* }}} *//* {{{ proto int levenshtein(string str1, string str2[, int cost_ins, int cost_rep, int cost_del])   Calculate Levenshtein distance between two strings */PHP_FUNCTION(levenshtein){int argc = ZEND_NUM_ARGS();char *str1, *str2;char *callback_name;int str1_len, str2_len, callback_len;long cost_ins, cost_rep, cost_del;int distance = -1;switch (argc) {case 2: /* just two strings: use maximum performance version */if (zend_parse_parameters(2 TSRMLS_CC, "ss", &str1, &str1_len, &str2, &str2_len) == FAILURE) {return;}distance = reference_levdist(str1, str1_len, str2, str2_len, 1, 1, 1);break;case 5: /* more general version: calc cost by ins/rep/del weights */if (zend_parse_parameters(5 TSRMLS_CC, "sslll", &str1, &str1_len, &str2, &str2_len, &cost_ins, &cost_rep, &cost_del) == FAILURE) {return;}distance = reference_levdist(str1, str1_len, str2, str2_len, cost_ins, cost_rep, cost_del);break;case 3: /* most general version: calc cost by user-supplied function */if (zend_parse_parameters(3 TSRMLS_CC, "sss", &str1, &str1_len, &str2, &str2_len, &callback_name, &callback_len) == FAILURE) {return;}distance = custom_levdist(str1, str2, callback_name TSRMLS_CC);break;default:WRONG_PARAM_COUNT;}if (distance < 0 && /* TODO */ ZEND_NUM_ARGS() != 3) {php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument string(s) too long");}RETURN_LONG(distance);}/* }}} */




/* {{{ proto int similar_text(string str1, string str2 [, float percent])   Calculates the similarity between two strings */PHP_FUNCTION(similar_text){char *t1, *t2;zval **percent = NULL;int ac = ZEND_NUM_ARGS();int sim;int t1_len, t2_len;if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|Z", &t1, &t1_len, &t2, &t2_len, &percent) == FAILURE) {return;}if (ac > 2) {convert_to_double_ex(percent);}if (t1_len + t2_len == 0) {if (ac > 2) {Z_DVAL_PP(percent) = 0;}RETURN_LONG(0);}sim = php_similar_char(t1, t1_len, t2, t2_len);if (ac > 2) {Z_DVAL_PP(percent) = sim * 200.0 / (t1_len + t2_len);}RETURN_LONG(sim);}static int php_similar_char(const char *txt1, int len1, const char *txt2, int len2){int sum;int pos1, pos2, max;php_similar_str(txt1, len1, txt2, len2, &pos1, &pos2, &max);if ((sum = max)) {if (pos1 && pos2) {sum += php_similar_char(txt1, pos1,txt2, pos2);}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;}

Another difference

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;
For (p = (char *) txt1; p <end1; p ++ ){
For (q = (char *) txt2; q <end2; q ++ ){
For (l = 0; (p + l <end1) & (q + l <end2) & (p [l] = q [l]); l ++ );
If (l> * max ){
* Max = l;
* Pos1 = p-txt1;
* Pos2 = q-txt2;
}
}
}
}

PHP_FUNCTION (similar_text)
In
Sim = php_similar_char (t1, t1_len, t2, t2_len );

Z_DVAL_PP (percent) = sim * 200.0/(t1_len + t2_len); // calculate similarity
And
Return sum; // returns the number of matching characters for two strings.

It is a little different.
Sim * 200.0/(t1_len + t2_len)
It indicates the percentage of matched characters to the average length of the source string.
There is not much difference from the 1-edit distance/the maximum length of the source string

Another difference

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 *)......

In the manual, the complexity is O (N ** 3). does it seem like this function?
Is the recursion of php_similar_char not computed in complexity?

It should be said that the designers of the similar_text function are considerate.
When the length of the two strings is the same, the similarity calculated is no different from that calculated theoretically.
When the length of the Two Strings is different, the similarity is not as steep as in theory. That is to say, the probability of matching increases.
Of course, if you do not want this, you can calculate it by yourself. the strings are yours and the matched quantity is returned. It is not difficult to calculate

Is it possible to consider the complexity?

The complexity of php_similar_char. the ideal condition is logN, but the worst is N.


For the php_similar_str function
For (l = 0; (p + l <end1) & (q + l <end2) & (p [l] = q [l]); l ++); statement

This statement is a loop, but it is related to the preceding php_similar_char. This will remove the longest string of the same

Each time the longest string is found + 1, the outer layer is recursive, and the worst complexity is-1.



In addition, there are two different algorithms. I don't know why lz is looking for such a rule. I think you will be futile. Different algorithms

PHP_FUNCTION (similar_text)
In
Sim = php_similar_char (t1, t1_len, t2, t2_len );

Z_DVAL_PP (percent) = sim * 200.0/(t1_len + t2_len); // calculate similarity
And
Return sum; // returns the number of matching characters for two strings.

It is a little different.
Sim * 200 ......


I cannot understand the sim. how can I get the number of matched characters?
Taking seller_id and selr_id as examples, how can we compare the sim values of seller_id and selr_id through similarity inversion? Could you please elaborate?

It should be said that the designers of the similar_text function are considerate.
When the length of the two strings is the same, the similarity calculated is no different from that calculated theoretically.
When the length of the Two Strings is different, the similarity is not as steep as in theory. That is to say, the probability of matching increases.
Of course, if you do not want this, you can calculate it by yourself. the strings are yours and the matched quantity is returned. It is not difficult to calculate


"When the length of the Two Strings is different, the similarity is not as steep as in theory." The theory here refers to 1-(editing distance/(the length of the longest string )) this?
$ Str1 = "esca ";
$ Str2 = "sbca ";
If the value is calculated as 1-(the length of the longest string), the value is 50, and the value is 75.
It does get bigger. But why? what is the basis for this?

Isn't it? the probability of matching increases.
In many cases, the input matching condition has a large error.
Strict filtering conditions for multiple points can only be ineffective

What is the basis? Practice
Theory is based on practice and cannot be used to constrain practice.

Isn't it? the probability of matching increases.
In many cases, the input matching condition has a large error.
Strict filtering conditions for multiple points can only be ineffective

What is the basis? Practice
Theory is based on practice and cannot be used to constrain practice.


Well, what you mean is that in practice, the similarity that people intuitively feel is generally greater than 1-the maximum length of the editing distance/source string ~~~
In addition, what is the sim value? the recursive code is really understandable .. Taking seller_id and selr_id as examples, the sim value is 7. can you tell me how to get the sim value of 7? Thank you.

Similar_text calculation is independent of the editing distance.
The similar_text calculation method is
The ratio of the longest similarity length of two characters to the half of the length of two strings

$ Max_similar_len = 0;
$ Percent = 0;
$ Max_similar_len = similar_text ($ string1, $ string2, $ percent );
$ Perc = $ max_similar_len * 2/(strlen ($ string1) + strlen ($ string2 ));
$ Perc is equal to $ percent.

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.