-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchTweetsNew copy.java
445 lines (373 loc) · 12.4 KB
/
SearchTweetsNew copy.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
* Class to support search of Twitter's tweets using twitter4j API.
* Concatenates tweets to a file.
*/
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;
/**
* Constructing a SearchTweets object will perform a single search
* and store them in a List.
*/
public class SearchTweetsNew {
private static final int MAXTWEETS = 100; // max tweets at once
private static List<Status> tweets; // tweets from query
private Query query; // query that generated the tweets
private static RedBlackBST<String,Double> sentiments;
private static String[] words;
/**
* Generate tweets based on typical Twitter search string.
* @param querystring (See https://dev.twitter.com/docs/using-search )
*/
public SearchTweetsNew(String querystring) {
this(new Query(querystring),20,null,0.0,Query.ResultType.recent);
}
public SearchTweetsNew(){
}
/**
* Retrieve tweets based on typical Twitter search string.
* @param query valid Query object
* @param count number of tweets to get (max 100)
* @param loc Central point of tweets (null to get all tweets)
* @param radius Radius (in km) of locations searched
* @param type type of tweets to get
*/
public SearchTweetsNew(Query query,int count, GeoLocation loc,
double radius, Query.ResultType type) {
this.query = query;
if (count > 0 && count < MAXTWEETS) // limit tweets we get
query.setCount(count);
query.setResultType(type);
if (loc != null) query.setGeoCode(loc,radius,Query.KILOMETERS);
Twitter twitter = new TwitterFactory().getInstance();
QueryResult result = null;
try {
result = twitter.search(query);
tweets = result.getTweets(); // store retrieved tweets
}
catch (TwitterException te) {
tweets = null;
te.printStackTrace();
System.out.println("Failed to search tweets: " + te.getMessage());
}
}
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 static Double analyzeTweets(){
System.out.println("The num of elements is:" + tweets != null);
System.out.println("size of tweets "+tweets.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 < tweets.size(); m++){
Status t = tweets.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]);
}
}
ave = count/words.length;
System.out.println(ave+ " ");
//avg.add(ave);
//double k = sentiments.get()
}
return ave;
//}
}
//return the value associated with the given key;
/* public Integer getTargetTweets(){
String words = "you";
int counter = 0;
for(Status t : tweets){
String s = t.getText();
if(s.toLowerCase().indexOf(words.toLowerCase()) != -1){
counter++;
System.out.println(counter + " ");
}
}
return counter;
}*/
/*public Double analyzeTweetsfq(){
double targetTweets = 0.0;
double totalTweets = 0.0;
double fq;
targetTweets = getTargetTweets();
totalTweets = tweets.size();
fq = targetTweets/totalTweets;
System.out.println(fq + " ");
return fq;
}*/
/**
* @return number of tweets in this object
*/
public int numTweets() {
if (tweets == null) return 0;
return tweets.size();
}
/**
* Add a method to return an arraylist consisting of every userid and the
* number of tweets by that user.
*/
public ArrayList<UserData> getCounts() {
if (tweets == null) return null;
ArrayList<String> results = new ArrayList<String>();
// get info about all tweets
for (Status tweet : tweets) {
String name = tweet.getUser().getScreenName();
results.add(name);
}
Collections.sort(results);
ArrayList<UserData> userdata = new ArrayList<UserData>();
// Count tweets by username, assume userdata is sorted by name.
for (int i = 0; i < results.size(); i++) {
String name = results.get(i);
int count = 0;
// loop over all of same name, update count
while (i < results.size() && name.equals(results.get(i))) {
i++;
count++;
}
userdata.add(new UserData(name,count));
i--;
}
return userdata;
}
/**
* Print all tweets to standard output.
*/
public void print() {
if (tweets == null) return;
// Print out info about all tweets
for (Status tweet : tweets) {
System.out.println("\n\n@" + tweet.getUser().getScreenName() +
" - " + tweet.getText());
System.out.println("date: " + tweet.getCreatedAt());
//System.out.println("User: " + tweet.getUser());
System.out.println("Reply to: " + tweet.getInReplyToScreenName());
}
}
/**
* Save all tweets to a file. Saves all Status data
* as serialized objects.
* @param filename Name of output file. Will append
* all unique tweets to any file of same name.
*/
public void save(String filename) {
// TODO
try {
//Twitter tw = new Twitter();
FileOutputStream temp = new FileOutputStream(filename);
ObjectOutputStream dou = new ObjectOutputStream(temp);
for(Status t : tweets){
dou.writeObject(t);
}
temp.close();
dou.close();
}catch(EOFException ef){
System.err.println("Error: locations file not found.");
}
catch (IOException ex) {
System.err.println("Error: locations file not found.");
}
}
/**
* Load tweets from a saved file of Status objects.
* Appends to current this.tweets all tweets not already stored.
* @param filename Name of input file.
*/
public List<Status> load(String filename) throws IOException {
ObjectInputStream objectinputstream = null;
try{
FileInputStream tempp = new FileInputStream(filename);
objectinputstream = new ObjectInputStream(tempp);
List<Status> read = (List<Status>) objectinputstream.readObject();
//Object twp = douu.readObject();
//Status twq = (Status)twp;
//while(twp != null){
//Boolean added = false;
/*for(Status t : read){
if(!tweets.contains(t)){
tweets.add(t);
System.out.println("added text");
}
} */
return read;
}catch(IOException e){
e.printStackTrace();
}catch(Exception e){
}
return null;
// TODO
}
/* public void load(String filename) throws IOException {
try{
FileInputStream tempp = new FileInputStream(filename);
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()))
added = true;
}
if(added == false)
tweets.add((Status)twp);
twp = douu.readObject();
//twp = douu.readObject();
}
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");
}
// TODO
}*/
/**
* Load location data from a file.
* @param fname File from which to load data
* @return Arraylist of all cities in the file.
*/
public static ArrayList<Location> loadLocations(String fname) {
ArrayList<Location> locations = new ArrayList<Location>();
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;
}
// Loop over lines and split into fields we keep.
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
StringTokenizer tokenizer = new StringTokenizer(line);
String[] fields = line.split(",");
long pop = Long.parseLong(fields[4]);
if (pop > 100000) { // Ignore small towns.
// See Location.java to figure out these fields.
Location loc = new Location(fields[0],fields[1],
Double.parseDouble(fields[2]),Double.parseDouble(fields[3]),pop);
locations.add(loc);
//System.out.println("Added " + loc.toString());
}
}
scanner.close();
return locations;
}
/*public List<Status> getTwee(){
}*/
/**
* Collect tweets and add all resulting tweets to a file.
* @param numtweets Max number of tweets to gather
* @param searchwords String of all words that will form twitter searches
* @param loc Location to search for tweets
* @param radius Radius (kilometers) about loc to search
* @param type Type of results to find
*/
public static void collectTweets(int numtweets, String searchwords, GeoLocation loc,
double radius, Query.ResultType type, String outfile) {
StringTokenizer searchtok = new StringTokenizer(searchwords);
while (searchtok.hasMoreTokens()) {
String word = searchtok.nextToken();
SearchTweetsNew tweets= new SearchTweetsNew(new Query(word),numtweets,loc,radius, type);
System.out.println("Num Tweets: " + tweets.numTweets());
tweets.print();
tweets.save(outfile);
}
}
/**
* Usage: java twitter4j.examples.search.SearchTweets [query]
*
* @param args
*/
public static void main(String[] args) {
if (args.length != 0) {
System.out.println("java SearchTweets");
System.exit(-1);
}
/*
* TODO: Right now this just searches for these words in big cities.
*/
String searchWords = "#Bard #Food2Families";
int maxnum = 10; // max num tweets to get at one time
final double radius = 10; // radius around location to search
GeoLocation gl = new GeoLocation(43.1731,-77.62474); // London SoHo
// 10 tweets within 20 Kilometers
SearchTweetsNew tweets= new SearchTweetsNew(new Query("gay"),
20,gl,20,Query.ResultType.recent);
System.out.println("Num Tweets: " + tweets.numTweets());
//try{
tweets.save("AllTweets.ser");
//System.out.println(tweets.size() + "-----");
//tweets.load("AllTweets.ser");
//}
//catch(IOException ex){
//ex.printStackTrace();
//}
//tweets.analyzeTweets();
tweets.print();
loadSentiments("sentiments.csv");
analyzeTweets();
//System.out.println("Frequency of tweets about words:" + tweets.analyzeTweetsfq());
//RedBlackBST<String,Double> sentiments = loadSentiments("sentiments.csv");
//this.analyzeTweets();
//RedBlackBST<String,Double> senti = sentiments.get(i).getScore();
}
}
/* // OMIT FOR NOW
// Get citites data.
ArrayList<Location> locations = loadLocations("us-cities.txt");
long minpop = 6000000L; // min city size to consider
// WARNING: If you get too much data, you will be //
// locked out for 15 minutes! //
for (int i = 0; i < locations.size(); i++ ) {
if (locations.get(i).getPopulation() > minpop) {
System.out.println(locations.get(i) + "===================");
GeoLocation loc = locations.get(i).getLocation();
collectTweets(maxnum,searchWords,loc,radius,
Query.ResultType.recent,"alltweets.ser");
}
}*/