0

我正在制作一个程序来计算给定单词/句子中的音节数。有没有可以准确计算音节的工具?我在这里有我的代码,但我认为我需要一种更准确的计算音节的方法。

import java.util.*;

public class Word 
{
    private String word;
    private int syllableCount;

    public void setWord(String word)
    {
        this.word = word;
    }
    public String getWord()
    {
        return word;
    }
    public int getSyllableCount()
    {
        Scanner sc = new Scanner(word);
                sc.useDelimiter("[aeiouy]+");

        while(sc.hasNext())
            {
                syllableCount++;
                sc.next();
            }

        if (!word.endsWith("a") && !word.endsWith("e") && !word.endsWith("i")  && !word.endsWith("o") && 
            !word.endsWith("u") && !word.endsWith("y"))
            syllableCount = syllableCount - 1;

        return syllableCount;
    }

    public static void main (String [] args)
    {
        int syllables;

        Word newWord = new Word();
        newWord.setWord("The quick brown fox jumps over the lazy dog.");

        syllables = newWord.getSyllableCount();
        System.out.println(syllables + " " + "syllables");
    }
}
4

0 回答 0