我使用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'>如何访问各个元组以使用依赖关系解析?
发布于 2015-04-16 11:50:28
http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/trees/TypedDependency.html上提供了TypedDependency类的文档。我无法将模型加载到我的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
printhttps://stackoverflow.com/questions/29596693
复制相似问题