斯坦福依赖解析器Jython

时间:2015-04-13 01:36:34

标签: dependencies nlp jython

我正在使用来自的代码 http://blog.gnucom.cc/2010/using-the-stanford-parser-with-jython/ 生成依赖关系解析。

import sys
sys.path.append('/path/to/jar/stanford-parser-2008-10-26.jar')

from java.io import CharArrayReader
from edu.stanford.nlp import *

lp = parser.lexparser.LexicalizedParser('/path/to/englishPCFG.ser.gz')
tlp = trees.PennTreebankLanguagePack()
lp.setOptionFlags(["-maxLength", "80", "-retainTmpSubcategories"])

sentence = 'One of my favorite features of functional programming \
languages is that you can treat functions like values.'

toke = tlp.getTokenizerFactory().getTokenizer(CharArrayReader(sentence));
wordlist = toke.tokenize()

if (lp.parse(wordlist)):
    parse = lp.getBestParse()

gsf = tlp.grammaticalStructureFactory()
gs = gsf.newGrammaticalStructure(parse)
tdl = gs.typedDependenciesCollapsed()

print parse.toString() 
print tdl

它给出了一个包含类型元组的列表:

<type 'edu.stanford.nlp.trees.TypedDependency'>

如何访问单个元组以使用依赖关系解析?

1 个答案:

答案 0 :(得分:0)

TypedDependency 类的文档可在http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/trees/TypedDependency.html获得。我无法使用我的Java版本加载模型(需要5个,但我有7个。)

这应该会给你一个开始:

for type_dep in tdl:
    print "Governor word:", type_dep.gov().toString() # .gov() is an IndexedWord
    print "Dependent word:", type_dep.dep().toString() # .dep() is an IndexedWord
    print "Relation:", type_dep.reln().toString() # .reln() is a GrammaticalRelation
    print