General Topics include: .NET Framework
Note: Most posts can be categorized into one of the forums below, where experts on that topic will be able to help you find a solution to your problem faster and more efficiently.

Go Back  Xtreme .NET Talk > .NET > General > Count Words occurrences in txt file


Reply
 
Thread Tools Display Modes
  #1  
Old 06-02-2006, 11:31 AM
Talk2Tom11's Avatar
Talk2Tom11 Talk2Tom11 is offline
Centurion

Preferred language:
VB .Net
 
Join Date: Feb 2004
Location: Westchester, NY
Posts: 144
Talk2Tom11 is on a distinguished road
Question Count Words occurrences in txt file



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
Reply With Quote
  #2  
Old 06-02-2006, 11:35 AM
Arch4ngel's Avatar
Arch4ngel Arch4ngel is offline
Senior Contributor

Preferred language:
C#, ASP.NET, VB.NET
 
Join Date: Mar 2004
Location: Montreal, QC
Posts: 940
Arch4ngel is on a distinguished road
Default

Here is an exemple.

Code:
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
Reply With Quote
  #3  
Old 06-02-2006, 11:39 AM
Talk2Tom11's Avatar
Talk2Tom11 Talk2Tom11 is offline
Centurion

Preferred language:
VB .Net
 
Join Date: Feb 2004
Location: Westchester, NY
Posts: 144
Talk2Tom11 is on a distinguished road
Default

yeah i pretty much need an algorithm to go through each word within a txt file and see how many times each one occurs.
Reply With Quote
  #4  
Old 06-02-2006, 12:01 PM
Arch4ngel's Avatar
Arch4ngel Arch4ngel is offline
Senior Contributor

Preferred language:
C#, ASP.NET, VB.NET
 
Join Date: Mar 2004
Location: Montreal, QC
Posts: 940
Arch4ngel is on a distinguished road
Talking

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:

Code:
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
__________________
"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

Last edited by Arch4ngel; 06-02-2006 at 12:05 PM. Reason: Added pattern
Reply With Quote
  #5  
Old 06-03-2006, 09:44 AM
Erel Erel is offline
Newcomer
 
Join Date: Jun 2005
Posts: 5
Erel is on a distinguished road
Default

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:
Code:
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.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Get numbers out of .txt file Lanc1988 General 10 02-18-2006 09:28 PM
count a word in .txt file Talk2Tom11 Directory / File IO / Registry 3 09-17-2005 02:03 PM
.txt file Talk2Tom11 Directory / File IO / Registry 1 09-14-2005 04:28 PM
txt file eramgarden General 0 05-23-2004 04:54 PM
Count words in a text box andrew104 General 4 12-03-2002 01:50 AM

Advertisement:







Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe