Dim words = inputText.ToLower().Split(" "c)
For Each word As String In words
If wordCountDict.ContainsKey(word) Then
wordCountDict(word) += 1
Else
wordCountDict(word) = 1
End If
Next
Explanation to the Code, If you didn’t understand it:
//Convert text to lower case and then convert to an Array using Split function
//You can use inputText.Split(" "c) if you want to consider case-sensitivity.
Dim words = inputText.ToLower().Split(" "c)
//Loop through the Array of Words
For Each word As String In words
// If the word is present in the Dictionary as a Key, Then Increment the count
If wordCountDict.ContainsKey(word) Then
wordCountDict(word) += 1
// If the word is not present in the Dictionary as a Key, then create it with count value 1
Else
wordCountDict(word) = 1
End If
Next
If you want the Output in a Certain Way, Do let me know.
If you add the below code with outputText as Out Argument in that Invoke Code, you will get a String Output.
outputText = ""
For Each keyValue As KeyValuePair(Of String, Integer) In wordCountDict
outputText &= keyValue.Key & ": " & keyValue.Value.ToString() & Environment.NewLine
Next
If you want only the words that are repeated (ignore the words with count=1), Then Update the Invoke code as shown below:
Dim words = inputText.ToLower().Split(" "c)
For Each word As String In words
If wordCountDict.ContainsKey(word) Then
wordCountDict(word) += 1
Else
wordCountDict(word) = 1
End If
Next
// The below code, will remove the words that have count = 1
For Each kvp As KeyValuePair(Of String, Integer) In wordCountDict
If kvp.Value = 1 Then
wordCountDict.Remove(kvp.Key)
End If
Next
Assign -> inputText = "big black bug bit a big black dog on his big black nose"
Assign -> words = inputText.ToLower().Split({" "c}, StringSplitOptions.RemoveEmptyEntries)
Assign -> wordCountDict = words.GroupBy(Function(w) w).ToDictionary(Function(g) g.Key, Function(g) g.Count())
Message Box -> String.Join(Environment.NewLine, wordCountDict.Select(Function(kvp) kvp.Key & ": " & kvp.Value))