Talk2Tom11 Posted June 2, 2006 Posted June 2, 2006 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 Quote
Arch4ngel Posted June 2, 2006 Posted June 2, 2006 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? Quote "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
Talk2Tom11 Posted June 2, 2006 Author Posted June 2, 2006 yeah i pretty much need an algorithm to go through each word within a txt file and see how many times each one occurs. Quote
Arch4ngel Posted June 2, 2006 Posted June 2, 2006 (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 June 2, 2006 by Arch4ngel Quote "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
Erel Posted June 3, 2006 Posted June 3, 2006 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.