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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15524133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:53:04  来源:igfitidea点击:

Count occurance of specific words in a text file in vb.net

vb.netword-count

提问by Kieran O' mahony

I'm trying to count the number of an item in a text file, by counting each instance the item was entered into the file earlier on in the program.

我正在尝试计算文本文件中某个项目的数量,方法是计算该项目早先在程序中输入到文件中的每个实例。

I already have the text read from the file and in a text box. The problem is that my current code was just counting the characters in the textbox and not the number of times my desired word was in the file.

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

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

This counts the characters in the textbox instead of counting the number of desired words. I tried repeatedly before asking help and searched extensively, but I just don't understand what's wrong with my code. Please help :)

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

回答by Kapil Khandelwal

You can use SplitFunction :

您可以使用拆分功能:

C#:

C#:

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

VB.net:

VB.net:

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

回答by SysDragon

Try this:

尝试这个:

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)

Function used:

使用的功能:

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

You will have not only the number of occurances, but the index positions in the file, grouped easily in a Dictionary.

您将不仅拥有出现次数,还拥有文件​​中的索引位置,可以轻松地分组到Dictionary.

回答by Dave Michener

I prefer to use Regular Expressions in this type of situation. They are very tricky to understand but they are extremely powerful and typically faster than other string manipulation techniques.

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

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

In your case you are looking for the value from AllMatchResults.Count.

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

Using a great Regular Expression tool like RegexBuddyto build and test the expressions is a great help too. (The above code snippet was generated by RegexBuddy!)

使用像RegexBuddy这样出色的正则表达式工具来构建和测试表达式也很有帮助。(以上代码片段由 RegexBuddy 生成!)

回答by Teach Me How To Sell

Try the following code

试试下面的代码

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