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
Re: Finding how many times the same word appears in a text file?
Would you consider "What" as the same as "what", or for that matter "WHAT"?
Re: Finding how many times the same word appears in a text file?
1 Attachment(s)
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:
Dim oWordCounter As CWords
Dim oWord As CWord
Set oWordCounter = New CWords
Call oWordCounter.CountWords("Hello what is your name? My name is Joacim.")
For Each oWord In oWordCounter
Debug.Print oWord.Word, oWord.Count
Next
Debug.Print "Number of different words: "; oWordCounter.Count
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.
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.
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:
For Each oWord In oWordCounter
'oWord.Word is the word itself while oWord.Count will be the number of times
'that word existed in the supplied text!
Debug.Print oWord.Word, oWord.Count
Next
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! :thumb:
Stilekid007 :wave:
1 Attachment(s)
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:
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!