Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You may use an IDE (BlueJ, Netbeans, etc) or just an editor and command line operations (javac, java) in Unix or Windows/DOS to develop your

image text in transcribedimage text in transcribed image text in transcribedYou may use an IDE (BlueJ, Netbeans, etc) or just an editor and command line operations (javac, java) in Unix or Windows/DOS to develop your program.

Use good design (dont put everything in one class).

Use a package for your classes and put your files in the appropriate directory structure. You don't need to create any GUI for this assignment. Command line operations are enough.

SentimentAnalysisApp.java:

package assign2;

/** * @author metsis @author tesic @author wen */ public class SentimentAnalysisApp { public static void main(String [] args) { ReviewHandler rh = new ReviewHandler(); int realClass = 0; rh.loadReviews(args[0], realClass); // MODIFY THIS TO ADD YOUR CODE. } }

AbstractReviewHandler.java:

package assign2;

import java.io.BufferedOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import sentiment.Sentiment;

/** * CS3354 Spring 2019 Review Handler Abstract Class specification @author metsis @author tesic @author wen */ public abstract class AbstractReviewHandler {

public AbstractReviewHandler() { database = new HashMap(); sentimentModel = new Sentiment(new File(TRAINED_MODEL_NAME)); }

/** * Loads reviews from a given path. If the given path is a .txt file, then * a single review is loaded. Otherwise, if the path is a folder, all reviews * in it are loaded. * @param filePath The path to the file (or folder) containing the review(sentimentModel). * @param realClass The real class of the review (0 = Negative, 1 = Positive * 2 = Unknown). * @return A list of reviews as objects. */ public abstract void loadReviews(String filePath, int realClass); /** * Reads a single review file and returns it as a MovieReview object. * This method also calls the method classifyReview to predict the polarity * of the review. * @param reviewFilePath A path to a .txt file containing a review. * @param realClass The real class entered by the user. * @return a MovieReview object. * @throws IOException if specified file cannot be openned. */ public abstract MovieReview readReview(String reviewFilePath, int realClass) throws IOException;

/** * Classifies a review as negative, or positive by using the text of the review. * It updates the predictedPolarity value of the review object and it also * returns the predicted polarity. * Note: to achieve the classification, this method depends on the external * library sentiment.jar. * @param review A review object. * @return 0 = negative, 1 = positive. */ public int classifyReview(MovieReview review) { int polarity = sentimentModel.classifyText(review.getText()); review.setPredictedPolarity(polarity); return polarity; }

/** * Deletes a review from the database, given its id. * @param id The id value of the review. */ public abstract void deleteReview(int id); /** * Auxiliary convenience method used to close a file and handle possible * exceptions that may occur. * * @param c The file to be closed */ public void close(Closeable c) { if (c == null) { return; } try { c.close(); } catch (IOException ex) { System.err.println(ex.toString()); ex.printStackTrace(); } }

/** * Saves the database in the working directory as a serialized object. */ public void saveSerialDB() { System.out.print("Saving database..."); //serialize the database OutputStream file = null; OutputStream buffer = null; ObjectOutput output = null; try { file = new FileOutputStream(DATA_FILE_NAME); buffer = new BufferedOutputStream(file); output = new ObjectOutputStream(buffer);

output.writeObject(database);

output.close(); } catch (IOException ex) { System.err.println(ex.toString()); ex.printStackTrace(); } finally { close(file); } System.out.println("Done."); }

/** * Loads review database. */ @SuppressWarnings("unchecked") public abstract void loadSerialDB();

/** * Searches the review database by id. * @param id The id to search for. * @return The review that matches the given id or null if the id does not * exist in the database. */ public abstract MovieReview searchById(int id);

/** * Searches the review database for reviews matching a given substring. * @param substring The substring to search for. * @return A list of review objects matching the search criterion. */ public abstract List searchBySubstring(String substring);

/** * A map of pairs. */ protected Map database; /** * The file name of where the database is going to be saved. */ protected static final String DATA_FILE_NAME = "database.ser"; private static final String TRAINED_MODEL_NAME = "model.ser"; private Sentiment sentimentModel; }

MovieReview.java:

package assign2;

import java.io.Serializable;

/** CS3354 Spring 2019 Review Class Implementation @author metsis @author tesic @author wen */ public class MovieReview implements Serializable {

/** * Constructor. * @param id * @param text * @param realPolarity */ public MovieReview(int id, String text, int realPolarity) { this.id = id; this.text = text; this.realPolarity = realPolarity; this.predictedPolarity = 0; // Set a default value. To be changed later. }

/** * * @return Tweet id field */ public int getId() { return id; }

public String getText() { return text; }

public int getPredictedPolarity() { return predictedPolarity; }

/** * * @param predictedPolarity */ public void setPredictedPolarity(int predictedPolarity) { this.predictedPolarity = predictedPolarity; }

public int getRealPolarity() { return realPolarity; }

/** * The id of the review (e.g. 2087). */ private final int id; /** * The text of the review. */ private final String text; /** * The predicted polarity of the tweet (0 = negative, 1 = positive). */ private int predictedPolarity; /** * The ground truth polarity of the tweet (0 = negative, 1 = positive, 2 = unknown). */ private final int realPolarity;

}

package-info.java:

/** package and methods for SentimentAnalysisApp

package assign2;

Project Title: Sentiment Analysis (stage 2) Goal: The goal of this assignment is to help students familiarize themselves with the following Java programming concepts: Object Oriented Design and Inheritance Java Exception Handling Java Object Serialization .Using existing codebase and libraries Description: In your first assignment, you created a program that could classify movie reviews based on their sentiment. In this assignment we will extend the functionality of the previous assignment in a number of ways: We will use an existing machine learning library to classify movie reviews as opposed to counting positive and negative words. The hope is that we will be able to achieve a better classification accuracy using a more advanced tool We will add some simple functionality for maintaining a database of movie reviews, which can be extended by adding new movie reviews. A user can later search that database to refer back to specific movie reviews. We will be able to save the database and load it again the next time the program runs. 1. 2. 3. Your program should present the user with the following menu: 0. Exit program. 1. Load new movie review collection (given a folder or a file path) 2. Delete movie review from database (given its id) 3. Search movie reviews in database by id or by matching a substring. Notes: The above menu should be coded in a loop, so that the user can choose among the different options multiple times, until they choose option '0', in which case the program terminates Every time your program loads, it should first check if there exists a database file (serialized object) in its working directory. If such a file exists, it should load its contents (movie reviews) into the main memory (a HashMap can be used). When the program exits (user selects action '0'), it should save the new database contents back to the database file, replacing the old one . . When the user selects option "1": The program should also ask the user to provide the real class of the review collection (if known). The user can choose from the options: 0. Negative, 1 Positive, and 2. Unknown Upon loading each review, your program should assign a unique ID to each review, which should not conflict with existing ones, and it should also assign the value of the real class (as provided by the user) Then the program should automatically classify each review, using the external library "sentiment.jar" and assign a value to the "predictedClass" field of each review. The overall classification accuracy should also be reported, if the real class is known o o o Finally, the newly loaded reviews should be added to the permanent database . When the user selects option "3", the results should be printed in a formatted manner The printed information should be a table with each row showing: review ID, first 50 characters of review text, predicted class, real class. Tasks: Extend the given code to create and object-oriented Java program that implements the functionality described above. Make use of the inheritance and polymorphism concepts that we saw in class. Try to make your program as robust as possible, by using Exception handling to deal with possible problems that may occur during the program execution Save your database as a serialized object Use javadoc comments for all of your classes and methods 1. 2. 3. 4. Project Title: Sentiment Analysis (stage 2) Goal: The goal of this assignment is to help students familiarize themselves with the following Java programming concepts: Object Oriented Design and Inheritance Java Exception Handling Java Object Serialization .Using existing codebase and libraries Description: In your first assignment, you created a program that could classify movie reviews based on their sentiment. In this assignment we will extend the functionality of the previous assignment in a number of ways: We will use an existing machine learning library to classify movie reviews as opposed to counting positive and negative words. The hope is that we will be able to achieve a better classification accuracy using a more advanced tool We will add some simple functionality for maintaining a database of movie reviews, which can be extended by adding new movie reviews. A user can later search that database to refer back to specific movie reviews. We will be able to save the database and load it again the next time the program runs. 1. 2. 3. Your program should present the user with the following menu: 0. Exit program. 1. Load new movie review collection (given a folder or a file path) 2. Delete movie review from database (given its id) 3. Search movie reviews in database by id or by matching a substring. Notes: The above menu should be coded in a loop, so that the user can choose among the different options multiple times, until they choose option '0', in which case the program terminates Every time your program loads, it should first check if there exists a database file (serialized object) in its working directory. If such a file exists, it should load its contents (movie reviews) into the main memory (a HashMap can be used). When the program exits (user selects action '0'), it should save the new database contents back to the database file, replacing the old one . . When the user selects option "1": The program should also ask the user to provide the real class of the review collection (if known). The user can choose from the options: 0. Negative, 1 Positive, and 2. Unknown Upon loading each review, your program should assign a unique ID to each review, which should not conflict with existing ones, and it should also assign the value of the real class (as provided by the user) Then the program should automatically classify each review, using the external library "sentiment.jar" and assign a value to the "predictedClass" field of each review. The overall classification accuracy should also be reported, if the real class is known o o o Finally, the newly loaded reviews should be added to the permanent database . When the user selects option "3", the results should be printed in a formatted manner The printed information should be a table with each row showing: review ID, first 50 characters of review text, predicted class, real class. Tasks: Extend the given code to create and object-oriented Java program that implements the functionality described above. Make use of the inheritance and polymorphism concepts that we saw in class. Try to make your program as robust as possible, by using Exception handling to deal with possible problems that may occur during the program execution Save your database as a serialized object Use javadoc comments for all of your classes and methods 1. 2. 3. 4

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

Step: 3

blur-text-image

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

Graph Databases

Authors: Ian Robinson, Jim Webber, Emil Eifrem

1st Edition

1449356265, 978-1449356262

More Books

Students also viewed these Databases questions