Results 1 to 2 of 2

Thread: word count

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2003
    Posts
    2

    Talking word count

    Unique words in a text box

    Hi, please can any one help me to correct my code! It s/b a word,letter and uniq words counter (text box and tree list boxes to displey letters, words and uniq words!
    , but I am missing something!

    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

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    '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.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    Friend WithEvents ListBox2 As System.Windows.Forms.ListBox
    Friend WithEvents ListBox3 As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.Button1 = New System.Windows.Forms.Button()
    Me.TextBox1 = New System.Windows.Forms.TextBox()
    Me.ListBox1 = New System.Windows.Forms.ListBox()
    Me.ListBox2 = New System.Windows.Forms.ListBox()
    Me.ListBox3 = New System.Windows.Forms.ListBox()
    Me.SuspendLayout()
    '
    'Button1
    '
    Me.Button1.Location = New System.Drawing.Point(176, 232)
    Me.Button1.Name = "Button1"
    Me.Button1.Size = New System.Drawing.Size(136, 24)
    Me.Button1.TabIndex = 0
    Me.Button1.Text = "Button1"
    '
    'TextBox1
    '
    Me.TextBox1.Location = New System.Drawing.Point(24, 8)
    Me.TextBox1.Name = "TextBox1"
    Me.TextBox1.Size = New System.Drawing.Size(464, 20)
    Me.TextBox1.TabIndex = 1
    Me.TextBox1.Text = "TextBox1"
    '
    'ListBox1
    '
    Me.ListBox1.Location = New System.Drawing.Point(24, 64)
    Me.ListBox1.Name = "ListBox1"
    Me.ListBox1.Size = New System.Drawing.Size(112, 160)
    Me.ListBox1.Sorted = True
    Me.ListBox1.TabIndex = 2
    '
    'ListBox2
    '
    Me.ListBox2.Location = New System.Drawing.Point(160, 64)
    Me.ListBox2.Name = "ListBox2"
    Me.ListBox2.Size = New System.Drawing.Size(152, 160)
    Me.ListBox2.Sorted = True
    Me.ListBox2.TabIndex = 3
    '
    'ListBox3
    '
    Me.ListBox3.Location = New System.Drawing.Point(328, 64)
    Me.ListBox3.Name = "ListBox3"
    Me.ListBox3.Size = New System.Drawing.Size(160, 160)
    Me.ListBox3.Sorted = True
    Me.ListBox3.TabIndex = 4
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(512, 266)
    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListBox3, Me.ListBox2, Me.ListBox1, Me.TextBox1, Me.Button1})
    Me.Name = "Form1"
    Me.Text = "Form1"
    Me.ResumeLayout(False)

    End Sub

    #End Region

    Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    End Sub

    Private Sub ListBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Declare a String variable to put the text to be analyzed
    Dim text As String

    'Copy the text from the textbox and convert it to upper case
    text = UCase(TextBox1.Text)

    'Declare an array for number of occurences of each character
    Dim letters(26) As Integer
    Dim i As Int64
    Dim j As Int64

    'Initialize each character's count to 0
    For i = 1 To 26
    letters([i] = 0)
    Next c

    'we will put every word to the words() array
    Dim word_count As Integer = 0
    Dim words() As String

    'inside_word will tell us whether we have started a word or are between words
    Dim inside_word As Boolean = False

    'we will append each character and construct the word, until we reach
    'a non-letter character
    Dim word As String = ""

    'scan through the text, count each character's occurences, identify words
    For i = 1 To Len(text)
    Dim c As String = text(i)
    If Asc(c) >= Asc("A") And Asc(c) <= Asc("Z") Then
    'it is a letter!
    'first, increment this letter's counter
    letters[Asc(c)] = letters[Asc(c)] + 1

    'if we have not started a word already, start it
    If Not inside_word Then
    inside_word = True
    End If

    'append this letter to the the word we are in
    word = word & c
    Else
    'it is a non-letter character, cut the word
    'make a room for it by expandin the array
    word_count = word_count + 1
    ReDim Preserve words(word_count)
    words(word_count) = word
    'reset the word so next letter starts with an empty word
    inside_word = False
    word = ""
    End If
    Next i
    'if we ended at a non-letter character, last word is still in progress,
    'put it into our array
    If inside_word Then
    word_count = word_count + 1
    ReDim Preserve words(word_count)
    words(word_count) = word
    End If

    'we now know each character's count as well as all words
    'next thing to do is sort the words and identify unique words


    'Bubble sort; slow algorithm, but works for sure
    For i = 1 To word_count - 1
    For j = i + 1 To word_count
    If words(j) < words(i) Then
    'swap the words
    Dim temp As String = words(j)
    words(j) = words(i)
    words(i) = temp
    End If
    Next j
    Next i

    'words array is now sorted; scan through it, identify unique words

    Dim unique_words() As String
    Dim unique_word_count As Integer = 0
    Dim last_word As String = ""

    For i = 1 To word_count
    'If next word is different than the previous, it is unique
    If Not words(i) = last_word Then
    'increase the counter
    unique_word_count = unique_word_count + 1
    'make a room for the new unique word
    ReDim Preserve unique_words(unique_word_count)
    'and put it into the unique_words array
    unique_words(unique_word_count) = words(i)

    'we will compare the next word to this one
    last_word = words(i)
    End If
    Next i


    'Everything is ready
    'The only thing left is to put the results on the screen

    'Let's start with the letter count
    For i = 1 To 26
    ListBox1.AddItem(Chr(i) & " : " || letters(i), i-1)
    Next i

    'Now, all words
    For i = 1 To word_count
    ListBox2.AddItem(words(i), i - 1)
    Next i

    'And finally, unique words
    For i = 1 To unique_word_count
    ListBox3.AddItem(unique_words(i), i - 1)
    Next i

    End Sub

    Private Sub ListBox1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    End Sub
    End Class



    thank you


  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    This is the VB6 forum, you can find the VB.NET forum here: http://www.vbforums.com/forumdisplay.php?s=&forumid=25

    You'll also probably get a better response there
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width