StanfordNLP不提取实体之间的关系

时间:2015-03-12 17:32:05

标签: stanford-nlp

我正在尝试使用StanfordNLP关系提取器,它根据http://nlp.stanford.edu/software/relationExtractor.shtml的页面有4个可以提取的关系:Live_In,located_In,OrgBased_In,Work_For。

我的代码是:

Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref, relation");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    String text = "Mary lives in Boston.";

    Annotation document = new Annotation(text);
    pipeline.annotate(document);

    List<RelationMention> relations = document.get(MachineReadingAnnotations.RelationMentionsAnnotation.class);

我希望得到一个Live_In关系,但关系变量为null。

我在代码中缺少什么?

由于

1 个答案:

答案 0 :(得分:2)

RelationMentionsAnnotation是一个句子级别的注释。您应首先迭代Annotation对象中的句子,然后尝试检索注释。

以下是如何迭代句子的基本示例:

// these are all the sentences in this document
// a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
List<CoreMap> sentences = document.get(SentencesAnnotation.class);

for(CoreMap sentence: sentences) {
    List<RelationMention> relations = sentence.get(MachineReadingAnnotations.RelationMentionsAnnotation.class);
    // ....
}