Imports System.Collections.Generic
Imports System.Text.RegularExpressions
Public Class WordAndTermCounter
Public Shared Function GetTermCount(ByVal pString As String, ByVal pTerms As String) As List(Of TermCount)
' Declare a variable named termCountHashTable of type HashTable;
' call the HashTable's New method;
' assign the HashTable object the New method returns to the termHashTable variable.
Dim termCountHashTable As New Hashtable
' If no terms were passed into this function
' count the occurences of every word in pString
If String.IsNullOrEmpty(pTerms) Then
' Get an array of all the words in pString.
Dim terms() As String = pString.Split(" "c)
' Loop the terms array.
For i As Integer = 0 To terms.GetUpperBound(0)
' Declare a variable named word of type String.
Dim word As String
' Assign the word at index i of the terms array to the word variable.
word = terms(i).Trim
If word <> String.Empty Then
' If the termCountHashTable already contains a termCount object for the word.
If termCountHashTable.ContainsKey(word) Then
' Increment the termCount object's TermCount property.
CType(termCountHashTable.Item(word), TermCount).IncrementTermCount()
Else
' Add a new TermCount object to the termCountHashTable;
' it's initial count will be set to 1.
termCountHashTable.Add(word, New TermCount(word))
End If
End If
Next
Else
' If terms were passed into this function
' count the occurence of each term.
' Get an array of all the terms in pTerms.
Dim terms() As String = pTerms.Split(","c)
' Loop through the terms array.
For ii As Integer = 0 To terms.GetUpperBound(0)
' Declare a variable named term of type String.
Dim term As String
' Assign the term at index i of the terms array to the terms variable.
term = terms(ii).Trim.ToUpper
' Declare and instantiate a RegEx object.
' Dim wordRegEx As New Regex(term)
' Delclare a Match object.
Dim theMatch As MatchCollection
' Call wordRegEx's RegEx Match method;
' assign the resulting Match object to theMatch variable.
theMatch = Regex.Matches(pString.ToUpper, term)
' Add a new TermCount object to the termCountHashTable.
termCountHashTable.Add(term, New TermCount(term, theMatch.Count))
Next
End If
Dim listOfTermCount As New List(Of TermCount)
Dim x As DictionaryEntry
For Each x In termCountHashTable
Dim theWordCount As TermCount = CType(x.Value, TermCount)
listOfTermCount.Add(theWordCount)
Next
Return listOfTermCount
End Function
End Class
Public Class TermCount
Implements IComparable(Of TermCount)
' Term Property
Private m_Term As String
Public Property Term() As String
Get
Return m_Term
End Get
Set(ByVal value As String)
m_Term = value
End Set
End Property
' TermCount Property
Private m_TermCount As Integer
Public Property TermCount() As Integer
Get
Return m_TermCount
End Get
Set(ByVal value As Integer)
m_TermCount = value
End Set
End Property
Public Sub New(ByVal word As String)
Me.m_Term = word
Me.IncrementTermCount()
End Sub
Public Sub New(ByVal word As String, ByVal wordCount As Integer)
Me.m_Term = word
Me.m_TermCount = wordCount
End Sub
Public Sub IncrementTermCount()
Me.m_TermCount += 1
End Sub
Public Function CompareTo(ByVal other As TermCount) As Integer Implements System.IComparable(Of TermCount).CompareTo
Return other.TermCount.CompareTo(Me.TermCount)
End Function
End Class