valence: Set or get the valences of dictionary values or keys

View source: R/textstat_valence.R

valenceR Documentation

Set or get the valences of dictionary values or keys

Description

Set or retrieve the valences of a dictionary object for the purposes of sentiment analysis. Valences consist of numerical values attached to each dictionary "value". For dictionaries with a more "polarity"-based approach, see textstat_polarity()

Usage

valence(x)

valence(x) <- value

Arguments

x

a quanteda dictionary object

value

named list consisting of numerical value. The names of the elements must correspond to a dictionary key. Each element must be:

  • a single numeric value that will be applied to all of the dictionary values in that key; or

  • a vector of numeric values that matches the length and order of the dictionary values in that key; or

  • a named numeric vector where each element name matches dictionary values in the key.

Details

Valences are used only in textstat_valence().

A dictionary may have only one set of valences at a time, but may be changed as needed.

Value

valences() returns the valences as a list named numeric vectors, where each list element corresponds to a key in the dictionary, and each numeric element matches a value within that key.

⁠valence<-⁠ sets the dictionary's valences.

See Also

textstat_valence(), valence()

Examples

library("quanteda")

# setting valences
dict <- dictionary(list(
    happiness = c("happy", "jubilant", "exuberant", "content"),
    anger = c("mad", "peeved", "irate", "furious", "livid")
))
valence(dict)
# using a 5-point scale: 1:1 match
valence(dict) <- list(happiness = c(3, 4, 5, 2),
                      anger = c(3.1, 2.4, 2.9, 4.1, 5.0))
valence(dict)
# with single valences applied to all values within the keys
valence(dict) <- c(happiness = 1, anger = -1)
valence(dict)
# with named elements - order does not matter
valence(dict) <- list(
    happiness = c(exuberant = 5, jubilant = 4, happy = 3, content = 2)
)
valence(dict)


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