0

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

4

2 回答 2

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))
于 2012-10-23T11:43:48.543 回答
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
于 2012-10-23T11:44:36.700 回答