0

I am using R to extract tweets and analyse their sentiment, however when I get to the lines below I get an error saying "Object of type 'closure' is not subsettable"

scores$drink = factor(rep(c("east"), nd))
scores$very.pos = as.numeric(scores$score >= 2)
scores$very.neg = as.numeric(scores$score <= -2)

Full code pasted below

load("twitCred.Rdata")
east_tweets <- filterStream("tweetselnd.json", locations = c(-0.10444, 51.408699, 0.33403, 51.64661),timeout = 120, oauth = twitCred)
tweets.df <- parseTweets("tweetselnd.json", verbose = FALSE)

 ##function score.sentiment

score.sentiment = function(sentences, pos.words, neg.words, .progress='none')
{
  # Parameters
  # sentences: vector of text to score
  # pos.words: vector of words of postive sentiment
  # neg.words: vector of words of negative sentiment
  # .progress: passed to laply() to control of progress bar

  scores = laply(sentences,
                 function(sentence, pos.words, neg.words)
                 {
                   # remove punctuation
                   sentence = gsub("[[:punct:]]", "", sentence)
                   # remove control characters
                   sentence = gsub("[[:cntrl:]]", "", sentence)
                   # remove digits?
                   sentence = gsub('\\d+', '', sentence)

                   # define error handling function when trying tolower
                   tryTolower = function(x)
                   {
                     # create missing value
                     y = NA
                     # tryCatch error
                     try_error = tryCatch(tolower(x), error=function(e) e)
                     # if not an error
                     if (!inherits(try_error, "error"))
                       y = tolower(x)
                     # result
                     return(y)
                   }
                   # use tryTolower with sapply 
                   sentence = sapply(sentence, tryTolower)

                   # split sentence into words with str_split (stringr package)
                   word.list = str_split(sentence, "\\s+")
                   words = unlist(word.list)

                   # compare words to the dictionaries of positive & negative terms
                   pos.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/FALSE
                   pos.matches = !is.na(pos.matches)
                   neg.matches = !is.na(neg.matches)

                   # final score
                   score = sum(pos.matches) - sum(neg.matches)
                   return(score)
                 }, pos.words, neg.words, .progress=.progress )

  # data frame with scores for each sentence
  scores.df = data.frame(text=sentences, score=scores)
  return(scores.df)
}

pos = readLines(file.choose())
neg = readLines(file.choose())

east_text = sapply(east_tweets, function(x) x$getText())

scores = score.sentiment(tweetseldn.json, pos, neg, .progress='text')

scores()$drink = factor(rep(c("east"), nd))
scores()$very.pos = as.numeric(scores()$score >= 2)
scores$very.neg = as.numeric(scores$score <= -2)

# how many very positives and very negatives
numpos = sum(scores$very.pos)
numneg = sum(scores$very.neg)

# global score
global_score = round( 100 * numpos / (numpos + numneg) )

If anyone could help with as to why I'm getting this error it will be much appreciated. Also I've seen other answeres about adding '()' when referring to the variable 'scores' such as scores()$.... but it hasn't worked for me. Thank you.

m mahmood
  • 1
  • 1
  • If you post the full error, there's a chance we can guess where it's coming from. If not, maybe review http://stackoverflow.com/q/11308367 – Frank Apr 14 '16 at 16:48
  • FULL ERROR : Error in scores$drink = factor(rep(c("east"), nd)) : object of type 'closure' is not subsettable – m mahmood Apr 14 '16 at 16:50
  • 1
    I thought the same thing but when I try I get the following error: Error in scores()$drink = factor(rep(c("east_text"), nd)) : invalid (NULL) left side of assignment – m mahmood Apr 14 '16 at 16:52
  • Oh, I see now. Maybe assign the result of scores, like `x <- scores()` and thereafter assign like `x$drink <- stuff`. – Frank Apr 14 '16 at 16:53
  • after trying to run x <- scores() I came across this error: Error in inherits(.data, "split") : argument "sentences" is missing, with no default , perhaps it's a simple thing that I'm completely missing. – m mahmood Apr 14 '16 at 16:56
  • Hm, it's nothing simple/obvious to me. I suggest trying to run smaller pieces of your code until you find the bug. If you run into a problem you can't solve in one of those steps, then post a small piece of code that reproduces the problem as a new question, as described here: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/28481250#28481250 – Frank Apr 14 '16 at 17:00

1 Answers1

0

The changes below got rid of the error:

x <- scores
x$drink = factor(rep(c("east"), nd))
x$very.pos = as.numeric(x$score >= 2)
x$very.neg = as.numeric(x$score <= -2)
m mahmood
  • 1
  • 1