trouble importing stanford pos tagger into nltk

房东的猫 提交于 2019-11-26 21:30:41

问题


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


回答1:


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')



回答2:


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.



来源:https://stackoverflow.com/questions/7344916/trouble-importing-stanford-pos-tagger-into-nltk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!