我正在关注 R twitteR 包的初学者教程,但遇到了障碍。(教程网址:https ://sites.google.com/site/miningtwitter/questions/sentiment/analysis )
我没有通过第 4 节中详述的 searchTwitter 函数导入推文列表,而是从 MySQL 数据库中导入推文数据框。我能够很好地从 MySQL 导入推文,但是当我尝试执行时:
wine_txt = sapply(wine_tweets, function(x) x$getText())
我收到一个错误:
x$getText 中的错误:$ 运算符对原子向量无效
数据已经是 data.frame 形式,我随后再次将其强制转换为 data.frame 以确保我仍然得到相同的错误。我在下面粘贴了我的完整代码,任何帮助将不胜感激。
library(twitteR)
library(plyr)
library(stringr)
library(RMySQL)
tweets.con<-dbConnect(MySQL(),user="XXXXXXXX",password="XXXXXXXX",dbname="XXXXXXX",host="XXXXXXX")
wine_tweets<-dbGetQuery(tweets.con,"select `tweet_text` from `tweets` where `created_at` BETWEEN timestamp(DATE_SUB(NOW(), INTERVAL 11 MINUTE)) AND timestamp(NOW())")
# 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
# create simple array of scores with laply
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)
}
# import positive and negative words
pos = readLines("/home/jgraab/R/scripts/positive_words.txt")
neg = readLines("/home/jgraab/R/scripts/negative_words.txt")
wine_txt = sapply(wine_tweets, function(x) x$getText())