Consumer Review Lab

The Consumer Review Lab is a new AP CS A Lab released in 2019 to practice loops and calling methods to prepare for the free response question on control structures and methods. Here is a link to the student guide. The teacher guide and the code files are available in the AP Course Audit Portal. The code files are also below.

Activity 0 Analyzing Reviews

In pairs or groups, pretend you are going to buy something from a shopping site on the internet that also has reviews by other people who have bought that product. We all use reviews on a regular basis, to help us determine which movie to see, which video game to play, or even which pair of headphones to buy. In your group,

  1. Find a positive review. What words make you think it is a positive review? Write them down. Which of these words are the most positive in your opinion? Which of these words are most often used in reviews?

  2. Find a negative review. What words make you think it is a negative review? Write them down. Which of these words are the most negative in your opinion? Which of these words are most often used in reviews?

  3. Do you think any of the reviews are fake reviews? How can you tell? Why would people write fake reviews?

  4. Report back to the class and discuss your answers. As a class, try to rank some of the positive and negative words as more or less positive or negative in comparison.

As a class, look at this cleanSentiment.csv list of words from the lab. Can you find your positive and negative words on the list? Notice that each word has a positive or negative integer value assigned to it. This value is called the sentiment value of the word. A large positive sentiment value means that word has appeared in a lot of positive contexts. The higher the number, the more positive the sentiment. And a large negative sentiment value means that word has appeared in a lot of negative contexts. This list was generated by a computer program that counted the frequency of each word in lots of online reviews that were rated by humans as positive or negative. Do you agree with the sentiment values on the list? The quality of the list really depends on the quality and quantity of the data used to generate it.

The shopping site you used may actually use sentiment analysis to group the reviews into positive and negative reviews for you. Many sites also try to catch fake reviews with sentiment analysis. Companies may use sentiment analysis to see if their reviews are more positive or negative and to make improvements to their products or marketing.

Activity 1 : Sentiment Value

Let’s try some code from this lab! Working in pairs, open the repl student files and click on Fork or start typing your name in the comments to make a copy of it or download the ConsumerReviewLabFiles to use in a different IDE.

In pairs, do the Activity 1 worksheet from the student guide. Find the sentimentVal() method seen below in the Review.java file (ctrl-f can be used to search a file) and try calling it from the main method in Main.java with different words. It returns the sentiment value from the cleanSentiment.csv file.

This method uses a try catch block for error-checking which is not covered in the AP exam. If you put some code in a try block and it has a runtime error (which is called an Exception in Java), the code in the catch block will be executed to handle that error. You also do not need to know some of the other complicated code with files and data structures in Review.java.

/**
 * @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;
    }
}

Notice that sentimentVal() is a static method. We’ve seen static methods before in the Math class, like Math.random(). How do you call static methods? You don’t need to create an object; you can just use the class name. Note that this method takes an argument (the word to check) and has a return value (the sentiment value of that word). You will need to call it correctly and print out what it returns to see the results.

Activity 2 :Total Sentiment Value and Star Ratings

Now that you have read reviews and started exploring the sentimentVal method, you will write code to determine the sentiment of an entire review by totaling the sentiment of each word in the review and a star rating that is determined by the total sentiment.

Working in pairs, pick an online review of your choice or make up a funny one. Copy and paste the content of the review into a new text file on repl or in your IDE, making sure to save the file with a .txt extension. There are also two test reviews already in the files called SimpleReview.txt and 26WestReview.txt that you could use as well.

In pairs, do the Activity 2 worksheet from the student guide using the repl student files or a different IDE. You will write the code for the methods totalSentiment() and starRating(). The method signatures for these methods have already been put into Review.java. You will need to fill in the code inside these methods.

Here are some hints to write the totalSentiment() method:

  1. The method totalSentiment() needs to use the method String textToString( String fileName ) to read in the file contents in the filename given as its argument into a String. Because this method is in the same class as the method totalSentiment(), it can be called without a class or object with just the method name, textToString(fileName);, but make sure you save the file contents it returns into a variable.

  2. You can use a loop to go through each word in the file contents and add up their sentiment values. The total sentiment value will be returned.

  3. How do you get each word in the file contents? Look for the spaces! You may want to review Lesson 4.3 Loops and Strings. Remember how we looped to find all the 1’s in a String? Here we’re looking for all the spaces (” “). You will need to use indexOf to find the spaces and substring to get each word. To make it simpler, after finding a word, you could set the file contents to the rest of the review without that word.

  4. To test the method, call it from the main method in Main.java and give it one of the review filenames like “SimpleReview.txt”. Print out what it returns. You could also put a print statement in the loop of the method to see what words it finds and the running total.

The starRating() method is actually simpler. It needs to first call the totalSentiment() method that you wrote and save its result and then use that to decide the number of stars using if statements. You will have to decide the cut off values for the number of stars between 0 and 4 stars. SimpleReview.txt should probably return 0 or 1 star, and 26WestReview.txt should probably return 4 stars.

Activity 3 : Autogenerate a Fake Review

If your class has time, continue on with Activity 3 where you write code that will create a fake review by replacing adjectives marked with * in the one of the given reviews with randomly selected good or bad adjectives.

First, you need put in some positive and negative adjectives in the files positiveAdjectives.txt and negativeAdjectives.txt one word per line, and put * in front of the adjectives in simpleReview.txt or other review files.

Then, write a public static String fakeReview(String filename) method that reads the contents of the argument filename into a String using the textToString method like in Activity 2, and generates and returns a fake review by replacing any word that starts with a * with a random adjective using the given randomAdjective() method.

Activity 4 : Create a More Positive or Negative Review

If your class has time continue on with Activity 4 which changes the fake review by replacing negative words with positive words or the opposite to make a review more positive or more negative. You can choose whether you want to make your review more positive or more negative and use the given methods randomPositiveAdjective() and randomNegativeAdjective().

Activity 5 : Open-ended Activity

If your class has time continue on with Activity 5 which is an open ended activity of your own design.

You have attempted of activities on this page