我如何计算特定文本文件中的特定单词使用vb.net
这样的东西会帮助您:
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")
这将在文本文件"示例.txt"中寻找一个单词"示例"并返回计数。
另外,可能不是一个好的,但是单行方法是:
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
假设4.0 ...
这个词必须是确切的匹配(不包括混合情况)。如果要计算匹配子字的情况,例如搜索" sub"并将"地铁"计数为单词,请更改为lcase(wrord)。
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))