CommencerCommencer gratuitement

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:

  1. Inner join the text to the lexicon by word.
  2. Count the sentiments by line.
  3. Reshape the data so each sentiment has its own column.
  4. (Depending upon the lexicon) Calculate the polarity as positive score minus negative score.
  5. Plot the polarity time series.

This exercise should look familiar: it extends Bing tidy polarity: Call me Ishmael (with ggplot2)!.

Cet exercice fait partie du cours

Sentiment Analysis in R

Afficher le cours

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

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
    ___ = ___()
  )
Modifier et exécuter le code