简体   繁体   中英

Stanford part of speech tagger gives Attribute error

I tried different tutorials to learn use of the Stanford part of speech tagger in Python. At present I am using the following code for Pos tagging using the Stanford tagger. However, I am getting an AttributeError . My code is below:

import nltk
from nltk.tag.stanford import StanfordPOSTagger
english_postagger = StanfordPOSTagger('/home/szk/Downloads/NL2API/NL2API/tutorials/postags/stanford-postagger-2018-10-16/models/english-bidirectional-distsim.tagger', '/home/szk/Downloads/NL2API/NL2API/tutorials/postags/stanford-postagger-2018-10-16/stanford-postagger.jar')
english_postagger.tag('this is stanford postagger in nltk for python users'.split())

The error trace is below:

Traceback (most recent call last):
  File "stanfordpostag.py", line 4, in <module>
    english_postagger.tag('this is stanford postagger in nltk for python users'.split())
  File "/home/szk/Downloads/NL2API/NL2API/newv/local/lib/python2.7/site-packages/nltk/tag/stanford.py", line 93, in tag
    return sum(self.tag_sents([tokens]), [])
  File "/home/szk/Downloads/NL2API/NL2API/newv/local/lib/python2.7/site-packages/nltk/tag/stanford.py", line 116, in tag_sents
    cmd, classpath=self._stanford_jar, stdout=PIPE, stderr=PIPE
  File "/home/szk/Downloads/NL2API/NL2API/newv/local/lib/python2.7/site-packages/nltk/internals.py", line 112, in java
    subprocess_output_dict = {'pipe': subprocess.PIPE, 'stdout': subprocess.STDOUT, 'devnull': subprocess.DEVNULL}
AttributeError: 'module' object has no attribute 'DEVNULL'

Hopefully someone can provide a solution.

I'm not sure why this doesn't work – it's still meant to – but from NLTK version 3.2.3 forward, you're much better off (for speed and scalability reasons) using the newer Stanford CoreNLP server interface discussed here: https://github.com/nltk/nltk/wiki/Stanford-CoreNLP-API-in-NLTK .

So you might try that. It's fine to follow those instructions but substitute everywhere the current 2018-10-05 CoreNLP release rather than the previous version referenced in the instructions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM