2017-08-16 14 views
1

NLP圖書館總是返回情緒整數-1NLP總是返回情緒爲-1

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; 

public class NLP { 
    static StanfordCoreNLP pipeline; 

    public static void init() { 
     pipeline = new StanfordCoreNLP("MyPropFile.properties"); 
    } 

    public static int findSentiment(String tweet) { 

     int mainSentiment = 0; 
     if (tweet != null && tweet.length() > 0) { 
      int longest = 0; 
      Annotation annotation = pipeline.process(tweet); 
      for (CoreMap sentence : annotation 
        .get(CoreAnnotations.SentencesAnnotation.class)) { 
       Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class); 
       int sentiment = RNNCoreAnnotations.getPredictedClass(tree); 
       String partText = sentence.toString(); 
       if (partText.length() > longest) { 
        mainSentiment = sentiment; 
        longest = partText.length(); 
       } 

      } 
     } 
     return mainSentiment; 
    } 
} 

無論我通過什麼樣的句子,它總是返回爲-1。 例: 「谷歌是很好的」 返回-1 「谷歌是壞」 返回-1

+0

你的屬性文件是什麼樣的? –

+0

annotators =標記化,ssplit,parse,sentiment – enemyofnone

+0

我的答案是否解決了問題? –

回答

1

此行

Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class); 

只需切換到該

Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class); 

現在,它應該工作。

+0

你能告訴我哪個罐子有嗎?似乎它仍然是拋出錯誤... – enemyofnone

+0

工作,有問題與罐子版本:D – enemyofnone