Skip to content

Instantly share code, notes, and snippets.

@programmingthomas
Created March 23, 2013 10:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save programmingthomas/5227229 to your computer and use it in GitHub Desktop.
Save programmingthomas/5227229 to your computer and use it in GitHub Desktop.
Estimating syllables
public int numberOfSyllables (String word)
{
int syllabeleCount = 0;
bool lastWasVowel = false;
string lowerCase = word.ToLower ().Replace ("ome", "um").Replace ("ime", "im").Replace ("imn", "imen").Replace ("ine", "in").Replace ("ely", "ly").Replace("ure", "ur").Replace("ery", "ry");
for (int n = 0; n < lowerCase.Length; n++) {
if (isVowel (lowerCase [n])) {
if (!lastWasVowel)
syllabeleCount++;
lastWasVowel = true;
} else {
lastWasVowel = false;
}
}
if (lowerCase.EndsWith ("ing") || lowerCase.EndsWith ("ings")) {
if (lowerCase.Length > 4 && isVowel (lowerCase [lowerCase.Length - 4]))
syllabeleCount++;
}
if (lowerCase.EndsWith ("e") && !lowerCase.EndsWith("le")) {
syllabeleCount--;
}
if (lowerCase.EndsWith ("es")) {
if (lowerCase.Length > 4 && isVowel(lowerCase[lowerCase.Length - 4]))
syllabeleCount--;
}
if (lowerCase.EndsWith ("e's")) {
if (lowerCase.Length > 5 && isVowel(lowerCase[lowerCase.Length - 5]))
syllabeleCount--;
}
if (lowerCase.EndsWith ("ed") && !lowerCase.EndsWith ("ted") && !lowerCase.EndsWith ("ded")) {
syllabeleCount--;
}
return syllabeleCount > 0 ? syllabeleCount : 1;
}
public bool isVowel (char c)
{
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y';
}
@RHulshizer
Copy link

Thanks. Helpful little routine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment