Python配置StanfordNLP(初级版)

分享 未结 0 2467
Leslee
Leslee LV4 2018-07-04
悬赏:20积分
Python使用nltk下的Stanford nlp接口对文本进行分析
最近需要对句子进行分词、词性标注、句法分析,需要用到Stanford nlp。刚好Stanford nlp提供了对应的接口,可以使用Python调用。具体的教程如下链接:
http://www.tuicool.com/articles/VbYnuuI 这篇文章对怎么配置的讲解非常详细,是基于Linux下配置classpath 。刚好我的Ubuntu崩了,只能用Windows搞,所以踩了点不大不小的坑,在此记下。
(要按照教程中所给的链接下载对应版本的jar包,因为不同版本的jar包,改动不一样很容易遇到bug 除了最后一个dependency我使用的3.7.0,其他全部采用了3.6.0) python版本3.6,NLTK版本3.2.2
Windows下classpath具体的配置:
首先,和linux是不同的。毋庸置疑,

首先将

路径加到类似于HOME的路径里
然后在classpath中%%\\对应的jar包即可。问题就在于此,我使用了\和/和\\。一直试到最后一个才成功。很艰难,因为可能错误的地方实在太多了。一定要用\\才能找到jar包
否则一直报错找不到jar包。




大概如下所示,这样classpath就没有问题了。
但是这时候运行程序还是报错找不到jar包,咋回事?都配置好了还是找不到!!要抓狂了!
后来发现原因在于虽然我们配置了classpath 但是它不是即时生效的。所需要做的是重启一下explorer 也就是资源管理器。
http://blog.csdn.net/friendan/article/details/51005003见该链接

但是,问题还是有错,还是找不到jar包,这又是怎么回事!?
到了这里 我也快要放弃了,但是我不停的尝试,非常偶然的一次尝试,第一次我都没有意识到怎么回事 ,居然成功了!没有报jar包找不到,出结果了!
后来多次尝试,发现,每次我都是开着spyder 配置的classpath 和重启的资源管理器。(我每次都是配置好一个jar包就测试一下能不能用,很繁琐,很痛苦)
后来,先关闭spyder,然后重启资源管理器,再然后启动spyder 成功了!
对应的测试代码:
from nltk.tokenize import StanfordSegmenter
#from nltk.tag import StanfordNERTagger
#分词已成功
segmenter = StanfordSegmenter(
#path_to_jar = "E:/standford/segmenter/stanford-segmenter.jar",
#path_to_slf4j = "E:/standford/segmenter/slf4j-api.jar",
path_to_sihan_corpora_dict="E:/standford/segmenter/data/",
path_to_model="E:/standford/segmenter/data/pku.gz",
path_to_dict="E:/standford/segmenter/data/dict-chris6.ser.gz"
)
res = segmenter.segment("北海已成为中国对外开放中升起的一颗明星")
print (res)
#词性标注
from nltk.tag import StanfordPOSTagger

chi_tagger = StanfordPOSTagger('E:/standford/postagger/models/chinese-distsim.tagger')
sent = u'北海 已 成为 中国 对外开放 中 升起 的 一 颗 明星'
for _, word_and_tag in chi_tagger.tag(sent.split()):
word, tag = word_and_tag.split('#')
print (word, tag)
#NER
from nltk.tag import StanfordNERTagger

chi_tagger = StanfordNERTagger('E:/standford/ner/classifiers/chinese.misc.distsim.crf.ser.gz')
sent = u'北海 已 成为 中国 对外开放 中 升起 的 一 颗 明星'
for word, tag in chi_tagger.tag(sent.split()):
print (word, tag)
#parser
from nltk.parse.stanford import StanfordParser

chinese_parser = StanfordParser(model_path=u'E:/standford/parser/models/lexparser/chineseFactored.ser.gz')
#par = list(chinese_parser.parse("北海 已 成为 中国 对外开放 中 升起 的 一 颗 明星".split()))
trees = chinese_parser.parse("北海 已 成为 中国 对外开放 中 升起 的 一 颗 明星".split())
for tree in trees:
print (tree.draw())
#依赖
from nltk.parse.stanford import StanfordDependencyParser

eng_parser = StanfordDependencyParser(model_path=u'E:/standford/parser/models/lexparser/xinhuaPCFG.ser.gz')
res = list(eng_parser.parse("北海 已 成为 中国 对外开放 中 升起 的 一 颗 明星".split()))
for row in res[0].triples():
print (row)

在其中尝试的时候,得到需要\\这样的分隔符是因为:
https://github.com/nltk/nltk/issues/1239







参考文献:http://www.52nlp.cn/python%E8%87%AA%E7%84%B6%E8%AF%AD%E8%A8%80%E5%A4%84%E7%90%86%E5%AE%9E%E8%B7%B5-%E5%9C%A8nltk%E4%B8%AD%E4%BD%BF%E7%94%A8%E6%96%AF%E5%9D%A6%E7%A6%8F%E4%B8%AD%E6%96%87%E5%88%86%E8%AF%8D%E5%99%A8

https://github.com/nltk/nltk/issues/1239

http://stackoverflow.com/questions/13883277/stanford-parser-and-nltk

http://stackoverflow.com/questions/13883277/stanford-parser-and-nltk

http://stackoverflow.com/questions/36942270/nltk-was-unable-to-find-the-gs-file

http://www.eecs.qmul.ac.uk/~dm303/stanford-dependency-parser-nltk-and-anaconda.html
回帖
  • 消灭零回复