Programming Exercise 5
Strings and String Processing
This problem is take from Cay Horstmann's book Computing Concepts with Java 2 Essentials, second edition.
The Flesch Readability Index was invented by Rudolf Flesch as a simple tool to gauge the legibility of a document without linguistic analysis. The index is calculated via the following procedure:
Count all the words in the document. A word is any sequence
of characters delimited by white space, whether or not it is an actual
English word. White space is defined as the space, tab ( '\t'), and
new line characters ('\n').
Count all syllables in each word. To make this simple use the following rules:
a. Each group of adjacent vowels counts as one syllable. Vowels consist of upper and lower case a, e, i, o, u, y. For example, the "ea" in "real contributes one syllable, but the "e" and the "a" in "regal" count as two syllables.
b. An "e" at the end of a word is an exception to the previous rule. So "rule" has a single syllable according to these rules.
c. Each word has at least one syllable, even if rules a and
b give it a count of 0.
Count all the sentences. A sentence ends with a period,
colon, semicolon, question mark, or exclamation mark.
The readability index is computed by the equation:
Index = 206.835 - 84.6 * ( Number of syllables / Number of words ) - 1.015 *
( Number of Words / Number of Sentences )
rounded to the nearest integer.
The index is a number, usually between 0 and 100 indicating how difficult the text is to read. Some examples for various publications are:
Comics 95
Consumer ads 82
Sports Illustrated 65
Time 57
New York Times 39
Auto Insurance Policy 10
Internal Revenue Code -6
The purpose of the index is to force authors to rewrite their text until the index is high enough for the target audience. This is achieved by reducing the length of sentences and by removing long words. For example, the sentence:
The following index was invented by Flesh as a simple tool to estimate the legibility of a document without linguistic analysis.
can be rewritten as
Flesch invented an index to check whether a text is easy to read. To compute the index, you need not look at the meaning of the words.
Here is a small example:
"This is a sentence. So is this!"
Number of sentences: 2
Number of words: 7
Number of syllables: 8
Flesch readability index: 107
Here is another example.
"The following index was invented by Flesh as a simple tool to estimate the legibility of a document without linguistic analysis."
Number of sentences: 1
Number of words: 21
Number of syllables: 41
Flesch readability index: 20
Write a program that takes a String from the user and determines the Flesch readability score. To get a string from the user use the ConsoleInput class.
String s = ConsoleInput.readString();
Print out the number of sentences, words, syllables, and Flesch readability index for the String entered by the user.