我是靠谱客的博主 谨慎帽子,最近开发中收集的这篇文章主要介绍字符串相似度匹配算法python_字符串相似度算法Python实现,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

写了一个代码, 是解决求最大不含重复字符子字符串的问题的 有兴趣的同仁可以看一看,指出有什么不足的地方 public int Fun(string st) { int count = st.Length; int countSum=1; int sInfo; int eInfo;

#!/usr/bin/env python

def levenshtein(a,b):

"Calculates the Levenshtein distance between a and b."

n, m = len(a), len(b)

if n > m:

# Make sure n <= m, to use O(min(n,m)) space

a,b = b,a

n,m = m,n

current = range(n+1)

for i in range(1,m+1):

previous, current = current, [i]+[0]*n

for j in range(1,n+1):

add, delete = previous[j]+1, current[j-1]+1

change = previous[j-1]

if a[j-1] != b[i-1]:

change = change + 1

current[j] = min(add, delete, change)

return current[n]

def levenshtein_distance(first, second):

"""Find the Levenshtein distance between two strings."""

if len(first) > len(second):

first, second = second, first

if len(second) == 0:

return len(first)

first_length = len(first) + 1

second_length = len(second) + 1

distance_matrix = [range(second_length) for x in range(first_length)]

for i in range(1, first_length):

for j in range(1, second_length):

deletion = distance_matrix[i-1][j] + 1

insertion = distance_matrix[i][j-1] + 1

substitution = distance_matrix[i-1][j-1]

if first[i-1] != second[j-1]:

substitution += 1

distance_matrix[i][j] = min(insertion, deletion, substitution)

return distance_matrix[first_length-1][second_length-1]

if __name__=="__main__":

#from sys import argv

first = "abcabcaa"

second = "cbsaaecaa"

print levenshtein(first,second)

print levenshtein_distance(first,second)

最后

以上就是谨慎帽子为你收集整理的字符串相似度匹配算法python_字符串相似度算法Python实现的全部内容,希望文章能够帮你解决字符串相似度匹配算法python_字符串相似度算法Python实现所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(91)

评论列表共有 0 条评论

立即
投稿
返回
顶部