Results 1 to 2 of 2
Thread: Help with Flesch Index
- 03-31-2008, 04:00 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 1
- Rep Power
- 0
Help with Flesch Index
Hi,
I need to calculate the Flesch Index of a given extract.
To do this I have done a Flesch Index class as below but it does not count my words,sentences and syllables.Please can anyone assist in what I am doing incorrect.
import javax.swing.*;
import java.util.StringTokenizer;
class FleschIndex
{
String extract;
public void setExtract(String ext)
{
extract=ext;
}
public String getExtract()
{
return extract;
}
public int readabilityIndex(String extract)
{
String delimiters = ".,':;?{}[]=-+_!@#$%^&*() ";
StringTokenizer token = new StringTokenizer(extract,delimiters);
String word;
int numberOfSyllables = 0;
int numberOfSentences = 0;
int numberOfWords = 0;
int CalculatedIndex;
//count the words
String wordDelimiter=" ";
StringTokenizer wordTokenizer=new StringTokenizer(extract,wordDelimiter);
numberOfWords=wordTokenizer.countTokens();
//count the sentences
String sentenceDelimiters=".:;?!";
StringTokenizer sentenceTokenizer = new StringTokenizer(extract,sentenceDelimiters);
numberOfSentences= sentenceTokenizer.countTokens();
//count syllables
while (token.hasMoreTokens())
{
word = token.nextToken();
for(int x=0; x<word.length();x++)
{
char letter=word.charAt(x);//first loop checks first letter,second loop checks second letter....
if((letter=='a')||
(letter=='e')||
(letter=='i')||
(letter=='o')||
(letter=='u')||
(letter=='y'))
{
numberOfSyllables++;
}
char endLetter;
endLetter=word.charAt(word.indexOf(" ")-1);
if ((endLetter == 'e') && (numberOfSyllables != 1))
{
numberOfSyllables--;
}
}
}
CalculatedIndex=(int)Math.round(206.835-((84.6)*((numberOfSyllables))/(numberOfWords))-((1.015)*((numberOfWords)/(numberOfSentences))));
return CalculatedIndex;
}
public String educationLevel(int CalculatedIndex)
{
String educationLevel="";
if (CalculatedIndex<0)
{educationLevel="Law School Graduate";}
else if (CalculatedIndex>=0&&CalculatedIndex<=30)
{educationLevel="College Graduate";}
else if ((CalculatedIndex>30)&&(CalculatedIndex<=50))
{educationLevel="College Student";}
else if (CalculatedIndex>50&&CalculatedIndex<=60)
{educationLevel="High School Student";}
else if (CalculatedIndex>60&&CalculatedIndex<=66)
{educationLevel="9th Grader";}
else if (CalculatedIndex>66&&CalculatedIndex<=70)
{educationLevel="8th Grader";}
else if (CalculatedIndex>70&&CalculatedIndex<=80)
{educationLevel="7th Grader";}
else if (CalculatedIndex>80&&CalculatedIndex<=90)
{educationLevel="6th Grader";}
else if ((CalculatedIndex>90)&&(CalculatedIndex<=100))
{educationLevel="5th Grader";}
return educationLevel;
}
}
This is my Run class:
class Run
{
public static void main (String [] args)
{
FleschIndex FI=new FleschIndex ();
FI.setExtract("The string tokenizer class allows an application to break a string into tokens.Hello.");
System.out.println("The extract is:"+FI.getExtract());
int CalculatedIndex=FI.readabilityIndex(FI.getExtract( ));
System.out.println("The Flesch Index is:"+FI.readabilityIndex(FI.getExtract()));
System.out.println("The education level is:" +FI.educationLevel(CalculatedIndex));
}
}
- 03-31-2008, 05:30 PM #2
C:\jexp>java FleschIndex
The extract is:The string tokenizer class allows an application to break a string into tok
ens. Hello.
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of
range: -2
at java.lang.String.charAt(Unknown Source)
at FleschIndex.readabilityIndex(FleschIndex.java:59)
at FleschIndex.main(FleschIndex.java:114)Java Code:char endLetter; // If word.indexOf(" ") = -1 // then word.indexOf(" ")-1 will evaluate to -2. //endLetter=word.charAt(word.indexOf(" ")-1); // Try: endLetter = word.charAt(word.length()-1);
Similar Threads
-
Replacing at an index
By bugger in forum New To JavaReplies: 2Last Post: 01-29-2008, 06:33 AM -
how to load a servlet on index.jsp page...
By sujithc34 in forum Java ServletReplies: 1Last Post: 01-19-2008, 12:28 AM -
z-Index problem
By mjdousti in forum AWT / SwingReplies: 1Last Post: 12-29-2007, 01:34 AM -
problems with array index
By mary in forum New To JavaReplies: 2Last Post: 08-01-2007, 04:30 PM -
Accessing index.dat files
By vissu007 in forum NetworkingReplies: 1Last Post: 07-01-2007, 04:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks