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?

Dani AI

Generated

Good start — 's loop is the right pattern: iterate the words and test each one. The simple “count vowel groups” trick is a useful baseline but miscounts many English words because of silent final “e”, words ending in “le”, silent “ed”, and the fact that “y” is not always a vowel. A practical approach is to keep the loop but replace the naive matcher with a small heuristic per-word estimator plus an exceptions table for irregular words.

The estimator below:

  • normalizes and strips non-letters,
  • treats aeiou as primary vowels and treats y as a vowel only when not the first letter,
  • counts contiguous vowel groups (so “boat” → 1 group),
  • subtracts a silent final e except when it forms a consonant+“le” syllable (e.g., “table”),
  • subtracts an often-silent trailing “ed” in many verbs,
  • enforces a minimum of one syllable,
  • and allows overriding via an exceptions dictionary.

This is heuristic — it improves accuracy for ordinary text but is not perfect. For high accuracy, pair the heuristics with an exceptions dictionary (hard-coded or loaded from a small file) or use a pronunciation lexicon such as CMU Pronouncing Dictionary.

' Estimate syllables for a single word (heuristic)
Function SyllableCount(ByVal input As String) As Integer
    If String.IsNullOrWhiteSpace(input) Then Return 0
    Dim w As String = Regex.Replace(input.ToLowerInvariant(), "[^a-z']", "")
    If w.Length = 0 Then Return 0
    If w.Length <= 3 Then Return 1

    Dim vowels As String = "aeiou"
    Dim count As Integer = 0
    Dim prevWasVowel As Boolean = False

    For i As Integer = 0 To w.Length - 1
        Dim c As Char = w(i)
        Dim isVowel As Boolean = vowels.IndexOf(c) >= 0 OrElse (c = "y"c AndAlso i > 0)
        If isVowel AndAlso Not prevWasVowel Then count += 1
        prevWasVowel = isVowel
    Next

    If w.EndsWith("e") Then
        If Not (w.EndsWith("le") AndAlso w.Length > 2 AndAlso vowels.IndexOf(w(w.Length - 3)) = -1) Then
            count -= 1
        End If
    End If

    If w.EndsWith("ed") AndAlso w.Length > 2 AndAlso vowels.IndexOf(w(w.Length - 3)) = -1 Then
        count -= 1
    End If

    If count < 1 Then count = 1
    Return count
End Function

' Example: count words with exactly 3 syllables
Dim threeCount As Integer = 0
For Each m As Match In Regex.Matches(text, "\b[A-Za-z']+\b")
    If SyllableCount(m.Value) = 3 Then threeCount += 1
Next

Notes: keep an exceptions dictionary for words that break the rules, log word→count during testing to find systematic errors, and treat hyphenated compounds as either separate words or single tokens depending on desired behavior. This combination (loop + heuristic + exceptions) gives practical, maintainable results for most applications.

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.