Python allows you to calculate the minimum editing distance and the minimum python editing distance.

Source: Internet
Author: User

Python allows you to calculate the minimum editing distance and the minimum python editing distance.

Levenshtein refers to the minimum number of edits that are converted from string A to string B. The following editing operations are allowed: delete, insert, and replace. For more information, see Levenstein distance in Wikipedia. Generally, the code is implemented by using the dynamic planning algorithm to find the minimum step for each step from A to B. Images borrowed from Google images,

Python code implementation (note that the subscript of the matrix starts from 1, and the subscript of the string starts from 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]

Recently, I saw that the Python Library provides A package difflib to convert object B from object A. The code for calculating the minimum editing distance can also be written as follows:

 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

Code address

Articles you may be interested in:
  • Python distance calculation example based on latitude and longitude
  • Example of calculating the configuration speed based on distance and duration in python
  • Python calculates the word Distance Based on the Dynamic Planning Algorithm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.