Search code examples
javastanford-nlp

Stanford CoreNLP depparse throwing OutofMemoryException


I am using the Stanford CoreNLP (in JAVA) for some Information extraction (using OpenIE annotators). PFB my code -

public void getInformation(String fileName){
    Properties prop = new Properties();
    prop.setProperty("annotators", "tokenize, ssplit, pos, lemma, depparse, natlog, openie");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(prop);
    Annotation annotation = new Annotation(IOUtils.slurpFileNoExceptions(fileName));
    pipeline.annotate(annotation);
    pipeline.prettyPrint(annotation, out_data);
    System.out.println("=============================");
    System.out.println("The top level annotation");
    System.out.println(annotation.toString());

    List<CoreMap> sentences = (List<CoreMap>) annotation.get(CoreAnnotations.SentencesAnnotation.class);
    if(sentences!=null && !sentences.isEmpty())
    {
        CoreMap sentence = sentences.get(0);

        Collection<RelationTriple> triples = sentence.get(NaturalLogicAnnotations.RelationTriplesAnnotation.class);

        // Print the triples
        for(RelationTriple triple : triples) 
        {
            System.out.println(triple.confidence + "\t" +
            triple.subjectLemmaGloss() + "\t" +
            triple.relationLemmaGloss() + "\t" +
            triple.objectLemmaGloss());
        }
    }
}

But am getting the following error (java.lang.OutOfMemoryError: Java heap space) while running my code.

INFO edu.stanford.nlp.parser.nndep.DependencyParser - Loading depparse model file: edu/stanford/nlp/models/parser/nndep/english_UD.gz ... 
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at edu.stanford.nlp.parser.nndep.Classifier.preCompute(Classifier.java:661)
at edu.stanford.nlp.parser.nndep.Classifier.preCompute(Classifier.java:643)
at edu.stanford.nlp.parser.nndep.DependencyParser.initialize(DependencyParser.java:1168)
at edu.stanford.nlp.parser.nndep.DependencyParser.loadModelFile(DependencyParser.java:605)
at edu.stanford.nlp.parser.nndep.DependencyParser.loadFromModelFile(DependencyParser.java:498)
at edu.stanford.nlp.pipeline.DependencyParseAnnotator.<init>(DependencyParseAnnotator.java:57)
at edu.stanford.nlp.pipeline.AnnotatorImplementations.dependencies(AnnotatorImplementations.java:273)
at edu.stanford.nlp.pipeline.AnnotatorFactories$18.create(AnnotatorFactories.java:480)
at edu.stanford.nlp.simple.Document$5.get(Document.java:154)
at edu.stanford.nlp.simple.Document$5.get(Document.java:148)
at edu.stanford.nlp.simple.Document.runDepparse(Document.java:946)
at edu.stanford.nlp.simple.Document.runNatlog(Document.java:966)
at edu.stanford.nlp.simple.Document.runOpenie(Document.java:986)
at edu.stanford.nlp.simple.Sentence.openieTriples(Sentence.java:890)
at edu.stanford.nlp.simple.Sentence.openieTriples(Sentence.java:900)
at com.automatics.nlp.OpenIEDemo.main(OpenIEDemo.java:18)

How should I overcome this exception?


Solution

  • When you run your program you need to give it at least 2 GB of RAM, possibly more depending on what other Stanford CoreNLP annotators you are using. You should keep adding RAM until the crash goes away.