1

I am developing a student project and need to write a function to count syllables in word. The function is like long countSyllables(String word).

How to count syllables in word using Java? Any suggestions?

The provided rules are:

  1. To count the number of syllables you should use letters a, e, i, o, u, y as vowels.

  2. Count the number of vowels in the word.

  3. Do not count double-vowels (for example, "rain" has 2 vowels but is only 1 syllable)

  4. If the last letter in the word is 'e' do not count it as a vowel (for example, "side" is 1 syllable) 

  5. If at the end it turns out that the word contains 0 vowels, then consider this word as 1-syllable.

I had already write the function but I think it is not optimal. So I just would like to see other possible solutions. If any.

Full description for the task: https://hyperskill.org/projects/39/stages/208/implement

Current implementation:

public static int countSyllables(final String word) {
    return max(1, word.toLowerCase()
            .replaceAll("e$", "")
            .replaceAll("[aeiouy]{2}", "a")
            .replaceAll("[^aeiouy]", "")
            .length());
}

kutschkem
  • 7,826
  • 3
  • 21
  • 56
Jegors Čemisovs
  • 608
  • 6
  • 14
  • Does this answer your question? [Detecting syllables in a word](https://stackoverflow.com/questions/405161/detecting-syllables-in-a-word) – kutschkem May 05 '20 at 13:33
  • 1
    So, you are not looking for an algorithm for syllable counting, only a more efficient version for your algorithm? Because I saw in the description of the task that a simple algorithm is already given. This is different than asking for syllable counting because I am reasonably sure that is more complicated than those four rules. – kutschkem May 05 '20 at 13:37
  • I'm sure those rules will get your assignment done, but English is more complex than that. Sometimes two vowels together indicate two syllables (reiterate). Sometimes a final e is a syllable (fiance). – NomadMaker May 05 '20 at 15:57
  • How have you optimized the algorithm? – Ose Daniel Okojie Oct 24 '21 at 09:26

1 Answers1

1
    public static int countSyllables(final String word) {
        return max(1, word.toLowerCase()
                    //in words that end with "e" replace 
                    //"e" with ""
                    .replaceAll("e$", "") //e.g base=bas
                    //when two vowels appear together, 
                    //replace them with "a" 
                    .replaceAll("[aeiouy]{2}", "a") //e.g you == au, 
                    //beautiful==bautiful
                    //again, when two vowels appear together, 
                    //replace them with "a" 
                    .replaceAll("[aeiouy]{2}", "a") //e.g au == a,
                    //bautiful==batiful
                    //replace any character that isn't aeiouy with ""
                    .replaceAll("[^aeiouy]", "") //e.g, batiful==aiu, 
                     //a == a
                    .length() //aiu == 3 syllables, a == 1 syllable
                );
        }