In the first section of this assignment, you will write methods to perform basic text analysis on a document.

  • Count the number of words in the text. You can assume that words are separated by a space.
  • Count the number of sentences in the text. You can assume that a new sentence begins following a punctuation mark. Note: you only need to consider the following punctuation: "." "?" "!"
  • Find the top 10 words in the text and return the number of times they occur. This information should be outputted to a text file in an easy-to-read format. Note: The top 10 words refers to the 10 most frequently used words in the text file.
  • A method that accepts a string as a parameter and returns True if the word is found in the text document and False otherwise.

Here is a text file with a portion of President Barack Obama's acceptance speech. You can test your programs using this text file. PoliticalSpeech.txt

Here is the code that you can use to read in a text file from your computer. You will need to add the path in the appropriate place to identify where the text file is saved on your own computer.

You may not use any of the built-in Java methods or libraries that many entirely solve any of the above problems. You can use built-in methods such as "length()" for finding the length of a string.

TextFileReader:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Test {
public static void main(String [] args) {

String fileName = "*****************INSERT PATH HERE********************";

String line = null;

try {
FileReader fileReader =
new FileReader(fileName);

BufferedReader bufferedReader =
new BufferedReader(fileReader);

while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}

bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
}
}
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.