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
| public void testCountWords() throws Exception {
String csv = "" +
"element 1.1, element 1.2, element 1.3, element 1.4\n" +
"element 2.1, element 2.2\n" +
"element 3.1, element 3.2, element 3.3\n" +
"element 4.1, element 4.2, element 4.3, element 4.4, element 4.5\n" +
"";
int count = split(csv, "\n").<Integer>map(new Function<Integer, Arguments2<String, Integer>>() {
@Override
public Integer invoke(Arguments2<String, Integer> arguments) {
// the line
System.out.println("Line : " + arguments.getArgument1());
return split(arguments.getArgument1(), " ").<Integer>map(new Function<Integer, Arguments2<String, Integer>>() {
@Override
public Integer invoke(Arguments2<String, Integer> arguments) {
// the element
System.out.println("Word :" + arguments.getArgument1());
if ("element".equalsIgnoreCase(arguments.getArgument1())) {
return 1;
} else {
return 0;
}
}
}).reduce(new Function<Integer, Arguments2<Integer, Integer>>() {
@Override
public Integer invoke(Arguments2<Integer, Integer> arguments) {
return arguments.getArgument1() + arguments.getArgument2();
}
});
}
}).reduce(new Function<Integer, Arguments2<Integer, Integer>>() {
@Override
public Integer invoke(Arguments2<Integer, Integer> arguments) {
return arguments.getArgument1() + arguments.getArgument2();
}
});
System.out.println("number of 'element' word in text : " + count);
} |
Partager