Python实现计算最小编辑距离

yipeiwu_com6年前Python基础

最小编辑距离或莱文斯坦距离(Levenshtein),指由字符串A转化为字符串B的最小编辑次数。允许的编辑操作有:删除,插入,替换。具体内容可参见:维基百科—莱文斯坦距离。一般代码实现的方式都是通过动态规划算法,找出从A转化为B的每一步的最小步骤。从Google图片借来的图,

Python代码实现, (其中要注意矩阵的下标从1开始,而字符串的下标从0开始):

 def normal_leven(str1, str2):
   len_str1 = len(str1) + 1
   len_str2 = len(str2) + 1
   #create matrix
   matrix = [0 for n in range(len_str1 * len_str2)]
   #init x axis
   for i in range(len_str1):
     matrix[i] = i
   #init y axis
   for j in range(0, len(matrix), len_str1):
     if j % len_str1 == 0:
       matrix[j] = j // len_str1

   for i in range(1, len_str1):
     for j in range(1, len_str2):
       if str1[i-1] == str2[j-1]:
         cost = 0
       else:
         cost = 1
       matrix[j*len_str1+i] = min(matrix[(j-1)*len_str1+i]+1,
                     matrix[j*len_str1+(i-1)]+1,
                     matrix[(j-1)*len_str1+(i-1)] + cost)

   return matrix[-1]

最近看文章看到Python库提供了一个包difflib实现了从对象A转化对象B的步骤,那么计算最小编辑距离的代码也可以这样写了:

 def difflib_leven(str1, str2):
  leven_cost = 0
  s = difflib.SequenceMatcher(None, str1, str2)
  for tag, i1, i2, j1, j2 in s.get_opcodes():
    #print('{:7} a[{}: {}] --> b[{}: {}] {} --> {}'.format(tag, i1, i2, j1, j2, str1[i1: i2], str2[j1: j2]))

    if tag == 'replace':
      leven_cost += max(i2-i1, j2-j1)
    elif tag == 'insert':
      leven_cost += (j2-j1)
    elif tag == 'delete':
      leven_cost += (i2-i1)
  return leven_cost

代码地址

相关文章

numpy.linspace函数具体使用详解

numpy.linspace函数具体使用详解

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) 在指定的间隔内返回均匀间隔的数...

python bottle框架支持jquery ajax的RESTful风格的PUT和DELETE方法

python bottle框架支持jquery ajax的RESTful风格的PUT和DELETE方法

这两天在用python的bottle框架开发后台管理系统,接口约定使用RESTful风格请求,前端使用jquery ajax与接口进行交互,使用POST与GET请求时都正常,而Reque...

python批量识别图片指定区域文字内容

Python批量识别图片指定区域文字内容,供大家参考,具体内容如下 简介 对于一张图片,需求识别指定区域的内容 1.截取原始图上的指定图片当做模板 2.根据模板相似度去再原始图片上识别准...

TensorFlow dataset.shuffle、batch、repeat的使用详解

直接看代码例子,有详细注释!! import tensorflow as tf import numpy as np d = np.arange(0,60).reshape([6...

使用python实现男神女神颜值打分系统(推荐)

使用python实现男神女神颜值打分系统(推荐)

先给大家展示效果图,感觉不错,请参考实现代码。 具体代码如下所示: #!/usr/bin/env python # -*- coding:utf-8 -*- """ pip in...