2016-09-22 3 views
0

Py4jを使ってPythonでJavaのセンチメント解析プログラムを使用しました。私はPythonでJavaオブジェクトを作成できますが、メソッドにアクセスしようとするとjava.lang.NullPointerExceptionが発生します。あなたは助けてくれますか?ありがとう。corenlpの感情PythonでPy4j経由のJavaプログラムでエラーが発生する

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) 
+0

[NullPointerExceptionとは何ですか?それを修正するにはどうすればいいですか?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix -it) – sidgate

+0

@sidgateありがとう。私はポストを読んだ。オブジェクトの開始は入力を必要としません。 findSentimentメソッドでは、Pythonコードで追加したString入力が必要です。まだこの問題があります。 –

答えて

1

を私はあなたが()のinitを呼び出すために忘れてしまったと考えています。

これは、ライン#43のようです(自分のスタックトレースを参照してください):それは(INITでインスタンス化された)とinit()がメインで呼び出されていないため

Annotation annotation = pipeline.process(tweet); 

パイプラインがnullである可能性が高いです( )メソッドまたはPythonから取得します。

+0

ありがとうございます。私はいくつかの間違いをしています。しかし、これは私が求めている問題を解決するだろう;-)。 Btw ..私はJavaに新しいです。私は主にPythonをJavaに比べて使用していますが、実際にはたくさんのことについて考えるのが怠惰です... –

関連する問題