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)
object to be coerced or checked; current legal values are a
data.frame with the fields word
and sentiment
(as per the
tidytext package)
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.
# 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