# NOT RUN {
key <- data.frame(
words = sample(letters),
polarity = rnorm(26),
stringsAsFactors = FALSE
)
(mykey <- as_key(key))
## Looking up values
mykey[c("a", "k")][[2]]
## Drop terms from key
update_key(mykey, drop = c("f", "h"))
## Add terms to key
update_key(mykey, x = data.frame(x = c("dog", "cat"), y = c(1, -1)))
## Add terms & drop to/from a key
update_key(mykey, drop = c("f", "h"), x = data.frame(x = c("dog", "cat"), y = c(1, -1)))
## Explicity key type (wrapper for `update_key` for sentiment table.
## See `update_valence_shifter_table` a corresponding valence shifter updater.
library(lexicon)
updated_hash_sentiment <- sentimentr:::update_polarity_table(lexicon::hash_sentiment_huliu,
x = data.frame(
words = c('frickin', 'hairy'),
polarity = c(-1, -1),
stringsAsFactors = FALSE
)
)
## Checking if you have a key
is_key(mykey)
is_key(key)
is_key(mtcars)
is_key(update_key(mykey, drop = c("f", "h")))
## Using syuzhet's sentiment lexicons
# }
# NOT RUN {
library(syuzhet)
as_key(syuzhet:::bing)
as_key(syuzhet:::afinn)
as_key(syuzhet:::syuzhet_dict)
nrc <- data.frame(
words = rownames(syuzhet:::nrc),
polarity = syuzhet:::nrc[, "positive"] - syuzhet:::nrc[, "negative"],
stringsAsFactors = FALSE
)
as_key(nrc[nrc[["polarity"]] != 0, ])
sentiment(gsub("Sam-I-am", "Sam I am", sam_i_am), as_key(syuzhet:::bing))
# }
# NOT RUN {
## Using 2 vectors of words
# }
# NOT RUN {
install.packages("tm.lexicon.GeneralInquirer", repos="http://datacube.wu.ac.at", type="source")
require("tm.lexicon.GeneralInquirer")
positive <- terms_in_General_Inquirer_categories("Positiv")
negative <- terms_in_General_Inquirer_categories("Negativ")
geninq <- data.frame(
x = c(positive, negative),
y = c(rep(1, length(positive)), rep(-1, length(negative))),
stringsAsFactors = FALSE
) %>%
as_key()
geninq_pol <- with(presidential_debates_2012,
sentiment_by(dialogue,
person,
polarity_dt = geninq
))
geninq_pol %>% plot()
# }
Run the code above in your browser using DataLab