using System;
using System.Collections.Generic;
using System.Linq;
/*
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 C# collections and sorting.
*/
public class TopWordsCSharp
{
// ---------------------------------------------------------------
// Helper: check punctuation
// ---------------------------------------------------------------
private static bool IsPunct(char c)
{
return "!?,.;:\"'()[]{}".Contains(c);
}
// ---------------------------------------------------------------
// Tokenize text into words (simple whitespace split)
// ---------------------------------------------------------------
private static List<string> Tokenize(string text)
{
var words = new List<string>();
foreach (var raw in text.Split((char[])null, StringSplitOptions.RemoveEmptyEntries))
{
string w = raw;
// Remove punctuation at the edges
while (w.Length > 0 && IsPunct(w[0]))
w = w.Substring(1);
while (w.Length > 0 && IsPunct(w[w.Length - 1]))
w = w.Substring(0, w.Length - 1);
if (w.Length > 0)
words.Add(w.ToLower());
}
return words;
}
// ---------------------------------------------------------------
// Count word frequencies, skipping stopwords
// ---------------------------------------------------------------
private static Dictionary<string, int> CountWordFrequencies(
List<string> words,
HashSet<string> stopwords)
{
var freq = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
foreach (var w in words)
{
if (!stopwords.Contains(w)) {
if (freq.ContainsKey(w))
freq[w]++;
else
freq[w] = 1;
}
}
return freq;
}
// ---------------------------------------------------------------
// Extract the top N most frequent words
// ---------------------------------------------------------------
private static List<KeyValuePair<string, int>> TopN(
Dictionary<string, int> freq,
int n)
{
var items = freq.ToList();
// Sort by frequency descending, then alphabetically
items.Sort((a, b) =>
{
int cmp = b.Value.CompareTo(a.Value);
if (cmp != 0) return cmp;
return a.Key.CompareTo(b.Key);
});
return items.Take(n).ToList();
}
// ---------------------------------------------------------------
// Main
// ---------------------------------------------------------------
public static void Main()
{
// 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
var stopwords = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"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
var words = Tokenize(text);
// Count frequencies
var freq = CountWordFrequencies(words, stopwords);
// Get top n
int n = 7;
var topn = TopN(freq, n);
// Print results
Console.WriteLine($"Top {n} most frequent non-stopwords:");
foreach (var entry in topn)
{
Console.WriteLine($"{entry.Key} : {entry.Value}");
}
}
}
/*
run:
Top 7 most frequent non-stopwords:
c : 3
language : 2
programming : 2
systems : 2
1972 : 1
computers : 1
cpu : 1
*/