Unhappy ending? Chronological polarity
Sometimes you want to track sentiment over time. For example, during an ad campaign you could track brand sentiment to see the campaign's effect. You saw a few examples of this at the end of the last chapter.
In this exercise you'll recap the workflow for exploring sentiment over time using the novel Moby Dick. One should expect that happy moments in the book would have more positive words than negative. Conversely dark moments and sad endings should use more negative language. You'll also see some tricks to make your sentiment time series more visually appealing.
Recall that the workflow is:
- Inner join the text to the lexicon by word.
- Count the sentiments by line.
- Reshape the data so each sentiment has its own column.
- (Depending upon the lexicon) Calculate the polarity as positive score minus negative score.
- Plot the polarity time series.
This exercise should look familiar: it extends Bing tidy polarity: Call me Ishmael (with ggplot2)!.
Diese Übung ist Teil des Kurses
Sentiment Analysis in R
Interaktive Übung
Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.
moby_polarity <- moby %>%
# Inner join to the lexicon
___(___, by = c("___" = "___")) %>%
# Count by sentiment, index
___(___, ___) %>%
# Pivot sentiments wider
___(names_from = ___, values_from = ___, values_fill = ___) %>%
mutate(
# Add polarity field
___ = ___ - ___,
# Add line number field
___ = ___()
)