user425727
user425727

Reputation:

stanford.py NotImplementedError

I get a NotImplementedError when i run below code. Why?

import nltk
from nltk import *
from nltk.tag import stanford
from nltk.tag.stanford import StanfordTagger

st = StanfordTagger('C:\\Python27\\stanford-postagger\\models\\english-bidirectional-distsim.tagger',
                    'C:\\Python27\\stanford-postagger\\stanford-postagger.jar')
print st.tag('What is the airspeed of an unladen swallow ?'.split()) 

Error Message:

  File "C:\Python27\lib\site-packages\nltk\tag\stanford.py", line 51, in _cmd
    raise NotImplementedError
NotImplementedError

Upvotes: 0

Views: 226

Answers (2)

Padraic Cunningham
Padraic Cunningham

Reputation: 180471

Looking at the source code here:

The _cmd method is not implemented in StanfordTagger - _cmd property: A property that returns the command that will be executed.

@property
    def _cmd(self):
      raise NotImplementedError

So when it is called here causes your problem:

 # Run the tagger and get the output
    stanpos_output, _stderr = java(self._cmd,classpath=self._stanford_jar, \
                                                    stdout=PIPE, stderr=PIPE)

In POSTagger the method looks like:

@property
def _cmd(self):
    return ['edu.stanford.nlp.tagger.maxent.MaxentTagger', \
            '-model', self._stanford_model, '-textFile', \
            self._input_file_path, '-tokenize', 'false']

You could edit the method in StanfordTagger to match the POSTTagger.

Upvotes: 1

user425727
user425727

Reputation:

this is not the answer to my question but a workaround: use POSTagger instead of StanfordTagger

import nltk
from nltk import *
from nltk.tag import stanford
#from nltk.tag.stanford import StanfordTagger
from nltk.tag.stanford import POSTagger

st = POSTagger('C:\\Python27\\stanford-postagger\\models\\english-bidirectional-distsim.tagger',
                    'C:\\Python27\\stanford-postagger\\stanford-postagger.jar')
print st.tag('What is the airspeed of an unladen swallow ?'.split()) 

Upvotes: 0

Related Questions