I have a column made up of 741 reviews. I would like to get other two columns with a value which indicate the number of positive and negative words for each review.
can someone of you help me?
I tried to use this function but it gives me a score by subtracting negative and positive words for each review. I would like to get 2 columns containing just the number of positive and negative words.
score.sentiment = function(sentence, pos.words, neg.words, .progress=’none’)
{
require(plyr)
require(stringr)
scores = laply(sentences, function(sentence, pos.words, neg.words) {
sentence = gsub(‘[[:punct:]]’, ‘’, sentence)
sentence = gsub(‘[[:cntrl:]]’, ‘’, sentence)
sentence = gsub(‘\\d+’, ‘’, sentence)
sentence = tolower(sentence)
word.list = str_split(sentence, ‘\\s+’)
words = unlist(word.list)
pos.matches = match(words, pos.words)
neg.matches = match(words, neg.words)
pos.matches = !is.na(pos.matches)
neg.matches = !is.na(neg.matches)
score = sum(pos.matches) — sum(neg.matches)
return(score)
}, pos.words, neg.words, .progress=.progress )
scores.df = data.frame(score=scores, text=sentence)
return(scores.df)
}