Solved

VB.NET Word Count algorithm

Posted on 2009-04-06
2
1,404 Views
Last Modified: 2012-05-06
Can anyone suggest a model algorithm to (a) output the number of words in the text in the code window and (b) a separate algorithm to count the frequency of each word appearing in the text?

pythonV
The vessel, which is operated by an Italian company, carried a crew of 24, from Bulgaria, Ukraine, Russia and the Philippines, Britain's Telegraph newspaper reported.

Open in new window

0
Comment
Question by:pythonV
2 Comments
 
LVL 15

Accepted Solution

by:
ChloesDad earned 250 total points
ID: 24082299
Hi, The first one is fairly easy. Thers is a string.split function which returns a string array

dim Words() as string = mytext.split(convert.tochar(" "))
dim NumberofWords as integer = Words.length

You can then use the array to work out the number of times each word appears, although not a problem with the given text, the punctuation would have to be removed from the original string first otherwise "vessel," and "vessel" would be treated as different words.
0
 
LVL 10

Assisted Solution

by:MrClyfar
MrClyfar earned 250 total points
ID: 24082418
Hi there.
Here's a quick and dirty way to get the number of times each word is used in the string. As ChloesDad noted, you may need to change the code to take care of characters such as ',:; etc.
You need to use .NET framework 3.5 for my code example to work.
Hope this helps.
Jas.

Imports System.Text.RegularExpressions
 

Module Module1
 

    Sub Main()

        Dim inputString As String = "The vessel, which is operated by an Italian company, carried a crew of 24, from the country of Bulgaria, Ukraine, Russia and the Philippines, Britain's Telegraph newspaper reported."
 

        Dim wordCounts As Dictionary(Of String, Integer) = GetWordUsageCount(inputString)
 

        For Each kvp As KeyValuePair(Of String, Integer) In wordCounts

            Console.WriteLine("Word = {0}, Count = {1}", kvp.Key, kvp.Value)

        Next
 

    End Sub
 

    Private Function GetWordUsageCount(ByVal input As String) As Dictionary(Of String, Integer)
 

        Dim m As MatchCollection = Regex.Matches(input, "[^\ ^\t^\n^,]+", RegexOptions.Singleline)
 

        Dim words = (From word In m _

                     Select word.value).ToList()
 

        Dim wordGroups = From word In words _

                         Group By word.ToString.ToLower _

                         Into wordCount = Count()
 

        Return wordGroups.ToDictionary(Of String, Integer)(Function(key) key.ToLower, Function(value) value.wordCount)
 
 

    End Function
 

End Module

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now