首页 > 编程语言 > 详细

自然语言处理词编辑距离计算

时间:2020-05-07 01:59:50      阅读:60      评论:0      收藏:0      [点我收藏+]
#自然语言处理词纠错,编辑距离计算,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‘))

  

自然语言处理词编辑距离计算

原文:https://www.cnblogs.com/BetterThanEver_Victor/p/12840054.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!