【问题标题】:corenlp sentiment Java program via Py4j in Python, raises errorscorenlp 情绪 Java 程序通过 Python 中的 Py4j,引发错误
【发布时间】:2016-09-22 20:35:43
【问题描述】:

我通过 Py4j 制作了一个用于 Python 的 Java 情感分析程序。我可以在 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)

NullPointerException

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)

【问题讨论】:

  • @sidgate 谢谢。我读了这篇文章。对象启动不需要输入。在方法 findSentiment 中确实需要输入字符串,为此我在 Python 代码中添加了该输入。还是有这个问题。

标签: java python stanford-nlp py4j


【解决方案1】:

我相信你忘了调用 init()。

这似乎是第 43 行(查看您的堆栈跟踪):

Annotation annotation = pipeline.process(tweet);

管道很可能为空,因为它是在 init() 中实例化的,而 init() 并未在 main() 方法或 Python 中调用。

【讨论】:

  • 谢谢。虽然我给出了其他一些错误。但这将解决我要问的问题;-)。顺便说一句..我是 Java 新手。我主要使用 Python,与 Java 相比,它真的让我懒得去想很多事情......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-11
  • 1970-01-01
  • 2015-11-01
  • 2016-04-24
  • 1970-01-01
  • 1970-01-01
  • 2013-01-24
相关资源
最近更新 更多