计算vb.net中文本文件中特定单词的出现次数

时间:2013-03-20 12:55:07

标签: vb.net word-count

我正在尝试计算文本文件中项目的编号,方法是在项目早期计算项目输入到文件中的每个实例。

我已经从文件和文本框中读取了文本。问题是我当前的代码只是计算文本框中的字符数,而不是我想要的单词在文件中的次数。

For Each desiredword As String In txtContentofFile.Text
        intdesiredword = intdesiredword + 1
        txtdesiredwordcount.Text = intdesiredword
Next

这会对文本框中的字符进行计数,而不是计算所需单词的数量。我在寻求帮助之前反复尝试并广泛搜索,但我只是不明白我的代码有什么问题。请帮助:)

4 个答案:

答案 0 :(得分:0)

您可以使用Split功能:

C#:

int count = txtContentofFile.Text.Split(desiredword).Length - 1;

VB.net:

Dim count As Integer = txtContentofFile.Text.Split(desiredword).Length - 1

答案 1 :(得分:0)

试试这个:

Dim text As String = IO.File.ReadAllText("C:\file.txt")
Dim wordsToSearch() As String = New String() {"Hello", "World", "foo"}
Dim words As New List(Of String)()
Dim findings As Dictionary(Of String, List(Of Integer))

'Dividing into words
words.AddRange(text.Split(New String() {" ", Environment.NewLine()}, StringSplitOptions.RemoveEmptyEntries))

findings = SearchWords(words, wordsToSearch)
Console.WriteLine("Number of 'foo': " & findings("foo").Count)

使用的功能:

Private Function SearchWords(ByVal allWords As List(Of String), ByVal wordsToSearch() As String) As Dictionary(Of String, List(Of Integer))
    Dim dResult As New Dictionary(Of String, List(Of Integer))()
    Dim i As Integer = 0

    For Each s As String In wordsToSearch
        dResult.Add(s, New List(Of Integer))

        While i >= 0 AndAlso i < allWords.Count
            i = allWords.IndexOf(s, i)
            If i >= 0 Then dResult(s).Add(i)
            i += 1
        End While
    Next

    Return dResult
End Function

您不仅会有出现次数,还会有文件中的索引位置,可以轻松归入Dictionary

答案 2 :(得分:0)

我更喜欢在这种情况下使用正则表达式。它们非常难以理解,但它们非常强大,通常比其他字符串操作技术更快。

Dim AllMatchResults As MatchCollection
Try
    Dim RegexObj As New Regex(desiredword)
    AllMatchResults = RegexObj.Matches(txtContentofFile.Text)
    If AllMatchResults.Count > 0 Then
        ' Access individual matches using AllMatchResults.Item[]
    Else
        ' Match attempt failed
    End If
Catch ex As ArgumentException
    'Syntax error in the regular expression
End Try

在您的情况下,您正在寻找AllMatchResults.Count。

中的值

使用像RegexBuddy这样的优秀正则表达式工具来构建和测试表达式也是一个很好的帮助。 (上面的代码片段是由RegexBuddy生成的!)

答案 3 :(得分:-1)

尝试以下代码

Function word_frequency(word_ As String, input As String) As Integer
    Dim ct = 0
    Try
        Dim wLEN = word_.Length
        Do While input.IndexOf(word_) <> -1
            Dim idx = input.IndexOf(word_) + wLEN
            ct += 1
            input = input.Substring(idx)
        Loop
    Catch ex As Exception

    End Try
    Return ct
End Function