-
Notifications
You must be signed in to change notification settings - Fork 0
/
PunctuationMapperClass.java
50 lines (40 loc) · 1.26 KB
/
PunctuationMapperClass.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
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class PunctuationMapperClass extends Mapper<LongWritable, Text, Text, LongWritable>{
Text commaText = new Text("Comma's count");
Text semicolonText = new Text("Semicolon's count");
Text periodText = new Text("Period's count");
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String lineValue = value.toString();
if(lineValue.contains(",")) {
context.write(commaText, new LongWritable(countPunct(lineValue,',')));
}
if(lineValue.contains(".")) {
context.write(periodText, new LongWritable(countPunct(lineValue,'.')));
}
if(lineValue.contains(",")) {
context.write(semicolonText, new LongWritable(countPunct(lineValue,';')));
}
}
/**
* Method to find the occurrences of give character
* @param line
* @param punctChar
* @return
*/
private int countPunct(String line, char punctChar) {
int charCount = 0;
int numOfPunchar=0;
while(charCount < line.length()) {
char myChar = line.charAt(charCount);
if(myChar == punctChar) {
numOfPunchar++;
}
charCount++;
}
return numOfPunchar;
}
}