java - corenlp 情绪通过 Python 中的 Py4j Java 程序引发错误

标签 java python stanford-nlp py4j

我制作了一个 Java 情感分析程序,通过 Py4j 在 Python 中使用。我可以在 Python 中创建 Java 对象,但尝试访问该方法时,它给出了 java.lang.NullPointerException。你能帮忙吗?谢谢。

Java 代码:编译正确,运行没有错误。

import java.util.List;
import java.util.Properties;
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.SentimentAnnotatedTree;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.util.ArrayCoreMap;
import edu.stanford.nlp.util.CoreMap;
import py4j.GatewayServer;
import org.ejml.simple.SimpleMatrix;


public class CoreNLPSentiScore {
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 main(String[] args) {
        CoreNLPSentiScore app = new CoreNLPSentiScore();
        // app is now the gateway.entry_point
        GatewayServer server = new GatewayServer(app);
        server.start();
    }

    //public static void main(String tweet) {
    //public static String findSentiment(String tweet) {
    public String findSentiment(String tweet) {
        //String SentiReturn = "2";
        //String[] SentiClass ={"very negative", "negative", "neutral", "positive", "very positive"};

        //Sentiment is an integer, ranging from 0 to 4. 
        //0 is very negative, 1 negative, 2 neutral, 3 positive and 4 very positive.
        //int sentiment = 2;
        SimpleMatrix senti_score = new SimpleMatrix();
        if (tweet != null && tweet.length() > 0) {
            Annotation annotation = pipeline.process(tweet);

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

                ArrayCoreMap sentence = (ArrayCoreMap) sentences.get(0);                
                //Tree tree = sentence.get(SentimentAnnotatedTree.class);  
                Tree tree = sentence.get(SentimentAnnotatedTree.class);  
                senti_score = RNNCoreAnnotations.getPredictions(tree);             

                //SentiReturn = SentiClass[sentiment];
            }
        }
        //System.out.println(senti_score);
        return senti_score.toString();
        //System.out.println(senti_score);
    }

}

Python 代码:

from py4j.java_gateway import JavaGateway
gateway = JavaGateway()
app = gateway.entry_point
test = 'i like this product'
app.findSentiment(test)

空指针异常

Traceback (most recent call last):

  File "<ipython-input-1-cefdf18ada67>", line 5, in <module>
    app.findSentiment(test)

  File "C:\Anaconda3\envs\sandbox\lib\site-packages\py4j\java_gateway.py", line 1026, in __call__
    answer, self.gateway_client, self.target_id, self.name)

  File "C:\Anaconda3\envs\sandbox\lib\site-packages\py4j\protocol.py", line 316, in get_return_value
    format(target_id, ".", name), value)

Py4JJavaError: An error occurred while calling t.findSentiment.: java.lang.NullPointerException
at CoreNLPSentiScore.findSentiment(CoreNLPSentiScore.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:237)
at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
at py4j.Gateway.invoke(Gateway.java:280)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:214)
at java.lang.Thread.run(Unknown Source)

最佳答案

我相信您忘记调用 init()。

这似乎是第 43 行(请参阅堆栈跟踪):

Annotation annotation = pipeline.process(tweet);

pipeline 可能为 null,因为它是在 init() 中实例化的,并且 init() 不是在 main() 方法中或从 Python 中调用的。

关于java - corenlp 情绪通过 Python 中的 Py4j Java 程序引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39648414/

相关文章:

java - 在java中使用Stanford postagger,得到java.lang.InknownClassChangeError

stanford-nlp - CoreNLP 加载语言特定属性失败

java - Stanford Dependency Parser - 如何获得跨度?

java - 根据请求体中的数据转发http请求

Python填充字符串列 "forward"并将groupby结果附加到数据帧

java - 定期刷新 JavaFX TableView

python - RandomForestClassifier .fit 在 ec2 上因内存错误而失败,但在本地运行时没有错误

python - 在新的 subprocess.Popen 对象列表上使用 poll()

java - 将消息从 JAVA 放入队列

java - 用户凭据与数据库的比较