How I can count accurately the number of sentences from the file?
I have a text in my file. There are 7 sentences but my code shows that there are 9 sentences.
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 = 0;
int countNotLetter = 0;
int letterCount = 0;
int wordInParagraph = 0;
List<Integer> wordsPerParagraph = new ArrayList<>();
while ((line = br.readLine()) != null) {
if (line.equals("")) {
paragraphCount++;
wordsPerParagraph.add(wordInParagraph);
System.out.printf("In %d paragraph there are %d words\n", paragraphCount, wordInParagraph);
wordInParagraph = 0;
} else {
characterCount += line.length();
String[] wordList = line.split("[\\s—]");
countWord += wordList.length;
wordInParagraph += wordList.length;
String[] letterList = line.split("[^a-zA-Z]");
countNotLetter += letterList.length;
String[] sentenceList = line.split("[.:]");
sentenceCount += sentenceList.length;
}
letterCount = characterCount - countNotLetter;
}
if (wordInParagraph != 0) {
wordsPerParagraph.add(wordInParagraph);
}
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);