2017-08-16 46 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