I am attempting to use lexicon based scoring method to do some sentiment analysis on texts. I directly borrowed my code from http://analyzecore.com/2014/04/28/twitter-sentiment-analysis/ after reading the stack overflow post: R sentiment analysis with phrases in dictionaries
Here's a bit summary about my data set:
> summary(data$text)
Length Class Mode
30 character character
> str(data$text)
chr [1:30] "Hey everybody, are you guys free on Sunday for a game play + dinner afterwards? I'll reserve a"| __truncated__ ...
and the code i'm using:
require(plyr)
require(stringr)
require(data.table)
score.sentiment = function(sentences, pos.words, neg.words, .progress='none')
{
scores = laply(sentences, function(sentence, pos.words, neg.words) {
sentence = gsub('[[:punct:]]', '', sentence)
sentence = gsub('[[:cntrl:]]', '', sentence)
sentence = gsub('\\d+', '', sentence)
# and convert to lower case:
sentence = tolower(sentence)
# split into words. str_split is in the stringr package
word.list = str_split(sentence, '\\s+')
# sometimes a list() is one level of hierarchy too much
words = unlist(word.list)
# compare our words to the dictionaries of positive & negative terms
pos.matches = match(words, pos.words)
neg.matches = match(words, neg.words)
pos.matches = !is.na(pos.matches)
neg.matches = !is.na(neg.matches)
# and conveniently enough, TRUE/FALSE will be treated as 1/0 by sum():
score = (sum(pos.matches) - sum(neg.matches))
return(score)
} , pos.words, neg.words, .progress=.progress)
scores.df = data.frame(score = scores, text = sentences)
return(scores.df)
}
I am using Bing Liu's opinion dictionary, and I loaded them as:
pos_BL = read.table(file = 'positive-words.txt', stringsAsFactors = F)
neg_BL = read.table(file = 'negative-words.txt', stringsAsFactors = F)
and here's the code I used to run the data and dictionary through the scoring function:
score_result = score.sentiment(sentences = data$text,
pos.words = pos_BL,
neg.words = neg_BL,
.progress= 'text')
However, no matter what I do, I only get scores of 0 for all my 30 strings. (see below table for output summary):
> table(score_result$score)
0
30
I am out of ideas on where to fix (I did spot many errors in my own code before posting this question here). Any help is much appreciated!