Sentiment analysis R syuzhet NRC Word-Emotion Asso

2019-08-25 02:35发布

问题:

How do you find the associated words to the eight basic emotions (anger, fear, anticipation, trust, surprise, sadness, joy, and disgust) (NRC Word-Emotion Association Lexicon) when using get_nrc_sentiment of the using the syuzhet package?

a <- c("I hate going to work it is dull","I love going to work it is fun")

a_corpus = Corpus(VectorSource(a))

a_tm <- TermDocumentMatrix(a_corpus)

a_tmx <- as.matrix(a_tm)

a_df<-data.frame(text=unlist(sapply(a, `[`)), stringsAsFactors=F)

a_sent<-get_nrc_sentiment(a_df$text) 

e.g. we can see in a_sent that one term has been classified as anger, but how do we find what that term was? So I want to list all the sentiments and the terms associated in my example.

Thanks.

回答1:

library(tidytext)

library(tm)

a <- c("I hate going to work it is dull","I love going to work it is fun")

a_corpus = Corpus(VectorSource(a))

a_tm <- TermDocumentMatrix(a_corpus)

a_tmx <- as.matrix(a_tm)

a_df<-data.frame(text=unlist(sapply(a, `[`)), stringsAsFactors=F)

a_sent<-get_nrc_sentiment(a_df$text) 

lexicon <- get_sentiments("nrc")

v <- sort(rowSums(a_tmx),decreasing=TRUE)

d <- data.frame(word = names(v),freq=v)

# List the words in common between the text provided and NRC lexicon
intersect(lexicon$word, d$word)

# Show the words in common and their sentiments
s <- cbind(lexicon$word[lexicon$word%in%d$word], lexicon$sentiment[lexicon$word%in%d$word])

print(s)