Issue
I am trying to run the code provided in the tutorial. However, I ran into the following errors:
0 [main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator tokenize
9 [main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator ssplit
14 [main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator parse
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 stanford.corenlp@4.4.0/edu.stanford.nlp.parser.common.ParserGrammar.loadModel(ParserGrammar.java:199)
at stanford.corenlp@4.4.0/edu.stanford.nlp.pipeline.ParserAnnotator.loadModel(ParserAnnotator.java:227)
at stanford.corenlp@4.4.0/edu.stanford.nlp.pipeline.ParserAnnotator.<init>(ParserAnnotator.java:126)
at stanford.corenlp@4.4.0/edu.stanford.nlp.pipeline.AnnotatorImplementations.parse(AnnotatorImplementations.java:135)
at stanford.corenlp@4.4.0/edu.stanford.nlp.pipeline.StanfordCoreNLP.lambda$getNamedAnnotators$14(StanfordCoreNLP.java:574)
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.<init>(StanfordCoreNLP.java:278)
at stanford.corenlp@4.4.0/edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:194)
at stanford.corenlp@4.4.0/edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:190)
at org.example/org.example.Test.init(Test.java:19)
at org.example/org.example.SentenceSentiment.main(SentenceSentiment.java:12)
Caused by: java.io.IOException: Unable to open "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz" 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.io.IOUtils.readObjectFromURLOrClasspathOrFileSystem(IOUtils.java:309)
at stanford.corenlp@4.4.0/edu.stanford.nlp.parser.common.ParserGrammar.loadModel(ParserGrammar.java:196)
... 13 more
Process finished with exit code 1
There are several answers on the error message
java.io.IOException: Unable to open "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz" as class path, filename or URL
I checked of them but still could not resolve the issue. Specifically, I made sure the following:
- The
pom.xmlis correctly set as following.
<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>
- The
stanford-corenlp-4.4.0-models.jarfile is indeed downloaded in.m2/repository
4.4.0/
├── _remote.repositories
├── stanford-corenlp-4.4.0.jar
├── stanford-corenlp-4.4.0.jar.sha1
├── stanford-corenlp-4.4.0-models.jar
├── stanford-corenlp-4.4.0-models.jar.sha1
├── stanford-corenlp-4.4.0.pom
└── stanford-corenlp-4.4.0.pom.sha1
Question
I am not sure what I am missing and it caused these errors.
Code
SentenceSentiment.java
package org.example;
import org.apache.log4j.BasicConfigurator;
public class SentenceSentiment
{
public static void main(String[] args)
{
BasicConfigurator.configure();
String text = "This is an excellent book. I enjoy reading it. I can read on Sundays. Today is only Tuesday. Can't wait for next Sunday. The working week is unbearably long. It's awful.";
Test.init();
Test.estimatingSentiment(text);
}
}
Test.java
package org.example;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.util.CoreMap;
import java.util.Properties;
public class Test {
static StanfordCoreNLP pipeline;
public static void init()
{
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
pipeline = new StanfordCoreNLP(props);
}
public static void estimatingSentiment(String text)
{
int sentimentInt;
String sentimentName;
Annotation annotation = pipeline.process(text);
for(CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class))
{
Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);
sentimentInt = RNNCoreAnnotations.getPredictedClass(tree);
sentimentName = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
System.out.println(sentimentName + "\t" + sentimentInt + "\t" + sentence);
}
}
}