gpt4 book ai didi

java - 如何获取树的 Stanford Dependencies "nsubj"主题的值?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:53:53 25 4
gpt4 key购买 nike

这是我的代码的一部分:

String sentence="My dog also likes eating sausage.";
LexicalizedParser lp = new LexicalizedParser("englishPCFG.ser.gz");
TokenizerFactory tf = PTBTokenizer.factory(false, new WordTokenFactory());
TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");

List tokens = tf.getTokenizer(new StringReader(sentence)).tokenize();
lp.parse(tokens); // parse the tokens
Tree t = lp.getBestParse();

如何获取主题(狗)的值?

这是一个我想提取主题的依赖项:

nsubj(likes-4, dog-2)

最佳答案

尝试这样的事情:

String sentence="My dog also likes eating sausage.";
LexicalizedParser lp = new LexicalizedParser("resources/stanford-parser-2011-06-27/grammar/englishPCFG.ser.gz");
TokenizerFactory tf = PTBTokenizer.factory(false, new WordTokenFactory());
TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");

List tokens = tf.getTokenizer(new StringReader(sentence)).tokenize();
lp.parse(tokens); // parse the tokens
Tree t = lp.getBestParse();

TreebankLanguagePack languagePack = new PennTreebankLanguagePack();
GrammaticalStructure structure = languagePack.grammaticalStructureFactory().newGrammaticalStructure(t);
Collection<TypedDependency> typedDependencies = structure.typedDependenciesCollapsed();

for(TypedDependency td : typedDependencies) {
if(td.reln().equals(EnglishGrammaticalRelations.NOMINAL_SUBJECT)) {
System.out.println(td);
}
}

将打印:

nsubj(likes-4, dog-2)

关于java - 如何获取树的 Stanford Dependencies "nsubj"主题的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8021696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com