Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,102 questions

55,976 answers

573 users

How to find the N most frequent non‑stopwords in a text in Java

1 Answer

0 votes
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

*/

 



answered Aug 30 by avibootz
edited Aug 30 by avibootz
...