-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentAnalysis.java
More file actions
280 lines (217 loc) · 8.8 KB
/
SentAnalysis.java
File metadata and controls
280 lines (217 loc) · 8.8 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
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
package bigdata.sparky;
import static org.apache.spark.sql.functions.col;
import java.sql.Time;
import java.util.Arrays;
import java.util.List;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.mllib.regression.LabeledPoint;
import org.apache.spark.mllib.feature.HashingTF;
import org.apache.spark.mllib.tree.GradientBoostedTrees;
import org.apache.spark.mllib.tree.configuration.BoostingStrategy;
import org.apache.spark.mllib.tree.configuration.Strategy;
import org.apache.spark.mllib.tree.model.GradientBoostedTreesModel;
import org.apache.spark.mllib.tree.model.Predict;
import org.apache.spark.mllib.linalg.Vector;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.SparkSession.Builder;
import org.apache.spark.sql.types.StructType;
import scala.Tuple2;
import shapeless.labelled;
public class SentAnalysis {
private static final String topic = "pizza";
/*
* Some variables using for the evaluating of the model
*/
static long happyTotal = 0;
static long sadTotal = 0;
static long happyCorrect = 0;
static long sadCorrect = 0;
static StructType schemaText = new StructType()
.add("tweet_id", "long")
.add("user_id", "long")
.add("text", "String")
.add("geo_lat", "long")
.add("geo_long", "long")
.add("place_full_name", "String")
.add("place_id", "long");
public static void main(String[] args) {
// SparkSession
Builder builder = new Builder().appName("SparkSQL Examples");
builder.master("local");
SparkSession spark = builder.getOrCreate();
Logger.getLogger("org").setLevel(Level.OFF);
Logger.getLogger("akka").setLevel(Level.OFF);
/*
* Regarding the tweet's texts all we need is tweet_id, user_id, text
* So we do a select on the tweet table
* then we print the schema
*/
Dataset<Row> text = spark
.read()
.option("mode", "DROPMALFORMED")
.schema(schemaText)
.csv("<path>/dataset.csv");
text = text.select(col("text"));
text.printSchema();
/*
* @FROMRAWDATATODATAFRAME
* Let's start with clean the record,
* We want to remove any tweet that doesn’t contain “happy” or “sad”. Why? 'coz
* we can easily make the assumption that if a tweet contains happy it's an happy
* tweet, and the same for sad.
* What about the "I am not happy" case???? --> Not handle
*/
//text.select("text").createOrReplaceTempView("text");
long analysedTweetsNumber = text.count();
System.out.println("There are : " + analysedTweetsNumber + " tweets");
/*
* Keep only the tweet that contains happy or sad
*/
Dataset<Row> happyTweet = text.filter(col("text").contains("happy"));
long counterOfHappyTweets = happyTweet.count();
happyTweet.show();
Dataset<Row> sadTweet = text.filter(col("text").contains("sad"));
long counterOfSadTweets = sadTweet.count();
sadTweet.show();
System.out.println("#happy: " + counterOfHappyTweets + "\n#sad: " + counterOfSadTweets);
/*
* We keep an equal number sad and happy tweets in order prevent bias in the model
* then we create a new view using an an equal number of happy and sad tweets
*/
long minNumber = Math.min(counterOfHappyTweets, counterOfSadTweets);
System.out.println(minNumber);
Dataset<Row> tmp1 = happyTweet.limit((int) minNumber).union(sadTweet.limit((int) minNumber)); //LOOK FOR UNIONALL
tmp1.show();
JavaRDD<Row> textRdd = tmp1.javaRDD();
JavaRDD<Tuple2<Integer,String[]>> statusAndSplit = textRdd.map(
(Function<Row,Tuple2<Integer,String[]>>)s -> {
try {
String txt = s.getString(0).toLowerCase();
int isHappy = 0;
if(txt.contains("happy")) {
isHappy = 1;
}else if(txt.contains("sad")) {
isHappy = 0;
}
txt = txt.replaceAll("happy", "");
txt = txt.replaceAll("sad", "");
Tuple2<Integer,String[]> ret = new Tuple2<>(isHappy, txt.split(" "));
return ret;
}catch(Exception e){
System.out.println(e);
};
return null;
});
long exceptions = textRdd.count() - statusAndSplit.count();
System.out.println("Number of records now:" + statusAndSplit.count());
System.out.println("Number of exceptions: " + exceptions);
for(int i = 0; i < 5; i++) {
System.out.println(statusAndSplit.take(5).get(i));
}
/*
* @DATATRANSFORMATION
*/
HashingTF hashingTF = new HashingTF(3000);
JavaRDD<LabeledPoint> textRdd2 =
statusAndSplit
.map((Function<Tuple2<Integer,String[]>,Tuple2<Integer,Vector>>) t -> {
return new Tuple2<Integer,Vector>(
t._1,
hashingTF.transform(Arrays.asList(t._2)));
})
.map((Function<Tuple2<Integer,Vector>,LabeledPoint>)t -> {
return new LabeledPoint(t._1.doubleValue(), t._2);
});
for(int i = 0; i < 10; i++) {
System.out.println(textRdd2.take(10).get(i));
}
/*
* @SPLITOFSETS
* 30% for the validation set
*
*/
// Split the data into training and test sets (30% held out for testing)
JavaRDD<LabeledPoint>[] splits = textRdd2.randomSplit(new double[] {0.7, 0.3});
JavaRDD<LabeledPoint> trainingData = splits[0];
JavaRDD<LabeledPoint> validationData = splits[1];
/*
* @BUILDTHEMODEL
*/
BoostingStrategy bs = BoostingStrategy.defaultParams("Classification");
bs.setNumIterations(30);
bs.treeStrategy().setNumClasses(2);
bs.treeStrategy().setMaxDepth(10);
GradientBoostedTreesModel model = GradientBoostedTrees.train(trainingData, bs);
/*
* @EVALUATETHEMODEL
*/
JavaRDD<Tuple2<Double,Double>> labelAndPredictTrain = trainingData.map(
x -> {
double prediction = model.predict(x.features());
return new Tuple2<Double,Double>(x.label(), prediction);
});
JavaRDD<Tuple2<Double,Double>> labelAndPredictValid = validationData.map(
x -> {
double prediction = model.predict(x.features());
return new Tuple2<Double,Double>(x.label(), prediction);
});
List<Tuple2<Double,Double>> results = labelAndPredictTrain.collect();
results.forEach(
x -> {
if(x._1 == 1) {happyTotal++;}
else if(x._1 == 0) {sadTotal++;}
if(x._1 == 1 && x._2 == 1) {happyCorrect++;}
else if(x._1 == 0 && x._2 == 0) {sadCorrect++;}
});
System.out.println("\n\n=========" );
System.out.println(" TOPIC ----------------> " + topic );
System.out.println(" #Analysed Tweets -----> " + analysedTweetsNumber + "\n");
System.out.println("=====TRAIN=====" );
System.out.println("Total happys+sad: " + (happyTotal + sadTotal));
System.out.println("=HAPPY=: " );
System.out.println("Total happys: " + happyTotal );
System.out.println("Total correct happys: " + happyCorrect );
System.out.println("Percentage of correct happys : " + new Double(happyCorrect)/new Double(happyTotal) );
System.out.println("=SAD=: " );
System.out.println("Total sads: " + sadTotal );
System.out.println("Total correct sads: " + sadCorrect );
System.out.println("Percentage of correct sads : " + new Double(sadCorrect)/ new Double(sadTotal) );
System.out.println("=ERROR=: " );
long tmpEr = (happyTotal + sadTotal) - (happyCorrect + sadCorrect);
double error = new Double(tmpEr) / new Double(happyTotal + sadTotal);
System.out.println("Total error: " + tmpEr );
System.out.println("Total error percentage: " + error );
results = labelAndPredictValid.collect();
results.forEach(
x -> {
if(x._1 == 1) {happyTotal++;}
else if(x._1 == 0) {sadTotal++;}
if(x._1 == 1 && x._2 == 1) {happyCorrect++;}
else if(x._1 == 0 && x._2 == 0) {sadCorrect++;}
});
System.out.println("\n\n=====VALID=====" );
System.out.println("Total happys+sad: " + (happyTotal + sadTotal));
System.out.println("=HAPPY=: " );
System.out.println("Total happys: " + happyTotal );
System.out.println("Total correct happys: " + happyCorrect );
System.out.println("Percentage of correct happys : " + new Double(happyCorrect)/new Double(happyTotal) );
System.out.println("=SAD=: " );
System.out.println("Total sads: " + sadTotal );
System.out.println("Total correct sads: " + sadCorrect );
System.out.println("Percentage of correct sads : " + new Double(sadCorrect)/ new Double(sadTotal) );
System.out.println("=ERROR=: " );
tmpEr = (happyTotal + sadTotal) - (happyCorrect + sadCorrect);
error = new Double(tmpEr) / new Double(happyTotal + sadTotal);
System.out.println("Total error: " + tmpEr );
System.out.println("Total error percentage: " + error );
System.out.println(new Time(System.currentTimeMillis()).toGMTString().toString());
labelAndPredictTrain.saveAsTextFile("hdfs://localhost:9000/bigdata/project/train.csv");
labelAndPredictValid.saveAsTextFile("hdfs://localhost:9000/bigdata/project/valid.csv");
}
}