'NameError: name 'wordnet' is not defined
I'm trying to run this piece of code for synonym-based text augmentation.
import nlpaug.augmenter.char as nac
import nlpaug.augmenter.word as naw
import nlpaug.augmenter.sentence as nas
import nlpaug.flow as nafc
from nlpaug.util import Action
text = 'The quick brown fox jumps over the lazy dog .'
aug = naw.SynonymAug(aug_src='wordnet')
augmented_text = aug.augment(text)
Getting this exception
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-30-9e2190c38612> in <module>
----> 1 aug = naw.SynonymAug(aug_src='wordnet')
2 augmented_text = aug.augment(text)
/anaconda/envs/azureml_py36/lib/python3.6/site-packages/nlpaug/augmenter/word/synonym.py in __init__(self, aug_src, model_path, name, aug_min, aug_max, aug_p, lang, stopwords, tokenizer, reverse_tokenizer, stopwords_regex, force_reload, verbose)
62 self.model_path = model_path
63 self.lang = lang
---> 64 self.model = self.get_model(aug_src, lang, model_path, force_reload)
65
66 def skip_aug(self, token_idxes, tokens):
/anaconda/envs/azureml_py36/lib/python3.6/site-packages/nlpaug/augmenter/word/synonym.py in get_model(cls, aug_src, lang, dict_path, force_reload)
136 def get_model(cls, aug_src, lang, dict_path, force_reload):
137 if aug_src == 'wordnet':
--> 138 return nmw.WordNet(lang=lang, is_synonym=True)
139 elif aug_src == 'ppdb':
140 return init_ppdb_model(dict_path=dict_path, force_reload=force_reload)
/anaconda/envs/azureml_py36/lib/python3.6/site-packages/nlpaug/model/word_dict/wordnet.py in __init__(self, lang, is_synonym)
31 nltk.download('averaged_perceptron_tagger')
32
---> 33 self.model = self.read()
34
35 def read(self):
/anaconda/envs/azureml_py36/lib/python3.6/site-packages/nlpaug/model/word_dict/wordnet.py in read (self)
34
35 def read(self):
---> 36 return wordnet
37
38 def predict(self, word, pos=None):
NameError: name 'wordnet' is not defined
I've installed nltk and downloaded wordnet but got same error.
Solution 1:[1]
Restart your kernel.
Assuming like me, you were running on a jupyter notebook, were missing the nltk package and installed it on kernel using !pip install nltk, you would have to restart kernel to make it work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eran Nussinovitch |
