i am stuck. I have a text with “And how about you, Count Peter Kirílych? If they call up the militia, you too will have to mount a horse,” remarked the old count, addressing Pierre.
so i am trying to split it and Count how often the word occurs in the text and return the results with the Max repeated to less ones. So the question how can i return results with Max repeated to less one And Some of results are wrong ,
Unfortunately my regex is not working for dash(-), (`) and quote ("). Would be glad for any help Here is what i did :
StringBuilder sb= new StringBuilder();
Map<String, Integer> counterMap = new HashMap<>();
for(Object str:lines){
sb.append(str.toString()+" ");
}
String boom[] = sb.toString().split("['@?-` \\p{Punct}]+\\s*");
for (String word : boom) {
if(word.length()>=4){
if(!word.isEmpty()) {
word = word.trim();
Integer count = counterMap.get(word);
if(count == null) {
count = 0;
}
counterMap.put(word, ++count);
}}
}
StringBuilder bb = new StringBuilder();
Map.Entry<String,Integer> maxEntry = null;
for(String word : counterMap.keySet()) {
System.out.println(word + ": " + counterMap.get(word));
bb.append(word + " - "+counterMap.get(word)+"\n");
}
return bb.toString();
}