Count Words occurrences in txt file

Talk2Tom11

Centurion
Joined
Feb 22, 2004
Messages
144
Location
Westchester, NY
Hey... I am looking to open a txt file and count how many time each word occurs within that text file. I know how to check for a specific word with in a txt file and count how many times it occurs but i need to just read the txt file and then output a list of all the words within it and the number of times each one occurs.

If anyone could help me out... or help get me in the right dirrection then that would be great. Thanks
 
Here is an exemple.

Visual Basic:
Dim fs as StreamReader = File.OpenText(path)
Dim text as String = fs.ReadToEnd()
fs.Close()
'There you put your algorithm to parse words

How you define a word is up to you. Did you needed an algorithm to do that?
 
It all depend on how you want to calcultate words. But if you want here is something in VB.NET that would need some refining but that could give you a quick start:

Visual Basic:
Dim regex As New System.Text.RegularExpressions.Regex(inputPattern)
regex.Matches(input).Count


The only work on your side now would be to find the right pattern.

Additionally, you could just do a Split on spaces and count the amount of word you have in your string. The only question is how many words the word "don't" have. 1 or 2 ?

EDIT:
Here is the pattern: \b(\w+?)\b

Damn... I'm too kind with you guys. You really should look by yourself! :P
 
Last edited:
As Arch4ngel wrote, you can build an array of words from the whole text using Split.
Now, you can use a Hashtable to count how many times each word appears in the text.
For example:
C#:
Hashtable htWords = new Hashtable();
String[] words;
...
words = txt.Split(); //txt is the string from the text file
Foreach (string word in words)
{
     if (htWords.Contains(word))
          htWords[word] = (int)htWords[word]+1;
     else
        htWords.add(word,1)
}
At the end the Hashtable should include a list of all the words and the number of times each word appears.
 
Back
Top