原文:http://blog.csdn.net/lr231654111/article/details/74129620
基本环境搭建
主要参考下面的文章进行搭建:
NLTK中使用Stanford parser:http://blog.csdn.net/sherrylml/article/details/45197187
但遇到了下面的报错:
OSError: Java command failed :
参考下面的两篇问答解决了这个问题:
https://stackoverflow.com/questions/35624245/java-command-failed-when-running-nltk-stanfordparser
https://github.com/nltk/nltk/issues/1239
所用的方法是第二个问答里提到的“hack the stanford parser classpath”,主要的代码是:
- from nltk.internals import find_jars_within_path
- parser._classpath = tuple(find_jars_within_path(stanford_dir))
from nltk.internals import find_jars_within_path
parser._classpath = tuple(find_jars_within_path(stanford_dir))
中文分析
我的目的是比较stanford parser和LTP的依存分析结果的一致性如何。
用stanford处理中文要注意就两点:一是使用中文模型,二是注意编码(unicode输入)。
stanford 提供的中文模型在下面的参数中设置:
- parser = stanford.StanfordDependencyParser('D:/jars/stanford-parser.jar',
- 'D:/jars/stanford-parser-3.8.0-models.jar',
- 'D:/LearnNLTK/stanfordNLTK/stanford-parser-3.8.0-models/edu/stanford/nlp/models/lexparser/chinesePCFG.ser.gz') # 指定使用的模型
parser = stanford.StanfordDependencyParser('D:/jars/stanford-parser.jar',
'D:/jars/stanford-parser-3.8.0-models.jar',
'D:/LearnNLTK/stanfordNLTK/stanford-parser-3.8.0-models/edu/stanford/nlp/models/lexparser/chinesePCFG.ser.gz') # 指定使用的模型
主要模型有:chinesePCFG(最简洁快速),chineseFactored(据说更准确,但吃内存),xinhuaPCFG和xinhuaFactored(是根据某个国内的语料库训练的模型)<前面的模型都需要先进行分词>,xinhuaFactoredSegmenting(这个内置分词)。
然而,我最后没能做出stanford parser和LTP的依存分析结果对比,因为stanford parser对句子的完整度要求比较高,对于一些不完整的句子会报错:
the graph doesn't contain a node that depends on the root element.我想这个和我的语料中有大量无主句,不完整句子有关。
主要参考文献
- NLTK中使用Stanford parser:http://blog.csdn.net/sherrylml/article/details/45197187
- Python自然语言处理实践: 在NLTK中使用斯坦福中文分词器: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
- 【NLP】干货!详述PythonNLTK下如何使用stanfordNLP工具包:http://www.w2bc.com/article/185348
- 试用Stanford Parser处理中文语句:http://bradyzhu.iteye.com/blog/2271186
- 采用Stanford Parser进行中文语法解析:http://www.xuebuyuan.com/1789420.html
- Stanford Dependency-Parser 分享:http://blog.csdn.net/allenshi_szl/article/details/6093582