Skip to content

Instantly share code, notes, and snippets.

@Zenuncl
Created November 16, 2014 05:38
Show Gist options
  • Save Zenuncl/2b86024180b2a5e93e6a to your computer and use it in GitHub Desktop.
Save Zenuncl/2b86024180b2a5e93e6a to your computer and use it in GitHub Desktop.
MapReduce - Mapper
import java.io.IOException;
import java.util.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;
public class Mappers extends Mapper<LongWritable, Text, Text, IntWritable> {
//private List<String> pos = new ArrayList<String>();
//private List<String> neg = new ArrayList<String>();
private Text prodID = new Text();
private IntWritable totalIW = new IntWritable();
public void map(LongWritable key, Text values, OutputCollector<Text, IntWritable> output, Reporter report) throws IOException {
String review = values.toString();
String[] reviewSet = review.split("\t");
String productsID = reviewSet[1];
String body = reviewSet[7];
String[] posWords = Analysis.pos;
String[] negWords = Analysis.neg;
// Mapper Function
// Check each word in Body and compare it with neg/pos Words list
int numPos = 0;
int numNeg = 0;
int total = 0;
StringTokenizer bodyWords = new StringTokenizer(body);
while(bodyWords.hasMoreTokens()){
String word = bodyWords.nextToken();
//word = word.toLowerCase();
//word = word.replaceAll("[^A-Za-z]", "");
for (int i = 0; i < posWords.length; i ++){
if(word.equals(posWords[i])){
numPos ++;
}
}
for (int i = 0; i < negWords.length; i ++){
if(word.equals(negWords[i])){
numNeg ++;
}
}
}
total = numPos - numNeg;
this.totalIW.set(total);
this.prodID.set(productsID);
output.collect(this.prodID, this.totalIW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment