Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何计算两个.txt文件之间的Levenshtein距离?_Python_Linux_Levenshtein Distance - Fatal编程技术网

Python 如何计算两个.txt文件之间的Levenshtein距离?

Python 如何计算两个.txt文件之间的Levenshtein距离?,python,linux,levenshtein-distance,Python,Linux,Levenshtein Distance,有标准的linux命令吗?如果没有,谁能描述一个python脚本来做同样的事情呢?我不建议这样做。 Levenshtein距离函数的复杂度几乎为O(n*m),当文本相似时,其复杂度为O(n²) 但是如果你想你可以做。。。 pip安装python Levenshtein 代码是这样的: 从Levenshtein导入* txt1=open(“text1.txt”).read() txt2=open(“text2.txt”).read() 打印(“距离:”,距离(txt1,txt2)) 视情况而定。当

有标准的linux命令吗?如果没有,谁能描述一个python脚本来做同样的事情呢?

我不建议这样做。 Levenshtein距离函数的复杂度几乎为O(n*m),当文本相似时,其复杂度为O(n²)

但是如果你想你可以做。。。
pip安装python Levenshtein

代码是这样的:

从Levenshtein导入*
txt1=open(“text1.txt”).read()
txt2=open(“text2.txt”).read()
打印(“距离:”,距离(txt1,txt2))

视情况而定。当ocr输出相似且存在一些预期差异时,您可以进行“拆分”并比较每个单词/行等。 并且仅对线数量相同时出现差异的零件使用levenshtein距离。例如:

def textLevi(txt1,txt2):
   lines = list(zip(txt1.split("\n"),txt2.split("\n")))
   distance = 0
   for i,ele in enumerate(lines,1):
        line1,line2 = ele
       if line1 != line2:
           actDistance = distance(line1,line2)
           print( "Distance of line %d: " %(i),actDistance)
           distance += actDistance


   print( "Sum of Lv Distances:",distance)
 
textLevi("Hello I \n like cheese","Hello I \n like cheddar")
将创建输出:

2号线距离:4

低压距离总和:4

伟大的工作:)。你有更好的方法来比较两个文件吗?我正在比较两个ocr程序的输出。