Skip to content

Instantly share code, notes, and snippets.

@trinker
Created December 3, 2016 02:50
Show Gist options
  • Save trinker/9ff4f516873fb3dd620a7a81386f961c to your computer and use it in GitHub Desktop.
Save trinker/9ff4f516873fb3dd620a7a81386f961c to your computer and use it in GitHub Desktop.
Sentiment Scoring
if (!require("pacman")) install.packages("pacman")
pacman::p_load_gh("trinker/stansent", 'wrathematics/meanr')
pacman::p_load(syuzhet, qdap, sentimentr, microbenchmark, RSentiment)
ase <- c(
"I haven't been sad in a long time.",
"I am extremely happy today.",
"It's a good day.",
"But suddenly I'm only a little bit happy.",
"Then I'm not happy at all.",
"In fact, I am now the least happy person on the planet.",
"There is no happiness left in me.",
"Wait, it's returned!",
"I don't feel so bad after all!"
)
syuzhet <- setNames(as.data.frame(lapply(c("bing", "afinn", "nrc"),
function(x) get_sentiment(ase, method=x))), c("bing", "afinn", "nrc"))
left_just(data.frame(
stanford = sentiment_stanford(ase)[["sentiment"]],
hu_liu = round(sentiment(ase, question.weight = 0)[["sentiment"]], 2),
sentiword = round(sentiment(ase, sentiword, question.weight = 0)[["sentiment"]], 2),
RSentiment = calculate_score(ase),
meanr = score(ase)$score,
syuzhet,
sentences = ase,
stringsAsFactors = FALSE
), "sentences")
## stanford hu_liu sentiword RSentiment meanr bing afinn nrc sentences
## 1 -0.5 0.35 0.18 -1 -1 -1 -2 0 I haven't been sad in a long time.
## 2 1 0.8 0.65 1 1 1 3 1 I am extremely happy today.
## 3 0.5 0.5 0.32 1 1 1 3 1 It's a good day.
## 4 -0.5 0 0 0 1 1 3 1 But suddenly I'm only a little bit happy.
## 5 -0.5 -0.41 -0.56 -1 1 1 3 1 Then I'm not happy at all.
## 6 -0.5 0.06 0.11 1 1 1 3 1 In fact, I am now the least happy person on the planet.
## 7 -0.5 -0.38 -0.05 -1 1 1 2 1 There is no happiness left in me.
## 8 0 0 -0.14 0 0 0 0 -1 Wait, it's returned!
## 9 -0.5 0.38 0.24 -1 -1 -1 -3 -1 I don't feel so bad after all!
ase_100 <- rep(ase, 100)
stanford <- function() {sentiment_stanford(ase_100)}
sentimentr_hu_liu <- function() sentiment(ase_100)
sentimentr_sentiword <- function() sentiment(ase_100, sentiword)
RSentiment <- function() calculate_score(ase_100)
syuzhet_binn <- function() get_sentiment(ase_100, method="bing")
syuzhet_nrc <- function() get_sentiment(ase_100, method="nrc")
syuzhet_afinn <- function() get_sentiment(ase_100, method="afinn")
meanr <- function() score(ase_100)$score
microbenchmark(
stanford(),
sentimentr_hu_liu(),
sentimentr_sentiword(),
RSentiment(),
meanr(),
syuzhet_binn(),
syuzhet_nrc(),
syuzhet_afinn(),
times = 5
)
## Unit: milliseconds
## expr min lq mean median uq max neval cld
## stanford() 26049.630509 26582.16834 28061.970376 26608.887503 27728.346567 33340.818958 5 b
## sentimentr_hu_liu() 236.048823 272.29553 274.037475 272.853871 286.759067 302.230087 5 a
## sentimentr_sentiword() 959.647424 1024.73664 1047.736414 1051.780539 1057.367660 1145.149809 5 a
## RSentiment() 694.730641 699.80786 708.982585 705.759142 718.520967 726.094312 5 a
## meanr() 1.356034 1.63808 1.717397 1.835142 1.874143 1.883586 5 a
## syuzhet_binn() 391.055432 394.30121 402.412862 395.948732 396.496400 434.262537 5 a
## syuzhet_nrc() 897.140547 925.76382 948.401006 943.427158 965.656997 1010.016505 5 a
## syuzhet_afinn() 166.468647 169.91313 173.710105 171.194853 177.713094 183.260804 5 a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment