Instructions for use
First read the description of the levenshtein() function in the manual:
The levenshtein() function returns the Levenshtein distance between two strings.
Levenshtein distance, also known as edit distance, refers to the minimum number of edit operations required between two strings to convert one into the other. Permitted editing operations include replacing one character with another, inserting a character, and deleting a character.
For example, convert kitten to sitting:
sitten (k→s)
sittin (e→i)
sitting (→g) levenshtein() function gives equal weight to each operation (replacement, insertion and deletion). However, you can define the cost of each operation by setting the optional insert, replace, and delete parameters.
Grammar:
levenshtein(string1,string2,insert,replace,delete)
Parameter Description
string1 required. The first string to compare.
string2 required. The second string to compare.
insert is optional. The cost of inserting a character. The default is 1.
replace optional. The cost of replacing a character. The default is 1.
delete optional. The cost of deleting a character. The default is 1.
Tips and Notes
If one of the strings exceeds 255 characters, the levenshtein() function returns -1.
The levenshtein() function is not case sensitive.
levenshtein() function is faster than similar_text() function. However, the similar_text() function provides more accurate results that require fewer modifications.
Example
Source code analysis
levenshtein() is a standard function, and there is a file specifically implemented for this function in the /ext/standard/ directory: levenshtein.c.
levenshtein() will select the implementation method based on the number of parameters. For the cases where the parameter is 2 and the parameter is 5, the reference_levdist() function will be called to calculate the distance. The difference is that for the last three parameters, when the parameter is 2, the default value 1 is used.
And in the implementation source code, we found a situation that was not explained in the documentation: the levenshtein() function can also pass three parameters, which will eventually call the custom_levdist() function. It takes the third parameter as the implementation of a custom function. The calling example is as follows:
The implementation algorithm of the reference_levdist() function is a classic DP problem.
Given two strings x and y, find the minimum number of modifications to change x into y. The modified rules can only be one of the following three types: deletion, insertion, or change.
Use a[i][j] to represent the minimum number of operations required to change the first i characters of x into the first j characters of y. Then the state transition equation is:
The simple implementation process is as follows:
Levenshtein distance description
Levenshtein distance was first invented by Russian scientist Vladimir Levenshtein in 1965 and named after him. If you don’t know how to pronounce it, you can call it edit distance. Levenshtein distance can be used for:
Spell checking (spelling check)
Speech recognition (sentence recognition)
DNA analysis (DNA analysis)
Plagiarism detection (plagiarism detection) LD uses a mn matrix to store distance values .