// This program computes the Flesch Index for documents. The flesch // index is a value that maps to a scale of reading levels. The higher // the value the easier the document should read, and the lower the // grade level of the document. #include #include #include #include // IsVowel // Description: This function returns true if c is a vowel, or it // returns false if c is not a vowel. bool IsVowel(char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; } // IsSyllable // Description: This function determines if the string starting at // position 'pos' is a syllable. A syllable is a group of 1 or more // consecutive vowels in a word. All words have at least one // syllable. The 'newpos' is the position in the string at the end of the // syllable if one is found. 'word' is the word that is being parsed // for syllables. bool IsSyllable(string& word, int pos, int& newpos) { newpos = pos; // if the char at pos is not a vowel, then its not a syllable if ( !IsVowel(tolower(word[pos])) ) return false; // the char is a vowel, and so we have to look for consecutive // vowel. for (;pos < word.length(); pos++) { // if we find a consonant, then set the newpos to the char // before, and return true. if ( !IsVowel(tolower(word[pos])) ) { newpos = pos-1; return true; } } } // ComputeNumOfSylls // Description: Given a word 'theWord' compute the number of syllables // in that word. float ComputeNumOfSylls(string& theWord) { float syllCount=0; int newpos = 0; int curpos = 0; // curpos will move through the string to examine each character // for the start of a syllable. while (curpos < theWord.length()) { // if a syllable is found here, then count it if (IsSyllable(theWord, curpos, newpos)) { syllCount++; curpos = newpos; } curpos++; // move to the next position after a syll or not } // if the word ends in an e then don't count that sequence as a // syllable if ( (theWord[theWord.length()-1] == 'e') && (syllCount > 1) ) syllCount--; // if no sylls were found so far, then every word has at least one if (syllCount == 0) syllCount = 1; return syllCount; } // IsSentence // Description: Returns true is c is a punctuation character that // represents the end of a sentence. bool IsSentence(char c) { return c == '.' || c == '?' || c == '!' || c == ';' || c == ':'; } // ComputeNumOfSents // Description: Scan the parameter 'theWord' and look for sentence // ending punctuation. float ComputeNumOfSents(string theWord) { float sentenceCount=0; // if last char is punctuation char count a sentence if (IsSentence(theWord[theWord.length()-1])) { sentenceCount++; } return sentenceCount; } // ComputeIndex // Description: This function computes the Flesch Index float ComputeIndex(float NumOfWords, float SyllablesCount, float SentenceCount) { if ( (NumOfWords > 0) && (SentenceCount > 0) ) return 206.835 - 84.6 * (SyllablesCount/NumOfWords) - 1.015*(NumOfWords/SentenceCount); else return -1; } // ProcessFile // Description: This function processes a file one word at a time. void ProcessFile(ifstream &input, string Name) { string Aword; float NumOfWords=0; float SyllablesCount=0; float SentenceCount=0; // read one word at a time till end of file and count components while (input >> Aword) { NumOfWords++; SentenceCount += ComputeNumOfSents(Aword); SyllablesCount += ComputeNumOfSylls(Aword); } // outputs the information when done cout << "The # of words " << NumOfWords << endl; cout << "The # of syllables " << SyllablesCount << endl; cout << "The # of sentences " << SentenceCount << endl; cout << "The Flesch index for " << Name << " : " << ComputeIndex(NumOfWords, SyllablesCount, SentenceCount) << endl; } // This is the driver program void main() { string FileName; ifstream InFile; // loop until user inputs the 'quit' command as a file name do { cout << "Please enter the name of a file (enter quit to exit): "; cin >> FileName; InFile.open(FileName.c_str()); // if file opens then process it, otherwise prompt again if (InFile) { ProcessFile(InFile, FileName); InFile.close(); } else { if (FileName != "quit") cerr << "The file, " << FileName << " could not be opened" << endl; } } while (FileName != "quit"); }