import java.util.Map;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.stream.Collectors;
public class Main {
static String removeWord(String str, String word) {
String[] words = str.toLowerCase().split("\\W+");
String new_str = "";
for (String s : words) {
if (!s.equals(word)) {
new_str += s + " ";
}
}
return new_str;
}
public static Map<String, Long> getTopNWords(String str, int n) {
// Exclude stop words (commonly used words)
str = removeWord(str, "is");
str = removeWord(str, "a");
str = removeWord(str, "to");
str = removeWord(str, "as");
str = removeWord(str, "can");
str = removeWord(str, "that");
str = removeWord(str, "on");
// Split the string into words
String[] words = str.toLowerCase().split("\\W+");
// Count the occurrences of each word
Map<String, Long> wordCount = Arrays.stream(words)
.collect(Collectors.groupingBy(word -> word, Collectors.counting()));
// Sort the words by their occurrences and get the top N words
return wordCount.entrySet().stream()
.sorted(Map.Entry.<String, Long>comparingByValue(Comparator.reverseOrder())
.thenComparing(Map.Entry.comparingByKey()))
.limit(n)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}
public static void main(String[] args) {
String str = "Java is a high-level, class-based, object-oriented " +
"programming language that is designed to have as few implementation " +
"dependencies as possible. It is a general-purpose programming language " +
"intended to let programmers write once, run anywhere, " +
"meaning that compiled Java code can run on all platforms that support Java " +
"without the need to recompile. Java applications are typically compiled " +
"to bytecode that can run on any Java virtual machine (JVM) " +
"regardless of the underlying computer architecture.";
int n = 5;
Map<String, Long> topNWords = getTopNWords(str, n);
topNWords.forEach((key, value) -> System.out.println(key));
}
}
/*
run:
java
run
compiled
language
programming
*/