Stanford SemanticGraph get sentence subject

时间:2016-07-16 13:40:32

标签: stanford-nlp

How does one get the subject of a sentence (in a general way) using the SemanticGraph component from Stanford CoreNLP?

I've tried the code posted below, but the output indicates subject is null.

String sentence = "Carl has 84 Skittles.";
Annotation doc = InitUtil.initStanford(sentence, "tokenize, ssplit, pos, lemma, ner, parse");
SemanticGraph semGraph = doc.get(SENTENCE).get(0).get(DEPENDENCIES);
IndexedWord verb = semGraph.getFirstRoot();
IndexedWord subject = semGraph.getChildWithReln(verb, GrammaticalRelation.valueOf("nsubj"));
System.out.println(subject);

If I try the same code replacing the second to last line with the 3 lines below, I get the expected output of "Carl". The difference appears to be a private field of GrammaticalRelation called specific, but the value of this field appears to be sentence-specific. My question is how to get the subject in a way that can be applied to all or nearly all sentences.

Set<GrammaticalRelation> relations = semGraph.childRelns(verb);
GrammaticalRelation relation = relations.iterator().next();
IndexedWord subject = semGraph.getChildWithReln(verb, relation);

1 个答案:

答案 0 :(得分:1)

原来问题不在specific字段。

SemanticGraph.getChildWIthReln依赖于GrammaticalRelation.equals(),它检查两个对象的语言是否兼容。 GrammaticalRelation.valueOf(String)返回GrammaticalRelation,语言为Language.English,而斯坦福大学的解析器使用Language.UniversalEnglish。由于某种原因,这两种语言不兼容。将通话更改为GrammaticalRelation.valueOf(String)GrammaticalRelation.valueOf(Language, String)解决了问题。