【问题标题】:Count occurance of specific words in a text file in vb.net计算 vb.net 中文本文件中特定单词的出现次数
【发布时间】:2020-07-22 11:06:09
【问题描述】:

我正在尝试计算文本文件中项目的数量,方法是计算项目之前在程序中输入到文件中的每个实例。

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

For Each desiredword As String In txtContentofFile.Text
        intdesiredword = intdesiredword + 1
        txtdesiredwordcount.Text = intdesiredword
Next

这会计算文本框中的字符数,而不是计算所需单词的数量。在寻求帮助和广泛搜索之前,我反复尝试过,但我就是不明白我的代码有什么问题。请帮忙:)

【问题讨论】:

    标签: vb.net word-count


    【解决方案1】:

    你可以使用Split函数:

    C#:

    int count = txtContentofFile.Text.Split(desiredword).Length - 1;
    

    VB.net:

    Dim count As Integer = txtContentofFile.Text.Split(desiredword).Length - 1
    

    【讨论】:

      【解决方案2】:

      试试这个:

      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
      

      您不仅会获得出现次数,还会获得文件中的索引位置,这些位置可以轻松分组在 Dictionary 中。

      【讨论】:

      • 感谢您如此详细的回答!我在 vb.net 上的表现显然比你差很多,所以我需要一段时间才能真正获得基于我自己的迂回代码工作的东西。如何从中获取答案并使其实际显示在文本框中? txtnumberofdesired1.Text = dResult 对我不起作用
      • @KieranO'mahony 非常感谢。看一下第一部分的用法,它是一个功能示例。基本上,您调用该函数将要查看的单词和要搜索的单词传递给它,然后它会返回一个字典,其中包含 每个 单词的结果。用途是:TextBox1.Text = dResult("myWord").Count
      【解决方案3】:

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

      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 这样出色的正则表达式工具来构建和测试表达式也很有帮助。 (上面的代码sn-p是RegexBuddy生成的!)

      【讨论】:

        【解决方案4】:

        试试下面的代码

        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
        

        【讨论】:

        • 没有解释就没有用,因为这远非唯一可行的方法。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-24
        • 1970-01-01
        • 1970-01-01
        • 2015-06-09
        相关资源
        最近更新 更多