【发布时间】:2012-10-24 15:07:45
【问题描述】:
如何使用 vb.net 计算特定文本文件中的特定单词
【问题讨论】:
-
将其加载到字符串变量中,然后执行以下操作:stackoverflow.com/questions/9121907/count-words-in-visual-basic 与
"\bFind Me\b"结合使用
标签: vb.net word-count
如何使用 vb.net 计算特定文本文件中的特定单词
【问题讨论】:
"\bFind Me\b" 结合使用
标签: vb.net word-count
这样的事情会帮助你:
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
【讨论】:
假设 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))
【讨论】: