【问题标题】:Why is this counting the number of words incorrectly?为什么这会错误地计算字数?
【发布时间】:2021-11-26 02:24:46
【问题描述】:

公共静态空词(字符串文本){ int numWords = 1;

    for (int charNum = 0; charNum < text.length(); charNum++) {
        char letter = text.charAt(charNum);
        
        if(charNum + 1 < text.length()) {
            char nextLetter = text.charAt(charNum + 1);
            if(letter == '.' || letter == ':' || letter == ';' || letter == '?' || letter == '!'|| letter == ' ' || letter == '\n' || letter == '\t') {
                if(nextLetter != '.' || nextLetter != ':' || nextLetter != ';' || nextLetter != '?' || nextLetter != '!'|| nextLetter != ' ' || nextLetter != '\n' || nextLetter != '\t')
                    numWords += 1;
            }   
        }
    }
    
    System.out.println("Number of words: " + numWords);
}

字符串“是 this_one_long_word 还是几个???你怎么看??太多”应该打印 10 个单词和“!这使用句点.as.word.delimiters 并且可能很棘手。”应该打印 10 个字。

这是描述:一个单词是一个或多个字符的序列,由空格或句子终止符(句点、冒号、分号、问号、感叹号)分隔,无论它是否是实际的英文单词。空白定义为空格、制表符 ('\t')、换行符 ('\n') 和字符串本身的结尾。这再次给出了一些可能没有意义的结果。例如文本“I_don't_like_to_use_SPACES-EVER!”有一个词:I_don't_like_to_use_SPACES-EVER。

【问题讨论】:

    标签: java counting


    【解决方案1】:

    以下行总是正确的:

    if(nextLetter != '.' || nextLetter != ':' || nextLetter != ';' || nextLetter != '?' || nextLetter != '!'|| nextLetter != ' ' || nextLetter != '\n' || nextLetter != '\t')
    

    改成:

    if(nextLetter != '.' && nextLetter != ':' && nextLetter != ';' && nextLetter != '?' && nextLetter != '!'&& nextLetter != ' ' && nextLetter != '\n' && nextLetter != '\t')
    

    int numwords = 1; 开始计数会导致计数不正确。从 0 开始。

    您的第二个输入以 ! 开头,它在计数中添加了一个单词。
    您必须检查终止符后的字符是否为字母以避免这种行为:

     public static void words(String text) {
            int numWords = 0;
            final String wordTerminators = ".:;?! \n\t";
            for (int charNum = 0; charNum < text.length() - 1; charNum++) {
                char letter = text.charAt(charNum);
               
                char nextLetter = text.charAt(charNum + 1);
                if (wordTerminators.contains(letter + "")) {
                    if (!wordTerminators.contains(nextLetter + "") && !(nextLetter+"").matches("[A-Za-z]"))
                        numWords += 1;
                }
    
            }
            System.out.println("Number of words: " + numWords);
        }
    

    这是我的解决方案的更简洁版本:

        public static void words(String text) {
            int numWords = 0;
            
            String[] splitted = text.split("[.:;?! \\n\\t]+");
            for (String word : splitted) {
                if (word.length() > 0)
                    numWords++;
            }
            System.out.println("Number of words: " + numWords);
        }
    

    两种方法的输出都是:

    Number of words: 10
    Number of words: 10
    

    【讨论】:

      【解决方案2】:

      您在最后一个 if (最嵌套的一个)中有无效条件 - 您需要确保下一个字符(在您的代码中称为 nextLetter)不是您检查的字符,但您使用 OR 而不是 AND 条件所以它总是正确的(因为字符不能同时是两个不同的字符)。

      所以你可以把这个if改成:

      if(nextLetter != '.' && nextLetter != ':' && nextLetter != ';' && nextLetter != '?' && nextLetter != '!'&& nextLetter != ' ' && nextLetter != '\n' && nextLetter != '\t')
      

      它会正确计算单词。确保您也计算第一个单词,因为目前如果您以 numWords 设置为零开始,它将为您的示例打印 9 而不是 10 - 因为不计算第一个单词。所以你也应该数一下。

      顺便说一句。您的代码可以通过多种方式显着简化,您还可以使用其他方法,例如 split 函数(来自 String)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-05
        • 1970-01-01
        • 1970-01-01
        • 2022-11-15
        • 2014-11-02
        相关资源
        最近更新 更多