计算编辑距离
# -*- coding: utf-8 -*- def distacal(s1,s2):#计算编辑距离 m = len(s1) n = len(s2) colsize, matrix = m + 1, [] for i in range((m + 1) * (n + 1)): matrix.append(0) for i in range(colsize): matrix[i] = i for i in range(n + 1): matrix[i * colsize] = i for i in range(n + 1)[1:n + 1]: for j in range(m + 1)[1:m + 1]: if s1[j - 1] == s2[i - 1]: cost = 0 else: cost = 1 minValue = matrix[(i - 1) * colsize + j] + 1 if minValue > matrix[i * colsize + j - 1] + 1: minValue = matrix[i * colsize + j - 1] + 1 if minValue > matrix[(i - 1) * colsize + j - 1] + cost: minValue = matrix[(i - 1) * colsize + j - 1] + cost matrix[i * colsize + j] = minValue return matrix[n * colsize + m] distance = distacal("你说你是谁","我不知道你是谁") print distance
时间: 2024-10-24 08:00:15