Created
March 23, 2013 10:27
-
-
Save programmingthomas/5227229 to your computer and use it in GitHub Desktop.
Estimating syllables
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
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'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks. Helpful little routine.