Last active
December 10, 2015 17:48
-
-
Save abromberg/4470287 to your computer and use it in GitHub Desktop.
This file contains 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
#function to calculate number of words in each category within a sentence | |
sentimentScore <- function(sentences, vNegTerms, negTerms, posTerms, vPosTerms){ | |
final_scores <- matrix('', 0, 5) | |
scores <- laply(sentences, function(sentence, vNegTerms, negTerms, posTerms, vPosTerms){ | |
initial_sentence <- sentence | |
#remove unnecessary characters and split up by word | |
sentence <- gsub('[[:punct:]]', '', sentence) | |
sentence <- gsub('[[:cntrl:]]', '', sentence) | |
sentence <- gsub('\\d+', '', sentence) | |
sentence <- tolower(sentence) | |
wordList <- str_split(sentence, '\\s+') | |
words <- unlist(wordList) | |
#build vector with matches between sentence and each category | |
vPosMatches <- match(words, vPosTerms) | |
posMaches <- match(words, posTerms) | |
vNegMatches <- match(words, vNegTerms) | |
negMatches <- match(words, negTerms) | |
#sum up number of words in each category | |
vPosMatches <- sum(!is.na(vPosMatches)) | |
posMatches <- sum(!is.na(posMatches)) | |
vNegMatches <- sum(!is.na(vNegMatches)) | |
negMatches <- sum(!is.na(negMatches)) | |
score <- c(vNegMatches, negMatches, posMatches, vPosMatches) | |
#add row to scores table | |
newrow <- c(initial_sentence, score) | |
final_scores <- rbind(final_scores, newrow) | |
return(final_scores) | |
}, vNegTerms, negTerms, posTerms, vPosTerms) | |
return(scores) | |
} | |
#build tables of positive and negative sentences with scores | |
posResult <- as.data.frame(sentimentScore(posText, vNegTerms, negTerms, posTerms, vPosTerms)) | |
negResult <- as.data.frame(sentimentScore(negText, vNegTerms, negTerms, posTerms, vPosTerms)) | |
posResult <- cbind(posResult, 'positive') | |
colnames(posResult) <- c('sentence', 'vNeg', 'neg', 'pos', 'vPos', 'sentiment') | |
negResult <- cbind(negResult, 'negative') | |
colnames(negResult) <- c('sentence', 'vNeg', 'neg', 'pos', 'vPos', 'sentiment') | |
#combine the positive and negative tables | |
results <- rbind(posResult, negResult) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment