java - 如何在我的代码 java 中计算文件中每个段落的单词数?
问题描述
你能帮我在这段代码中添加一个额外的检查,以帮助我找到每个段落的单词数吗?
在此处输入代码
字符串路径 = "C:/CT_AQA - 复制/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
与所有行的总字数相同。
如果必须计算每段的单词数,那么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);
}
推荐阅读
- vb.net - 如何在 VB.NET 中使对象跟随鼠标?
- java - 如果其他条件无法正常工作
- python - python循环列表索引超出范围
- linux-kernel - 如何在两个模块之间导出变量?
- oracle - 重新创建大表的主键的最佳方法是什么?
- nginx - nginx 用 proxy_pass 重写返回 404
- python - Python检测变量首先实例化的模块
- python - 部署模型时在 azure ml 入口脚本中导入数据和 python 脚本
- css - 为什么圆锥梯度的结果随着远离中心变得越来越模糊?
- mongodb - 在 ReactiveMongo 0.18.8 中,如何在单个命令中更新多个具有不同值的文档?