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,990 questions

51,935 answers

573 users

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

1 Answer

0 votes
using System;
using System.Linq;
using System.Collections.Generic;

public class TopNWords
{
    static string RemoveWord(string str, string word) {
        string[] words = str.ToLower().Split(new[] { ' ', ',', '.', ';', ':', '!', '?' }, StringSplitOptions.RemoveEmptyEntries);
        string newStr = "";

        foreach (string s in words) {
            if (!s.Equals(word)) {
                newStr += s + " ";
            }
        }

        return newStr.Trim();
    }

    public static Dictionary<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");
        str = RemoveWord(str, "and");

        // Split the string into words
        string[] words = str.ToLower().Split(new[] { ' ', ',', '.', ';', ':', '!', '?' }, StringSplitOptions.RemoveEmptyEntries);

        // Count the occurrences of each word
        var wordCount = words.GroupBy(word => word)
                             .ToDictionary(g => g.Key, g => (long)g.Count());

        // Sort the words by their occurrences and get the top N words
        return wordCount.OrderByDescending(kvp => kvp.Value)
                        .ThenBy(kvp => kvp.Key)
                        .Take(n)
                        .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
    }

    public static void Main(string[] args)
    {
        string str = "C# is a general-purpose high-level programming language " +
                     "supporting multiple paradigms. C# encompasses static typing " +
                     "strong typing, lexically scoped, imperative, declarative, " +
                     "functional, generic, object-oriented class-based, and " +
                     "component-oriented programming disciplines.";
        int n = 5;

        var topNWords = GetTopNWords(str, n);

        foreach (var kvp in topNWords) {
            Console.WriteLine(kvp.Key);
        }
    }
}


/*
run:

c#
programming
typing
class-based
component-oriented

*/

 



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 95 views
...