编程语言
首页 > 编程语言> > java – 如何获取树的Stanford Dependencies“nsubj”的主题值?

java – 如何获取树的Stanford Dependencies“nsubj”的主题值?

作者:互联网

这是我的代码的一部分:

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)

标签:stanford-nlp,java
来源: https://codeday.me/bug/20190826/1732705.html