from difflib import SequenceMatcher
sentences = []
with open('./bp.txt', 'r') as f:
for line in f:
# only consider lines that have numbers at the beginning
if line.split('.')[0].isdigit():
sentences.append(line.split('\n')[0])
max_prob = 0
similar_sentence = None
length = len(sentences)
for i in range(length):
for j in range(i+1,length):
match_ratio = SequenceMatcher(None, sentences[i], sentences[j]).ratio()
if match_ratio > max_prob:
max_prob = match_ratio
similar_sentence = sentences[j]
if similar_sentence is not None:
print(similar_sentence)
我同意约翰·科尔曼的建议。
difflib
可以帮助您找到两个字符串之间的相似性度量。以下是一种可能的方法:相关问题 更多 >
编程相关推荐