The package sentimentr
provides tools to calculate text polarity sentiment at the sentence level and
optionally aggregate by rows or grouping variable. One of its functions, sentiment
, approximates the sentiment (polarity) of text by sentence. In particular,
sentiment(text.var, polarity_dt = lexicon::hash_sentiment_jockers_rinker, ...)
text.var
is the text variable, whereas polarity_dt
is the dictionary provided by lexicon
package. I would like to know whether it is possible to extend the set of terms in the lexicon
dictionaries, by adding words (with their corresponding scores) to them.
You can. The sentiment tables are just data.tables. If you have words to add just create your own table and add these to the lexicon. See example below.
library(sentimentr)
library(data.table)
extra_terms <- data.table(x = c("word1", "word2"),
y = c(1.0, -1.0),
key = "x")
# merge data with lexicon
my_lex <- merge(lexicon::hash_sentiment_jockers_rinker, extra_terms, by = c("x", "y"), all = TRUE)
sentiment(text.var, polarity_dt = my_lex, ...)