"Count Words In Text"
Your solution will be scored against some number of hidden test cases. A sample is provided below.
You are given a document with text, write a program to print the top k most recurring words with their frequencies.
Ignore punctuation marks such as commas, periods, double quotes, and parentheses.
Input
First-line contains two integers N and k, N denoting the number of lines of text.
The next N lines contain the text in which all the words need to be counted.
Output
Print top k recurring words along with the frequency of occurrence, separated by a space. Print one word in each line in descending order of a number of occurrences.
If two words have the same number of occurrences, then sort the output in alphabetic order
Code evaluation is based on your output, please follow the sample format and do NOT print anything else.
Example:
Input:
3 2
This is a test.
This is the second test.
This is the third test.
Output:
test 3
the 2
public static String codeHere(String inputData) {
//this line is to remove all punctuation and putting the words to lowercase, so that all words will be the same
inputData = inputData.replaceAll("\\p{Punct}", "").toLowerCase();
// created an arrays of words
String [] words = null;
// fill the array with words from inputData
words = inputData.split(" ");
// make a hashmap to store words frequency
HashMap<String,Integer> map = new HashMap<>();
List<String> result = new ArrayList<String>();
//add the words and its frequency to the map
for(int i = 0; i < words.length; i++){
if(!map.containsKey(words[i])){
map.put(words[i], 1);
}
else {
map.put(words[i], map.get(words[i]) +1 );
}
}
// loops through map to get all words and their frequency to the string list
for(Map.Entry<String,Integer> entry : map.entrySet()){
if(entry.getValue() >= 2){
String test = entry.getKey() + " " + String.valueOf(entry.getValue());
result.add(test);
}
}
// Sort the collections in reverse order
Collections.sort(result, Collections.reverseOrder());
String delim = "\n";
String finalResult = String.join(delim,result);
return finalResult;