繁体   English   中英

如何从我的代码java中的文件中计算每个段落中的单词数?

[英]how can I count number of words from each paragraph from the file in my code java?

你能帮我在这段代码中添加一个额外的检查来帮助我找到每个段落中的单词数吗?

在此处输入代码

String path = "C:/CT_AQA - Copy/src/main/resources/file.txt";

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));

String line = " ";
int countWord = 0;
int sentenceCount = 0;
int characterCount = 0;
int paragraphCount = 1;
int countNotLetter = 0;
int letterCount = 0;
int wordInParagraph = 0;

while ((line = br.readLine()) != null) {
    if (line.equals("")) {
       paragraphCount++;
    } else {
        characterCount += line.length();
        String[] wordList = line.split("\\s+");
        countWord += wordList.length;
        String[] sentenceList = line.split("[!?.:]+");
        sentenceCount += sentenceList.length;
        String[] letterList = line.split("[^a-zA-Z]+");
        countNotLetter += letterList.length;
    }

    letterCount = characterCount - countNotLetter;
}
br.close();
System.out.println("The amount of words are " + countWord);
System.out.println("The amount of sentences are " + sentenceCount);
System.out.println("The amount of paragraphs are " + paragraphCount);
System.out.println("The amount of letters are " + letterCount);

爪哇

段落中的单词wordCount与所有行中单词wordCount的总数相同。

如果每个段的字的数量必须被计数,则wordsInParagraph应该是整数列表List<Integer> wordsPerParagraph其可以被计算如下:

int wordInParagraph = 0;
List<Integer> wordsPerParagraph = new ArrayList<>();

while ((line = br.readLine()) != null) {
    if (line.equals("")) {
       paragraphCount++;
       wordsPerParagraph.add(wordInParagraph);
       wordInParagraph = 0;
    } else {
        characterCount += line.length();
        String[] wordList = line.split("\\s+");
        countWord += wordList.length;
        wordInParagraph += wordList.length; // !!!

        String[] sentenceList = line.split("[!?.:]+");
        sentenceCount += sentenceList.length;
        String[] letterList = line.split("[^a-zA-Z]+");
        countNotLetter += letterList.length;
    }

    letterCount = characterCount - countNotLetter;
}
// in case the last paragraph does not have trailing empty line
if (wordInParagraph != 0) {
   wordsPerParagraph.add(wordInParagraph);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM