This program will read in the contents of a text file contai

This program will read in the contents of a text file containing a normal text document and reorganize its contents by separately storing each sentence of the text.

For our purposes, the end of a sentence is marked by any word that ends with one of the three characters . ? !

Read in the lines of text, process them into words, and store them into an array (or, optionally, an ArrayList) of objects of your Sentence class. Each Sentence object should start out empty, and have words added to it as they are received from the file. Your Sentence class must contain at least the following:

text — (String) the text of the sentence;

wordCount — (int) the number of words in the sentence (only count words, where a word contains one or more letters); and

add(String word) — add the given word to the sentence.

If you are storing the sentences in an array, you can assume that the text file contains at most 1000 sentences.

Once you have read in the contents of the file, process it in the following way:

Print the first five sentences in the file. Number the sentences according to their sequence in the document (the first sentence is number 1).

Print the last five sentences in the file. Number the sentences according to their sequence in the document.

Print summary statistics over the entire document, including the number of letters (counting only letters, not digits, spaces, or other punctuation), words, and sentences, and the Automated Readability Index of the text. The ARI is calculated as follows:

ARI=4.71*(letters/words)+0.5*(words/sentences)-21.43,

and provides an estimate of the readability of the text according to its grade level. Round it to one decimal place.

For example, given the following text file:

your output would end like this (the first five sentences not shown):

The input should come from a file named a2b.txt.

a2b.txt:

Solution

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class Sentence {
   public static void main(String[] args) throws IOException {
       BufferedReader br = new BufferedReader(new FileReader(\"a2b.txt\"));
       ArrayList<StringBuilder> al = new ArrayList<StringBuilder>();
       try {
       StringBuilder l = new StringBuilder();
           StringBuilder w = new StringBuilder();
           int sentences=0,words=0,letters=0;
       String line = br.readLine();
           while(line != null){
               for(int i=0;i<line.length();i++){
                   char a = line.charAt(i);
                   if(a == \'.\' || a==\'!\' || a==\'?\'){
                       w.append(a);
                       l.append(w);
                   sentences++;
                       words++;
                       al.add(l);
                       l = new StringBuilder();
                       w = new StringBuilder();
                   }
                   else if(a == \' \'){
                       w.append(a);
                       l.append(w);
                       words++;
                       w = new StringBuilder();
                   }
                   else{
                       //l.append(a);
                       w.append(a);
                       if(Character.isLetter(a))
                           letters++;
                   }                  
               }
               line = br.readLine();
           }
           double ARI=4.71*(letters/words)+0.5*(words/sentences)-21.43;
           for(int i=0;i<5;i++){
               int j = i+1;
               System.out.println(\"(\"+ j +\")\"+al.get(i));
           }
       System.out.println(\"Sentences:\"+sentences+\"\ Words:\"+words+\"\ Letters:\"+letters+\"\ Readability:\"+ARI);
       } finally {
       br.close();
       }
   }
}

/* sample output

(1)Question 2.
(2) Read in the lines of text, process them into words, and store theminto an array (or, optionally,
an ArrayList) of objects of your Sentence class.
(3)Each Sentence object should start out empty, and have words added to it as theyare received from
the file.
(4) Your Sentence class must contain at least thefollowing.
(5) If you are storing the sentences in an array, you can assume that thetext file contains at most
1000 sentences.
Sentences:6
Words:92
Letters:404
Readability:6.9

*/

This program will read in the contents of a text file containing a normal text document and reorganize its contents by separately storing each sentence of the t
This program will read in the contents of a text file containing a normal text document and reorganize its contents by separately storing each sentence of the t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site