cristian.petroaca
cristian.petroaca

Reputation: 355

StanfordNLP does not extract relations between entities

I'm trying out the StanfordNLP Relation Extractor which according to the page at http://nlp.stanford.edu/software/relationExtractor.shtml has 4 relations that it can extract : Live_In, Located_In, OrgBased_In, Work_For.

My code is :

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);

I am expecting to get a Live_In relation but the relations variable is null.

What am I missing in the code?

Thanks

Upvotes: 0

Views: 269

Answers (1)

Jon Gauthier
Jon Gauthier

Reputation: 25582

The RelationMentionsAnnotation is a sentence-level annotation. You should first iterate over the sentences in the Annotation object and then try to retrieve the annotation.

Here's a basic example of how to iterate over sentences:

// 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);
    // ....
}

Upvotes: 2

Related Questions