⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 Mastering VBNet Include Source Code
💻 VB
字号:
Imports System.io
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization.Formatters

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
        Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
        Friend WithEvents LoadTable As System.Windows.Forms.MenuItem
    Friend WithEvents FrquencyMenu As System.Windows.Forms.MenuItem
    Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
    Friend WithEvents SaveBinary As System.Windows.Forms.MenuItem
    Friend WithEvents SaveSOAP As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents LoadBinary As System.Windows.Forms.MenuItem
    Friend WithEvents SaveText As System.Windows.Forms.MenuItem
    Friend WithEvents LoadText As System.Windows.Forms.MenuItem
                
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.Container

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.FrquencyMenu = New System.Windows.Forms.MenuItem()
        Me.SaveBinary = New System.Windows.Forms.MenuItem()
        Me.SaveText = New System.Windows.Forms.MenuItem()
        Me.LoadBinary = New System.Windows.Forms.MenuItem()
        Me.LoadText = New System.Windows.Forms.MenuItem()
        Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.Button3 = New System.Windows.Forms.Button()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.MainMenu1 = New System.Windows.Forms.MainMenu()
        Me.MenuItem1 = New System.Windows.Forms.MenuItem()
        Me.LoadTable = New System.Windows.Forms.MenuItem()
        Me.SaveSOAP = New System.Windows.Forms.MenuItem()
        Me.SuspendLayout()
        '
        'FrquencyMenu
        '
        Me.FrquencyMenu.Index = 0
        Me.FrquencyMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.SaveBinary, Me.SaveText, Me.LoadBinary, Me.LoadText})
        Me.FrquencyMenu.Text = "Frequency Table"
        '
        'SaveBinary
        '
        Me.SaveBinary.Index = 0
        Me.SaveBinary.Text = "Save Binary"
        '
        'SaveText
        '
        Me.SaveText.Index = 1
        Me.SaveText.Text = "Save SOAP"
        '
        'LoadBinary
        '
        Me.LoadBinary.Index = 2
        Me.LoadBinary.Text = "Load Binary"
        '
        'LoadText
        '
        Me.LoadText.Index = 3
        Me.LoadText.Text = "Load SOAP"
        '
        'SaveFileDialog1
        '
        Me.SaveFileDialog1.FileName = "doc1"
        '
        'TextBox1
        '
        Me.TextBox1.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.TextBox1.Location = New System.Drawing.Point(8, 8)
        Me.TextBox1.MaxLength = 0
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ReadOnly = True
        Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.TextBox1.Size = New System.Drawing.Size(280, 312)
        Me.TextBox1.TabIndex = 4
        Me.TextBox1.Text = ""
        '
        'Button2
        '
        Me.Button2.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.Button2.Location = New System.Drawing.Point(48, 360)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(192, 23)
        Me.Button2.TabIndex = 1
        Me.Button2.Text = "Show Word Count"
        '
        'Button3
        '
        Me.Button3.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.Button3.Location = New System.Drawing.Point(48, 392)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(192, 23)
        Me.Button3.TabIndex = 3
        Me.Button3.Text = "Sort Words by Frequency"
        '
        'Button1
        '
        Me.Button1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.Button1.Location = New System.Drawing.Point(48, 328)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(192, 23)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Read Text File"
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.FrquencyMenu})
        '
        'MenuItem1
        '
        Me.MenuItem1.Index = -1
        Me.MenuItem1.Text = "Load SOAP"
        '
        'LoadTable
        '
        Me.LoadTable.Index = -1
        Me.LoadTable.Text = "Load BINARY"
        '
        'SaveSOAP
        '
        Me.SaveSOAP.Index = -1
        Me.SaveSOAP.Text = "Save SOAP"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(296, 421)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox1, Me.Button3, Me.Button2, Me.Button1})
        Me.Menu = Me.MainMenu1
        Me.Name = "Form1"
        Me.Text = "Count Word Frequencies"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim WordFrequencies As New SortedList()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' prompt for text file
        OpenFileDialog1.DefaultExt = "TXT"
        OpenFileDialog1.Filter = "Text|*.TXT|All Files|*.*"
        OpenFileDialog1.ShowDialog()
        If OpenFileDialog1.FileName = "" Then Exit Sub
        Dim str As StreamReader
        Dim txtFile As File
        ' establish a StreamReader object to the file
        str = File.OpenText(OpenFileDialog1.FileName)

        Dim txtLine As String
        Dim Words() As String
        ' these are the common word delimiters
        Dim Delimiters() As Char = {CType(" ", Char), CType(".", Char), _
                                    CType(",", Char), CType("'", Char), _
                                    Chr(10), Chr(13)}
        Me.Text = "Calculating word count"
        ' read text and store into txtLine variable
        txtLine = str.ReadToEnd
        ' break text into individual words and store them into the Words array
        Words = txtLine.Split(Delimiters)
        Dim uniqueWords As Integer
        Dim iword As Integer, word As String
        ' iterate through all the words and add the unique ones to the SortedList
        ' Each word is a key for the word's count
        For iword = 0 To Words.GetUpperBound(0)
            word = Words(iword).ToUpper
            If IsValidWord(word) Then
                ' if word is in the list already, increase its count by 1
                ' if not, add the word and set its count to 1
                If Not WordFrequencies.ContainsKey(word) Then
                    WordFrequencies.Add(word, 1)
                    uniqueWords += 1
                Else
                    WordFrequencies(word) = CType(WordFrequencies(word), Integer) + 1
                End If
            End If
        Next
        MsgBox("Read " & Words.Length & " words and found " & _
                    uniqueWords & " unique words")
        TextBox1.Clear()
    End Sub

    ' This function returns False is the word passed as argument is invalid
    ' Valid words are made up of letters ("alter-ego" is not a valid word, for example)
    Protected Function IsValidWord(ByVal word As String) As Boolean
        If Trim(word).Length = 0 Then
            Return (False)
        End If
        Dim ch As Char
        Dim iChar As Integer
        For iChar = 0 To Len(word) - 1
            ch = word.Chars(iChar)
            If Not System.Char.IsLetter(ch) Then
                Return (False)
            End If
        Next
        Return (True)
    End Function

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim wEnum As IDictionaryEnumerator
        Dim occurrences As Integer
        Dim allWords As New System.Text.StringBuilder()
        ' iterate through the list and display words and their count
        wEnum = WordFrequencies.GetEnumerator
        While wEnum.MoveNext
            allWords.Append(wEnum.Key.ToString & vbTab & "-->" & vbTab & wEnum.Value.ToString & vbCrLf)
        End While
        TextBox1.Text = allWords.ToString
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim wEnum As IDictionaryEnumerator
        Dim Words(WordFrequencies.Count) As String
        Dim Frequencies(WordFrequencies.Count) As Double
        Dim allWords As New System.Text.StringBuilder()
        Dim i, totCount As Integer
        ' iterate through the list and calculateword frequencies
        wEnum = WordFrequencies.GetEnumerator
        While wEnum.MoveNext
            Words(i) = CType(wEnum.Key, String)
            Frequencies(i) = CType(wEnum.Value, Integer)
            totCount = totCount + Frequencies(i)
            i = i + 1
        End While
        ' display words and their frequencies
        For i = 0 To Words.GetUpperBound(0)
            Frequencies(i) = Frequencies(i) / totCount
        Next
        Words.Sort(Frequencies, Words)
        TextBox1.Clear()
        For i = Words.GetUpperBound(0) To 0 Step -1
            allWords.Append(Words(i) & vbTab & "-->" & vbTab & Format(100 * Frequencies(i), "#.000") & vbCrLf)
        Next
        TextBox1.Text = allWords.ToString
    End Sub

    Private Sub SaveText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveText.Click
        Dim saveFile As FileStream
        SaveFileDialog1.DefaultExt = "XML"
        If SaveFileDialog1.ShowDialog = DialogResult.OK Then
            saveFile = File.OpenWrite(SaveFileDialog1.FileName)
            saveFile.Seek(0, SeekOrigin.End)
            Dim Formatter As Soap.SoapFormatter = New Soap.SoapFormatter()
            Formatter.Serialize(saveFile, WordFrequencies)
            saveFile.Close()
            MsgBox("File saved")
        End If
    End Sub

    Private Sub LoadBinary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadBinary.Click
        Dim readFile As FileStream
        OpenFileDialog1.DefaultExt = "BIN"
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            readFile = File.OpenRead(OpenFileDialog1.FileName)
            Dim BFormatter As BinaryFormatter
            BFormatter = New BinaryFormatter()
            WordFrequencies = CType(BFormatter.Deserialize(readFile), SortedList)
            readFile.Close()
            MsgBox("Read " & WordFrequencies.Count & " words and their counts ")
        End If
    End Sub

    Private Sub SaveBin(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBinary.Click
        Dim saveFile As FileStream
        SaveFileDialog1.DefaultExt = "BIN"
        If SaveFileDialog1.ShowDialog = DialogResult.OK Then
            saveFile = File.OpenWrite(SaveFileDialog1.FileName)
            saveFile.Seek(0, SeekOrigin.End)
            Dim Formatter As BinaryFormatter = New BinaryFormatter()
            Formatter.Serialize(saveFile, WordFrequencies)
            saveFile.Close()
            MsgBox("File saved")
        End If
    End Sub

    Private Sub LoadText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadText.Click
        Dim readFile As FileStream
        OpenFileDialog1.DefaultExt = "XML"
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            readFile = File.OpenRead(OpenFileDialog1.FileName)
            Dim Formatter As Soap.SoapFormatter
            Formatter = New Soap.SoapFormatter()
            WordFrequencies = CType(Formatter.Deserialize(readFile), SortedList)
            readFile.Close()
            MsgBox("Read " & WordFrequencies.Count & " words and their counts ")
        End If
    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -