trinker / sentimentr

Dictionary based sentiment analysis that considers valence shifters
Other
426 stars 84 forks source link

Changing polarity of words in the dictionary #120

Closed GabriellaS-K closed 4 years ago

GabriellaS-K commented 4 years ago

Hi,

Thanks for an excellent package! How can I get a list of all the words in the dictionary that sentimentr uses, so that I can update/edit it? In my dataset I have 'tender' but in this context it refers to tender-pain, and thus is negative, so I need to update the dictionary used.

Many thanks

trinker commented 4 years ago

The lexicon is lexicon::hash_sentiment_jockers_rinker and the tool to change it is update_key(). Because 'tender' already exists in the dictionary you must drop it first as I show below:

library(sentimentr)
library(data.table)

my_key <- sentimentr::update_key(
    lexicon::hash_sentiment_jockers_rinker[x != 'tender'], 
    x = data.frame(x = c("tender"), 
    y = c(-1))
)

lexicon::hash_sentiment_jockers_rinker['tender']
my_key['tender']

No pass my_key into the polarity_dt argument in sentiment():

sentimentr::sentiment(x)

##    element_id sentence_id word_count sentiment
## 1:          1           1          6 0.3061862

sentimentr::sentiment(x, polarity_dt = my_key)

##    element_id sentence_id word_count  sentiment
## 1:          1           1          6 -0.4082483
GabriellaS-K commented 4 years ago

That is brilliant, thanks so much! can I ask how you assigned the specific polarity of 'tender'?