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
Exercise instructions
- Examine the conversation data frame,
conversation
. Note the valence shifters like "never" in the text column. - Apply
polarity()
to thetext
column ofconversation
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 ofconversation
. - The text variable is
text
and the grouping variable isstudent
.
- Call
- To see the student level results, use
scores()
onstudent_pol
. - The
counts()
function applied tostudent_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 withplot()
.
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
___