Skip to content

Instantly share code, notes, and snippets.

@almogsi
Last active October 3, 2017 11:49
Show Gist options
  • Select an option

  • Save almogsi/8ad7ad8c677989a38663b0189ffaf4d0 to your computer and use it in GitHub Desktop.

Select an option

Save almogsi/8ad7ad8c677989a38663b0189ffaf4d0 to your computer and use it in GitHub Desktop.
NoVAD is the Norms of Valence, Arousal and Dominance (Warriner, Kuperman & Brysbaert , 2013). The function takes a string as an argument and returns the mean value of valence, arousal and dominance.
novad <- read.csv("Download_NoVAD_csv.csv") #Change for you local csv file
novad$Word <- as.character(novad$Word)
library(stringr)
library(tm)
NoVAD <- function(a){
a <- as.character(a)
sms_corpus <- Corpus(VectorSource(a))
#translate all letters to lower case
clean_corpus <- tm_map(sms_corpus, tolower)
# remove numbers
clean_corpus <- tm_map(clean_corpus, removeNumbers)
# remove punctuation
clean_corpus <- tm_map(clean_corpus, removePunctuation)
clean_corpus <- tm_map(clean_corpus, stripWhitespace)
b <- strsplit(clean_corpus[[1]]$content, " ")
b <- unlist(b)
sums_c <- double(length(b))
sums_d <- double(length(b))
sums_e <- double(length(b))
for (i in 1:length(b)){
M_str <- str_detect(b[i], novad$Word)==T
M_char <- nchar(b[i])==nchar(novad$Word)
c <- sum(novad$V.Mean.Sum[M_char*M_str==T]) #valence
d <- sum(novad$A.Mean.Sum[M_char*M_str==T]) #arousal
e <- sum(novad$D.Mean.Sum[M_char*M_str==T]) #dominance
sums_c[i] <- c
sums_d[i] <- d
sums_e[i] <- e
}
valence <- sum(sums_c)/length(sums_c[sums_c>0])
arousal <- sum(sums_d)/length(sums_d[sums_d>0])
dominance <- sum(sums_e)/length(sums_e[sums_e>0])
return(c(valence, arousal, dominance))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment