Pythorian

Calculating Kincaid Readability Index

Aug 20th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1.  class Readability
  2.     {
  3.         public double CalculateReadability(string Passage)
  4.         {
  5.             string[] sentences = Passage.Split(new[] { '.', '!', '?' }, StringSplitOptions.RemoveEmptyEntries);
  6.             double avgSentenceLength = sentences.Average(x => x.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Length);
  7.             double avgSyllablesPerWord;
  8.             int syllables = 0;
  9.             int words = 0;
  10.             foreach (string sentence in sentences)
  11.             {
  12.                 foreach(string word in sentence.Split(' '))
  13.                 {
  14.                     syllables += CountSyllables(word);
  15.                     words++;
  16.                 }
  17.             }
  18.             avgSyllablesPerWord = syllables / words;
  19.             int numberofSentences = sentences.Length;
  20.             return 206.835 - (1.015 * avgSentenceLength) - (84.6 * avgSyllablesPerWord);
  21.         }
  22.  
  23.  
  24.         private int CountSyllables(string word)
  25.         {
  26.             char[] vowels = { 'a', 'e', 'i', 'o', 'u', 'y' };
  27.             string currentWord = word;
  28.             int numVowels = 0;
  29.             bool lastWasVowel = false;
  30.             foreach (char wc in currentWord)
  31.             {
  32.                 bool foundVowel = false;
  33.                 foreach (char v in vowels)
  34.                 {
  35.                     //don't count diphthongs
  36.                     if (v == wc && lastWasVowel)
  37.                     {
  38.                         foundVowel = true;
  39.                         lastWasVowel = true;
  40.                         break;
  41.                     }
  42.                     else if (v == wc && !lastWasVowel)
  43.                     {
  44.                         numVowels++;
  45.                         foundVowel = true;
  46.                         lastWasVowel = true;
  47.                         break;
  48.                     }
  49.                 }
  50.  
  51.                 //if full cycle and no vowel found, set lastWasVowel to false;
  52.                 if (!foundVowel)
  53.                     lastWasVowel = false;
  54.             }
  55.             //remove es, it's _usually? silent
  56.             if (currentWord.Length > 2 &&
  57.                 currentWord.Substring(currentWord.Length - 2) == "es")
  58.                 numVowels--;
  59.             // remove silent e
  60.             else if (currentWord.Length > 1 &&
  61.                 currentWord.Substring(currentWord.Length - 1) == "e")
  62.                 numVowels--;
  63.  
  64.             return numVowels;
  65.         }
  66.     }
Advertisement
Add Comment
Please, Sign In to add comment