Learn R Programming

quanteda (version 1.3.13)

as.dictionary: Coercion and checking functions for dictionary objects

Description

Convert a dictionary from a different format into a quanteda dictionary, or check to see if an object is a dictionary.

Usage

as.dictionary(x)

is.dictionary(x)

Arguments

x

object to be coerced or checked; current legal values are a data.frame with the fields word and sentiment (as per the tidytext package)

Value

as.dictionary returns a dictionary object. This conversion function differs from the dictionary constructor function in that it converts an existing object rather than creates one from components or from a file.

is.dictionary returns TRUE if an object is a quanteda dictionary.

Examples

Run this code
# NOT RUN {
data(sentiments, package = "tidytext")
as.dictionary(subset(sentiments, lexicon == "nrc"))
as.dictionary(subset(sentiments, lexicon == "bing"))
# to convert AFINN into polarities - adjust thresholds if desired
afinn <- subset(sentiments, lexicon == "AFINN")
afinn[["sentiment"]] <-
    with(afinn,
         sentiment <- ifelse(score < 0, "negative",
                             ifelse(score > 0, "positive", "netural"))
    )
with(afinn, table(score, sentiment))
as.dictionary(afinn)
# }
# NOT RUN {
is.dictionary(dictionary(list(key1 = c("val1", "val2"), key2 = "val3")))
## [1] TRUE
is.dictionary(list(key1 = c("val1", "val2"), key2 = "val3"))
## [1] FALSE
# }

Run the code above in your browser using DataLab