SharkIng
11/16/2014 - 5:38 AM

MapReduce - Mapper

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);
		
	}
}