Ruby实现的最短编辑距离计算方法

利用动态规划算法,实现最短编辑距离的计算。


#encoding: utf-8
#author: xu jin
#date: Nov 12,2012
#Editdistance
#to find the minimum cost by using Editdistance algorithm
#example output:
#  "Please input a string: "
#  exponential
#  "Please input the other string: "
polynomial
#  "The expected cost is 6"
#  The result is :
#    ["e","x","p","o","n","e","-","t","i","a","l"]
#    ["-","l","y","m","l"]

p "Please input a string: "
x = gets.chop.chars.map{|c| c}
p "Please input the other string: "
y = gets.chop.chars.map{|c| c}
x.unshift(" ")
y.unshift(" ")
e = Array.new(x.size){Array.new(y.size)}
flag = Array.new(x.size){Array.new(y.size)}
DEL,INS,CHA,FIT = (1..4).to_a  #deleat,insert,change,and fit
 
def edit_distance(x,y,e,flag)
  (0..x.length - 1).each{|i| e[i][0] = i}
  (0..y.length - 1).each{|j| e[0][j] = j}
  diff = Array.new(x.size){Array.new(y.size)}
  for i in(1..x.length - 1) do
    for j in(1..y.length - 1) do
      diff[i][j] = (x[i] == y[j])? 0: 1
      e[i][j] = [e[i-1][j] + 1,e[i][j - 1] + 1,e[i-1][j - 1] + diff[i][j]].min
      if e[i][j] == e[i-1][j] + 1
        flag[i][j] = DEL
      elsif e[i][j] == e[i-1][j - 1] + 1
        flag[i][j] = CHA
      elsif e[i][j] == e[i][j - 1] + 1
        flag[i][j] = INS      
      else flag[i][j] = FIT
      end    
    end
  end 
end

out_x,out_y = [],[]

def solution_structure(x,flag,i,j,out_x,out_y)
  case flag[i][j]
  when FIT
    out_x.unshift(x[i])
    out_y.unshift(y[j]) 
    solution_structure(x,i - 1,j - 1,out_y)
  when DEL
    out_x.unshift(x[i])
    out_y.unshift('-')
    solution_structure(x,out_y)
  when INS
    out_x.unshift('-')
    out_y.unshift(y[j])
    solution_structure(x,out_y)
  when CHA
    out_x.unshift(x[i])
    out_y.unshift(y[j])
    solution_structure(x,out_y)
  end
  #if flag[i][j] == nil,go here
  return if i == 0 && j == 0   
  if j == 0
      out_y.unshift('-')
      out_x.unshift(x[i])
      solution_structure(x,out_y)
  elsif i == 0
      out_x.unshift('-')
      out_y.unshift(y[j])
      solution_structure(x,out_y)
  end
end

edit_distance(x,flag)
p "The expected edit distance is #{e[x.length - 1][y.length - 1]}"
solution_structure(x,x.length - 1,y.length - 1,out_y)
puts "The result is : \n  #{out_x}\n  #{out_y}"


相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...