Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. import java.util.Properties;
  2.  
  3.  
  4. import edu.stanford.nlp.ling.CoreAnnotations;
  5. import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
  6. import edu.stanford.nlp.pipeline.Annotation;
  7. import edu.stanford.nlp.pipeline.StanfordCoreNLP;
  8. import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
  9. import edu.stanford.nlp.trees.Tree;
  10. import edu.stanford.nlp.util.CoreMap;
  11.  
  12. public class StanfordSentiment {
  13.  
  14.  
  15. StanfordCoreNLP pipeline;
  16.  
  17.  
  18.  
  19. public StanfordSentiment(){
  20. Properties props = new Properties();
  21. props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
  22.  
  23. pipeline = new StanfordCoreNLP(props);
  24.  
  25.  
  26. }
  27.  
  28. public float calculateSentiment (String text) {
  29.  
  30.  
  31. float mainSentiment = 0;
  32.  
  33. int longest = 0;
  34. Annotation annotation = pipeline.process(text);
  35. for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
  36. Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
  37. int sentiment = RNNCoreAnnotations.getPredictedClass(tree) - 2;
  38. String partText = sentence.toString();
  39. if (partText.length() > longest) {
  40. mainSentiment = sentiment;
  41. longest = partText.length();
  42. }
  43.  
  44. }
  45.  
  46. return mainSentiment;
  47.  
  48.  
  49.  
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement