Get startedGet started for free

Happy songs!

Of course just positive and negative words aren't enough. In this exercise you will learn about valence shifters which tell you about the author's emotional intent. Previously you applied polarity() to text without valence shifters. In this example you will see amplification and negation words in action.

Recall that an amplifying word adds 0.8 to a positive word in polarity() so the positive score becomes 1.8. For negative words 0.8 is subtracted so the total becomes -1.8. Then the score is divided by the square root of the total number of words.

Consider the following example from Frank Sinatra:

  • "It was a very good year"

"Good" equals 1 and "very" adds another 0.8. So, 1.8/sqrt(6) results in 0.73 polarity.

A negating word such as "not" will inverse the subjectivity score. Consider the following example from Bobby McFerrin:

  • "Don't worry Be Happy"

"worry is now 1 due to the negation "don't." Adding the "happy", +1, equals 2. With 4 total words, 2 / sqrt(4) equals a polarity score of 1.

This exercise is part of the course

Sentiment Analysis in R

View Course

Exercise instructions

  • Examine the conversation data frame,conversation. Note the valence shifters like "never" in the text column.
  • Apply polarity() to the text column of conversation to calculate polarity for the entire conversation.
  • Calculate the polarity scores by student, assigning the result to student_pol.
    • Call polarity() again, this time passing two columns of conversation.
    • The text variable is text and the grouping variable is student.
  • To see the student level results, use scores() on student_pol.
  • The counts() function applied to student_pol will print the sentence level polarity for the entire data frame along with lexicon words identified.
  • The polarity object, student_pol, can be plotted with plot().

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Examine conversation
___

# Polarity - All
___

# Polarity - Grouped
student_pol <- conversation %$%
  ___(___, ___)

# Student results
___

# Sentence by sentence
___

# qdap plot
___
Edit and Run Code