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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

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

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,996 questions

51,941 answers

573 users

How to get the N top words of a string by occurrences in Java

1 Answer

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

*/

 



answered Feb 2, 2025 by avibootz

Related questions

1 answer 111 views
1 answer 93 views
1 answer 99 views
1 answer 90 views
1 answer 89 views
1 answer 96 views
...