2015-12-13 2 views
0

Это вопрос использования StanfordCoreNLP для анализа настроений. Я не уверен, основанный на моем исследовании «SentimentCoreAnnotations.AnnotatedTree.class» был изменен на «SentimentCoreAnnotations.SentimentAnnotatedTree.class». Потому что я получаю «SentimentCoreAnnotations.AnnotatedTree не может быть разрешен для типа». Однако, когда я перешел на «SentimentCoreAnnotations.SentimentAnnotatedTree.class», я получил NULL. Может кто-то прояснить? Спасибо! Я использую ниже код, доступный в Интернете. Я обнаружил, что большинство реализаций схожи. Я сталкиваюсь с той же проблемой почти во всех реализациях, которые я пробовал.Использование StanfordCoreNLP в анализе настроений

package crawler; 
import java.util.Properties; 
import org.ejml.simple.SimpleMatrix; 
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.sentiment.SentimentCoreAnnotations.SentimentAnnotatedTree; 
import edu.stanford.nlp.trees.Tree; 
import edu.stanford.nlp.util.CoreMap; 
import java.util.List; 
import java.util.Properties; 
import edu.stanford.*; 


public class NLP { 
    public static void main(String[] args) { 
     findSentiment("life is good."); 
    } 

    public static void findSentiment(String line) { 

     Properties props = new Properties(); 
     props.setProperty("annotators", "tokenize, ssplit, parse, sentiment"); 
     StanfordCoreNLP pipeline = new StanfordCoreNLP(props); 
     int mainSentiment = 0; 

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

      } 
     } 
     if (mainSentiment == 2 || mainSentiment > 4 || mainSentiment < 0) { 
      System.out.println("Neutral " + line); 
     } 
     else{ 
     } 
     /* 
     * TweetWithSentiment tweetWithSentiment = new TweetWithSentiment(line, 
     * toCss(mainSentiment)); return tweetWithSentiment; 
     */ 

    } 
} 

ответ

0
  1. Скачать Стэнфорд CoreNLP 3.6.0 отсюда: http://stanfordnlp.github.io/CoreNLP/

  2. Некоторые примеры кода доступа настроения:

    import edu.stanford.nlp.hcoref.CorefCoreAnnotations; 
    import edu.stanford.nlp.hcoref.data.CorefChain; 
    import edu.stanford.nlp.hcoref.data.Mention; 
    import edu.stanford.nlp.ling.CoreAnnotations; 
    import edu.stanford.nlp.pipeline.Annotation; 
    import edu.stanford.nlp.sentiment.SentimentCoreAnnotations.*; 
    import edu.stanford.nlp.pipeline.StanfordCoreNLP; 
    import edu.stanford.nlp.util.CoreMap; 
    
    import java.util.Properties; 
    
    public class SentimentExample { 
    
        public static void main(String[] args) throws Exception { 
    
         Annotation document = new Annotation("The movie was great!"); 
         Properties props = new Properties(); 
         props.setProperty("annotators", "tokenize, ssplit, parse, sentiment"); 
         StanfordCoreNLP pipeline = new StanfordCoreNLP(props); 
         pipeline.annotate(document); 
         for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) { 
          System.out.println("---"); 
          System.out.println(sentence); 
          System.out.println(sentence.get(SentimentAnnotatedTree.class)); 
          System.out.println(sentence.get(SentimentClass.class)); 
    
         } 
        } 
    } 
    
+0

Я работал с 3.5 (3.6 не может быть найденный в репозитории maven.Спасибо! Один быстрый вопрос. Значение оценки - NaN из tree.score() дерева Tree = sentence.get (Sen timentAnnotatedTree.class); Как получить правильную оценку? Благодаря! –

Смежные вопросы