VB.NET counting NOUNS, VERBS, etc in text / paragraph. Best way / component?

Posted on 2007-07-21
Last Modified: 2013-11-26
If using VB.NET what is the best way (or service / plug-in / api / component) to take a block of text like a paragraph and determine the count of nouns, verbs, adjectives, adverbs, etc in it?

Something local like a dictionary component would be awesome, anyone know of anything like this?

Question by:mcainc
    LVL 7

    Accepted Solution

    You should be able to modify this for your needs:
    Sub Main()
            Const dictionaryUrl = "{0}"
            Dim testString = "Now is the winter of our discontent, made glorious summer by this sun of York"
            Dim psaRegex = New Regex("<span class=""psa"">(?<psa>[^<]*)</span>")

            Dim wordCount As New Dictionary(Of String, Integer)
            wordCount.Add("Unknown", 0)

            Using dictionaryWebClient As New WebClient()
                'Split the text up into words.
                Dim dictionaryMatch As Match
                For Each dictionaryMatch In New Regex("[\w]*").Matches(testString)
                    If String.IsNullOrEmpty(dictionaryMatch.Value) Then
                        Continue For
                    End If

                    Dim dictionarySource As String = dictionaryWebClient.DownloadString(String.Format(dictionaryUrl, dictionaryMatch.Value))
                    Dim wordDefinitionStart As Integer = dictionarySource.IndexOf(String.Format("<span class=""hwd"">{0}</span>", dictionaryMatch.Value), StringComparison.InvariantCultureIgnoreCase)

                    'If no exact match is found, then simply move onto the next word.
                    If wordDefinitionStart = -1 Then
                        wordCount("Unknown") += 1
                        Continue For
                    End If

                    Dim psaMatch As Match = psaRegex.Match(dictionarySource, wordDefinitionStart)

                    If Not psaMatch.Success Then
                        wordCount("Unknown") += 1
                        Continue For
                    End If

                    Dim psaValue As String = psaMatch.Groups("psa").Value
                    If wordCount.ContainsKey(psaValue) Then
                        wordCount(psaValue) += 1
                        wordCount.Add(psaValue, 1)
                    End If
            End Using

            Dim psa As String
            For Each psa In wordCount.Keys
                Console.WriteLine(String.Format("{0}: {1}", psa, wordCount(psa)))
        End Sub

    Author Comment

    thanks a ton! this should be perfect & it looks like I can reference the thesaurus as well which is great

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    734 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