首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >斯坦福依赖关系解析器Jython

斯坦福依赖关系解析器Jython
EN

Stack Overflow用户
提问于 2015-04-13 09:36:34
回答 1查看 145关注 0票数 1

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

代码语言:javascript
复制
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

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

代码语言:javascript
复制
<type 'edu.stanford.nlp.trees.TypedDependency'>

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

EN

回答 1

Stack Overflow用户

发布于 2015-04-16 11:50:28

http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/trees/TypedDependency.html上提供了TypedDependency类的文档。我无法将模型加载到我的Java版本中(需要5个,但我有7个)。

不过,这应该会给你一个开始:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29596693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档