python-3.xanacondanltknltk-trainernltk-book

nltk pos_tag error in windows Anaconda


while I wish to collect POS tags using pos_tag function the following error occurs.i included all packages required for nltk. nltk version is 3.3 and running in conda environment . python version is 3.6. every nltk packages are downloaded using nltk download function , but every time when i run pos_tag function it throws following error.

>>> from nltk import pos_tag, word_tokenize
>>> pos_tag(word_tokenize("John's big idea isn't all that bad."))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\User\Anaconda3\envs\sow\lib\site-packages\nltk\tag\__init__.py", line 133, in pos_tag
    tagger = _get_tagger(lang)
  File "C:\Users\User\Anaconda3\envs\sow\lib\site-packages\nltk\tag\__init__.py", line 97, in _get_tagger
    tagger = PerceptronTagger()
  File "C:\Users\User\Anaconda3\envs\sow\lib\site-packages\nltk\tag\perceptron.py", line 141, in __init__
    self.load(AP_MODEL_LOC)
  File "C:\Users\User\Anaconda3\envs\sow\lib\site-packages\nltk\tag\perceptron.py", line 223, in load
    self.model.weights, self.tagdict, self.classes = load(loc)
  File "C:\Users\User\Anaconda3\envs\sow\lib\site-packages\nltk\data.py", line 836, in load
    opened_resource = _open(resource_url)
  File "C:\Users\User\Anaconda3\envs\sow\lib\site-packages\nltk\data.py", line 957, in _open
    return find(path_, ['']).open()
  File "C:\Users\User\Anaconda3\envs\sow\lib\site-packages\nltk\data.py", line 675, in find
    raise LookupError(resource_not_found)
LookupError:
**********************************************************************
  Resource [93mD:[0m not found.
  Please use the NLTK Downloader to obtain the resource:

  [31m>>> import nltk
  >>> nltk.download('D:')
  [0m
  Searched in:
    - ''
********************************************************************** 

Solution

  • You need to install nltk's corpora i.e data. Your code tries to look up pos tags and tokenize data.

    >>> import nltk
    >>> nltk.download()
    

    This should solve your issue.

    Reference : nltk data

    Edit after @nikki's suggestion.

    In case you have had previous installation of nltk_data it will download it to the same location. In that case you should do the below. That is the conflict which is causing you the issue.

    nltk.data.path.append('old_location_of_nltk_data')
    

    or

    set NLTK_DATA environment variable.