标签:highlight distance for else style range 自然语言 pytho ==
#自然语言处理词纠错,编辑距离计算,DP算法
def edit_distance(s,p):
if len(s)==0:return len(p)
if len(p)==0:return len(s)
dp=[[0 for i in range(len(p)+1)] for j in range(len(s)+1)]
for i in range(len(s)):
dp[i][len(p)]=len(s)-i
for j in range(len(p)):
dp[len(s)][j]=len(p)-j
dp[len(s)][len(p)]=0
for i in range(len(s)-1,-1,-1):
for j in range(len(p)-1,-1,-1):
if s[i]==p[j]:
dp[i][j]=dp[i+1][j+1]
else:
dp[i][j] =1+min(dp[i+1][j+1],dp[i][j+1],dp[i+1][j])
return dp[0][0]
print(edit_distance(‘apdp‘,‘app‘))
标签:highlight distance for else style range 自然语言 pytho ==
原文地址:https://www.cnblogs.com/BetterThanEver_Victor/p/12840054.html