我在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和我的语法是一样的。我不知道如何解决它。请帮助我。
你的第一个return语句应该在if下缩进。目前,您的else语句超出了范围。