【问题标题】:What is the best way to calculate word frequency in VB.NET?在 VB.NET 中计算词频的最佳方法是什么?
【发布时间】:2015-03-15 21:47:48
【问题描述】:

有一些关于如何在 C# 中计算词频的好例子,但没有一个是全面的,我真的需要一个在 VB.NET 中。

我目前的方法仅限于每个频率计数一个单词。什么是最好的方法来改变这个,这样我就可以得到一个完全准确的词频列表?

wordFreq = New Hashtable()

Dim words As String() = Regex.Split(inputText, "(\W)")
    For i As Integer = 0 To words.Length - 1
        If words(i) <> "" Then
            Dim realWord As Boolean = True
            For j As Integer = 0 To words(i).Length - 1
                If Char.IsLetter(words(i).Chars(j)) = False Then
                    realWord = False
                End If
            Next j

            If realWord = True Then
                If wordFreq.Contains(words(i).ToLower()) Then
                    wordFreq(words(i).ToLower()) += 1
                Else
                    wordFreq.Add(words(i).ToLower, 1)
                End If
            End If
        End If
    Next

Me.wordCount = New SortedList

For Each de As DictionaryEntry In wordFreq
        If wordCount.ContainsKey(de.Value) = False Then
            wordCount.Add(de.Value, de.Key)
        End If
Next

我更喜欢实际的代码 sn-p,但通用的“哦,是的......使用这个并运行那个”也可以。

【问题讨论】:

    标签: vb.net hashtable word-frequency


    【解决方案1】:

    这可能是您正在寻找的:

        Dim Words = "Hello World ))))) This is a test Hello World"
        Dim CountTheWords = From str In Words.Split(" ") _
                            Where Char.IsLetter(str) _
                            Group By str Into Count()
    

    我刚刚测试过它确实有效

    编辑!我添加了代码以确保它只计算字母而不计算符号。

    仅供参考:我发现一篇关于如何使用 LINQ 和目标 2.0 的文章,感觉有点脏,但它可能对某人有所帮助 http://weblogs.asp.net/fmarguerie/archive/2007/09/05/linq-support-on-net-2-0.aspx

    【讨论】:

    • 我使用的是 .net 2.0,所以很遗憾我不能使用 LINQ。
    • Awww 这完全就是把它全部填满。
    • 这对你来说很容易。
    • 使用新的编译器和目标 2.0 框架。从 Mono 中复制 enumerable.cs,然后直接复制。
    • 我刚刚找到了这个,这可能会有所帮助:weblogs.asp.net/fmarguerie/archive/2007/09/05/…
    【解决方案2】:
    Public Class CountWords
    
        Public Function WordCount(ByVal str As String) As Dictionary(Of String, Integer)
            Dim ret As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)
    
            Dim word As String = ""
            Dim add As Boolean = True
            Dim ch As Char
    
            str = str.ToLower
            For index As Integer = 1 To str.Length - 1 Step index + 1
                ch = str(index)
                If Char.IsLetter(ch) Then
                    add = True
                    word += ch
                ElseIf add And word.Length Then
                    If Not ret.ContainsKey(word) Then
                        ret(word) = 1
                    Else
                        ret(word) += 1
                    End If
                    word = ""
                End If
            Next
    
            Return ret
        End Function
    
    End Class
    

    然后,对于一个快速演示应用程序,创建一个带有一个名为 InputBox 的多行文本框、一个名为 OutputList 的列表视图和一个名为 CountBtn 的按钮的 winforms 应用程序。在列表视图中创建两列 - “Word”和“Freq”。选择“详细信息”列表类型。为 CountBtn 添加事件处理程序。然后使用此代码:

    Imports System.Windows.Forms.ListViewItem
    
    Public Class MainForm
    
        Private WordCounts As CountWords = New CountWords
    
        Private Sub CountBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CountBtn.Click
            OutputList.Items.Clear()
            Dim ret As Dictionary(Of String, Integer) = Me.WordCounts.WordCount(InputBox.Text)
            For Each item As String In ret.Keys
                Dim litem As ListViewItem = New ListViewItem
                litem.Text = item
                Dim csitem As ListViewSubItem = New ListViewSubItem(litem, ret.Item(item).ToString())
    
                litem.SubItems.Add(csitem)
                OutputList.Items.Add(litem)
    
                Word.Width = -1
                Freq.Width = -1
            Next
        End Sub
    End Class
    

    你做了一件可怕的事情让我用 VB 写这个,我永远不会原谅你。

    :p

    祝你好运!

    编辑

    修复空字符串错误和大小写错误

    【讨论】:

    • index As Integer = 0 应该是 = 1,否则会漏掉第一个单词的第一个字符。否则这很好,谢谢。恭喜获得 2,000 分!
    【解决方案3】:

    【讨论】:

    • 我已经看过了——所有东西要么使用 LINQ,要么不在 .net 中
    【解决方案4】:

    非常接近,但 \w+ 是一个很好的匹配正则表达式(仅匹配单词字符)。

    Public Function CountWords(ByVal inputText as String) As Dictionary(Of String, Integer)
        Dim frequency As New Dictionary(Of String, Integer)
    
        For Each wordMatch as Match in Regex.Match(inputText, "\w+")
            If frequency.ContainsKey(wordMatch.Value.ToLower()) Then
                frequency(wordMatch.Value.ToLower()) += 1
            Else
                frequency.Add(wordMatch.Value.ToLower(), 1)
            End If
        Next
        Return frequency
    End Function
    

    【讨论】:

    • 你和你喜欢的正则表达式。我刚从为我的一个项目编写 Lexer 回来,并在那里处于“lexing”模式。不过,您的解决方案更好……也许没有那么快?我将不得不做研究。 +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    • 2015-08-21
    • 1970-01-01
    • 2010-09-13
    • 2021-12-03
    • 2013-03-17
    相关资源
    最近更新 更多