Question Count occurance of words in a .txt file

gazeranco

Well-known member
Joined
Feb 11, 2006
Messages
45
Location
Englandshire!
Programming Experience
Beginner
Hey, I am new to programming and I have something I want to do which I think might be simple but I cannot figure it out, could someone point me in the direction of a useful example?

So it should work something like this...

VB.NET:
Dim mywords(5)
mywords(0) = hello
mywords(1) = world
mywords(2) = how
mywords(3) = are
mywords(4) = you

for each mywords in textfile.txt
i = i + 1
next
msgbox.show("your words appeared" I "times")
end

I think its obviously harder then that but you get the idea of what I am trying to acheive??

Hope someone can help,

Thanks!
 
Here's an example using LINQ to group RegEx matches and output an anonymous type with the word and frequency it appears in your string.

VB.NET:
        Dim input As String = "Peter Piper picked a peck of pickled peppers.  A peck of pickled peppers Peter Piper picked.  Peter Piper was tired."
        Dim keywords() As String = {"peter", "peck", "pickled"}
        Dim regex As New System.Text.RegularExpressions.Regex("\w+")

        Dim wordsByCount = regex.Matches(input) _
                .Cast(Of Match)() _
                .Select(Function(c) c.Value.ToLowerInvariant()) _
                .Where(Function(c) keywords.Contains(c.ToLowerInvariant)) _
                .GroupBy(Function(c) c) _
                .Select(Function(g) New With {.Word = g.Key, .Count = g.Count()}) _
                .OrderByDescending(Function(g) g.Count) _
                .ThenBy(Function(g) g.Word)
 
Thanks for that Matt, I have played around with it for ages but its a little advanced for me I think. I cant figure out how to see any output of the words or just whats going on there? Any chance someone could comment the code to help me understand how this works and how I can use it to solve my problem?

Thanks,

Gaz
 
I have the following code working now, just need to figure out how to make it use a text file and not the string. Cheers for help guys! :)

VB.NET:
Imports System.Text.RegularExpressions

Module Module1
    Sub Main()
        ' Input string.
        Dim value As String = "word word word"
        Dim xnum As Int16 = 0
        ' Call Regex.Matches method.
        Dim matches As MatchCollection = Regex.Matches(value, "word")
        ' Loop over matches.
        For Each m As Match In matches
            xnum = xnum + 1
            ' Loop over captures.
            For Each c As Capture In m.Captures
                ' Display.
                Console.WriteLine("Index={0}, Value={1}", c.Index, c.Value)
                Console.WriteLine(xnum)
            Next
        Next
        InputBox("input")
    End Sub
End Module
 
Thanks for that Matt, I have played around with it for ages but its a little advanced for me I think. I cant figure out how to see any output of the words or just whats going on there? Any chance someone could comment the code to help me understand how this works and how I can use it to solve my problem?

Thanks,

Gaz

A LINQ query will give you an IEnumerable (of anonymous type in this case) so you can use a For loop to iterate through each item.

VB.NET:
        For Each entry In wordsByCount
            MessageBox.Show(String.Format("The word {0} occurred {1} time(s)", entry.Word, entry.Count))
        Next
 
Back
Top