-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentiment.java
More file actions
137 lines (103 loc) · 3.51 KB
/
Copy pathSentiment.java
File metadata and controls
137 lines (103 loc) · 3.51 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
package cisc.mapreduce;
// written by Hannah Wilkinson
///resources include: https://www.cloudera.com/documentation/other/tutorial/CDH5/topics/ht_example_4_sentiment_analysis.html
///https://hadoop.apache.org/docs/r1.2.1/mapred_tutorial.html
//Input: ./hadoop FilePathForNegativeWords FilePathForPositiveWords SampleData output_path
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class Sentiment {
public static Set<String> goodWords = new HashSet<String>();
public static Set<String> badWords = new HashSet<String>();
public static class TokenizerMapper extends
Mapper<Object, Text, Text, Text> {
private Text productID = new Text();
private Text body = new Text();
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
String[] strings = line.split("\t", -1);
if (strings.length == 8) {
try {
productID.set(strings[1]);
body.set(strings[7]);
context.write(productID, body);
} catch (Exception e) {
}
}
}
}
public static class IntSumReducer extends Reducer<Text, Text, Text, Text> {
private Text result = new Text();
public void reduce(Text key, Iterable<Text> value, Context context)
throws IOException, InterruptedException {
int counter = 0;
for (Text val : value) {
String review = val.toString();
String review2 = review.replaceAll("[-+.^:,]", "");
String[] arr = review2.split(" ");
for (String x : arr) {
if (goodWords.contains(x)) {
counter = counter + 1;
}
// Filter and count "bad" words.
if (badWords.contains(x)) {
counter = counter - 1;
}
}
}
if (counter > 0) {
result.set("positive");
} else if (counter < 0) {
result.set("negative");
} else
result.set("neutral");
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "Sentiment");
job.setJarByClass(Sentiment.class);
job.setMapperClass(TokenizerMapper.class);
job.setReducerClass(IntSumReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(args[2]));
FileOutputFormat.setOutputPath(job, new Path(args[3]));
File file = new File(args[0]);
File file2 = new File(args[1]);
try {
BufferedReader fis = new BufferedReader(new FileReader(file2));
String goodWord;
while ((goodWord = fis.readLine()) != null) {
goodWords.add(goodWord);
}
} catch (IOException ioe) {
System.err.println("Caught exception parsing cached file '");
}
try {
BufferedReader fis = new BufferedReader(new FileReader(file));
String badWord;
while ((badWord = fis.readLine()) != null) {
badWords.add(badWord);
}
} catch (IOException ioe) {
System.err.println("Caught exception parsing cached file '");
}
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}