textstat_valence: Compute sentiment from word valences

View source: R/textstat_valence.R

textstat_valenceR Documentation

Compute sentiment from word valences

Description

Compute sentiment scores from tokens or document-feature matrices, based on the valences of dictionary keys and values.

Usage

textstat_valence(
  x,
  dictionary,
  normalization = c("dictionary", "all", "none"),
  ...
)

Arguments

x

a character, corpus, tokens, or dfm object containing text, tokens, or features whose sentiment will be scored.

dictionary

a quanteda dictionary that has valence set, in the form of numerical valences associated with sentiment

normalization

the baseline for normalizing the sentiment counts after scoring. Sentiment scores within keys are weighted means of the tokens matched to dictionary values, weighted by their valences. The default "dictionary" is to average over only the valenced words. "all" averages across all tokens, and "none" does no normalization.

...

not used here

Value

a data.frame of sentiment scores

Note

If the input item is a dfm, then multi-word values will not be matched unless the features of the dfm have been compounded previously. The input objects should not have had dictionaries applied previously.

References

For a discussion of how to aggregate sentiment scores to the document level, see:

Lowe, W., Benoit, K. R., Mikhaylov, S., & Laver, M. (2011). Scaling Policy Preferences from Coded Political Texts. Legislative Studies Quarterly, 36(1), 123–155. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1111/j.1939-9162.2010.00006.x")}

See Also

valence()

Examples

library("quanteda")
## Not run: 

# AFINN
afinn <- read.delim(system.file("extdata/afinn/AFINN-111.txt", 
                                package = "quanteda.sentiment"),
                    header = FALSE, col.names = c("word", "valence"))
data_dictionary_afinn <- dictionary(list(afinn = afinn$word))
valence(data_dictionary_afinn) <- list(afinn = afinn$valence)
textstat_valence(toks, dictionary = data_dictionary_afinn)

# ANEW
anew <- read.delim(url("https://bit.ly/2zZ44w0"))
anew <- anew[!duplicated(anew$Word), ] # because some words repeat
data_dictionary_anew <- dictionary(list(pleasure = anew$Word,
                                        arousal = anew$Word,
                                        dominance = anew$Word))
valence(data_dictionary_anew) <- list(pleasure = anew$ValMn,
                                      arousal = anew$AroMn,
                                      dominance = anew$DomMn)
textstat_valence(toks, data_dictionary_anew["pleasure"])
textstat_valence(toks, data_dictionary_anew["arousal"])
## End(Not run)


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