emotion {sentimentr} | R Documentation |
Compute Emotion Rate
Description
Detect the rate of emotion at the sentence level. This method uses a simple
dictionary lookup to find emotion words and then compute the rate per sentence.
The emotion
score ranges between 0 (no emotion used) and 1 (all
words used were emotional). Note that a single emotion phrase would count as
just one in the emotion_count
column but would count as two words in
the word_count
column.
Usage
emotion(
text.var,
emotion_dt = lexicon::hash_nrc_emotions,
valence_shifters_dt = lexicon::hash_valence_shifters,
drop.unused.emotions = FALSE,
un.as.negation = TRUE,
un.as.negation.warn = isTRUE(all.equal(valence_shifters_dt,
lexicon::hash_nrc_emotions)),
n.before = 5,
n.after = 2,
retention_regex = "[^[:alpha:];:,']",
...
)
Arguments
text.var |
The text variable. Can be a |
emotion_dt |
A data.table with a |
valence_shifters_dt |
A data.table of valence shifters that can alter a polarized word's meaning and an integer key for negators (1), amplifiers [intensifiers] (2), de-amplifiers [downtoners] (3) and adversative conjunctions (4) with x and y as column names. For this purpose only negators is required/used. |
drop.unused.emotions |
logical. If |
un.as.negation |
logical. If |
un.as.negation.warn |
logical. If |
n.before |
The number of words to consider as negated before
the emotion word. To consider the entire beginning portion of a sentence
use |
n.after |
The number of words to consider as negated after
the emotion word. To consider the entire ending portion of a sentence
use |
retention_regex |
A regex of what characters to keep. All other
characters will be removed. Note that when this is used all text is lower
case format. Only adjust this parameter if you really understand how it is
used. Note that swapping the |
... |
ignored. |
Value
Returns a data.table of:
element_id - The id number of the original vector passed to
emotion
sentence_id - The id number of the sentences within each
element_id
word_count - Word count
emotion_type - Type designation from the
emotion
column of theemotion_dt
tableemotion_count - Count of the number of emotion words of that
emotion_type
emotion - A score of the percentage of emotion words of that
emotion_type
References
Plutchik, R. (1962). The emotions: Facts and theories, and a new
model. Random House studies in psychology. Random House.
Plutchik, R. (2001). The nature of emotions: Human emotions have deep
evolutionary roots, a fact that may explain their complexity and provide tools
for clinical practice. American Scientist , 89 (4), 344-350.
See Also
Other emotion functions:
emotion_by()
Examples
mytext <- c(
"I am not afraid of you",
NA,
"",
"I love it [not really]",
"I'm not angry with you",
"I hate it when you lie to me. It's so humiliating",
"I'm not happpy anymore. It's time to end it",
"She's a darn good friend to me",
"I went to the terrible store",
"There is hate and love in each of us",
"I'm no longer angry! I'm really experiencing peace but not true joy.",
paste("Out of the night that covers me, Black as the Pit from pole to",
"pole, I thank whatever gods may be For my unconquerable soul."
),
paste("In the fell clutch of circumstance I have not winced nor cried",
"aloud. Under the bludgeonings of chance My head is bloody, but unbowed."
),
paste("Beyond this place of wrath and tears Looms but the Horror of the",
"shade, And yet the menace of the years Finds, and shall find, me unafraid."
),
paste("It matters not how strait the gate, How charged with punishments",
"the scroll, I am the master of my fate: I am the captain of my soul."
)
)
## works on a character vector but not the preferred method avoiding the
## repeated cost of doing sentence boundary disambiguation every time
## `emotion` is run
emotion(mytext)
## preferred method avoiding paying the cost
split_text <- get_sentences(mytext)
(emo <- emotion(split_text))
emotion(split_text, drop.unused.emotions = TRUE)
## Not run:
plot(emo)
plot(emo, drop.unused.emotions = FALSE)
plot(emo, facet = FALSE)
plot(emo, facet = 'negated')
library(data.table)
fear <- emo[
emotion_type == 'fear', ][,
text := unlist(split_text)][]
fear[emotion > 0,]
brady <- get_sentences(crowdflower_deflategate)
brady_emotion <- emotion(brady)
brady_emotion
## End(Not run)