Last active
December 17, 2020 02:59
-
-
Save Plotist/92f4ebb262fdac45f891969b66d0c143 to your computer and use it in GitHub Desktop.
Edit distance calculation [ Ruby ]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Analyzer | |
class EditDistance | |
attr_accessor :word1, :word2, :matrix | |
def initialize(word1, word2) | |
@word1 = word1 | |
@word2 = word2 | |
@w1l = @word1.length | |
@w2l = @word2.length | |
@matrix = Array.new(@w1l+1){Array.new(@word2.length+1){|i| 0}} | |
end | |
def calculate_matrix | |
for j in 0..(@w2l) | |
@matrix[0][j] = j | |
end | |
for i in 0..(@w1l) | |
@matrix[i][0] = i | |
end | |
for i in 1..(@w1l) | |
for j in 1..(@w2l) | |
@matrix[i][j] = [[@matrix[i][j-1]+1, @matrix[i-1][j]+1].min, @matrix[i-1][j-1] + compare_symbs(@word1[i-1],@word2[j-1]) ].min | |
end | |
end | |
@range = @matrix[@w1l][@w2l] | |
end | |
def get_range | |
@range | |
end | |
private | |
def compare_symbs(a,b) | |
a==b ? 0 : 1 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment