1

Need to count number of syllables in given text. Every contiguous sequence of one or more vowels, except for a lone “e” at the end of a word if the word has another vowel or set of contiguous vowels, makes up one syllable(Consider "y" as vowel)

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int count =0;
    String text    = "This is a test.  How many???  Senteeeeeeeeeences are here... there should be 5!  Right?";
    Pattern pat = Pattern.compile("[Ee]+(?!\\b)|[aiouyAIOUY]+");
    Matcher m = pat.matcher(text);
    while (m.find()) {
            count++;
            System.out.println(m.group());
    }
    System.out.println(count);
}

Output of above program is 15 It needs to be 16 It should to eliminate count of e's when it is last character in a word not containing any vowel i.e.., It should not eliminate count of e's in word(be) How to specify that condition in Pattern

4
  • If I run your code the output is 15. You expect to have 15. So what's the problem? Commented Nov 12, 2015 at 12:23
  • I think the expected result should be 16. Commented Nov 12, 2015 at 12:24
  • @Fredo Than it make more sense. Commented Nov 12, 2015 at 12:29
  • 1
    Not directly related but the regex could be simplified by using the case insensitive flag for Pattern: Pattern.compile("[e]+(?!\\b)|[aiouy]+", Pattern.CASE_INSENSITIVE) Commented Nov 12, 2015 at 12:31

2 Answers 2

1

try this

"(\\b[^aiouyeEAIOUY]+[Ee]\\b)|([aiouyAIOUY]\\b)|([aiouyeAIOUYE]{2,}\\b)|([aiouyeAIOUYE]+(?!\\b))"

Adn with the purpose of driu:

Pattern pat = Pattern.compile("(\\b[^aiouye]+e\\b)|([aiouy]\\b)|([aiouye]{2,}\\b)|([aiouye]+(?!\\b))", Pattern.CASE_INSENSITIVE);

I observe 4 scenarios to count (I group the 4 parts to better debug):

  1. An e is at end and there are no other vowel in the word
  2. One vowel (except e) is at end of the word
  3. Two or more vowels (including e) are at end of the word
  4. One or more vowels (including e) are in the word but not at the end
Sign up to request clarification or add additional context in comments.

2 Comments

Works fine but problem is for words like serIEs it should count IE as single @fredo
can u explain your solution
0

Correct Solution

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int count =0;
    //String text    = "Here is a series of test sentences. Your program should find 3 sentences, 33 words, and 49 syllables. Not every word will have the correct amount of syllables (example, for example), but most of them will.";
    String text = "series";
    Pattern pat = Pattern.compile("e(?!$)[aeiouy]*|[aieyou]*e(?!$)|[ayiou]+|\\b[^aiouye]+[e]\\b",Pattern.CASE_INSENSITIVE);
    Matcher m = pat.matcher(text);
    while (m.find()) {
            count++;
            System.out.println(m.group());
    }
    System.out.println(count);
}

2 Comments

Are you sure? I tried with this string: heitahore Suries e aei baoeifife bce Seraie Seirio I think that's not ok for heitahore.
Nope... take this word: haeitahore. The first part will find e at end even the word contains vowels

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.