Description Usage Arguments Details Value Examples
as_key
- Create your own hash keys from a data frame for use in key
arguments such as polarity_dt
in the sentiment
function.
update_key
- Add/remove terms to a current key.
update_polarity_table
- Wrapper for update_key
specifically for
updating polarity tables.
update_valence_shifter_table
- Wrapper for update_key
specifically for updating valence shifter tables.
is_key
- Logical check if an object is a key.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | as_key(x, comparison = lexicon::hash_valence_shifters, sentiment = TRUE, ...)
update_key(
key,
drop = NULL,
x = NULL,
comparison = lexicon::hash_valence_shifters,
sentiment = FALSE,
...
)
update_polarity_table(
key,
drop = NULL,
x = NULL,
comparison = lexicon::hash_valence_shifters,
sentiment = FALSE,
...
)
update_valence_shifter_table(
key,
drop = NULL,
x = NULL,
comparison = lexicon::hash_sentiment_jockers_rinker,
sentiment = FALSE,
...
)
is_key(key, sentiment = TRUE)
|
x |
A |
comparison |
A |
sentiment |
logical. If |
key |
A sentimentr hash key. |
drop |
A vector of terms to drop. |
... |
ignored. |
For updating keys via update_key
note that a
polarity_dt
and valence_shifters_dt
are the primary dictionary
keys used in the sentimentr package. The polarity_dt
takes a
2 column data.frame
(named x and y) with the first column being
character and containing the words and the second column being numeric values
that are positive or negative. valence_shifters_dt
takes a 2 column
data.frame
(named x and y) with the first column being character and
containing the words and the second column being integer corresponding to:
(1) negators, (2) amplifiers, (3) de-amplifiers, and (4) dversative
conjunctions (i.e., 'but', 'however', and 'although'). Also, note that if
you are updating a valence_shifters_dt
you need an appropriate
comparison
; most likely, comparison = sentimentr::polarity_dt
.
Returns a data.table object that can be used as a hash key.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | 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)
(bing_key <- as_key(syuzhet:::bing))
as_key(syuzhet:::afinn)
as_key(syuzhet:::syuzhet_dict)
sam <- gsub("Sam-I-am", "Sam I am", sam_i_am)
sentiment(sam, , polarity_dt = bing_key)
## The nrc dictionary in syuzhet requires a bit of data wrangling before it
## is in the correct shape to convert to a key.
library(syuzhet)
library(tidyverse)
nrc_key <- syuzhet:::nrc %>%
dplyr::filter(
sentiment %in% c('positive', 'negative'),
lang == 'english'
) %>%
dplyr::select(-lang) %>%
mutate(value = ifelse(sentiment == 'negative', value * -1, value)) %>%
dplyr::group_by(word) %>%
dplyr::summarize(y = mean(value)) %>%
sentimentr::as_key()
sentiment(sam, polarity_dt = nrc_key)
## The lexicon package contains a preformatted nrc sentiment hash table that
## can be used instead.
sentiment(sam, polarity_dt = lexicon::hash_sentiment_nrc)
## End(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()
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.