Created
October 1, 2017 18:46
-
-
Save bruno78/c3c8c69e01f2ad724a776e2c971f5455 to your computer and use it in GitHub Desktop.
How to separate and count syllables in a word.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Regex function that return a list of items split by the pattern. | |
public List<String> getTokens(String pattern) | |
{ | |
ArrayList<String> tokens = new ArrayList<String>(); | |
Pattern tokSplitter = Pattern.compile(pattern); | |
Matcher m = tokSplitter.matcher(text); | |
while (m.find()) { | |
tokens.add(m.group()); | |
} | |
return tokens; | |
} | |
// Helper function to count syllables | |
public int countSyllables(String word) | |
{ | |
int numSyllables = 0; | |
boolean newSyllable = true; | |
String vowels = "aeiouy"; | |
char[] cArray = word.toCharArray(); | |
for (int i = 0; i < cArray.length; i++) { | |
// dealing with lone 'e's and 'e's in the end of the word. | |
if (i == cArray.length-1 && Character.toLowerCase(cArray[i]) == 'e' && | |
newSyllable && numSyllables > 0) { | |
numSyllables--; | |
} | |
// if the syllable's character is a vowel. Then it stops and count as a syllable. | |
if (newSyllable && vowels.indexOf(Character.toLowerCase(cArray[i])) >= 0) { | |
newSyllable = false; | |
numSyllables++; | |
} | |
// if the current character is NOT a vowel, starts the new syllable. | |
else if (vowels.indexOf(Character.toLowerCase(cArray[i])) < 0) { | |
newSyllable = true; | |
} | |
} | |
return numSyllables; | |
} | |
public int getNumSyllables() | |
{ | |
// get a list of words. | |
List<String> tokens = getTokens("[a-zA-Z]+"); | |
int totalSyllables = 0; | |
for (String word : tokens) { | |
totalSyllables += countSyllables(word); | |
} | |
return totalSyllables; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment