Jump to content

I have a project where I have to calculate the flesch readability of a text file. I have everything figured out but the syllables. I need help with how to count them. I know i need methods for counting the vowels in a word, but only counting one vowel if there is two adjacent vowels and dropping the "e" at the end of a word if the word already has 1 syllable or more (cause every word is at least one syllable). Here is my code for the class file. Any help is greatly appreciated. I'm using an array for putting all the vowels in btw (we have to). 

/* * a class to figure the difficulty of a reading. *//** * * @author */public class Word {    private int words = 0;    private int syllables = 0;    private int sentences = 0;    private double index;    public String type;    public char Sens[] = {':', '.', '?', '!', ';'};    public char Syll[] = {'a', 'e', 'i', 'o', 'u', 'y', 'A', 'E', 'I', 'O', 'U', 'Y'};    public char wordss[] = {' '};    public Word(String file){        type = file;    }        public int getWords() {        int words = 1;        for (int a = 0; a < wordss.length; a++)             for (int i = 0; i < type.length(); i++)                 if (type.charAt(i) == wordss[a])                     words++;        return words;    }        public int getSentences() {        int sentences = 0;        for (int a = 0; a < Sens.length; a++)             for (int i = 0; i < type.length(); i++)                 if (type.charAt(i) == Sens[a])                     sentences++;        return sentences;    }        public int getSyllables() {        int syllables = 0;        {            for (int a = 0; a < Syll.length; a++)              for (int i = 0; i < type.length(); i++)                if (type.charAt(i) == Syll[a])                     syllables++;        }           return syllables;    }        public double getIndex() {        index = 206.835 - 84.6 * (getSyllables() / getWords()) - 1.015 * (getWords() / getSentences());        if (index >= 91 && index <= 100)            System.out.println("This example is equivalent to: 5th Grade Reading Level");        else if (index >= 81 && index <= 90)            System.out.println("This example is equivalent to: 6th Grade Reading Level");        else if (index >= 71 && index <= 80)            System.out.println("This example is equivalent to: 7th Grade Reading Level");        else if (index >= 66 && index <= 70)            System.out.println("This example is equivalent to: 8th Grade Reading Level");        else if (index >= 61 && index <= 65)            System.out.println("This example is equivalent to: 9th Grade Reading Level");        else if (index >= 51 && index <= 60)            System.out.println("This example is equivalent to: High School Student Reading Level");        else if (index >= 31 && index <= 50)            System.out.println("This example is equivalent to: College Student Reading Level");        else if (index >= 0 && index < 30)            System.out.println("This example is equivalent to: College Graduate Reading Level");        else if (index < 0)            System.out.println("This example is equivalent to: Law School Graduate Reading Level");        return index;    }}

i7 3930k @ 4.4Ghz | AMD 7970 @ 1150/1823 | Asus P9X79 Pro | 16GB G.Skill 2133mhz | 2x WD Caviar Black 1TB in RAID 0 & 250GB Samsung Evo SSD | Rosewill Blackhawk Ultra with Side Window | XSPC RX360 Watercooling Kit w/ UV Blue Tubing | Corsair Vengeance K70 Keyboard w/ Corsair M65 Mouse

 

Link to comment
https://linustechtips.com/topic/260960-how-to-count-syllables-in-java/
Share on other sites

Link to post
Share on other sites

Well the way I would approach this in thinking is to first get an if statement that just count vowels itself *ie your getsyllables method.

 

The next step is to expand that section, so that it matches your criteria. (ending vowel e does not count and adjacent vowels).  To do this before you increment the count, just add in a few if statements.

 

An example of this would be adjacent

    public int getSyllables() {        int syllables = 0;        {            for (int a = 0; a < Syll.length; a++)              for (int i = 0; i < type.length(); i++)                if (type.charAt(i) == Syll[a]) {                    if(i != 0 && type.charAt(i=1) == *check for syllables*)                       continue; //Okay so not valid syntax but I am rushing, just do another for loop here and don't allow the syllables to increment if it the previous value was a vowel                    syllables++;                }        }           return syllables;    }

0b10111010 10101101 11110000 00001101

Link to post
Share on other sites

Well the way I would approach this in thinking is to first get an if statement that just count vowels itself *ie your getsyllables method.

 

The next step is to expand that section, so that it matches your criteria. (ending vowel e does not count and adjacent vowels).  To do this before you increment the count, just add in a few if statements.

 

An example of this would be adjacent

public int getSyllables() {        int syllables = 0;        {            for (int a = 0; a < Syll.length; a++)              for (int i = 0; i < type.length(); i++)                if (type.charAt(i) == Syll[a]) {                    if(i != 0 && type.charAt(i=1) == *check for syllables*)                       continue; //Okay so not valid syntax but I am rushing, just do another for loop here and don't allow the syllables to increment if it the previous value was a vowel                    syllables++;                }        }           return syllables;    }
So after the if statements that check for e at the end , I will add a for loop that won't allow for adjacent vowels (it'll count them as one vowel). So how would I do that? Thank you!!

i7 3930k @ 4.4Ghz | AMD 7970 @ 1150/1823 | Asus P9X79 Pro | 16GB G.Skill 2133mhz | 2x WD Caviar Black 1TB in RAID 0 & 250GB Samsung Evo SSD | Rosewill Blackhawk Ultra with Side Window | XSPC RX360 Watercooling Kit w/ UV Blue Tubing | Corsair Vengeance K70 Keyboard w/ Corsair M65 Mouse

 

Link to post
Share on other sites

Use booleans to help you keep track. When you find a vowel set a boolean to true. Then on the next check, if it's a vowel, you can use the boolean to know that it's a double vowel. Reset the boolean when a character is a consonant.

 

Endings are easy to check because you can use the endsWith() method.

Link to post
Share on other sites

Use booleans to help you keep track. When you find a vowel set a boolean to true. Then on the next check, if it's a vowel, you can use the boolean to know that it's a double vowel. Reset the boolean when a character is a consonant.

 

Thank you. How would i compare that if I'm holding my vowels in a array though? I havent gotten to learning that yet :(

i7 3930k @ 4.4Ghz | AMD 7970 @ 1150/1823 | Asus P9X79 Pro | 16GB G.Skill 2133mhz | 2x WD Caviar Black 1TB in RAID 0 & 250GB Samsung Evo SSD | Rosewill Blackhawk Ultra with Side Window | XSPC RX360 Watercooling Kit w/ UV Blue Tubing | Corsair Vengeance K70 Keyboard w/ Corsair M65 Mouse

 

Link to post
Share on other sites

Thank you. How would i compare that if I'm holding my vowels in a array though? I havent gotten to learning that yet  :(

 

You wouldn't use your array with endsWith(). Just use it with the endings you want to check.

// Based on #5 here http://www.howmanysyllables.com/howtocountsyllables.htmlif (word.endsWith("le") || word.endsWith("les")) {    syllables++;}else if (word.endswith("e")) {    syllables--;} // etc

However, you'll need to handle things word by word if you want to use this method. type.endsWith() would check the end of the entire block of text.

Link to post
Share on other sites

Use booleans to help you keep track. When you find a vowel set a boolean to true. Then on the next check, if it's a vowel, you can use the boolean to know that it's a double vowel. Reset the boolean when a character is a consonant.

 

Endings are easy to check because you can use the endsWith() method.

Alright I think i got the double vowels sorted out, now with the e as the ending letter, would i do if " if (type.endsWith("e")) " or would that just see if the last word in the whole text ends with an e? 

 

Edit: Ok, how would i do it word by word? and sorry i posted that right before i saw your post about the block text 

i7 3930k @ 4.4Ghz | AMD 7970 @ 1150/1823 | Asus P9X79 Pro | 16GB G.Skill 2133mhz | 2x WD Caviar Black 1TB in RAID 0 & 250GB Samsung Evo SSD | Rosewill Blackhawk Ultra with Side Window | XSPC RX360 Watercooling Kit w/ UV Blue Tubing | Corsair Vengeance K70 Keyboard w/ Corsair M65 Mouse

 

Link to post
Share on other sites

It can depend on how complicated your text is. If you don't have punctuation to deal with, then you should just be able to use type.split(" ") to split the text up by spaces.

 

If you have punctuation but nothing inside words (like O'Reilly or low-budget) then you can remove all the punctuation first, then split the text.

 

Past that, you'll want to get into regular expressions.

Link to post
Share on other sites

It can depend on how complicated your text is. If you don't have punctuation to deal with, then you should just be able to use type.split(" ") to split the text up by spaces.

 

If you have punctuation but nothing inside words (like O'Reilly or low-budget) then you can remove all the punctuation first, then split the text.

 

Past that, you'll want to get into regular expressions.

Okay hmm so what would the code look like if i took out punctuation and then split the text, then did the method to check if it ends with an e or not? 

i7 3930k @ 4.4Ghz | AMD 7970 @ 1150/1823 | Asus P9X79 Pro | 16GB G.Skill 2133mhz | 2x WD Caviar Black 1TB in RAID 0 & 250GB Samsung Evo SSD | Rosewill Blackhawk Ultra with Side Window | XSPC RX360 Watercooling Kit w/ UV Blue Tubing | Corsair Vengeance K70 Keyboard w/ Corsair M65 Mouse

 

Link to post
Share on other sites

Okay hmm so what would the code look like if i took out punctuation and then split the text, then did the method to check if it ends with an e or not? 

 

To remove characters do this

// Replace a character with an empty string to remove ittype.replaceAll(".", "");// To replace multiple characters at once put them between [ ]type.replaceAll("[.,!?]", ""); // covers periods, commas, exclamation marks, and question marks. Add more as needed.
Note: If you need to remove single or double quotes or backslashes, then they need to be escaped.
type.replaceAll("[\'\"\\]", "")
 
 
So inside your method you could so something like
int totalSyllables = 0;String typeWithoutPunc = type.replaceAll("[.,!?]", "")String[] words = typeWithoutPunc.split(" ");for (int i = 0; i < words.length(); i++) {    // Check the syllables in words[i] and add them to totalSyllables    // Personally I'd create a new method to could the syllables in 1 word like so    totalSyllables += CountSyllables(words[i]);}
Link to post
Share on other sites

 

To remove characters do this

// Replace a character with an empty string to remove ittype.replaceAll(".", "");// To replace multiple characters at once put them between [ ]type.replaceAll("[.,!?]", ""); // covers periods, commas, exclamation marks, and question marks. Add more as needed.
Note: If you need to remove single or double quotes or backslashes, then they need to be escaped.
 
 
So inside your method you could so something like
int totalSyllables = 0;String typeWithoutPunc = type.replaceAll("[.,!?]", "")String[] words = typeWithoutPunc.split(" ");for (int i = 0; i < words.length(); i++) {    // Check the syllables in words[i] and add them to totalSyllables    // Personally I'd create a new method to could the syllables in 1 word like so    totalSyllables += CountSyllables(words[i]);}

 

Alright thank you so much!! that seems to work!! Thank you so much again!! 

i7 3930k @ 4.4Ghz | AMD 7970 @ 1150/1823 | Asus P9X79 Pro | 16GB G.Skill 2133mhz | 2x WD Caviar Black 1TB in RAID 0 & 250GB Samsung Evo SSD | Rosewill Blackhawk Ultra with Side Window | XSPC RX360 Watercooling Kit w/ UV Blue Tubing | Corsair Vengeance K70 Keyboard w/ Corsair M65 Mouse

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×