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
答案 0 :(得分:1)
试试这个
"(\\b[^aiouyeEAIOUY]+[Ee]\\b)|([aiouyAIOUY]\\b)|([aiouyeAIOUYE]{2,}\\b)|([aiouyeAIOUYE]+(?!\\b))"
以驱逐为目的:
Pattern pat = Pattern.compile("(\\b[^aiouye]+e\\b)|([aiouy]\\b)|([aiouye]{2,}\\b)|([aiouye]+(?!\\b))", Pattern.CASE_INSENSITIVE);
我观察了4个要计算的场景(我将4个部分分组以便更好地调试):
答案 1 :(得分:0)
正确的解决方案
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);
}