Question
Any words not in the list will have a return value of zero. Make sure to keep calling the method until you have at least
- Any words not in the list will have a return value of zero. Make sure to keep calling the method until you have at least three strings that have a return value other than zero. Record the method calls, including provided strings and return values.
Experiment using the sentimentVal method in this exercise, and answer the questions in the next.
In this activity you'll be calling the sentimentVal method. Find this method in the Review class, take note of the answers to the following 3 questions and provide these answers when you get to the next activity:
In the ReviewTester class, write code that tests the sentimentVal method by calling it with several different String parameters and printing out the return value.
Test out sentimentVal on the words "happily", "cold", and "terribly".
Any words not in the list will have a return value of zero. Make sure to keep calling the method until you have at least three strings that have a return value other than zero. Record the method calls, including provided strings and return values.
REVIEW TESTER.JAVA
public class ReviewTester{
REVIEW.JAVA
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
**/
public class Review {
private static HashMap
private static ArrayList
private static 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\n" + 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 fileName )
{
String temp = "";
try {
Scanner input = new Scanner(new File(fileName));
//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 " + fileName);
}
//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;
}
/**
* 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();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started