Results 1 to 10 of 10

Thread: Finding how many times the same word appears in a text file? {RESOLVED} THANKS JOACIM

  1. #1

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Finding how many times the same word appears in a text file? {RESOLVED} THANKS JOACIM

    Hello,

    I have a .txt file and it loads into a TextBox on my program. Once loaded I need a cmd button to click that will tell me how many times each word that exists in the textbox is.

    For example.

    Textbox.Text = Hello, My name is Stilekid007, what is your name?

    RESULTS WOULD BE:

    1 Hello
    1 My
    2 Name
    2 Is
    1 Stilekid007
    1 What
    1 Your
    -------

    Does anyone know if this can be done in VB 6.0?

    Thank you so muc for any replys!
    Stilekid007
    Last edited by stilekid007; Jun 13th, 2005 at 07:24 PM.

  2. #2

  3. #3

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Finding how many times the same word appears in a text file?

    Yes I would Sir.

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Finding how many times the same word appears in a text file?

    Add the two attached classes to your project and use code simular to the below to use them.
    VB Code:
    1. Dim oWordCounter As CWords
    2. Dim oWord As CWord
    3.  
    4. Set oWordCounter = New CWords
    5. Call oWordCounter.CountWords("Hello what is your name? My name is Joacim.")
    6. For Each oWord In oWordCounter
    7.     Debug.Print oWord.Word, oWord.Count
    8. Next
    9. Debug.Print "Number of different words: "; oWordCounter.Count
    Attached Files Attached Files

  5. #5

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Finding how many times the same word appears in a text file?

    hmmmmmm It only displays which words are in the sentence. Not how many times each appear.

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Finding how many times the same word appears in a text file?

    Oh yes it does! The oWordCounter is a collection containing CWord objects (called oWord above). This object has two properties, Word and Count, the Count property will be how many times that particular word existed in the text. The CWords collection, called oWordCounter above also has a Count property but that will contain the number of different words that existed in the supplied text.

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Finding how many times the same word appears in a text file?

    Quote Originally Posted by stilekid007
    hmmmmmm It only displays which words are in the sentence. Not how many times each appear.
    Look at this part of the code:
    VB Code:
    1. For Each oWord In oWordCounter
    2.     'oWord.Word is the word itself while oWord.Count will be the number of times
    3.     'that word existed in the supplied text!
    4.     Debug.Print oWord.Word, oWord.Count
    5. Next

  8. #8

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Finding how many times the same word appears in a text file?

    aha! Now I see where it is displaying the numbers! Well quite clever!

    I do thank you Joacim!

    I will post if I have any more non avoidable quesitons lol.

    Thank you so much for the code and time!
    Stilekid007

  9. #9
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Finding how many times the same word appears in a text file?

    Quote Originally Posted by stilekid007
    hmmmmmm It only displays which words are in the sentence. Not how many times each appear.
    Joacim,
    Nice code, BTW here is my output:
    Attached Images Attached Images  
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  10. #10

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Finding how many times the same word appears in a text file?

    Yea my immediate window was puched to far to the right so I didn't see the numbers just the words.

    It is really nice code!

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