我正在尝试编写一个函数来检查一个单词是否在字符串中,或者该单词是否与字符串中的每个单词有len(word)-1
个共同的字符。你知道吗
例如:
word: match string: There is a match -> True
word: matck string: There is a match -> True
对于这两个示例,输出都必须为True,因为matck-1=matc
和match-1=matc
到目前为止,我已经编写了以下代码:
for idx, f in enumerate(files):
for word in words:
if term in f:
numOfWord[idx] += 1
else:
file_words = f.split()
for f_word in file_words:
if word[:-1] == file_word[:-1]:
numOfWords[idx] += 1
但这并不好,因为我有一个非常大的word列表和非常大的长文件目录,所以运行时间是不现实的。你知道吗
你可以用Levenshtein距离来检查
https://rosettacode.org/wiki/Levenshtein_distance#Python
相关问题 更多 >
编程相关推荐