如何使用Java计算单词的音节数?

huangapple go评论385阅读模式
英文:

How to count syllables in word using Java?

问题

以下是翻译好的内容:

我正在开发一个学生项目,需要编写一个函数来计算单词中的音节。函数的形式类似于 long countSyllables(String word)

如何使用Java计算单词中的音节?有什么建议吗?

提供的规则如下:

  1. 要计算音节数,应该使用字母 a、e、i、o、u、y 作为元音字母。

  2. 统计单词中元音字母的数量。

  3. 不要计算双元音字母(例如,“rain” 有2个元音字母,但只有1个音节)。

  4. 如果单词的最后一个字母是 'e',不要将其视为元音字母(例如,“side” 是1个音节)。

  5. 如果最终发现单词中不包含元音字母,则将此单词视为1个音节。

我已经编写了这个函数,但我认为它不是最优的。所以我想看到其他可能的解决方案。如果有的话。

任务的完整描述:https://hyperskill.org/projects/39/stages/208/implement

当前的实现:

public static int countSyllables(final String word) {
    return max(1, word.toLowerCase()
            .replaceAll("e$", "")
            .replaceAll("[aeiouy]{2}", "a")
            .replaceAll("[^aeiouy]", "")
            .length());
}
英文:

I am developing a student project and need to write a function to count syllables in word. The function is like long countSyllables(String word).

How to count syllables in word using Java? Any suggestions?

The provided rules are:

  1. To count the number of syllables you should use letters a, e, i, o, u, y as vowels.

  2. Count the number of vowels in the word.

  3. Do not count double-vowels (for example, "rain" has 2 vowels but is only 1 syllable)

  4. If the last letter in the word is 'e' do not count it as a vowel (for example, "side" is 1 syllable) 

  5. If at the end it turns out that the word contains 0 vowels, then consider this word as 1-syllable.

I had already write the function but I think it is not optimal. So I just would like to see other possible solutions. If any.

Full description for the task: https://hyperskill.org/projects/39/stages/208/implement

Current implementation:

public static int countSyllables(final String word) {
    return max(1, word.toLowerCase()
            .replaceAll("e$", "")
            .replaceAll("[aeiouy]{2}", "a")
            .replaceAll("[^aeiouy]", "")
            .length());
}

答案1

得分: 1

public static int countSyllables(final String word) {
    return max(1, word.toLowerCase()
                //在以“e”结尾的单词中,将“e”替换为空字符串
                .replaceAll("e$", "") //例如 base=bas
                //当连续两个元音字母在一起时,
                //用字母“a”替换它们
                .replaceAll("[aeiouy]{2}", "a") //例如 you == au, 
                //beautiful==bautiful
                //同样,当连续两个元音字母在一起时,
                //用字母“a”替换它们
                .replaceAll("[aeiouy]{2}", "a") //例如 au == a,
                //bautiful==batiful
                //将不是aeiouy的任何字符替换为空字符串
                .replaceAll("[^aeiouy]", "") //例如 batiful==aiu, 
                 //a == a
                .length() //aiu == 3 音节, a == 1 音节
            );
}
英文:
    public static int countSyllables(final String word) {
        return max(1, word.toLowerCase()
                    //in words that end with "e" replace 
                    //"e" with ""
                    .replaceAll("e$", "") //e.g base=bas
                    //when two vowels appear together, 
                    //replace them with "a" 
                    .replaceAll("[aeiouy]{2}", "a") //e.g you == au, 
                    //beautiful==bautiful
                    //again, when two vowels appear together, 
                    //replace them with "a" 
                    .replaceAll("[aeiouy]{2}", "a") //e.g au == a,
                    //bautiful==batiful
                    //replace any character that isn't aeiouy with ""
                    .replaceAll("[^aeiouy]", "") //e.g, batiful==aiu, 
                     //a == a
                    .length() //aiu == 3 syllables, a == 1 syllable
                );
        }

huangapple
  • 本文由 发表于 2020年5月5日 19:53:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61612508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定