Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2007-07-21
2
Medium Priority
?
1,180 Views
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?

0
Comment
Question by:mcainc
2 Comments
 
LVL 7

Accepted Solution

by:
icr earned 2000 total points
ID: 19542091
You should be able to modify this for your needs:
Sub Main()
        Const dictionaryUrl = "http://www.chambersharrap.co.uk/chambers/features/chref/chref.py/main?title=21st&query={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
                Else
                    wordCount.Add(psaValue, 1)
                End If
            Next
        End Using

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

Author Comment

by:mcainc
ID: 19542692
thanks a ton! this should be perfect & it looks like I can reference the thesaurus as well which is great
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

564 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