-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNgramRF.java
159 lines (125 loc) · 6.39 KB
/
NgramRF.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
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.MapWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
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 NgramRF{
public static class TokenizerMapper
extends Mapper<Object, Text, Text, MapWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
List ls = new ArrayList();
@SuppressWarnings("unchecked")
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
String[] stringArray = itr.nextToken().split("\\W+");
for( String s : stringArray){
if(s.length() != 0)
ls.add(s);
}
}
}
@Override
protected void cleanup(Context context) throws IOException, InterruptedException {
int n = Integer.parseInt(context.getConfiguration().get("N"));
Double theta = Double.parseDouble(context.getConfiguration().get("THETA"));
MapWritable final_map_output = new MapWritable(); //<Text(string head), MapWritable(mini-stripe)>
MapWritable stripe = new MapWritable();
StringBuffer str_head = new StringBuffer();
StringBuffer str_sub = new StringBuffer();
for (int i = 0; i < ls.size() - (n-1); i++) {
int k=i;
for(int j=0;j<n;j++) {
if(j == 0){
str_head = str_head.append(ls.get(k));
if(!final_map_output.containsKey(new Text(str_head.toString()))){
final_map_output.put(new Text(str_head.toString()), new MapWritable());
}
}
else if(j == 1){
str_sub = str_sub.append(ls.get(k));
}
else {
str_sub = str_sub.append(" ");
str_sub = str_sub.append(ls.get(k));
}
k++;
}
word.set(str_head.toString());
stripe.put(new Text(str_sub.toString()), one);
stripe.put(new Text("*"), one);
str_head.delete(0, str_head.length());
str_sub.delete(0, str_sub.length());
MapWritable word_map = (MapWritable) final_map_output.get(word);
for (Writable stripe_key : stripe.keySet()) {
int map1val = ((IntWritable) word_map.getOrDefault(stripe_key, new IntWritable(0))).get();
word_map.put(stripe_key, new IntWritable(map1val + 1));
}
stripe.clear();
}
for (Writable word_key : final_map_output.keySet()){
Text output_word = new Text(word_key.toString());
context.write(output_word, (MapWritable) final_map_output.get(word_key));
}
}
}
public static class IntSumReducer
extends Reducer<Text,MapWritable,Text,DoubleWritable> {
private MapWritable result = new MapWritable();
public void reduce(Text key, Iterable<MapWritable> values,
Context context
) throws IOException, InterruptedException {
Double theta = Double.parseDouble(context.getConfiguration().get("THETA"));
for (MapWritable val : values) {
for (Writable mapkey : val.keySet()) {
int map1val = ((IntWritable) result.getOrDefault(mapkey, new IntWritable(0))).get();
int map2val = ((IntWritable) val.get(mapkey)).get();
result.put(mapkey, new IntWritable(map1val + map2val));
}
}
IntWritable test = (IntWritable) result.get(new Text("*"));
double sum_substring = (double)test.get();
for (Writable mapkey : result.keySet()) {
if(!mapkey.toString().equals("*")){
double frequency = ((IntWritable)result.get(mapkey)).get()/sum_substring;
if(frequency >= theta){
String full_string = key.toString() + " " + mapkey.toString();
context.write(new Text(full_string), new DoubleWritable(frequency));
}
}
}
result.clear();
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.set("N", args[2]);
conf.set("THETA",args[3]);
Job job = Job.getInstance(conf, "N gram RF");
job.setJarByClass(NgramRF.class);
job.setMapperClass(TokenizerMapper.class);
job.setReducerClass(IntSumReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(MapWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(DoubleWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}