I found this ruby method on a wiki that finds the number of differences between two strings.
def levenshtein(first, second)
matrix = [(0..first.length).to_a]
(1..second.length).each do |j|
matrix << [j] + [0] * (first.length)
end
(1..second.length).each do |i|
(1..first.length).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
How can I edit it so that it also returns the physical differences between the two strings, meaning that if I were to enter "test test test test" and "test tebt test test" it would return "test te/b/t test test" with slashes around the difference so that i can display it differently. (I still want the number as well.)