You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Name of QuantLet: 'Countword'Published in:
Description: 'Counts positive and negative words using the lexicon by Bing Liu'Keywords: 'text, nlp, tokenization, opinion mining, media news, sentiment'See also:
Author: 'Guo Li'Submitted: 'Mon, September 5 2015 by Guo Li'Datafile: 'positve-words.txt negative-words.txt example.txt'Input:
Output:
Example:
setwd("C:~")
library("methods")
a= readLines("example.txt")
neg= readLines("negative-words.txt")
pos= readLines("positive-words.txt")
# function score.sentimentscore.sentiment=function(sentence, pos.words, neg.words)
{
# remove punctuationsentence= gsub("[[:punct:]]", "", sentence)
# remove control characterssentence= gsub("[[:cntrl:]]", "", sentence)
# remove digits?sentence= gsub('\\d+', '', sentence)
# define error handling function when trying tolowertryTolower=function(x)
{
# create missing valuey=NA# tryCatch errortry_error= tryCatch(tolower(x), error=function(e) e)
# if not an errorif (!inherits(try_error, "error"))
y= tolower(x)
# resultreturn(y)
}
# use tryTolower with sapply sentence= sapply(sentence, tryTolower)
# split sentence into words with str_split (stringr package)word.list= strsplit(sentence, "\\s+")
words= unlist(word.list)
# compare words to the dictionaries of positive & negative termspos.matches= match(words, pos.words)
neg.matches= match(words, neg.words)
# get the position of the matched term or NA# we just want a TRUE/FALSEpos.matches=!is.na(pos.matches)
neg.matches=!is.na(neg.matches)
# final scorescore= sum(pos.matches) - sum(neg.matches)
pos= sum(pos.matches)
neg= sum(neg.matches)
num= length(pos.matches)
sent=matrix(c(pos,neg,score,num),nrow=1)
scores.df=data.frame(score=sent)
names(scores.df) = c("pos","neg","opt","totalwords")
scores.df
}
#sent = paste(rep(sentences,3),collapse="")
score.sentiment(a, pos, neg)