Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objective: This closed lab will provide you with experience with designing a class that is being used by another. The Problem This is a two-part

Objective: This closed lab will provide you with experience with
designing a class that is being used by another.
The Problem
This is a two-part assignment that you will complete in order to build a
Movie Review Sentiment Analyzer. When all the parts of this project
are complete, you will be able to take a one-line review of some movie,
such as:
This film was a breath of fresh air
and report that this was a positive review, or one such as:
It made me want to poke out my eyeballs
and report it as a negative review.
Part I
You already completed the first step of this project in Closed
Lab last week. And that was to take a single word, and look for the
number of times it occurs in a file containing movie reviews. I have
provided you with a functioning version of that code, in case you do not
have yours from last week.
Attached with this assignment is a file called movieReviews.txt.
This file contains a number of one sentence reviews. Each of these
reviews also has a score that ranges from 0 (most negative) to 4 (most
positive). The score always appears as the first character of each line.
For example, here is a positive review:
4 If a horror movie 's primary goal is to
frighten and disturb , then They works
spectacularly well ... A shiver-inducing , nerve-
rattling ride .
And here is a negative review:

 

0 Comes across as a relic from a bygone era , and
its convolutions ... feel silly rather than
plausible .
Given these scores, if a word appears in a line, then the score for the
word is the score that the line itself has. For example, the words "relic"
and "silly" would have a score of 0 from the line above, while the word
"spectacularly" will have the score of 4.
Of course, the same word, depending on the context, may have
different scores. So we will average out the score from all occurrences.
Your Task
I have provided you with some starter code. This code already reads
each line from the file, and prints it out. Your job is to modify this code
to do the following:
1. Take from the user as input a single word
2. Count the number of lines of the file in which the word appears
3. Calculate the average score of the word
4. Print out the number of occurrences, and the average score
Here are some sample runs:
Please enter the word you are looking for:
darkly
The word darkly appears 4 times
The average score is 2.5
Please enter the word you are looking for:
mechanical
The word mechanical appears 6 times
The average score is 0.6666666666666666
Please enter the word you are looking for:
soulless
The word soulless appears 7 times
The average score is 0.7142857142857143
Please enter the word you are looking for:
entertaining
The word entertaining appears 95 times

The average score is 2.8
You can open up the movieReviews.txt in a regular text editor, and
verify that your program works correctly.
Part II
For this part of the assignment, you are going to complete the second
step of this project. You will ask for a one-line review of a movie, and
compute the average score of all the words in that review.
Your Task
Take the code you wrote for Part I of this assignment (or the code that
I've given you), and modify that code to do the following:
1. Refactor the code move the code that computes the score for a
single word into its own method, with the following signature:
public static double wordScore(String word);
2. Ask the user for a one-line movie review as input. You can
assume that the reviews have to be at most 25 words.
3. Place each word in the one-line review in a String array.
4. Calculate the word score for each word by calling the wordScore
method for each element in the array.
5. Calculate the average score of the scores, and print this out.
Note: Once you've completed this lab, please email the code to
yourself. You can use it for a future assignment, which will be a
continuation of this lab.
 

Import java.util.Scanner; import java.io.File; public class Lab10 {   public static void main(String[] args) {      // Create a new Scanner object to obtain      // input from System.in      Scanner input = new Scanner(System.in);            // Ask user for a word to search for. Print      // out a prompt      System.out.println("Please enter the word you want a score for:");            // Use the Scanner object you created above to      // take a word of input from the user.      String word = input.next();                  // *** Don't modify this line      // Create a new file object to represent the      // file we are reading from.      // *** Important: Make sure that the file      // movieReviews.txt is in the same directory as      // your Java source.        File file = new File("movieReviews.txt");            // Read from the file, and print out each line      try {         // Just like we've created a Scanner object         // to read from System.in, we can create one         // to read from any File object.         Scanner fInput = new Scanner(file);               // Variables to keep track of word scores         int wordScoreTotal = 0;         int count = 0;         double wordScore = 0.0;                 // Keep looping through the file, and         // process each line         while (fInput.hasNextLine()) {            // Get the next line            String line = fInput.nextLine();                        // For your assignment, replace the            // line of code above with your code to count            // the number of occurrences of the word            // that the user gave in the file.            // You will use methods such as getNumericValue            // in the Character class and the indexOf method            // in String.              int lineScore = Character.getNumericValue(line.charAt(0));            if (line.indexOf(word) >= 0) {               count++;               wordScoreTotal = wordScoreTotal + lineScore;            }             }                 // Print out the number of times the input word         // appears in the file movieReviews.txt         System.out.println("The word " + word +                     " appears " + count + " times in the file");                             // Calculate the average word score         if (count > 0) {            wordScore = wordScoreTotal / count;         }                 // Print out the average word score         System.out.println("The average score is " + wordScore);            // DO NOT modify the next line        } catch(Exception e) {         e.printStackTrace();      }     } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

International Marketing And Export Management

Authors: Gerald Albaum , Alexander Josiassen , Edwin Duerr

8th Edition

1292016922, 978-1292016924

More Books

Students also viewed these Programming questions

Question

What is a residual plot?

Answered: 1 week ago

Question

Question 17 D...OSMA Question 17 D...OSMA

Answered: 1 week ago

Question

What is e-business?

Answered: 1 week ago