NLTK wordnet not found

786 views Asked by At

I'm learning nlp with NLTK and one step is using WordNetLemmatizer, so i've downloaded it. But it says wordnet was not found when i run my code.

Console (Error)

PS C:\Users\renat\Downloads\myapp> & C:/Users/renat/AppData/Local/Programs/Python/Python310/python.exe c:/Users/renat/Downloads/myapp/nlp_test.py
Traceback (most recent call last):
  File "C:\Users\renat\AppData\Local\Programs\Python\Python310\lib\site-packages\nltk\corpus\util.py", line 80, in __load
    try: root = nltk.data.find('{}/{}'.format(self.subdir, zip_name))
  File "C:\Users\renat\AppData\Local\Programs\Python\Python310\lib\site-packages\nltk\data.py", line 675, in find        
    raise LookupError(resource_not_found)
LookupError: 
**********************************************************************
  Resource wordnet not found.
  Please use the NLTK Downloader to obtain the resource:

  >>> import nltk
  >>> nltk.download('wordnet')
  
  Searched in:
    - 'C:\\Users\\renat/nltk_data'
    - 'C:\\nltk_data'
    - 'D:\\nltk_data'
    - 'E:\\nltk_data'
    - 'C:\\Users\\renat\\AppData\\Local\\Programs\\Python\\Python310\\nltk_data'
    - 'C:\\Users\\renat\\AppData\\Local\\Programs\\Python\\Python310\\share\\nltk_data'
    - 'C:\\Users\\renat\\AppData\\Local\\Programs\\Python\\Python310\\lib\\nltk_data'
    - 'C:\\Users\\renat\\AppData\\Roaming\\nltk_data'
    - './nltk_data'
**********************************************************************


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\renat\Downloads\myapp\nlp_test.py", line 28, in <module>
    print(lemmatize_sentence(tweet_tokens[0]))
  File "c:\Users\renat\Downloads\myapp\nlp_test.py", line 25, in lemmatize_sentence
    lemmatized_sentence.append(lemmatizer.lemmatize(word, pos))
  File "C:\Users\renat\AppData\Local\Programs\Python\Python310\lib\site-packages\nltk\stem\wordnet.py", line 40, in lemmatize
    lemmas = wordnet._morphy(word, pos)
  File "C:\Users\renat\AppData\Local\Programs\Python\Python310\lib\site-packages\nltk\corpus\util.py", line 116, in __getattr__
    self.__load()
  File "C:\Users\renat\AppData\Local\Programs\Python\Python310\lib\site-packages\nltk\corpus\util.py", line 81, in __load
    except LookupError: raise e
  File "C:\Users\renat\AppData\Local\Programs\Python\Python310\lib\site-packages\nltk\corpus\util.py", line 78, in __load
    root = nltk.data.find('{}/{}'.format(self.subdir, self.__name))
  File "C:\Users\renat\AppData\Local\Programs\Python\Python310\lib\site-packages\nltk\data.py", line 675, in find
    raise LookupError(resource_not_found)
LookupError:
**********************************************************************
PS C:\Users\renat\Downloads\myapp> & C:/Users/renat/AppData/Local/Programs/Python/Python310/python.exe c:/Users/renat/Downloads/myapp/nlp_test.py
Traceback (most recent call last):
  File "C:\Users\renat\AppData\Local\Programs\Python\Python310\lib\site-packages\nltk\corpus\util.py", line 80, in __load      
    try: root = nltk.data.find('{}/{}'.format(self.subdir, zip_name))
  File "C:\Users\renat\AppData\Local\Programs\Python\Python310\lib\site-packages\nltk\data.py", line 675, in find
    raise LookupError(resource_not_found)
LookupError:
**********************************************************************
  Resource wordnet not found.
  Please use the NLTK Downloader to obtain the resource:

  >>> import nltk
  >>> nltk.download('wordnet')
  
  Searched in:
    - 'C:\\Users\\renat/nltk_data'
    - 'C:\\nltk_data'
    - 'D:\\nltk_data'
    - 'E:\\nltk_data'
    - 'C:\\Users\\renat\\AppData\\Local\\Programs\\Python\\Python310\\nltk_data'
    - 'C:\\Users\\renat\\AppData\\Local\\Programs\\Python\\Python310\\share\\nltk_data'
    - 'C:\\Users\\renat\\AppData\\Local\\Programs\\Python\\Python310\\lib\\nltk_data'
    - 'C:\\Users\\renat\\AppData\\Roaming\\nltk_data'
    - './nltk_data'
**********************************************************************


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\renat\Downloads\myapp\nlp_test.py", line 28, in <module>
    print(lemmatize_sentence(tweet_tokens[0]))
  File "c:\Users\renat\Downloads\myapp\nlp_test.py", line 25, in lemmatize_sentence
    lemmatized_sentence.append(lemmatizer.lemmatize(word, pos))
  File "C:\Users\renat\AppData\Local\Programs\Python\Python310\lib\site-packages\nltk\stem\wordnet.py", line 40, in lemmatize  
    lemmas = wordnet._morphy(word, pos)
  File "C:\Users\renat\AppData\Local\Programs\Python\Python310\lib\site-packages\nltk\corpus\util.py", line 116, in __getattr__
    self.__load()
  File "C:\Users\renat\AppData\Local\Programs\Python\Python310\lib\site-packages\nltk\corpus\util.py", line 81, in __load      
    except LookupError: raise e
  File "C:\Users\renat\AppData\Local\Programs\Python\Python310\lib\site-packages\nltk\corpus\util.py", line 78, in __load      
    root = nltk.data.find('{}/{}'.format(self.subdir, self.__name))
  File "C:\Users\renat\AppData\Local\Programs\Python\Python310\lib\site-packages\nltk\data.py", line 675, in find
    raise LookupError(resource_not_found)
LookupError:
**********************************************************************
  Resource wordnet not found.
  Please use the NLTK Downloader to obtain the resource:

  >>> import nltk
  >>> nltk.download('wordnet')

  Searched in:
    - 'C:\\Users\\renat/nltk_data'
    - 'C:\\nltk_data'
    - 'D:\\nltk_data'
    - 'E:\\nltk_data'
    - 'C:\\Users\\renat\\AppData\\Local\\Programs\\Python\\Python310\\nltk_data'
    - 'C:\\Users\\renat\\AppData\\Local\\Programs\\Python\\Python310\\share\\nltk_data'
    - 'C:\\Users\\renat\\AppData\\Local\\Programs\\Python\\Python310\\lib\\nltk_data'
    - 'C:\\Users\\renat\\AppData\\Roaming\\nltk_data'
    - './nltk_data'
**********************************************************************

My code:

import nltk

nltk.data.path.append('./nltk_data')

from nltk.tag import pos_tag
from nltk.stem.wordnet import WordNetLemmatizer
from nltk.corpus import twitter_samples

pos_tweets = twitter_samples.strings('positive_tweets.json')
neg_tweets = twitter_samples.strings('negative_tweets.json')
text = twitter_samples.strings('tweets.20150430-223406.json')

tweet_tokens = twitter_samples.tokenized('positive_tweets.json')

def lemmatize_sentence(tokens):
    lemmatizer = WordNetLemmatizer()
    lemmatized_sentence = []
    for word, tag in pos_tag(tokens):
        if tag.startswith('NN'):
            pos = 'n'
        elif tag.startswith('VB'):
            pos = 'v'
        else:
            pos = 'a'
        lemmatized_sentence.append(lemmatizer.lemmatize(word, pos))
    return lemmatized_sentence

print(lemmatize_sentence(tweet_tokens[0]))

My Folder Organization:

enter image description here

I've tried to download wordnet package again in CMD, but it seems OK:

So i've created a folder to download wordnet package and use on my python code, but same error appears. And when i try to download again:

How do i solve this?

2

There are 2 answers

0
newquant84 On

I had a similar problem and resolved by following the instructions here: https://www.nltk.org/data.html

I simply ran the command line installation.

python -m nltk.downloader all
0
Daniel Viglione On

NLTK provides in its documentation a complete list of NLTK corpora: https://www.nltk.org/nltk_data/ Notice it includes wordnet. You need to use the NLTK’s data downloader to install the data. Once you download it, then you will be able to utilize WordNetLemmatizer. Here is an example:

import nltk
nltk.download('wordnet')

So add nltk.download('wordnet’) to the second line of your code above. Then it will work. I cannot use your exact example, but here is a minimally working example:

import nltk
nltk.download('wordnet')
from nltk.stem import WordNetLemmatizer
wnl = WordNetLemmatizer()

for word in ['walking', 'walks', 'walked']:
    print(wnl.lemmatize(word))

Output:

walking
walk
walked