Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

VB.NET Word Count algorithm

Posted on 2009-04-06
2
1,406 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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