-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalyze.java
More file actions
executable file
·224 lines (185 loc) · 7.26 KB
/
Analyze.java
File metadata and controls
executable file
·224 lines (185 loc) · 7.26 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
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
import org.apache.lucene.document.Document;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
//import org.apache.lucene.queryParser.QueryParser;
/*
* (Really simple-dumb) Sentiment analysis for a lucene index of 1 million Tweets!
* Based on http://jeffreybreen.wordpress.com/2011/07/04/twitter-text-mining-r-slides/
*
*/
public class Analyze {
// path to lucene index
//private final static String inputFile = "SampleTweetText/sampleInput.txt";
// path to language profiles for classifier
// lucene queryParser for saving
//private static QueryParser queryParser;
// used to store positive and negative words for scoring
static List<String> posWords = new ArrayList<String>();
static List<String> negWords = new ArrayList<String>();
// keep some stats! [-1 / 0 / 1 / not english / foursquare / no text to
// classify]
static int[] stats = new int[6];
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
boolean quiet = false;
for (String arg : args)
{
if (arg.equals("-q"))
{
quiet = true;
}
}
// huh, how long?
long startTime = System.currentTimeMillis();
// open lucene index
BufferedReader reader = null;
try {
//FileReader fr = new FileReader(inputFile);
InputStreamReader isr = new InputStreamReader(System.in);
reader = new BufferedReader(isr);
System.out.println("START: reading file list");
// source: www.cs.uic.edu/~liub/FBS/sentiment-analysis.html
BufferedReader negReader = new BufferedReader(new FileReader(new File(
"negative-words.txt")));
BufferedReader posReader = new BufferedReader(new FileReader(new File(
"positive-words.txt")));
// currently read word
String word;
// add words to comparison list
while ((word = negReader.readLine()) != null) {
negWords.add(word);
}
while ((word = posReader.readLine()) != null) {
posWords.add(word);
}
// cleanup
negReader.close();
posReader.close();
System.out.println("FINISH: reading file list");
// ----------------------------------------------
System.out.println("START: calculating sentiment");
// current tweet
Document tweet;
// current score
int score = 0;
// current text
String text;
System.err.println("hello");
//System.err.println(docReader.directory());
// used to give some feedback during processing the 1 million tweets
// do we want to skip saving that document?
boolean skipSave = false;
int i = 0;
while ((text = reader.readLine()) != null) {
if (i % 100000 == 0) {
System.out.printf("PROCESSING: %d tweets processed...%n", i);
}
// reset, most of the times we want that.
skipSave = false;
try {
if (text.startsWith("I'm at")
|| text.startsWith("I just became the mayor")
|| text.startsWith("I just ousted")) {
// all your foursquare updates are belong to us.
stats[4]++;
// and we don't save them. yo.
skipSave = true;
} else {
// finally! retrieve sentiment score.
score = getSentimentScore(text);
// ++ index so we won't have -1 and stuff...
stats[score + 1]++;
if (quiet)
{
String out = "";
if (score > 0)
{
out = "+1";
}
else if (score == 0)
{
out = "0";
}
else
{
out = "-1";
}
System.out.println(out);
}
else
{
System.out.printf("Score: %d for Tweet (%d): %s%n", score, i, text);
}
}
} catch (Exception e) {
// something went wrong, ouuups!
e.printStackTrace();
System.err.printf("Error with tweet on line %d.%n", i);
}
++i;
}
// cleanup
reader.close();
//fr.close();
} catch (IOException e1) {
e1.printStackTrace();
System.err.println(e1.getMessage());
}
System.out.println("FINISH: calculating sentiment");
// ----------------------------------------------
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println("----------------------------------------------");
System.out.println("STATS - TIME: Analysis took "
+ TimeUnit.SECONDS.convert(totalTime, TimeUnit.MILLISECONDS)
+ " seconds");
// ----------------------------------------------
// get me some info!
System.out.println("STATS - COUNTS: [negative | neutral | positive | not english | foursquare | no text to classify]");
System.out.println("STATS - COUNTS: " + java.util.Arrays.toString(stats));
}
/**
* does some string mangling and then calculates occurrences in positive /
* negative word list and finally the delta
*
* @param input String: the text to classify
* @return score int: if < 0 then -1, if > 0 then 1 otherwise 0 - we don't
* care about the actual delta
*/
private static int getSentimentScore(String input) {
// normalize!
input = input.toLowerCase();
input = input.trim();
// remove all non alpha-numeric non whitespace chars
input = input.replaceAll("[^a-zA-Z0-9\\s]", "");
int negCounter = 0;
int posCounter = 0;
// so what we got?
String[] words = input.split(" ");
// check if the current word appears in our reference lists...
for (int i = 0; i < words.length; i++) {
if (posWords.contains(words[i])) {
posCounter++;
}
if (negWords.contains(words[i])) {
negCounter++;
}
}
// positive matches MINUS negative matches
int result = (posCounter - negCounter);
// negative?
if (result < 0) {
return -1;
// or positive?
} else if (result > 0) {
return 1;
}
// neutral to the rescue!
return 0;
}
}