Hi all. I need help to write a code which will count the number of words of 3 syllables. I've been able to write the code for counting number of words, and counting number of syllables in a text. However I have tried and written a code for the above without success.

Useful part of code counting number of words.

Dim count as Integer
count = Split(str, Space(1)).Length

MessageBox.Show("No. of words: " & count.ToString())

Useful part of code counting syllables.

Dim str As String = textbox1.Text

Dim pattern As String = "[aeiouy]+"
Dim count As Integer = Regex.Matches(str, pattern).Count

MessageBox.Show("No. of syllables: " & count.ToString())

I want to count number of words of specific number of syllables. Can anybody help?

Recommended Answers

All 2 Replies

Here's one solution

' Test string
Dim str As String = "The decorated pieplate contains a surprise"
Dim Words() As String
Dim Word As String
' Number of syllables to search for
Dim SearchSyllableCount As Integer = 3
Dim pattern As String = "[aeiouy]+"
Dim count As Integer = 0

' Get an array of words
Words = Split(str, Space(1))
count = 0
For Each Word In Words
  ' Has desired number of syllables?
  If Regex.Matches(Word, pattern).Count = SearchSyllableCount Then
    ' Yes it has, increase count
    count += 1
  End If
Next

MessageBox.Show("No. of words with " & SearchSyllableCount.ToString & " syllables: " & count.ToString())

I'm pretty sure that you get other solutions too. The code above loops all the words and I couldn't come up with a code without any loops :-/

HTH

Thank you very much. I tried it and it's working. I'm trying to improve it for more accuracy.

Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.