Reputation: 13
I'm trying to build a spell corrector but it's not working correctly.
Code from: https://www.kaggle.com/yk1598/symspell-spell-corrector/script
def spell_corrector(word_list, words_d) -> str:
result_list = []
for word in word_list:
if word not in words_d:
suggestion = ss.best_word(word, silent=True)
if suggestion is not None:
result_list.append(suggestion)
else:
result_list.append(word)
return " ".join(result_list)
def spell_check(text):
ss = SymSpell(max_edit_distance=2)
with open('corpus.txt') as bf:
words = bf.readlines()
chatbot_words = [word.strip() for word in words]
all_words_list = list(set(chatbot_words))
silence = ss.create_dictionary_from_arr(all_words_list, token_pattern=r'.+')
words_dict = {k: 0 for k in all_words_list}
tokens = spacy_tokenize(message)
correct_text = spell_corrector(tokens, words_dict)
return correct_text
message = input("")
spell_check(message)
The error I got was this:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-8-8d527844467a> in <module>()
330 message = input("")
--> 331 spell_check(message)
1 frames
<ipython-input-8-8d527844467a> in spell_corrector(word_list, words_d)
301 for word in word_list:
302 if word not in words_d:
--> 303 suggestion = ss.best_word(word, silent=True)
304 if suggestion is not None:
305 result_list.append(suggestion)
NameError: name 'ss' is not defined
I can't figure out the error can someone help me out on this? I've already tried putting
ss = SymSpell(max_edit_distance=2)
Under the spell_corrector
function but it returns the incorrect input words instead of the corrected text
Upvotes: 1
Views: 415
Reputation: 5223
Try defining ss
variable outside the function like this:
ss = SymSpell(max_edit_distance=2)
def spell_corrector(word_list, words_d) -> str:
result_list = []
for word in word_list:
if word not in words_d:
suggestion = ss.best_word(word, silent=True)
if suggestion is not None:
result_list.append(suggestion)
else:
result_list.append(word)
return " ".join(result_list)
def spell_check(text):
with open('corpus.txt') as bf:
words = bf.readlines()
chatbot_words = [word.strip() for word in words]
all_words_list = list(set(chatbot_words))
silence = ss.create_dictionary_from_arr(all_words_list, token_pattern=r'.+')
words_dict = {k: 0 for k in all_words_list}
tokens = spacy_tokenize(message)
correct_text = spell_corrector(tokens, words_dict)
return correct_text
message = input("")
spell_check(message)
Upvotes: 1