- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentimentAnalyzer.java
More file actions
Latest commit
104 lines (101 loc) · 4.04 KB
/
Copy pathSentimentAnalyzer.java
File metadata and controls
104 lines (101 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
importjava.net.URISyntaxException;
importjava.net.MalformedURLException;
importjava.util.Properties;
importedu.stanford.nlp.ie.AbstractSequenceClassifier;
importedu.stanford.nlp.ie.crf.*;
importedu.stanford.nlp.io.IOUtils;
importedu.stanford.nlp.ling.CoreLabel;
importedu.stanford.nlp.ling.CoreAnnotations;
importedu.stanford.nlp.sequences.DocumentReaderAndWriter;
importedu.stanford.nlp.util.Triple;
importedu.stanford.nlp.ling.CoreAnnotations;
importedu.stanford.nlp.pipeline.Annotation;
importedu.stanford.nlp.pipeline.StanfordCoreNLP;
importedu.stanford.nlp.rnn.RNNCoreAnnotations;
importedu.stanford.nlp.sentiment.SentimentCoreAnnotations;
importedu.stanford.nlp.trees.Tree;
importedu.stanford.nlp.util.CoreMap;
importcom.cybozu.labs.langdetect.Detector;
importcom.cybozu.labs.langdetect.DetectorFactory;
importcom.cybozu.labs.langdetect.LangDetectException;
importcom.cybozu.labs.langdetect.Language;
publicclassSentimentAnalyzer {
publicTweetWithSentimentfindSentiment(Stringline) {
Propertiesprops = newProperties();
props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
StanfordCoreNLPpipeline = newStanfordCoreNLP(props);
StringserializedClassifier = "classifiers/english.all.3class.distsim.crf.ser.gz";
intmainSentiment = 0;
if (line != null && line.length() > 0) {
intlongest = 0;
Annotationannotation = pipeline.process(line);
for (CoreMapsentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
Treetree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
intsentiment = RNNCoreAnnotations.getPredictedClass(tree);
StringpartText = sentence.toString();
if (partText.length() > longest) {
mainSentiment = sentiment;
longest = partText.length();
}
}
}
if (mainSentiment == 2 || mainSentiment > 4 || mainSentiment < 0) {
returnnull;
}
TweetWithSentimenttweetWithSentiment = newTweetWithSentiment(line, toCss(mainSentiment));
returntweetWithSentiment;
}
privateStringtoCss(intsentiment) {
switch (sentiment) {
case0:
return"Muy negativo";
case1:
return"Negativo";
case2:
return"Advertencia";
case3:
return"Positivo";
case4:
return"Muy Positivo";
default:
return"";
}
}
publicTweetWithSentimentgetSentiment(Stringtext,Stringlang){
Traductort = newTraductor();
TweetWithSentimenttweetWithSentiment = null;
Stringsentiment = "";
if(lang.equalsIgnoreCase("en")){
tweetWithSentiment = this.findSentiment(text.replace("'","''"));
sentiment = tweetWithSentiment.toString();
}else{
if(lang.equalsIgnoreCase("es")){
try{
tweetWithSentiment = this.findSentiment(t.traducir(text,"es","en").replace("'","''"));
} catch(MalformedURLExceptionf){
f.printStackTrace();
} catch(URISyntaxExceptionu){
u.printStackTrace();
}
}
}
returntweetWithSentiment;
}
publicStringgetLang(Stringt){
LangDetectrun = newLangDetect();
Stringlang = "";
try {
run.init("lib/profiles");
lang = run.detect(t);
} catch (LangDetectExceptione) {
e.printStackTrace();
}
returnlang;
}
publicstaticvoidmain(Stringargs[]) {
Stringlang = "";
SentimentAnalyzersentimentAnalyzer = newSentimentAnalyzer();
lang = sentimentAnalyzer.getLang(args[0]);
System.out.println(sentimentAnalyzer.getSentiment(args[0],lang));
}
}