Search code examples
javasentiment-analysis

Stanford CoreNLP properties File not found


I am trying to do sentiment analysis on tweets but getting strange Exception.

I am initializing pipeline with properties file and place properties file in resources directory, within src->main folder. enter image description here

But still getting Exception in init function :

Exception in thread "main" edu.stanford.nlp.io.RuntimeIOException:    java.io.IOException: Unable to open "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz" as class path, filename or URL
at edu.stanford.nlp.parser.common.ParserGrammar.loadModel(ParserGrammar.java:188)
at edu.stanford.nlp.pipeline.ParserAnnotator.loadModel(ParserAnnotator.java:212)
at edu.stanford.nlp.pipeline.ParserAnnotator.<init>(ParserAnnotator.java:115)
at edu.stanford.nlp.pipeline.AnnotatorImplementations.parse(AnnotatorImplementations.java:150)
at edu.stanford.nlp.pipeline.AnnotatorFactories$11.create(AnnotatorFactories.java:463)
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.mycompany.sentmentanalysisontweets.NLP.init(NLP.java:27)
at com.mycompany.sentmentanalysisontweets.WhatToThink.main(WhatToThink.java:15)

In main method i'm calling init() method.

public class Main {

    public static void main(String[] args) {
        NLP.init();
    }
}

class NLP {
    static StanfordCoreNLP pipeline;

    public static void init() {
        InputStream input = NLP.class.getClass().getResourceAsStream("/example.properties");
        Properties prop = new Properties();
        try {
          prop.load(input);
        } catch (IOException e) {
          e.printStackTrace();
        }

        pipeline = new StanfordCoreNLP(prop);
    }
}

Solution

  • Having the following structure:

    src/main/resources
                 |
                  - example.properties
    

    Executing the code below will solve your problem:

    public class Main {
    
        public static void main(String[] args) {
            NLP.init();
        }
    }
    
    class NLP {
       static StanfordCoreNLP pipeline;
    
       public static void init() {
            InputStream input = NLP.class.getClass().getResourceAsStream("/example.properties");
            Properties prop = new Properties();
            try {
                prop.load(input);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            pipeline = new StanfordCoreNLP(prop);
        }
    }
    

    EDIT

    Your pom.xml must contain the following dependencies:

    <dependencies>
        <dependency>
            <groupId>edu.stanford.nlp</groupId>
            <artifactId>stanford-corenlp</artifactId>
            <version>3.6.0</version>
        </dependency>
        <dependency>
            <groupId>edu.stanford.nlp</groupId>
            <artifactId>stanford-corenlp</artifactId>
            <version>3.6.0</version>
            <classifier>models</classifier>
        </dependency>
    </dependencies>