.. qnum::
   :prefix: 4-13-
   :start: 1

Consumer Review Lab
====================

.. |student guide| raw:: html

   <a href="https://apcentral.collegeboard.org/pdf/ap-computer-science-a-consumer-review-lab-student-guide.pdf" target="_blank" style="text-decoration:underline">student guide</a>

.. |cleansentiment.csv| raw:: html

   <a href="https://github.com/bhoffman0/APCSA-2019/tree/master/_sources/Unit4-Iteration/ConsumerReviewLabFiles/cleanSentiment.csv" target="_blank" style="text-decoration:underline">cleanSentiment.csv</a>

.. |ConsumerReviewLabFiles| raw:: html

   <a href="https://github.com/bhoffman0/APCSA-2019/tree/master/_sources/Unit4-Iteration/ConsumerReviewLabFiles/" target="_blank" style="text-decoration:underline">ConsumerReviewLabFiles</a>

.. |repl student files| raw:: html

   <a href="https://firewalledreplit.com/@BerylHoffman/AP-CSA-Consumer-Review-Lab-Student-Files" target="_blank" style="text-decoration:underline">repl student files</a>

.. |Lesson 4.3 Loops and Strings| raw:: html

   <a href="https://runestone.academy/runestone/books/published/csawesome/Unit4-Iteration/topic-4-3-strings-loops.html#while-find-and-replace-loop" target="_blank" style="text-decoration:underline">Lesson 4.3 Loops and Strings</a>


The Consumer Review Lab is a new AP CSA 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.



.. code-block:: 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, such as ``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.


.. mchoice:: staticMethodCall
   :answer_a: double value = sentimentVal();
   :answer_b: sentimentVal("terrible");
   :answer_c: word.sentimentVal("terrible");
   :answer_d: double value = Review.sentimentVal("terrible");
   :answer_e: int value = sentimentVal("terrible");
   :correct: d
   :feedback_a: sentimentVal takes a String argument and is in the class Review.
   :feedback_b: sentimentVal returns a value and is in the class Review.
   :feedback_c: sentimentVal returns a value and is a static method in the class Review.
   :feedback_d: That's right1 sentimentVal takes a String argument and returns a double value and is a static method that can be called with the class name Review.
   :feedback_e: sentimentVal returns a double value, not int, and it's a static method in the class Review.


   Which of the following correctly calls the method sentimentVal?


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.
