diff --git a/TopTitles.java b/TopTitles.java index 7943e41..00b48a8 100644 --- a/TopTitles.java +++ b/TopTitles.java @@ -126,20 +126,31 @@ protected void setup(Context context) throws IOException,InterruptedException { @Override public void map(Object key, Text value, Context context) throws IOException, InterruptedException { - // TODO + String line = value.toString(); + StringTokenizer tokenizer = new StringTokenizer(line, this.delimiters); + while (tokenizer.hasMoreTokens()) { + String nextToken = tokenizer.nextToken().trim().toLowerCase(); + if (!this.stopWords.contains(nextToken)) { + context.write(new Text(nextToken), new IntWritable(1)); + } + } } } public static class TitleCountReduce extends Reducer { @Override public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException { - // TODO + int count = 0; + for (IntWritable v : values) { + count += v.get(); + } + context.write(key, new IntWritable(count)); } } public static class TopTitlesMap extends Mapper { Integer N; - // TODO + private TreeSet> countToWordMap = new TreeSet>(); @Override protected void setup(Context context) throws IOException,InterruptedException { @@ -149,18 +160,27 @@ protected void setup(Context context) throws IOException,InterruptedException { @Override public void map(Text key, Text value, Context context) throws IOException, InterruptedException { - // TODO + Integer count = Integer.parseInt(value.toString()); + String word = key.toString(); + countToWordMap.add(new Pair(count, word)); + if (countToWordMap.size() > this.N) { + countToWordMap.remove(countToWordMap.first()); + } } @Override protected void cleanup(Context context) throws IOException, InterruptedException { - // TODO + for (Pair item : countToWordMap) { + String[] strings = {item.second, item.first.toString()}; + TextArrayWritable val = new TextArrayWritable(strings); + context.write(NullWritable.get(), val); + } } } public static class TopTitlesReduce extends Reducer { Integer N; - // TODO + private TreeSet> countToWordMap = new TreeSet>(); @Override protected void setup(Context context) throws IOException,InterruptedException { @@ -170,7 +190,22 @@ protected void setup(Context context) throws IOException,InterruptedException { @Override public void reduce(NullWritable key, Iterable values, Context context) throws IOException, InterruptedException { - // TODO + for (TextArrayWritable val: values) { + Text[] pair= (Text[]) val.toArray(); + String word = pair[0].toString(); + Integer count = Integer.parseInt(pair[1].toString()); + countToWordMap.add(new Pair(count, word)); + + if (countToWordMap.size() > this.N) { + countToWordMap.remove(countToWordMap.first()); + } + } + + for (Pair item: countToWordMap) { + Text word = new Text(item.second); + IntWritable value = new IntWritable(item.first); + context.write(word, value); + } } }