You will write an Average Word Length program that uses a Boolean parameter called caseSensitive to determine whether the Mapper class should treat upper and lower case letters as different (case-sensitive) or whether all letters should be converted to lower case (case-insensitive).
Write down the Mapper output, Reducer input, and Reducer output using the case- insensitive version of your program for the test input provided above.
import java.util.HashMap;
import java.util.Set;
class Untitled {
public static double foo(String s, Boolean caseSensitive)
{
HashMap map = new HashMap();
String[] words = s.split("[\\,\\(\\)\\.\\s\\r\\n\\-]");
for (String item : words)
{
if (item.trim().equals("")) continue;
if (!caseSensitive) item = item.toLowerCase();
if (!map.containsKey(item)) map.put(item, item.length());
}
double r = 0.0;
Set<String> wl = map.keySet();
for (String w : wl) r += (double)w.length();
r /= (double)wl.size();
return r;
}
public static void main(String[] args) {
String s = "You will write an Average Word Length program that uses a Boolean parameter called caseSensitive to determine whether the Mapper class should treat upper and lower case letters as different (case-sensitive) or whether all letters should be converted to lower case (case-insensitive).\r\nWrite down the Mapper output, Reducer input, and Reducer output using the case- insensitive version of your program for the test input provided above.";
System.out.println(foo(s, true));
System.out.println(foo(s, false));
}
}
根据你的题目,你求的是单词的平均长度,首先需要分离出句子中有多少单词,并且装入map,然后还需要判断大小写。你确认你理解了题目么?
运行结果:
5.326530612244898
5.333333333333333