import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.Map;
/**
This program finds the N most frequently appearing words in a text
after removing stopwords. It demonstrates clean structure, clear
comments, and efficient use of Java collections and sorting.
*/
public class TopWordsJava {
// ---------------------------------------------------------------
// Helper: check punctuation
// ---------------------------------------------------------------
private static boolean isPunct(char c) {
return "!?,.;:\"'()[]{}".indexOf(c) >= 0;
}
// ---------------------------------------------------------------
// Tokenize text into words (simple whitespace split)
// ---------------------------------------------------------------
private static List<String> tokenize(String text) {
List<String> words = new ArrayList<>();
for (String w : text.split("\\s+")) {
// Remove punctuation at the edges
while (!w.isEmpty() && isPunct(w.charAt(0))) {
w = w.substring(1);
}
while (!w.isEmpty() && isPunct(w.charAt(w.length() - 1))) {
w = w.substring(0, w.length() - 1);
}
if (!w.isEmpty()) {
words.add(w.toLowerCase());
}
}
return words;
}
// ---------------------------------------------------------------
// Count word frequencies, skipping stopwords
// ---------------------------------------------------------------
private static Map<String, Integer> countWordFrequencies(
List<String> words,
Set<String> stopwords
) {
Map<String, Integer> freq = new HashMap<>();
for (String w : words) {
if (!stopwords.contains(w)) {
freq.put(w, freq.getOrDefault(w, 0) + 1);
}
}
return freq;
}
// ---------------------------------------------------------------
// Extract the top N most frequent words
// ---------------------------------------------------------------
private static List<Map.Entry<String, Integer>> topN(
Map<String, Integer> freq,
int n
) {
List<Map.Entry<String, Integer>> items =
new ArrayList<>(freq.entrySet());
// Sort by frequency descending, then alphabetically
items.sort((a, b) -> {
int cmp = Integer.compare(b.getValue(), a.getValue());
if (cmp != 0) return cmp;
return a.getKey().compareTo(b.getKey());
});
return items.subList(0, Math.min(n, items.size()));
}
// ---------------------------------------------------------------
// Main
// ---------------------------------------------------------------
public static void main(String[] args) {
// Example text
String text =
"C is a general-purpose programming language created in 1972 by "
+ "Dennis Ritchie. C gives programmers direct access to the features "
+ "of CPU. It has been and continues to be used to implement "
+ "operating systems (especially kernels) and device "
+ "drivers. C programming language used on computers ranging from "
+ "supercomputers to microcontrollers and embedded systems.";
// Example stopwords
Set<String> stopwords = new HashSet<>(Arrays.asList(
"the","is","a","to","how","after","but","this","for","by","in",
"and","can","content","be","you","yes","no","next","about","used",
"access","been","continues"
));
// Tokenize
List<String> words = tokenize(text);
// Count frequencies
Map<String, Integer> freq = countWordFrequencies(words, stopwords);
// Get top N
int n = 7;
List<Map.Entry<String, Integer>> topn = topN(freq, n);
// Print results
System.out.println("Top " + n + " most frequent non-stopwords:");
for (Map.Entry<String, Integer> entry : topn) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}
/*
run:
Top 7 most frequent non-stopwords:
c : 3
language : 2
programming : 2
systems : 2
1972 : 1
computers : 1
cpu : 1
*/