计算txt文件vb.net中的特定单词

时间:2012-10-23 11:27:59

标签: vb.net word-count

如何使用vb.net

计算特定文本文件中的特定单词

2 个答案:

答案 0 :(得分:0)

假设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))

答案 1 :(得分:0)

这样的事情会对你有所帮助:

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