Question: Sentiment value and Star Ratings in Java 1a). Test the method by calling totalSentiment(SimpleReview.txt)and printing the returned value. 1b). Write the following method that determines

Sentiment value and Star Ratings in Java

1a). Test the method by calling totalSentiment(SimpleReview.txt)and printing the returned value.

1b). Write the following method that determines the star rating of a review. public static int starRating(String fileName)

1c). Explain how the totalSentiment method works, including how youre using strings and iteration in your solution.

2. Test your starRating method for multiple reviews, including your simple sample review.

a). Do the ratings make sense? Explain why or why not.

b). Describe at least two ways in which you could adjust your totalSentiment method so that your program returns even more reasonable ratings.

import java.util.Scanner;

import java.io.File;

import java.util.HashMap;

import java.util.ArrayList;

import java.util.Random;

import java.io.*;

/**

* Class that contains helper methods for the Review Lab

* (method removePunctuation() was added from teacher code)

**/

public class Review {

private static HashMap sentiment = new HashMap();

private static ArrayList posAdjectives = new ArrayList();

private static ArrayList negAdjectives = new ArrayList();

private static final String SPACE = " ";

static{

try {

Scanner input = new Scanner(new File("cleanSentiment.csv"));

while(input.hasNextLine()){

String[] temp = input.nextLine().split(",");

sentiment.put(temp[0],Double.parseDouble(temp[1]));

//System.out.println("added "+ temp[0]+", "+temp[1]);

}

input.close();

}

catch(Exception e){

System.out.println("Error reading or parsing cleanSentiment.csv");

}

//read in the positive adjectives in postiveAdjectives.txt

try {

Scanner input = new Scanner(new File("positiveAdjectives.txt"));

while(input.hasNextLine()){

String temp = input.nextLine().trim();

// System.out.println(temp);

posAdjectives.add(temp);

}

input.close();

}

catch(Exception e){

System.out.println("Error reading or parsing postitiveAdjectives.txt " + e);

}

//read in the negative adjectives in negativeAdjectives.txt

try {

Scanner input = new Scanner(new File("negativeAdjectives.txt"));

while(input.hasNextLine()){

negAdjectives.add(input.nextLine().trim());

}

input.close();

}

catch(Exception e){

System.out.println("Error reading or parsing negativeAdjectives.txt");

}

}

/**

* returns a string containing all of the text in fileName (including punctuation),

* with words separated by a single space

*/

public static String textToString( String clockReview )

{

String temp = "";

try {

Scanner input = new Scanner(new File(clockReview));

//add 'words' in the file to the string, separated by a single space

while(input.hasNext()){

temp = temp + input.next() + " ";

}

input.close();

}

catch(Exception e){

System.out.println("Unable to locate " + clockReview);

}

//make sure to remove any additional space that may have been added at the end of the string.

return temp.trim();

}

/**

* @returns the sentiment value of word as a number between -1 (very negative) to 1 (very positive sentiment)

*/

public static double sentimentVal( String word )

{

try

{

return sentiment.get(word.toLowerCase());

}

catch(Exception e)

{

return 0;

}

}

/**

* Returns the ending punctuation of a string, or the empty string if there is none

*/

public static String getPunctuation( String word )

{

String punc = "";

for(int i=word.length()-1; i >= 0; i--){

if(!Character.isLetterOrDigit(word.charAt(i))){

punc = punc + word.charAt(i);

} else {

return punc;

}

}

return punc;

}

/**

* Returns the word after removing any beginning or ending punctuation

*/

public static String removePunctuation( String word )

{

while(word.length() > 0 && !Character.isAlphabetic(word.charAt(0)))

{

word = word.substring(1);

}

while(word.length() > 0 && !Character.isAlphabetic(word.charAt(word.length()-1)))

{

word = word.substring(0, word.length()-1);

}

return word;

}

/**

* Randomly picks a positive adjective from the positiveAdjectives.txt file and returns it.

*/

public static String randomPositiveAdj()

{

int index = (int)(Math.random() * posAdjectives.size());

return posAdjectives.get(index);

}

/**

* Randomly picks a negative adjective from the negativeAdjectives.txt file and returns it.

*/

public static String randomNegativeAdj()

{

int index = (int)(Math.random() * negAdjectives.size());

return negAdjectives.get(index);

}

/**

* Randomly picks a positive or negative adjective and returns it.

*/

public static String randomAdjective()

{

boolean positive = Math.random() < .5;

if(positive){

return randomPositiveAdj();

} else {

return randomNegativeAdj();

}

}

/** Activity 2: totalSentiment()

* Write the code to total up the sentimentVals of each word in a review.

*/

public static double totalSentiment(String clockReview)

{

// read in the file contents into a string using the textToString method with the filename

String s = textToString(clockReview);

String arr[] = s.split(" ");

// set up a sentimentTotal variable

double sentimentTotal = 0.0;

// loop through the file contents

// find each word

// add in its sentimentVal

// set the file contents to start after this word

for(String i : arr) {

sentimentTotal += sentimentVal(i);

}

return sentimentTotal;

}

/** Activity 2 starRating method

Write the starRating method here which returns the number of stars for the review based on its totalSentiment.

*/

public static int starRating(String filename)

{

// call the totalSentiment method with the fileName

// determine number of stars between 0 and 4 based on totalSentiment value

int stars = 0; // change this!

// write if statements here

// return number of stars

return stars;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!