我对某些Python代码有点麻烦。我有一个名为“big.txt”的大文本文件。我在我的代码中迭代了它,将每个单词排序到一个数组(或列表)中,然后再次遍历它以删除任何不在字母表中的字符。我也有一个叫worddistance
的函数,看看两个单词有多相似,然后返回一个分数。我有另一个功能叫autocorrect
。我想通过这个函数拼写错误的单词,并打印'Did you mean...'
句子,在worddistance
函数上得分较低的单词(只要注意到差异,函数就会将计数值加1) - 分数越低,则越相似)。
奇怪的是,我不断收到错误:比较字符串时出现索引错误 - Python
"Index Error: string index out of range"
我处发生了什么损失!
我的代码如下。
在此先感谢您的答复,
塞缪尔·诺顿
f = open("big.txt", "r")
words = list()
temp_words = list()
for line in f:
for word in line.split():
temp_words.append(word.lower())
allowed_characters = 'abcdefghijklmnopqrstuvwxyz'
for item in temp_words:
temp_new_word = ''
for char in item:
if char in allowed_characters:
temp_new_word += char
else:
continue
words.append(temp_new_word)
list(set(words)).sort()
def worddistance(word1, word2):
counter = 0
if len(word1) > len(word2):
counter += len(word1) - len(word2)
new_word1 = word1[:len(word2) + 1]
for char in range(0, len(word2) + 1) :
if word2[char] != new_word1[char]:
counter += 1
else:
continue
elif len(word2) > len(word1):
counter += len(word2) - len(word1)
new_word2 = word2[:len(word1) + 1]
for char in range(0, len(word1) + 1):
if word1[char] != word2[char]:
counter += 1
else:
continue
return counter
def autocorrect(word):
word.lower()
if word in words:
print("The spelling is correct.")
return
else:
suggestions = list()
for item in words:
diff = worddistance(word, item)
if diff == 1:
suggestions.append(item)
print("Did you mean: ", end = ' ')
if len(suggestions) == 1:
print(suggestions[0])
return
else:
for i in range(0, len(suggestions)):
if i == len(suggestons) - 1:
print("or " + suggestions[i] + "?")
return
print(suggestions[i] + ", ", end="")
return
在哪一行你得到这个错误 – user3378649 2014-11-02 20:40:32