Imports System
Imports System.Collections.Generic
Imports 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 VB.NET collections and sorting.
'
Public Class TopWordsVB
' ---------------------------------------------------------------
' Helper: check punctuation
' ---------------------------------------------------------------
Private Shared Function IsPunct(c As Char) As Boolean
Return "!?,.;:""'()[]{}".Contains(c)
End Function
' ---------------------------------------------------------------
' Tokenize text into words (simple whitespace split)
' ---------------------------------------------------------------
Private Shared Function Tokenize(text As String) As List(Of String)
Dim words As New List(Of String)()
For Each raw In text.Split({" "}, StringSplitOptions.RemoveEmptyEntries)
Dim w As String = raw
' Remove punctuation at the edges
While w.Length > 0 AndAlso IsPunct(w(0))
w = w.Substring(1)
End While
While w.Length > 0 AndAlso IsPunct(w(w.Length - 1))
w = w.Substring(0, w.Length - 1)
End While
If w.Length > 0 Then
words.Add(w.ToLower())
End If
Next
Return words
End Function
' ---------------------------------------------------------------
' Count word frequencies, skipping stopwords
' ---------------------------------------------------------------
Private Shared Function CountWordFrequencies(
words As List(Of String),
stopwords As HashSet(Of String)
) As Dictionary(Of String, Integer)
Dim freq As New Dictionary(Of String, Integer)(StringComparer.OrdinalIgnoreCase)
For Each w In words
If Not stopwords.Contains(w) Then
If freq.ContainsKey(w) Then
freq(w) += 1
Else
freq(w) = 1
End If
End If
Next
Return freq
End Function
' ---------------------------------------------------------------
' Extract the top N most frequent words
' ---------------------------------------------------------------
Private Shared Function Top_N(
freq As Dictionary(Of String, Integer),
n As Integer
) As List(Of KeyValuePair(Of String, Integer))
Dim items = freq.ToList()
' Sort by frequency descending, then alphabetically
items.Sort(Function(a, b)
Dim cmp = b.Value.CompareTo(a.Value)
If cmp <> 0 Then Return cmp
Return a.Key.CompareTo(b.Key)
End Function)
Return items.Take(n).ToList()
End Function
' ---------------------------------------------------------------
' Main
' ---------------------------------------------------------------
Public Shared Sub Main()
' Example text
Dim text As String =
"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
Dim stopwords As New HashSet(Of String)(StringComparer.OrdinalIgnoreCase) From {
"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
Dim words = Tokenize(text)
' Count frequencies
Dim freq = CountWordFrequencies(words, stopwords)
' Get top n
Dim n As Integer = 7
Dim topn = Top_N(freq, n)
' Print results
Console.WriteLine("Top " & n & " most frequent non-stopwords:")
For Each entry In topn
Console.WriteLine(entry.Key & " : " & entry.Value)
Next
End Sub
End Class
'
' run:
'
' Top 7 most frequent non-stopwords:
' c : 3
' language : 2
' programming : 2
' systems : 2
' 1972 : 1
' computers : 1
' cpu : 1
'