Normalizing the edit distance
Solution 1
Yes, normalizing the edit distance is one way to put the differences between strings on a single scale from "identical" to "nothing in common".
A few things to consider:
- Whether or not the normalized distance is a better measure of similarity between strings depends on the application. If the question is "how likely is this word to be a misspelling of that word?", normalization is a way to go. If it's "how much has this document changed since the last version?", the raw edit distance may be a better option.
- If you want the result to be in the range
[0, 1]
, you need to divide the distance by the maximum possible distance between two strings of given lengths. That is,length(str1)+length(str2)
for the LCS distance andmax(length(str1), length(str2))
for the Levenshtein distance. - The normalized distance is not a metric, as it violates the triangle inequality.
Solution 2
I used the following successfully:
len = std::max(s1.length(), s2.length());
// normalize by length, high score wins
fDist = float(len - levenshteinDistance(s1, s2)) / float(len);
Then chose the highest score. 1.0 means an exact match.
Naufal Khalid
Updated on June 08, 2022Comments
-
Naufal Khalid almost 2 years
I have a question that can we normalize the levenshtein edit distance by dividing the e.d value by the length of the two strings? I am asking this because, if we compare two strings of unequal length, the difference between the lengths of the two will be counted as well. for eg: ed('has a', 'has a ball') = 4 and ed('has a', 'has a ball the is round') = 15. if we increase the length of the string, the edit distance will increase even though they are similar. Therefore, I can not set a value, what a good edit distance value should be.