knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "##",
  fig.path = "man/images/"
)
library("badger")

quanteda.sentiment

CRAN_Status_Badge r badge_devel("quanteda/quanteda.sentiment", "royalblue") Lifecycle: experimental Codecov test coverage R-CMD-check

Installation

You can install quanteda.sentiment from GitHub with:

remotes::install_github("quanteda/quanteda.sentiment")

The package is not yet on CRAN.

About

quanteda.sentiment extends the quanteda package with functions for computing sentiment on text. It has two main functions, for computing two types of sentiment. These follow the structure of a quanteda dictionary, which consists of key entries expressing the canonical concept, and value patterns (such as "good", "sad*", etc.) to be matched in a text and counted as occurrences of that key.

The approach to sentiment in this package approaches sentiment computation in two ways, depending on whether sentiment is considered a key attribute, in which case the keys are assigned a polarity such as positive or negative, or whether individual values are assigned a valence, in the form of some continuous value indicating a degree of sentiment. Each is implemented in a separate function:

The package comes with the following built-in dictionaries:

| Name | Description | Polarity | Valence | |:---------------------------------|:--------------------------------------------------------------|:--------:|:-------:| | data_dictionary_AFINN | Nielsen's (2011) 'new ANEW' valenced word list | | ✔ | | data_dictionary_ANEW | Affective Norms for English Words (ANEW) | | ✔ | | data_dictionary_geninqposneg | Augmented General Inquirer Positiv and Negativ dictionary | ✔ | | | data_dictionary_HuLiu | Positive and negative words from Hu and Liu (2004) | ✔ | | | data_dictionary_LoughranMcDonald | Loughran and McDonald Sentiment Word Lists | ✔ | | | data_dictionary_LSD2015 | Lexicoder Sentiment Dictionary (2015) | ✔ | | | data_dictionary_NRC | NRC Word-Emotion Association Lexicon | ✔ | | | data_dictionary_Rauh | Rauh's German Political Sentiment Dictionary | ✔ | | | data_dictionary_sentiws | SentimentWortschatz (SentiWS) | ✔ | ✔ |

Examples

For a polarity dictionary, we can use the positive and negative key categories from the General Inquirer dictionary:

library("quanteda.sentiment")

# inspect the dictionary and its polarities
print(data_dictionary_geninqposneg, max_nval = 8)

# compute sentiment
tail(data_corpus_inaugural) |>
  textstat_polarity(dictionary = data_dictionary_geninqposneg)

For a valence dictionary, we can compute this for the "pleasure" category of the Affective Norms for English Words (ANEW):

library("quanteda", warn.conflicts = FALSE, quietly = TRUE)
library("quanteda.sentiment")

# inspect the dictionary and its valences
print(data_dictionary_ANEW, max_nval = 8)
lapply(valence(data_dictionary_ANEW), head, 8)

# compute the sentiment
tail(data_corpus_inaugural) |>
  textstat_valence(dictionary = data_dictionary_ANEW["pleasure"])

We can compare two measures computed in different ways (although they are not comparable, really, since they are different lexicons):

# ensure we have this package's version of the dictionary
data("data_dictionary_LSD2015", package = "quanteda.sentiment")

sent_pol <- tail(data_corpus_inaugural, 25) |>
  textstat_polarity(dictionary = data_dictionary_LSD2015)
sent_pol <- dplyr::mutate(sent_pol, polarity = sentiment)
sent_val <- tail(data_corpus_inaugural, 25) |>
  textstat_valence(dictionary = data_dictionary_AFINN)

library("ggplot2")

ggplot(data.frame(sent_pol, valence = sent_val$sentiment),
       aes(x = polarity, y = valence)) +
  geom_point()

Good enough for government work!

Where to learn more

Each dictionary and function has extensive documentation, including references to social scientific research articles where each sentiment concept is described in detail. There is also a package vignette with more detailed examples.



quanteda/quanteda.sentiment documentation built on Feb. 26, 2024, 12:42 a.m.