SyllableCounter在一个单词中计算音节

时间:2014-12-04 03:48:30

标签: java

我正在尝试使用一个音节计数器来计算我输入的字符串中的音节。以下是说明。 -

Syllables are really useful for a lot of things. They are defined according to the following rules involving
consonants (non-vowels) and vowels (a, e, i, o, u, y):
    Starting y omitted:
        (a) when words begin with y, we don’t count the starting y as a vowel. (we are
            assuming there are no words that start with y followed immediately by a consonant)
    Silent e omitted:
        (a) when e is the last letter of a word, we’ll assume it is silent, unless the word is café or
            entrée (words borrowed from French). (* we’ll ignore all other words to simplify)
            For simplification, it may be best to create a new String without this silent e, before
            checking for more syllables.
    With the silent-e omitted, one-syllable units:
        (a) have a single vowel.
        (b) have two vowels that are the same letter in succession.
        (c) have two vowels in immediate succession that are ei, ie, ea, ou, ey, ay, oy, uy, ai.
        (d) have three vowels in immediate succession that are eau, iou (* yes, there are
            exceptions to this that we are ignoring here).
    With the silent-e omitted, two-syllable units:
        (a) two vowels in immediate succession where the vowels are different letters not
            following the rule above. For instance, oe, io, oi.
        (b) three vowels in immediate succession not following the rule above where the last
            vowel is not a silent e. For instance (“eye”) as in “meyer.”
Generate a program called SyllableCounter that counts syllables in a word or sentence (maximum one
line). Assume the user will not use any punctuation. Pseudocode and a testing plan are required.
Do not print in main().

Sample output:
Please enter your word or sentence, followed by a carriage return.
Sally where are you going
Your entry has 7 syllables.

这是我当前的代码(它编译,但有一个StringIndexOutOfBoundsException -

/* 
 * SyllableCounter.java
 * Prints the number of syllables based on an inputed string
 * 
 * Testing - What should work - All strings with letter characters
 *      What shouldn't work - Number values
 */

import java.util.Scanner; //import the scanner

public class SyllableCounter //class is SyllableCounter
{

    public static void main (String args[]) //main() method header
    { 

         String string = "";


         string = getInput(); //call getInput()
         int totalCount = calc(string); //call calc()
         printOut(totalCount); //call printOut()

    }

    public static String getInput() //getInput() method
    {
         Scanner console = new Scanner (System.in); //create an instance of the scanner
         System.out.println("Please enter your word or sentence, followed by a carrige return");
         String input = console.nextLine(); //get the inputted string and return it
         return input;
    }

    public static int calc (String string) 
    {
        //int finalCount = 0;
        //int index = string.indexOf(' ');
        return calcWord(string);

    }

    public static int calcWord(String word) //calc() method
    {

         int count = 0;
         //for loop goes through all charectors
         int length = word.length();
         for (int i = 0; i<length; i++)
         {
            if ((word == "entree") || (word == "cafe")) 
                return 2;

            else if (i==0)//if i is 0
            {
                if (word.charAt(i) == 'a' //if letter is a,e,i,o or u
                        || word.charAt(i) == 'e'
                        || word.charAt(i) == 'i'
                        || word.charAt(i) == 'o'
                        || word.charAt(i) == 'u')
                        count++ ; //count ++
                else //else
                    {} //nothing
            }
            else if (i==word.length()-1) //else if i is the last letter of the string
            {
                if ( (word.charAt(i) == 'a') || (word.charAt(i) == 'i') || (word.charAt(i) == 'o') || (word.charAt(i) == 'u') || (word.charAt(i) == 'y') ) 
                //else if letter is a,i,o,u or y (also 2 or 3 in a row)
                {
                    count ++ ;//count ++
                }

                else //else
                    {} //nothing
            }

            else if (word.charAt(word.length()-1) == 'e') {
                if (length >= i+2)
                    if ( word.substring(i,i+3) == "eau"
                        || word.substring(i,i+3) == "iou" )
                    {
                        count++;
                        i+=2;
                    }
                    else if ( word.substring(i,i+2) == "ei"
                            || word.substring(i,i+2) == "ie"
                            || word.substring(i,i+2) == "ea"
                            || word.substring(i,i+2) == "ou"
                            || word.substring(i,i+2) == "ey"
                            || word.substring(i,i+2) == "ay"
                            || word.substring(i,i+2) == "oy"
                            || word.substring(i,i+2) == "uy"
                            || word.substring(i,i+2) == "ai" ) 
                    {
                        count++;
                        i++;
                    }
                    else if( word.substring(i, i+2) == "oe"
                        || word.substring(i, i+2) == "io"
                        || word.substring(i, i+2) == "oi" )
                    {
                        count+=2;
                        i++;
                    }
                }
                else {
                    if (word.charAt(i) =='a'
                            || word.charAt(i) == 'e'
                            || word.charAt(i) == 'i'
                            || word.charAt(i) == 'o'
                            || word.charAt(i) == 'u' )
                    {
                        count++;
                    }
                }


         if  (!(word.charAt(word.length()-1) == 'e'))
        {
                if ( word.substring(i,i+3) == "eau")
                {
                        count++;
                        i+=2;
                }
                else if (word.charAt(i) == 'a'
                        || word.charAt(i) == 'e'
                        || word.charAt(i) == 'i'
                        || word.charAt(i) == 'o'
                        || word.charAt(i) == 'u' )
                {
                    count++;
                } 
            }
            else if (word.charAt(i) == 'a'
                    || word.charAt(i) == 'e'
                    || word.charAt(i) == 'i'
                    || word.charAt(i) == 'o'
                    || word.charAt(i) == 'u' )
            {
                count++;
            }
            else //else
                {} //nothing
        }
        return count;//return the count
    }

    public static void printOut(int count) //printOut() method
    {
        System.out.println(count);
        // print the count
    }
}

2 个答案:

答案 0 :(得分:3)

我使用“foo”这个词作为你程序的输入:

这里(我的第112行):

if  (!(word.charAt(word.length()-1) == 'e')){

    if ( word.substring(i,i+3) == "eau")

上面代码段失败的一个例子是使用foo:

这个词

字长-1的字符不是e,因此将评估上述条件。但是,当i等于1时,i + 3将等于4.因此,4超出了“foo”的长度,导致错误。

请记住,这只是此类故障的一个示例。确保无论何时从i到i + n获得子串,被评估的字符串都会留下i + n个字符。

答案 1 :(得分:0)

手动找到解决方案需要做很多工作。正则表达式可以很好地解决您的问题。请看下面的链接:

How to calculate syllables in text with regex and Java

另外请注意,上述参考文献并未将Y视为音节,您必须稍微调整正则表达式以获得所需的输出。加上元音&#34; e&#34;需要进行一些检查以获得准确的结果。