Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Readability
- {
- public double CalculateReadability(string Passage)
- {
- string[] sentences = Passage.Split(new[] { '.', '!', '?' }, StringSplitOptions.RemoveEmptyEntries);
- double avgSentenceLength = sentences.Average(x => x.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Length);
- double avgSyllablesPerWord;
- int syllables = 0;
- int words = 0;
- foreach (string sentence in sentences)
- {
- foreach(string word in sentence.Split(' '))
- {
- syllables += CountSyllables(word);
- words++;
- }
- }
- avgSyllablesPerWord = syllables / words;
- int numberofSentences = sentences.Length;
- return 206.835 - (1.015 * avgSentenceLength) - (84.6 * avgSyllablesPerWord);
- }
- private int CountSyllables(string word)
- {
- char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'y' };
- string currentWord = word;
- int numVowels = 0;
- bool lastWasVowel = false;
- foreach (char wc in currentWord)
- {
- bool foundVowel = false;
- foreach (char v in vowels)
- {
- //don't count diphthongs
- if (v == wc && lastWasVowel)
- {
- foundVowel = true;
- lastWasVowel = true;
- break;
- }
- else if (v == wc && !lastWasVowel)
- {
- numVowels++;
- foundVowel = true;
- lastWasVowel = true;
- break;
- }
- }
- //if full cycle and no vowel found, set lastWasVowel to false;
- if (!foundVowel)
- lastWasVowel = false;
- }
- //remove es, it's _usually? silent
- if (currentWord.Length > 2 &&
- currentWord.Substring(currentWord.Length - 2) == "es")
- numVowels--;
- // remove silent e
- else if (currentWord.Length > 1 &&
- currentWord.Substring(currentWord.Length - 1) == "e")
- numVowels--;
- return numVowels;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment