在nltk中使用带有python的Stanford解析器,如何设置模型路径

时间:2017-04-15 20:31:52

标签: python nltk stanford-parser

我想在nltk包中使用带有python的Stanford解析器,它在pos tagger和NER tagger上运行良好,但是当涉及到解析器时,它不起作用。 这是代码:

import nltk
from nltk.parse import stanford

import os
from nltk.parse import stanford

os.environ['CLASSPATH'] = '/Users/caoyue/PycharmProjects/iepy_/jars'
os.environ['STANFORD_MODELS'] = '/Users/caoyue/PycharmProjects/iepy_/jars'

from nltk.tag.stanford import StanfordPOSTagger
st = StanfordPOSTagger('english-left3words-distsim.tagger')
a=st.tag('What is the airspeed of an unladen swallow ?'.split())
print(a)
from nltk.tag import StanfordNERTagger
st = StanfordNERTagger('english.all.3class.distsim.crf.ser.gz')
b=st.tag('Rami Eid is studying at Stony Brook University in NY'.split())
print(b)
from nltk.parse.stanford import StanfordDependencyParser
dep_parser=StanfordDependencyParser(model_path="/Users/caoyue/PycharmProjects/iepy_/jars/english.all.3class.distsim.crf.ser.gz ")
list(dep_parser.raw_parse("the quick brown fox jumps over the lazy dog"))

enter image description here

错误是:

enter image description here

我认为model_path是错误的,我该如何设置它是正确的?

1 个答案:

答案 0 :(得分:0)

我没有测试它,但我认为english.all.3class.distsim.crf.ser.gz之后的尾随空格会导致一些混乱:

model_path="..../english.all.3class.distsim.crf.ser.gz ")

(删除部分路径以提高可读性)