当前位置: 首页 > 编程笔记 >

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

端木承业
2023-03-14
本文向大家介绍Ruby实现的最短编辑距离计算方法,包括了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", "n", "-", "t", "i", "a", "l"]

#    ["-", "-", "p", "o", "l", "y", "n", "o", "m", "i", "a", "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, y, 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, y, flag, i - 1, j - 1, out_x, out_y)   when DEL     out_x.unshift(x[i])     out_y.unshift('-')     solution_structure(x, y, flag, i - 1, j, out_x, out_y)   when INS     out_x.unshift('-')     out_y.unshift(y[j])     solution_structure(x, y, flag, i, j - 1, out_x, out_y)   when CHA     out_x.unshift(x[i])     out_y.unshift(y[j])     solution_structure(x, y, flag, i - 1, j - 1, out_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, y, flag, i - 1, j, out_x, out_y)   elsif i == 0       out_x.unshift('-')       out_y.unshift(y[j])       solution_structure(x, y, flag, i, j - 1, out_x, out_y)   end end

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


 类似资料:
  • 这道题是 LeetCode 72 题。 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数。你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 动态规划 解决两个字符串的动态规划问题,一般都是用两个指针 i,j 分别指向两个字符串的最后,然后一步步往前走,缩小问题的规模。 定义状态:dp[i][j] 表示 s1、s2 长

  • 本文向大家介绍编辑距离相关面试题,主要包含被问及编辑距离时的应答技巧和注意事项,需要的朋友参考一下 参考回答: 概念 编辑距离的作用主要是用来比较两个字符串的相似度的 编辑距离,又称Levenshtein距离(莱文斯坦距离也叫做Edit Distance),是指两个字串之间,由一个转成另一个所需的最少编辑操作次数,如果它们的距离越大,说明它们越是不同。许可的编辑操作包括将一个字符替换成另一个字符,

  • 本文向大家介绍Java实现的计算最大下标距离算法示例,包括了Java实现的计算最大下标距离算法示例的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Java实现的计算最大下标距离算法。分享给大家供大家参考,具体如下: 题目描述 给定一个整形数组,找出最大下标距离j−i, 当且A[i] < A[j] 和 i < j 解法 复杂度:三次扫描,每次的复杂度O(N) 算法:{5,3,4,0,1,4,

  • 我有一个多边形类型的几何体,我正在计算一个点的最小距离可能在多边形几何体内部(由360个点组成,作为闭合几何体)或多边形几何体外部。使用postgis的ST_distance方法,当点在几何体外部时,我得到精确的距离,但如果点在几何体内部,则得到0作为距离,我想要与多边形几何体最近点的点之间的最小距离,无论该点位于几何体内部还是外部。

  • 我需要计算汽车行驶的距离!不是距离,不是距离到否。如果我们通过谷歌提供的API计算,距离可以完全不同。谷歌可以提供从一个点到另一个点的1公里距离,但汽车可以按照骑手想要的方式行驶800米。使用加速计没有帮助。它适用于步行,但绝不适用于更快的速度。 我尝试过使用Google的位置API:距离到或距离之间根本不是一个选项。它可以给出与IN REAL截然不同的结果。在真实的汽车中,可以通过非常短的地方并

  • 本文向大家介绍Python文本相似性计算之编辑距离详解,包括了Python文本相似性计算之编辑距离详解的使用技巧和注意事项,需要的朋友参考一下 编辑距离 编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数。编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。一般来说,编辑距离越小,两个串的相似度越大。 例