Skip to content

Commit

Permalink
Added TopTitle implementation xldrx#1.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrés Gomez committed Sep 11, 2015
1 parent a5439e6 commit 140b27b
Showing 1 changed file with 42 additions and 7 deletions.
49 changes: 42 additions & 7 deletions TopTitles.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Text, IntWritable, Text, IntWritable> {
@Override
public void reduce(Text key, Iterable<IntWritable> 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<Text, Text, NullWritable, TextArrayWritable> {
Integer N;
// TODO
private TreeSet<Pair<Integer, String>> countToWordMap = new TreeSet<Pair<Integer, String>>();

@Override
protected void setup(Context context) throws IOException,InterruptedException {
Expand All @@ -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<Integer, String>(count, word));
if (countToWordMap.size() > this.N) {
countToWordMap.remove(countToWordMap.first());
}
}

@Override
protected void cleanup(Context context) throws IOException, InterruptedException {
// TODO
for (Pair<Integer, String> 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<NullWritable, TextArrayWritable, Text, IntWritable> {
Integer N;
// TODO
private TreeSet<Pair<Integer, String>> countToWordMap = new TreeSet<Pair<Integer, String>>();

@Override
protected void setup(Context context) throws IOException,InterruptedException {
Expand All @@ -170,7 +190,22 @@ protected void setup(Context context) throws IOException,InterruptedException {

@Override
public void reduce(NullWritable key, Iterable<TextArrayWritable> 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<Integer, String>(count, word));

if (countToWordMap.size() > this.N) {
countToWordMap.remove(countToWordMap.first());
}
}

for (Pair<Integer, String> item: countToWordMap) {
Text word = new Text(item.second);
IntWritable value = new IntWritable(item.first);
context.write(word, value);
}
}
}

Expand Down

0 comments on commit 140b27b

Please sign in to comment.