【问题标题】:Count specific word in txt file vb.net计算 txt 文件 vb.net 中的特定单词
【发布时间】:2012-10-24 15:07:45
【问题描述】:

如何使用 vb.net 计算特定文本文件中的特定单词

【问题讨论】:

标签: vb.net word-count


【解决方案1】:

这样的事情会帮助你:

Private Function GetWordCountInFile(ByVal filepath As String, ByVal word As String)
    Dim dict As New Dictionary(Of String, Integer)()
    Dim lst As New List(Of String)(IO.File.ReadAllText(filepath).Split(" "))

    For Each entry As String In lst
        If Not (dict.ContainsKey(entry.ToLower.Trim)) Then
            dict.Add(entry.ToLower.Trim, 1)
        Else
            dict(entry.ToLower.Trim) += 1
        End If
    Next
    lst.Clear()
    Return dict(word.ToLower.Trim)
End Function

你可以这样使用它:

   Dim count As Integer = GetWordCountInFile("../../Sample.txt", "sample")

这将在文本文件“sample.txt”中查找单词“sample”并返回一个计数。

另外,可能不是一个好的方法,但单行方法是:

Private Function GetWordCountInFile(ByVal filepath As String, ByVal word As String)
    Return System.Text.RegularExpressions.Regex.Matches(IO.File.ReadAllText(filepath), "(?i)\b(\s+)?" & word & "(\s+|\S{0})\b|^" & word & "\.?$|" & word & "[\.\,\;]").Count
End Function

或者是这样的:(无需声明额外的变量来保存字数)

   Private Function GetWordCountInFile(ByVal filepath As String, ByVal word As String)
    Dim lst As New List(Of String)(IO.File.ReadAllText(filepath).ToLower.Split(New Char() {" ", ",", ";", ".", ":"}))
    Return lst.FindAll(Function(c) c.Trim() = word.ToLower).Count()
   End Function

【讨论】:

    【解决方案2】:

    假设 4.0...

    单词必须完全匹配(不包括混合大小写)。如果要统计匹配的子词,比如搜索“sub”,把“subway”算作一个词,改成LCase(strWord).Contains(LCase("TargetWord"))...

        Dim intCount As Integer = 0
        IO.File.ReadAllText("C:\file.txt").Split(" ").ToList().ForEach(Sub(strWord As String)
                                                                           If LCase(strWord) = LCase("TargetWord") Then
                                                                               intCount += 1
                                                                           End If
                                                                       End Sub)
        MsgBox(CStr(intCount))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      • 2015-06-14
      • 2012-10-31
      • 2011-05-30
      • 2015-06-02
      • 1970-01-01
      相关资源
      最近更新 更多