帮朋友改的一小段关键词分析代码; 含两个单词复合计数
Private Function CollectWords() As Dictionary(Of String, Integer)
'Create a new dictionary Dim table As New Dictionary(Of String, Integer)
'Prompt for the user Console.WriteLine( "Enter a line : ")
'Get the user's input Dim input As String = Console.ReadLine()
'Split the line and make an array of words Dim words As String() = input.Split()
Dim wordPrev As String = "" 'Search the array For Each word In words
'We can get the lowercase, not necessary here Dim wordKey As String = word.ToLower()
'If the dictionay contains the words If table.ContainsKey(wordKey) Then 'increment table(wordKey) = table(wordKey) + 1 Else 'add a new word with a count of 1 in the dictionary table.Add(wordKey, 1) End If Dim wordKey2 As String = (wordPrev + " " + word).ToLower()
If wordPrev <> "" Then If table.ContainsKey(wordKey2) Then 'increment table(wordKey2) = table(wordKey2) + 1 Else 'add a new word with a count of 1 in the dictionary table.Add(wordKey2, 1) End If End If wordPrev = word Next Return table
End Function