【问题标题】:Java - count syllables in a large stringJava - 计算大字符串中的音节
【发布时间】:2015-12-11 15:22:53
【问题描述】:

所以 - 我有两个代码可以用来在 java 中编写一个计算字符串中音节数的程序。代码是:

  1. countSyllables(String word) 统计单个单词的音节数

  2. getTokens(String pattern) 将字符串中的单词分成字符串列表

现在,我正在尝试使用这两个来计算字符串的音节数。爆炸是我的代码在最后两行有错误。您能否确定我获取音节数量的逻辑是否正确?以及如何改进我的代码以获得我想要的结果?

protected int countSyllables1(String doc) 
 {

     //lower all the letters in the string

     String inputString = doc.toLowerCase(); 



     // put all the words of the string into a list of strings     

     List<String> WordTokens = getTokens("[a-zA-Z]+");


      //convert the ArrayList to an Array

      String[] WordTokensArr = new String[WordTokens.size()];
      WordTokensArr = WordTokens.toArray(WordTokensArr);

     //Iterate the array
    for(String s: WordTokensArr) 
       {

        //count the syllables

         int SyllabCount = WordTokenArr.countSyllables(doc);
        }

     return SyllabCount;

  }     

这是我正在使用的帮助代码:

protected int countSyllables(String word) {
        String input = word.toLowerCase();
        int i = input.length() - 1;
        int syllables = 0;
        // skip all the e's in the end
        while (i >= 0 && input.charAt(i) == 'e') {
            i--;
            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;
        }





protected List<String> getTokens(String pattern)
{
    ArrayList<String> tokens = new ArrayList<String>();
    Pattern tokSplitter = Pattern.compile(pattern);
    Matcher m = tokSplitter.matcher(text);

    while (m.find()) {
        tokens.add(m.group());
    }

    return tokens;
}

更新

CountSyllables1 方法已修改并且现在可以工作。它不能正确检测音节,但肯定会给出结果。

所以这就是我所做的更改: 1. 我将代码转移到另一个类,该类继承自包含 CountSyllables 和 getTokens 的类。我将方法的名称更改为 getNumSyllables()。 2. 该方法没有参数(String doc),我还删除了声明输入字符串的第一行和 toLowercase 方法,因为该方法已在 CountSyllables 类中使用。 3.迭代循环被修改,使得变量“result”被声明以帮助计数并返回音节的数量。

public int getNumSyllables() 
{

     // put all the words of the string into a list of strings     

     List<String> WordTokens = getTokens("[a-zA-Z]+");


      //convert the ArrayList to an Array

      String[] WordTokensArr = new String[WordTokens.size()];
      WordTokensArr = WordTokens.toArray(WordTokensArr);

    //Iterate the array
      int result = 0;
      for(String s: WordTokensArr) 
      {

          //count the syllables and add to the result sum
          result += countSyllables(s);
      }

      return result;

  }

【问题讨论】:

  • “最后两行的错误” - 这些错误是什么?
  • public boolean isVowel 之前缺少一个大括号,用于结束 countSyllables 方法
  • 另外,上次我检查时,“y”不是元音
  • aeiou 有时是 y
  • Laleh H 请解释一下这段代码WordTokenArr.countSyllables(doc); 变量WordTokenArr 的类型是什么?以及哪个类包含方法countSyllables

标签: java string arraylist count


【解决方案1】:

最后,您遍历单词以计算音节:

for(String s: WordTokensArr) {
    int SyllabCount = WordTokenArr.countSyllables(doc);
}
return SyllabCount;

变量SyllabCount 不能在for 循环之外访问。即使您在循环之前声明它,您也会覆盖每个单词的值。相反,添加每个单词的音节数:

int SyllabCount = 0; // Start with 0.
for(String s: WordTokensArr) {
    // Add the amount from this word
    SyllabCount = SyllabCount + WordTokenArr.countSyllables(s);
}
return SyllabCount;

【讨论】:

    【解决方案2】:

    我看到的一些东西:

    您根本不需要转换数组列表。您可以遍历每个列表!

       //convert the ArrayList to an Array
    
      String[] WordTokensArr = new String[WordTokens.size()];
      WordTokensArr = WordTokens.toArray(WordTokensArr);
    

    变量通常以小写开头

    int SyllabCount

    "WordTokenArr" 是一个糟糕的类名,因为它表明这实际上是一个 WordToken 数组而不是一个类。

    希望这对您未来有所帮助:)

    【讨论】:

    • 其实是一个数组!但正如你所说,我不应该首先将 ArrayList 转换为 Array 。我到了.... ;) 谢谢!!
    【解决方案3】:

    countSyllables 方法在哪里?它似乎在当前类中(如果我错了,请纠正我),但是您将其称为String[] 的方法,这是不对的。此外,您需要将每个单词的音节数相加并返回它们的总和。

    尝试用这个替换你的循环:

    //Iterate the array
    int result = 0;
    for(String s: WordTokensArr) 
    {
    
        //count the syllables and add to the result sum
        result += countSyllables(s);
    }
    
    return result;
    

    【讨论】:

    • OP 没有调用countSyllables 作为String[] 的方法。如果仔细看,有WordTokensArr,这是一个String[],还有WordTokenArr.countSyllables,这是一个方法。
    • OP应该告诉我们WordTokenArr的定义是什么
    • 我也怀疑countSyllables方法在同一个类,但她需要指定
    • 噢!谢谢,我的代码开始工作并使用您对最后一点所做的更改给我一些输出。我还将整个 CountSyllables 1 移到另一个班级。我现在可以看到在找到正确音节时的一些错误。听起来现在应该操作 CountSyllables 方法。
    • @LalehH 我很乐意提供帮助。如果它为您的问题提供了解决方案,请接受它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 2020-02-19
    • 2011-09-05
    • 2022-08-13
    相关资源
    最近更新 更多