Last active
October 3, 2017 11:52
-
-
Save almogsi/ddc106abc280437a2d8290d844d2f0b2 to your computer and use it in GitHub Desktop.
ANEW is the Affective Norms for English Words (Bradley & Lang, 2009). The function takes a string as an argument and returns the mean value of valence, arousal and dominance.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ANEW function | |
| ANEW_txt <- read.table("Downloaded_ANEW_txt.txt", header = T, sep = "\t") #you should download ANEW first | |
| ANEW_txt$Word <- as.character(ANEW_txt$Word) | |
| library(stringr) | |
| library(tm) | |
| ANEW <- 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], ANEW_txt$Word)==T | |
| M_char <- nchar(b[i])==nchar(ANEW_txt$Word) | |
| c <- sum(ANEW_txt$ValMn[M_char*M_str==T]) #valence | |
| d <- sum(ANEW_txt$AroMn[M_char*M_str==T]) #arousal | |
| e <- sum(ANEW_txt$DomMn[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