View Single Post
  #1 (permalink)  
Old 03-31-2008, 05:00 PM
L_22 L_22 is offline
Member
 
Join Date: Mar 2008
Posts: 1
L_22 is on a distinguished road
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));


}
}
Reply With Quote
Sponsored Links