在vb.net中计算文本文件中特定单词的出现次数

在vb.net中计算文本文件中特定单词的出现次数,vb.net,word-count,Vb.net,Word Count,我试图计算文本文件中的一个项目的数量,通过计算该项目在程序早期输入到文件中的每个实例 我已经从文件和文本框中读取了文本。问题是,我当前的代码只是计算文本框中的字符,而不是我想要的单词在文件中的次数 For Each desiredword As String In txtContentofFile.Text intdesiredword = intdesiredword + 1 txtdesiredwordcount.Text = intdesiredword N

我试图计算文本文件中的一个项目的数量,通过计算该项目在程序早期输入到文件中的每个实例

我已经从文件和文本框中读取了文本。问题是,我当前的代码只是计算文本框中的字符,而不是我想要的单词在文件中的次数

For Each desiredword As String In txtContentofFile.Text
        intdesiredword = intdesiredword + 1
        txtdesiredwordcount.Text = intdesiredword
Next
这会计算文本框中的字符数,而不是计算所需的字数。在请求帮助之前,我反复尝试并进行了广泛搜索,但我不明白我的代码出了什么问题。请帮助:

您可以使用以下功能:

C:

VB.net:

Dim count As Integer = txtContentofFile.Text.Split(desiredword).Length - 1
试试这个:

Dim text As String = IO.File.ReadAllText("C:\file.txt")
Dim wordsToSearch() As String = New String() {"Hello", "World", "foo"}
Dim words As New List(Of String)()
Dim findings As Dictionary(Of String, List(Of Integer))

'Dividing into words
words.AddRange(text.Split(New String() {" ", Environment.NewLine()}, StringSplitOptions.RemoveEmptyEntries))

findings = SearchWords(words, wordsToSearch)
Console.WriteLine("Number of 'foo': " & findings("foo").Count)
使用的功能:

Private Function SearchWords(ByVal allWords As List(Of String), ByVal wordsToSearch() As String) As Dictionary(Of String, List(Of Integer))
    Dim dResult As New Dictionary(Of String, List(Of Integer))()
    Dim i As Integer = 0

    For Each s As String In wordsToSearch
        dResult.Add(s, New List(Of Integer))

        While i >= 0 AndAlso i < allWords.Count
            i = allWords.IndexOf(s, i)
            If i >= 0 Then dResult(s).Add(i)
            i += 1
        End While
    Next

    Return dResult
End Function

您不仅可以获得发生次数,还可以获得文件中的索引位置,在字典中可以轻松地分组。

我更喜欢在这种情况下使用正则表达式。它们很难理解,但它们非常强大,通常比其他字符串操作技术更快

Dim AllMatchResults As MatchCollection
Try
    Dim RegexObj As New Regex(desiredword)
    AllMatchResults = RegexObj.Matches(txtContentofFile.Text)
    If AllMatchResults.Count > 0 Then
        ' Access individual matches using AllMatchResults.Item[]
    Else
        ' Match attempt failed
    End If
Catch ex As ArgumentException
    'Syntax error in the regular expression
End Try
在本例中,您正在从AllMatchResults.Count中查找值

使用一个很棒的正则表达式工具(比如构建和测试表达式)也是一个很大的帮助。上面的代码段是由RegexBuddy生成的

试试下面的代码

Function word_frequency(word_ As String, input As String) As Integer
    Dim ct = 0
    Try
        Dim wLEN = word_.Length
        Do While input.IndexOf(word_) <> -1
            Dim idx = input.IndexOf(word_) + wLEN
            ct += 1
            input = input.Substring(idx)
        Loop
    Catch ex As Exception

    End Try
    Return ct
End Function

谢谢你这么详细的回答!很明显,我在vb.net方面比你差得多,所以我需要一段时间才能真正从我自己的迂回代码中得到一些东西。如何从中获取答案,并使其实际显示在文本框中?txtnumberofdesired1.Text=dResult不适用于me@KieranO“马奥尼,非常感谢。看看第一部分的用法,这是一个功能示例。基本上,您可以调用函数将要查找的单词和要搜索的单词传递给它,然后它返回一个包含每个单词结果的字典。用法是:TextBox1.Text=dResultmyWord.count没有解释就没有用处,因为这远远不是唯一可能的方法。
Function word_frequency(word_ As String, input As String) As Integer
    Dim ct = 0
    Try
        Dim wLEN = word_.Length
        Do While input.IndexOf(word_) <> -1
            Dim idx = input.IndexOf(word_) + wLEN
            ct += 1
            input = input.Substring(idx)
        Loop
    Catch ex As Exception

    End Try
    Return ct
End Function