我用python写了一个代码,并导入了所有需要的库。我解决了所有错误,但仍然出现语法错误。
import enchant
from nltk.metrics import edit_distance
class SpellingReplacer(object):
def __init__(self, dict_name='en', max_dist=2):
self.spell_dict = enchant.Dict(dict_name)
self.max_dist = max_dist
def replace(self, word):
if self.spell_dict.check(word):
return word
suggestions = self.spell_dict.suggest(word)
if suggestions and edit_distance(word, suggestions[0]):
a=self.max_dist
return suggestions[0]
else:
return word
在第二行中,它给出了语法错误,该语法错误是在(else :)语句中使用了无效语法。据我所知,我做到了。另外,w3resourse中的语法 和我的一样。我不知道该怎么解决。请帮助我。
答案 0 :(得分:1)
您的第一个return语句应在if下缩进。当前,您的else语句超出范围。