Search code examples
nlpnltkstanford-nlpopennlp

Extracting age related information from using nlp


I am new to NLP and I have been trying to extract age related information from raw text. I googled and didn't get any reliable library in any language for this requirement. It would be great if I can get any help in this. I am open to any language and it is not a constraint. It can be in Java, Python or any other language too. Any help would be much appreciated. Thanks in advance. Cheers!

Update:

I tried adding the annotators, mentioned by Stanford help, to my java parser and I am facing below exception :

    ERROR: cannot create CorefAnnotator!
    java.lang.RuntimeException: Error creating coreference system
    at 

 edu.stanford.nlp.scoref.StatisticalCorefSystem.fromProps(StatisticalCorefSystem.java:58)
    at edu.stanford.nlp.pipeline.CorefAnnotator.<init>(CorefAnnotator.java:66)
    at edu.stanford.nlp.pipeline.AnnotatorImplementations.coref(AnnotatorImplementations.java:220)
    at edu.stanford.nlp.pipeline.AnnotatorFactories$13.create(AnnotatorFactories.java:515)
    at edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:85)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.construct(StanfordCoreNLP.java:375)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:139)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:135)
    at com.dateparser.SUtime.SUAgeParser.makeNumericPipeline(SUAgeParser.java:85)
    at com.dateparser.SUtime.SUAgeParser.<clinit>(SUAgeParser.java:60)
Caused by: java.lang.RuntimeException: Error initializing coref system
    at edu.stanford.nlp.scoref.StatisticalCorefSystem.<init>(StatisticalCorefSystem.java:36)
    at edu.stanford.nlp.scoref.ClusteringCorefSystem.<init>(ClusteringCorefSystem.java:24)
    at edu.stanford.nlp.scoref.StatisticalCorefSystem.fromProps(StatisticalCorefSystem.java:48)
    ... 9 more
Caused by: java.io.IOException: Unable to open "edu/stanford/nlp/models/hcoref/md-model.ser" as class path, filename or URL
    at edu.stanford.nlp.io.IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(IOUtils.java:485)
    at edu.stanford.nlp.io.IOUtils.readObjectFromURLOrClasspathOrFileSystem(IOUtils.java:323)
    at edu.stanford.nlp.hcoref.md.DependencyCorefMentionFinder.<init>(DependencyCorefMentionFinder.java:38)
    at edu.stanford.nlp.hcoref.CorefDocMaker.getMentionFinder(CorefDocMaker.java:149)
    at edu.stanford.nlp.hcoref.CorefDocMaker.<init>(CorefDocMaker.java:61)
    at edu.stanford.nlp.scoref.StatisticalCorefSystem.<init>(StatisticalCorefSystem.java:34)
    ... 11 more

I upgraded to version 1.6.0 and also added stanford-corenlp-models-current.jar to the classpath. Please let me know if I am missing something

Update 1:

The exception was fixed after upgrading to 3.9.1. But I am getting the ouput as per:duration relation instead of per:age

private static AnnotationPipeline makePipeline() {

    Properties props = new Properties();
    props.setProperty("annotators",
             "tokenize,ssplit,pos,lemma,ner,depparse,coref,kbp");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    return pipeline;


}

public static void parse(String str) {
    try {
        Annotation doc = new Annotation(str);
        pipeline.annotate(doc);
        ArrayList<CoreMap> resultRelations = new ArrayList<CoreMap>();
        List<CoreMap> mentionsAnnotations = doc.get(MentionsAnnotation.class);
        for (CoreMap currentCoreMap : mentionsAnnotations) {
            System.out.println(currentCoreMap.get(TextAnnotation.class));
            System.out.println(currentCoreMap.get(CharacterOffsetBeginAnnotation.class));
            System.out.println(currentCoreMap.get(CharacterOffsetEndAnnotation.class));
            System.out.println(currentCoreMap.get(NamedEntityTagAnnotation.class));
        }
    } catch (Exception e) {

    }
}

Is this normal behaviour or am I doing something wrong?


Solution

  • You may find the KBP relation extractor useful.

    Example text:

    Joe Smith is 58 years old.
    

    Command:

    java -Xmx12g edu.stanford.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,pos,lemma,ner,depparse,coref,kbp -file example.txt -outputFormat text
    

    This should attach Joe Smith to 58 years old with the per:age relation.