Stressed Out!
Here you will adjust the negative words to account for the specific text. You will then compare the basic and custom polarity() scores.
A popular song from Twenty One Pilots is called "Stressed Out". If you scan the song lyrics, you will observe the song is about youthful nostalgia. Overall, most people would say the polarity is negative. Repeatedly the lyrics mention stress, fears and pretending.
Let's compare the song lyrics using the default subjectivity lexicon and also a custom one.
To start, you need to verify the key.pol subjectivity lexicon does not already have the term you want to add. One way to check is with grep(). The grep() function returns the row containing characters that match a search pattern. Here is an example used while indexing.
data_frame[grep("search_pattern", data_frame$column), ]
After verifying the slang or new word is not already in the key.pol lexicon you need to add it. The code below uses sentiment_frame() to construct the new lexicon. Within the code sentiment_frame() accepts the original positive word vector, positive.words. Next, the original negative.words are concatenated to "smh" and "kappa", both considered negative slang. Although you can declare the positive and negative weights, the default is 1 and -1 so they are not included below.
custom_pol <- sentiment_frame(positive.words, c(negative.words, "hate", "pain"))
Now you are ready to apply polarity and it will reference the custom subjectivity lexicon!
Este exercício faz parte do curso
Sentiment Analysis in R
Instruções do exercício
We've created stressed_out which contains the lyrics to the song "Stressed Out", by Twenty One Pilots.
- Use
polarity()onstressed_outto see the default score. - Check
key.polfor any words containing "stress". Usegrep()to index the data frame by searching in thexcolumn. - Create
custom_polas a new sentiment data frame.- Call
sentiment_frame()and passpositive.wordsas the first argument without concatenating any new terms. - Next, use
c()to combinenegative.wordswith new terms "stressed" and "turn back".
- Call
- Reapply
polarity()tostressed_outwith the additional parameterpolarity.frame = custom_polto compare how the new words change the score to a more accurate representation of the song.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# stressed_out has been pre-defined
head(stressed_out)
# Basic lexicon score
___(___)
# Check the subjectivity lexicon
___[___("stress", x)]
# New lexicon
custom_pol <- ___(___, c(negative.words, "___", "___"))
# Compare new score
___(___, polarity.frame = ___)