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

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

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) }) is.dictionary(dictionary(list(key1 = c("val1", "val2"), key2 = "val3")))
#> [1] TRUE
## [1] TRUE is.dictionary(list(key1 = c("val1", "val2"), key2 = "val3"))
#> [1] FALSE
## [1] FALSE