0

I wanted to get started with the StanfordCorenlp library in Java. I added the edu.stanford.nlp.corenlp:4.4.0 library with the correct dependencies to the project. After that I wrote a simple program that should display the sentiment of a couple of sentences. I made two classes: Pipeline and SentimentAnalysis.

Pipeline class:

public class Pipeline {

    private static Properties properties;
    private static String propertiesName = "tokenize, ssplit, pos, lemma, ner";
    private static StanfordCoreNLP stanfordCoreNLP;

    private Pipeline() { }

    static {
        properties = new Properties();
        properties.setProperty("annotators", propertiesName);
    }

    public static StanfordCoreNLP getPipeline(){
        if (stanfordCoreNLP == null){
            stanfordCoreNLP = new StanfordCoreNLP(properties);
        }
        return stanfordCoreNLP;
    }
}

SentimentAnalysis class:

 public class SentimentAnlysis {
    
        public static void main(String[] args) {
    
            StanfordCoreNLP stanfordCoreNLP = Pipeline.getPipeline();
    
            String text = "Hello i hate this shit. My name is Johnson";
    
            CoreDocument coreDocument = new CoreDocument(text);
    
            stanfordCoreNLP.annotate(coreDocument);
    
            List<CoreSentence> sentences = coreDocument.sentences();
    
            for (CoreSentence sentence : sentences){
    
                String sentiment = sentence.sentiment();
    
                System.out.println(sentiment + "\t" + sentence);
    
            }
        }
    }

pom.xml

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

When i run my code i get the following error.

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" edu.stanford.nlp.io.RuntimeIOException: Error while loading a tagger model (probably missing model file)
at stanford.corenlp@4.4.0/edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:798)
at stanford.corenlp@4.4.0/edu.stanford.nlp.tagger.maxent.MaxentTagger.(MaxentTagger.java:322)
at stanford.corenlp@4.4.0/edu.stanford.nlp.tagger.maxent.MaxentTagger.(MaxentTagger.java:275)
at stanford.corenlp@4.4.0/edu.stanford.nlp.pipeline.POSTaggerAnnotator.loadModel(POSTaggerAnnotator.java:85)
at stanford.corenlp@4.4.0/edu.stanford.nlp.pipeline.POSTaggerAnnotator.(POSTaggerAnnotator.java:73)
at stanford.corenlp@4.4.0/edu.stanford.nlp.pipeline.AnnotatorImplementations.posTagger(AnnotatorImplementations.java:75)
at stanford.corenlp@4.4.0/edu.stanford.nlp.pipeline.StanfordCoreNLP.lambda$getNamedAnnotators$6(StanfordCoreNLP.java:566)
at stanford.corenlp@4.4.0/edu.stanford.nlp.pipeline.StanfordCoreNLP.lambda$constructAnnotatorPool$33(StanfordCoreNLP.java:647)
at stanford.corenlp@4.4.0/edu.stanford.nlp.util.Lazy$3.compute(Lazy.java:126)
at stanford.corenlp@4.4.0/edu.stanford.nlp.util.Lazy.get(Lazy.java:31)
at stanford.corenlp@4.4.0/edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:149)
at stanford.corenlp@4.4.0/edu.stanford.nlp.pipeline.StanfordCoreNLP.(StanfordCoreNLP.java:278)
at stanford.corenlp@4.4.0/edu.stanford.nlp.pipeline.StanfordCoreNLP.(StanfordCoreNLP.java:194)
at stanford.corenlp@4.4.0/edu.stanford.nlp.pipeline.StanfordCoreNLP.(StanfordCoreNLP.java:190)
at com.example.oefenenstanford/com.example.oefenenstanford.Pipeline.getPipeline(Pipeline.java:22)
at com.example.oefenenstanford/com.example.oefenenstanford.SentimentAnlysis.main(SentimentAnlysis.java:13)
Caused by: java.io.IOException: Unable to open "edu/stanford/nlp/models/pos-tagger/english-left3words-distsim.tagger" as class path, filename or URL
at stanford.corenlp@4.4.0/edu.stanford.nlp.io.IOUtils.getInputStreamFromURLOrClasspathOrFileSystem(IOUtils.java:501)
at stanford.corenlp@4.4.0/edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:795)

I have no idea what is causing this problem.

Janez Kuhar
  • 3,705
  • 4
  • 22
  • 45
jakoboo1
  • 1
  • 1
  • How are you building and running your project? What maven commands have you used? – Janez Kuhar Oct 17 '22 at 16:23
  • I simply [built a fat jar](https://stackoverflow.com/questions/16222748/building-a-fat-jar-using-maven) from your example and then ran: `java -cp ./target/* your.package.name.SentimentAnalysis`. The code compiled and executed normally. – Janez Kuhar Oct 17 '22 at 16:31

0 Answers0