Now You Code 4: Sentiment v1.0

Let's write a basic sentiment analyzer in Python. Sentiment analysis is the act of extracting mood from text. It has practical applications in analyzing reactions in social media, product opinions, movie reviews and much more.

The 1.0 version of our sentiment analyzer will start with a string of positive and negative words. For any input text and the sentiment score will be calculated by simply adding up the frequency of the positive words, then subtracting out the negative words.

So for example, if:

    positive_text = "happy glad like"
    negative_text = "angry mad hate"
    input_text = "Amazon makes me like so angry and mad"
    score = -1 [ +1 for like, -1 for angry, -1 for mad]

You will complete this program by first writing the sentiment function, then writing some tests for it.

You will conclude by writing the complete sentiment analyzer to score any sentence.

Problem Analysis For Sentiment Function

You want to write ScoreSentiment() as a function:

  • Function: ScoreSentiment()
  • Arguments (input): postive_text, negative_text, input_text
  • Returns (output): score (int)

Algorithm (Steps in Program):

for each word in our tokenized input_text
    if word in positive_text then
        increment seniment score
    else if word in negative_text then
        decrement sentiment score

Step 1: Write the function


In [ ]:
def ScoreSentiment(positive_text, negative_text, input_text):
    #TODO write code here
    
    return score

Step 2: Write tests for the function

With the function complete, we need to test our function. The simplest way to do that is call the function with inputs we expect and verify the output. For example:

pos_text='happy joy good'
neg_text ='sad pain bad'

WHEN input_text='I am sad with joy' We EXPECT ScoreSentiment(pos_text, neg_text, input_text) to return 0
WHEN input_text='I am sad and in pain' We EXPECT ScoreSentiment(pos_text, neg_text, input_text) to return -2
WHEN input_text='I am happy with joy' We EXPECT ScoreSentiment(pos_text, neg_text, input_text) to return 2

In [ ]:
## TODO write tests here.

Step 3: Write final program

Then write a main program that executes like this:

Sample Run

Sentiment Analyzer 1.0
Type 'quit' to exit.
Enter Text: i love a good book from amazon
2 positive.
Enter Text: i hate amazon their service makes me angry
-2 negative.
Enter Text: i love to hate amazon
0 neutral.
Enter Text: quit

NOTE: make up your own strings of positive and negative words to make the sentiment more accurate.

3.a : Problem Analysis

Inputs:

Outputs:

Algorithm:


In [ ]:
## TODO: 3b write program

Step 4: Questions

  1. What can be done to make the sentiment more accurate?

Answer:

  1. Do you see a problem with the method of scoring?

Answer:

  1. Does the function improve readability of the final program in 3.b? Why or why not?

Answer:

Step 5: Reflection

Reflect upon your experience completing this assignment. This should be a personal narrative, in your own voice, and cite specifics relevant to the activity as to help the grader understand how you arrived at the code you submitted. Things to consider touching upon: Elaborate on the process itself. Did your original problem analysis work as designed? How many iterations did you go through before you arrived at the solution? Where did you struggle along the way and how did you overcome it? What did you learn from completing the assignment? What do you need to work on to get better? What was most valuable and least valuable about this exercise? Do you have any suggestions for improvements?

To make a good reflection, you should journal your thoughts, questions and comments while you complete the exercise.

Keep your response to between 100 and 250 words.

--== Write Your Reflection Below Here ==--