-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnalyzeSentiment.java
256 lines (215 loc) · 6.54 KB
/
AnalyzeSentiment.java
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import twitter4j.*;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Collections;
import java.util.Scanner;
import java.util.StringTokenizer;
public class AnalyzeSentiment{
private int nums;
public static RedBlackBST<String,Double> sentiments;
private static List<Status> tw;
private static SearchTweets dd = new SearchTweets();
//private static QueryResult st;
private ArrayList<Double> avg = new ArrayList<Double>();
private static String[] words;
/*public static RedBlackBST loadSentiments(){
sentiments = new RedBlackBST<String,Double>();
//RedBlackBST<Value> value = new RedBlackBST<Value>();
File infile = new File("sentiments.csv");
Scanner scanner = null;
try{
scanner = new Scanner(infile);
}catch (FileNotFoundException ex){
System.err.println("Error: locations file not found.");
}
while(scanner.hasNextLine()){
String line = scanner.nextLine();
StringTokenizer tokenizer = new StringTokenizer(line);
String[] field = line.split(",");
double score = Double.parseDouble(field[1]);
Sentiments senti = new Sentiments(field[0],score);
sentiments.put(senti.getSenti(), senti.getScore());
}
scanner.close();
return sentiments;
}*/
public static RedBlackBST<String,Double> loadSentiments(String fname){
sentiments = new RedBlackBST<String,Double>();
//RedBlackBST<Value> value = new RedBlackBST<Value>();
File infile = new File(fname);
Scanner scanner = null;
try{
scanner = new Scanner(infile);
}catch (FileNotFoundException ex){
System.err.println("Error: locations file not found.");
return null;
}
while(scanner.hasNextLine()){
String line = scanner.nextLine();
StringTokenizer tokenizer = new StringTokenizer(line);
String[] field = line.split(",");
double score = Double.parseDouble(field[1]);
Sentiments senti = new Sentiments(field[0],score);
sentiments.put(senti.getSenti(), senti.getScore());
}
scanner.close();
return sentiments;
}
public AnalyzeSentiment(){
}
/*public static void collectTweets(){
GeoLocation gl = new GeoLocation(51.51316,-0.13118);
SearchTweets tweets= new SearchTweets(new Query("gay"),
50,gl,30,Query.ResultType.recent);
try{
tweets.save("AllTweets.ser");
}catch(IOException ex){
ex.printStackTrace();
}catch(ClassNotFoundException e){
System.err.println("Error:Class not found");
}
}*/
/*public void load(String filename) throws IOException{
try{
FileInputStream tem = new FileInputStream(filename);
ObjectInputStream doi = new ObjectInputStream(tem);
Object twp = doi.readObject();
while(twp != null){
Boolean added = false;
for(Status t : tw){
if(t.getText().equals(((Status)twp).getText()) && t.getUser().equals(((Status)twp).getUser()))
added = true;
if(added = false)
tw.add((Status)twp);
twp = doi.readObject();
}
}
doi.close();
tem.close();
}catch(IOException ex){
System.err.println("Error: locations file not found.");
}catch(ClassNotFoundException e){
System.err.println("Error:Class not found");
}
}*/
public static Double analyzeTweets(){
//tweets = tweets.load("AllTweets.ser");
try{
tw = dd.load("AllTweets.ser");
}catch(IOException e){
System.err.println("Error: locations file not found.");
}
//tw = "I love dog and i have computer.";
System.out.println("The num of elements is:" + tw != null);
System.out.println("size of tw"+tw.size());
double count = 0.0;
double ave = 0.0;
double getvalue = 0.0;
//File infile = new File("sentiments.csv");
//st.load("AllTweets.dat");
//String[] lines = Files.readAllLines(new File("AllTweets.dat").toPath()).toArray(new String[0]);
//tweets = st.load("AllTweets.dat").getTweets();
for(int m = 0; m < tw.size(); m++){
Status t = tw.get(m);
String s = t.getText();
System.out.println(s);
words = s.toLowerCase().replaceAll("^\\w]","").split("\\s+");
for(int i = 0; i<words.length; i++){
//getvalue = sentiments.get(words[i]);
//System.out.println(getvalue + " ");
if(sentiments.get(words[i]) != null){
count += sentiments.get(words[i]);
System.out.println(count+ " ");
}
}
ave = count/words.length;
//avg.add(ave);
//double k = sentiments.get()
}
//}
return ave;
}
/*public static SearchTweets getRandomTweets(){
GeoLocation gl = new GeoLocation(51.51316,-0.13118);
SearchTweets tweets= new SearchTweets(new Query(" "),
10000,gl,30,Query.ResultType.recent);
try{
tweets.save("AllTweets.dat");
}catch(IOException ex){
ex.printStackTrace();
}
return
}*/
/*public static Integer getTargetTweets(){
String words = "gay";
int counter = 0;
for(Status t : tw){
String s = t.getText();
if(s.toLowerCase().indexOf(words.toLowerCase()) != -1){
counter++;
System.out.println(counter + " ");
}
}
return counter;
}*/
/*
public static Double analyzeTweetsfq(){
double targetTweets = 0.0;
double totalTweets = 0.0;
double fq;
targetTweets = getTargetTweets();
totalTweets = 50;
fq = targetTweets/totalTweets;
System.out.println(fq + " ");
return fq;
}*/
public static void main(String[] args){
//GeoLocation gl = new GeoLocation(22.27832,114.17469); // London SoHo
//dd.collectTweets(100,"gay",gl,20,Query.ResultType.recent);
//tw = dd.load("AllTweets.ser");
loadSentiments("sentiments.csv");
analyzeTweets();
//System.out.println(tw.size());
//double k = 0;
if(args.length != 0){
System.out.println("java SearchTweets");
System.exit(-1);
}
//RedBlackBST<String,Double> sentiments = l
/* AnalyzeSentiment target = new AnalyzeSentiment();
target.loadSentiments();
//target.getTargetTweets();
try{
FileInputStream tempp = new FileInputStream("AllTweets.dat");
ObjectInputStream douu = new ObjectInputStream(tempp);
Object twp = douu.readObject();
while(twp != null){
Boolean added = false;
for(Status t : tweets){
if(t.getText().equals(((Status)twp).getText()) && t.getUser().equals(((Status)twp).getUser()))
k = target.analyzeTweets(t);
System.out.println(k + " ");
}
}
douu.close();
tempp.close();
}catch(IOException ex){
System.err.println("Error: locations file not found.");
}catch(ClassNotFoundException e){
System.err.println("Error:Class not found");
}
//target.analyzeTweetsfq();
/*for(int i = 0; i < sentiments.size(); i++){
if(sentiments.get(i).getScore())
}*/
}
}