extract_emotion_terms: Extract Emotion Words

Description Usage Arguments Value Examples

View source: R/extract_emotion_terms.R

Description

Extract the emotion words from a text.

Usage

1
2
3
4
5
6
7
extract_emotion_terms(
  text.var,
  emotion_dt = lexicon::hash_nrc_emotions,
  un.as.negation = TRUE,
  retention_regex = "[^[:alpha:];:,']",
  ...
)

Arguments

text.var

The text variable. Can be a get_sentences object or a raw character vector though get_sentences is preferred as it avoids the repeated cost of doing sentence boundary disambiguation every time emotion is run.

emotion_dt

A data.table with a token and emotion column (tokens are nested within the emotions. The table cannot contain any duplicate rows and must have the token column set as the key column (see ?data.table::setkey). The default emotion table is lexicon::hash_nrc_emotions.

un.as.negation

logical. If TRUE then emotion words prefixed with an 'un-' are treated as a negation. For example,"unhappy" would be treated as "not happy". If an emotion word has an un- version in the emotion_dt then no substitution is performed and an optional warning will be given.

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 \\{p} for [^[:alpha:];:,\'] may retain more alpha letters but will likely decrease speed.

...

Ignored.

Value

Returns a data.table with a columns of emotion terms.

Examples

 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
## Not run: 
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.",
      "In the fell clutch of circumstance I have not winced nor cried",
      "aloud. Under the bludgeonings of chance My head is bloody, but unbowed.",
      "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.",
      "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."
    )    
    
)

mytext2 <- get_sentences(mytext)
emotion(mytext2)

emo_words <- extract_emotion_terms(mytext2)
emo_words
emo_words$sentence
emo_words[, c('anger', 'anticipation', 'disgust', 'fear', 'joy', 'sadness', 'surprise', 'trust')]

attributes(emo_words)$counts
attributes(emo_words)$elements

## directly ona  character string (not recommended: use `get_sentences` first)
extract_emotion_terms(mytext)

brady <- get_sentences(crowdflower_deflategate)
brady_emo <- extract_emotion_terms(brady)

brady_emo
attributes(brady_emo)$counts
attributes(brady_emo)$elements

## End(Not run)

Example output

     element_id sentence_id word_count         emotion_type emotion_count
  1:          1           1          6                anger             0
  2:          1           1          6        anger_negated             0
  3:          1           1          6         anticipation             0
  4:          1           1          6 anticipation_negated             0
  5:          1           1          6              disgust             0
 ---                                                                     
300:         12           5         27             surprise             0
301:         12           5         27                trust             0
302:         12           5         27        trust_negated             1
303:         12           5         27      sadness_negated             0
304:         12           5         27     surprise_negated             0
        emotion
  1: 0.00000000
  2: 0.00000000
  3: 0.00000000
  4: 0.00000000
  5: 0.00000000
 ---           
300: 0.00000000
301: 0.00000000
302: 0.03703704
303: 0.00000000
304: 0.00000000
    element_id sentence_id               anger anticipation     disgust
 1:          1           1                                             
 2:          2           1                                             
 3:          3           1                                             
 4:          4           1                                             
 5:          5           1               angry                    angry
 6:          6           1            hate,lie                 hate,lie
 7:          6           2                                  humiliating
 8:          7           1                                             
 9:          7           2                             time            
10:          8           1                             good            
11:          9           1            terrible        store    terrible
12:         10           1                hate                     hate
13:         11           1               angry                    angry
14:         11           2                            peace            
15:         12           1                                             
16:         12           2                                             
17:         12           3              bloody                   bloody
18:         12           4 horror,menace,wrath                   horror
19:         12           5                             fate            
                   fear            joy  sadness surprise       trust
 1:              afraid                                             
 2:                                                                 
 3:                                                                 
 4:                               love                              
 5:                                                                 
 6:                hate                hate,lie                     
 7:                                                                 
 8:                                                                 
 9:                                                                 
10:                        friend,good              good friend,good
11:            terrible                terrible                     
12:                hate           love     hate                     
13:                                                                 
14:                     joy,peace,true                    peace,true
15:                                       black                     
16:                                        fell                     
17:              bloody                  bloody   chance            
18: horror,menace,wrath                  horror   horror            
19:                                                             gate
                                                                                                                                       sentence
 1:                                                                                                                      I am not afraid of you
 2:                                                                                                                                        <NA>
 3:                                                                                                                                            
 4:                                                                                                                      I love it [not really]
 5:                                                                                                                      I'm not angry with you
 6:                                                                                                               I hate it when you lie to me.
 7:                                                                                                                         It's so humiliating
 8:                                                                                                                     I'm not happpy anymore.
 9:                                                                                                                         It's time to end it
10:                                                                                                              She's a darn good friend to me
11:                                                                                                                I went to the terrible store
12:                                                                                                        There is hate and love in each of us
13:                                                                                                                        I'm no longer angry!
14:                                                                                             I'm really experiencing peace but not true joy.
15:                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.
16:                                                                       In the fell clutch of circumstance I have not winced nor cried aloud.
17:                                                                            Under the bludgeonings of chance My head is bloody, but unbowed.
18: 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.
19:       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.
 [1] "I am not afraid of you"                                                                                                                     
 [2] NA                                                                                                                                           
 [3] ""                                                                                                                                           
 [4] "I love it [not really]"                                                                                                                     
 [5] "I'm not angry with you"                                                                                                                     
 [6] "I hate it when you lie to me."                                                                                                              
 [7] "It's so humiliating"                                                                                                                        
 [8] "I'm not happpy anymore."                                                                                                                    
 [9] "It's time to end it"                                                                                                                        
[10] "She's a darn good friend to me"                                                                                                             
[11] "I went to the terrible store"                                                                                                               
[12] "There is hate and love in each of us"                                                                                                       
[13] "I'm no longer angry!"                                                                                                                       
[14] "I'm really experiencing peace but not true joy."                                                                                            
[15] "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."               
[16] "In the fell clutch of circumstance I have not winced nor cried aloud."                                                                      
[17] "Under the bludgeonings of chance My head is bloody, but unbowed."                                                                           
[18] "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."
[19] "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."      
                  anger anticipation     disgust                fear
 1:                                                           afraid
 2:                                                                 
 3:                                                                 
 4:                                                                 
 5:               angry                    angry                    
 6:            hate,lie                 hate,lie                hate
 7:                                  humiliating                    
 8:                                                                 
 9:                             time                                
10:                             good                                
11:            terrible        store    terrible            terrible
12:                hate                     hate                hate
13:               angry                    angry                    
14:                            peace                                
15:                                                                 
16:                                                                 
17:              bloody                   bloody              bloody
18: horror,menace,wrath                   horror horror,menace,wrath
19:                             fate                                
               joy  sadness surprise       trust
 1:                                             
 2:                                             
 3:                                             
 4:           love                              
 5:                                             
 6:                hate,lie                     
 7:                                             
 8:                                             
 9:                                             
10:    friend,good              good friend,good
11:                terrible                     
12:           love     hate                     
13:                                             
14: joy,peace,true                    peace,true
15:                   black                     
16:                    fell                     
17:                  bloody   chance            
18:                  horror   horror            
19:                                         gate
        words emotion_type emotion n
  1:    angry        anger       1 2
  2:    angry      disgust       1 2
  3:     hate        anger       1 2
  4:     hate      disgust       1 2
  5:     hate         fear       1 2
 ---                                
125: whatever         <NA>       0 1
126:     when         <NA>       0 1
127:   winced         <NA>       0 1
128:    years         <NA>       0 1
129:      yet         <NA>       0 1
      words element_id sentence_id emotion_type emotion
  1: afraid          1           1         fear       1
  2:  angry          5           1        anger       1
  3:  angry          5           1      disgust       1
  4:  angry         11           1        anger       1
  5:  angry         11           1      disgust       1
 ---                                                   
200:  years         12           4         <NA>       0
201:    yet         12           4         <NA>       0
202:    you          1           1         <NA>       0
203:    you          5           1         <NA>       0
204:    you          6           1         <NA>       0
    element_id sentence_id               anger anticipation     disgust
 1:          1           1                                             
 2:          2           1                                             
 3:          3           1                                             
 4:          4           1                                             
 5:          5           1               angry                    angry
 6:          6           1            hate,lie                 hate,lie
 7:          6           2                                  humiliating
 8:          7           1                                             
 9:          7           2                             time            
10:          8           1                             good            
11:          9           1            terrible        store    terrible
12:         10           1                hate                     hate
13:         11           1               angry                    angry
14:         11           2                            peace            
15:         12           1                                             
16:         12           2                                             
17:         12           3              bloody                   bloody
18:         12           4 horror,menace,wrath                   horror
19:         12           5                             fate            
                   fear            joy  sadness surprise       trust
 1:              afraid                                             
 2:                                                                 
 3:                                                                 
 4:                               love                              
 5:                                                                 
 6:                hate                hate,lie                     
 7:                                                                 
 8:                                                                 
 9:                                                                 
10:                        friend,good              good friend,good
11:            terrible                terrible                     
12:                hate           love     hate                     
13:                                                                 
14:                     joy,peace,true                    peace,true
15:                                       black                     
16:                                        fell                     
17:              bloody                  bloody   chance            
18: horror,menace,wrath                  horror   horror            
19:                                                             gate
                                                                                                                                       sentence
 1:                                                                                                                      I am not afraid of you
 2:                                                                                                                                        <NA>
 3:                                                                                                                                            
 4:                                                                                                                      I love it [not really]
 5:                                                                                                                      I'm not angry with you
 6:                                                                                                               I hate it when you lie to me.
 7:                                                                                                                         It's so humiliating
 8:                                                                                                                     I'm not happpy anymore.
 9:                                                                                                                         It's time to end it
10:                                                                                                              She's a darn good friend to me
11:                                                                                                                I went to the terrible store
12:                                                                                                        There is hate and love in each of us
13:                                                                                                                        I'm no longer angry!
14:                                                                                             I'm really experiencing peace but not true joy.
15:                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.
16:                                                                       In the fell clutch of circumstance I have not winced nor cried aloud.
17:                                                                            Under the bludgeonings of chance My head is bloody, but unbowed.
18: 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.
19:       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.
       element_id sentence_id anger anticipation disgust  fear joy sadness
    1:          1           1                                             
    2:          2           1                                             
    3:          3           1                                             
    4:          4           1                                             
    5:          5           1                                             
   ---                                                                    
21837:      21837           1                                             
21838:      21838           1                                             
21839:      21839           1                                             
21840:      21840           1                            erase            
21841:      21841           1                                             
       surprise trust
    1:               
    2:               
    3:               
    4:               
    5:               
   ---               
21837:               
21838:               
21839:               
21840:               
21841:               
                                                                                                   sentence
    1:                                         RT @DeSmogBlog: Bill Nye @TheScienceGuy: Screw #Deflategate.
    2:                                               You Should "Give a Fuck" About #ClimateChange Instead.
    3:                                                                             http://t.co/cKCXLgn9Xf _
    4: RT @CBSPittsburgh: Former Belichick Scout: /Bill has always worked to that edge, crossed that edge.'
    5:                                                                  #Deflategate http://t.co/vd9Lhjmtsy
   ---                                                                                                     
21837:      #DeflateGate If it was weather/atmosphere, would it not have also affected Colts footballs too?
21838:                                                                 #GiveUsABreak #BlatantLies #Patriots
21839:              I'll probably get slammed &amp; yes I'm a @Patriots fan, but I'm tired of #deflategate.
21840:                                                     Erase 1st half scores &amp; Pats STILL win 28-7.
21841:                                                                                             #blowout
          words emotion_type emotion   n
    1: football anticipation       1 410
    2: football          joy       1 410
    3:     time anticipation       1 316
    4:  deflate        anger       1 303
    5:  deflate      sadness       1 303
   ---                                  
24483:   zzvsvf         <NA>       0   1
24484:    zzxhj         <NA>       0   1
24485:     zzxr         <NA>       0   1
24486:    zzzzz         <NA>       0   1
24487:  zzzzzzz         <NA>       0   1
            words element_id sentence_id emotion_type emotion
     1:     abbot      15202           1        trust       1
     2: abduction      19475           1         fear       1
     3: abduction      19475           1      sadness       1
     4: abduction      19475           1     surprise       1
     5:  abortion       9058           1      disgust       1
    ---                                                      
231688:    zzvsvf      18742           1         <NA>       0
231689:     zzxhj       4530           1         <NA>       0
231690:      zzxr       5928           1         <NA>       0
231691:     zzzzz      19483           1         <NA>       0
231692:   zzzzzzz      12647           1         <NA>       0

sentimentr documentation built on Oct. 12, 2021, 9:06 a.m.