python-3.x 未找到NLTK wordnet

atmip9wb  于 2023-08-08  发布在  Python
关注(0)|答案(1)|浏览(121)

我正在用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]))

我的文件夹组织:


的数据
我已经尝试下载wordnet包再次在CMD,但它似乎OK:



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



我该怎么解决这个问题?

a6b3iqyw

a6b3iqyw1#

我遇到了类似的问题,并按照以下说明解决了:https://www.nltk.org/data.html
我只是运行了命令行安装。

python -m nltk.downloader all

字符串

相关问题