Question

This is probably a very trivial question. I am trying to use the stanford pos tagger through nltk given here The problem is that my nltk lib doesnt contain the stanford module. So I copied the same into the appropriate folder and compiled the same. Now when i try to run an example the module is getting detected but not the class inside the module. Can anyone tell me where I am going wrong?? Again this is probably very dumb.

>>> from nltk.tag import stanford 
>>> st = StanfordTagger('bidirection-distsim-wsj-0-18.tagger')

I used py_compile to compile the stanford.py file. Am i missing something

Was it helpful?

Solution

You are only importing stanford. In order to access StanfordTagger you need to use either:

>>> from nltk.tag.stanford import StanfordTagger

(assuming that `StanfordTagger is not further nested in a module) or access it by

>>> st = stanford.StanfordTagger('bidirection-distsim-wsj-0-18.tagger')

OTHER TIPS

If you want to use the Stanford parser, use this:

import os
from nltk.parse import stanford
os.environ['STANFORD_PARSER'] = '/folder/with/standford/jars'
os.environ['STANFORD_MODELS'] = '/folder/with/standford/jars'

parser = stanford.StanfordParser(model_path="/location/of/the/englishPCFG.ser.gz")
print parser.raw_batch_parse(("Hello, My name is Melroy.", "What is your name?"))

Output:

[Tree('ROOT', [Tree('S', [Tree('INTJ', [Tree('UH', ['Hello'])]), Tree(',', [',']), Tree('NP', [Tree('PRP$', ['My']), Tree('NN', ['name'])]), Tree('VP', [Tree('VBZ', ['is']), Tree('ADJP', [Tree('JJ', ['Melroy'])])]), Tree('.', ['.'])])]), Tree('ROOT', [Tree('SBARQ', [Tree('WHNP', [Tree('WP', ['What'])]), Tree('SQ', [Tree('VBZ', ['is']), Tree('NP', [Tree('PRP$', ['your']), Tree('NN', ['name'])])]), Tree('.', ['?'])])])]

Note 1: In this example both the parser & model jars are in the same folder.

Note 2:

  • File name of stanford parser is: stanford-parser.jar
  • File name of stanford models is: stanford-parser-x.x.x-models.jar

Note 3: The englishPCFG.ser.gz file can be found inside the models.jar file (/edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz). Please use come archive manager to 'unzip' the models.jar file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top