计算单词中的音节

时间:2015-12-10 18:29:53

标签: java

我希望创建一个计算单词中音节数的方法。在我的项目中定义的一个音节是一组连续的元音但不是一个元音。在这个词的最后。所以“显而易见”这个词很明显。根据这个定义,只有2个音节和字符串' obstinanceeeeeee'有3个音节。我的尝试是:

protected int countSyllables(String word)
String input = word.toLowerCase();
    int i = input.length() - 1;
    int syllables = 0, numOfE = 0;
    // skip all the e's in the end
    while (i >= 0 && input.charAt(i) == 'e') {
        i--;
        numOfE++;
    }

    // This counts the number of consonants within a word
    int j = 0;
    int consonants = 0;
    while (j < input.length()) {
        if (!isVowel(input.charAt(j))) {
            consonants++;
        }
        j++;
    }

    // This will return syllables = 1 if the string is all consonants except for 1 e at the end.
    if (consonants == input.length() - 1 && numOfE == 1) {
        syllables = 1;
    }

    boolean preVowel = false;
    while (i >= 0) {
        if (isVowel(input.charAt(i))) {
            if (!preVowel) {
               syllables++;
               preVowel = true;
            }
        } else {
            preVowel = false;
        }
        i--;
    }
    return syllables;
}

public boolean isVowel(char ch) {
   if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'y') {
       return true;
   }
   return false;
}

1 个答案:

答案 0 :(得分:1)

  protected int countSyllables(String word) {
    String input = word.toLowerCase();
    int i = input.length() - 1;
    // skip all the e's in the end
    while (i >= 0 && input.charAt(i) == 'e') {
        i--;
    }
    int syllables = 0;
    boolean preVowel = false;
    while (i >= 0) {
        if (isVowel(input.charAt(i))) {
            if (!preVowel) {
               syllables++;
               preVowel = true;
            }
        } else {
            preVowel = false;
        }
        i--;
    }
        return syllables;
    }

    public boolean isVowel(char ch) {
       if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'y') {
           return true;
       }
       return false;
    }

我希望这会有所帮助。您可以从字符串的末尾开始迭代,并在处理/计算音节之前忽略所有的e。 编辑原始答案。如果最后只有一个e,现在将把这个词计为音节

 protected int countSyllables(String word) {
    String input = word.toLowerCase();
    int syllables = 0,numOfEInTheEnd=0;

    int i = input.length() - 1;
    // count all the e's in the end
    while (i >= 0 && input.charAt(i) == 'e') {
        i--;
        numOfEInTheEnd++;
    }

    if (numOfEInTheEnd == 1) {
        syllables = 1;
    }

    boolean preVowel = false;
    while (i >= 0) {
        if (isVowel(input.charAt(i))) {
            if (!preVowel) {
               syllables++;
               preVowel = true;
            }
        } else {
            preVowel = false;
        }
        i--;
    }
    return syllables;
    }

    public boolean isVowel(char ch) {
    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'y') {
        return true;
    }
    return false;
    }

而不是跳过所有尾随的e,而不是现在计算它。如果尾随的e等于1,则音节设置为1.这将立即生效。

相关问题