LoslegenKostenlos loslegen

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)!.

Diese Übung ist Teil des Kurses

Sentiment Analysis in R

Kurs anzeigen

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
    ___ = ___()
  )
Code bearbeiten und ausführen