Created
February 24, 2013 23:30
-
-
Save glesica/5026233 to your computer and use it in GitHub Desktop.
A really simple sentiment analysis function implemented in R that illustrates some of the nifty features of the R language.
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
sentiment <- function(str, words, weights=NULL) { | |
if (is.null(weights)) { | |
weights <- rep(1, length(words)) | |
} | |
if (length(words) != length(weights)) { | |
stop('Length of words and weights not equal') | |
} | |
str.wts <- rep(0, length(str)) | |
for (i in 1:length(words)) { | |
cts <- str_count(str, words[[i]]) | |
str.wts <- str.wts + (cts * weights[[i]]) | |
} | |
str.wts | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment