找不到 NLTK wordnet

问题描述 投票:0回答:0

我正在使用 NLTK 学习 nlp,一步是使用 WordNetLemmatizer,所以我已经下载了它。但是它说当我运行我的代码时没有找到 wordnet。

控制台(错误)

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'
**********************************************************************

我的代码:

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]))

我的文件夹组织:

我尝试在CMD中再次下载wordnet包,但似乎没问题:

所以我创建了一个文件夹来下载 wordnet 包并在我的 python 代码上使用,但出现了同样的错误。当我再次尝试下载时:

我该如何解决这个问题?

python-3.x nltk wordnet
© www.soinside.com 2019 - 2024. All rights reserved.