So I'm aware that Levenshtein Distance algorithm takes into account the minimum number of deletions, insertions and substitutions required to change a String A into String B. But, I was wondering how you can separately keep track of number of deletions in the total edits required to make the change. I was looking at this implementation of the algorithm,
def levenshtein(first, second)
first = first.split
second = second.split
first_size = first.size
second_size = second.size
matrix = [(0..first_size).to_a]
(1..second_size ).each do |j|
matrix << [j] + [0] * (first_size)
end
count = 0
(1..second_size).each do |i|
(1..first_size).each do |j|
if first[j-1] == second[i-1]
matrix[i][j] = matrix[i-1][j-1]
else
matrix[i][j] = [matrix[i-1][j],matrix[i][j-1], matrix[i-1][j-1]].min + 1
end
end
end
return matrix.last.last
end
So in order to keep track of deletions, I tried:
if matrix[i-1[j] == [matrix[i-1][j],matrix[i][j-1], matrix[i-1][j-1]].min
then increase the count. But, this doesn't seem to work. I also tried to get the difference in size for two strings but it fails for the following case
String 1: "my response to prompt#1"
String 2: "my edited response to"
There is clearly 1 deletion here but simply getting the difference in size won't detect so.
I was wondering if anyone knows how to keep track of number of deletions that were involved in the total edits for changing string A into string B.
"my response to prompt#1", "my response to"
, we have a deletion of"prompt#1"
– kchoi"my response to prompt#1", "my edited response to prompt#1"
should return 1 because you addededited.
Case 2:"haha bob", "haha bobber"
should return 1 because you substituted bob with bobber. Case 3:"my response to prompt#1", "my response to"
should return -1 because there was 1 deletion. So this is what I want to do, return negative number when it is deleting. – kchoi