Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

Here is an exemple.

 

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?

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

Posted (edited)

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:

 

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

Edited by Arch4ngel

"If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown

"Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me

"A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend.

C# TO VB TRANSLATOR

Posted

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:

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...