R twitteR 패키지의 초보자 가이드를 따르고 있으며로드 블록을 실행했습니다. (자습서 URL : https://sites.google.com/site/miningtwitter/questions/sentiment/analysis)* twitteR, plyr, stringr 및 RMySQL 패키지와 함께 R을 사용하여 트윗의 센티멘트 분석
섹션 4에서 자세히 설명한 searchTwitter 함수를 통해 트윗 목록을 가져 오는 대신 MySQL 데이터베이스에서 트윗 데이터 프레임을 가져옵니다. 임 MySQL의 벌금에서 트윗을 가져올 수 있습니다,하지만 난 실행하려고하면
wine_txt = sapply (wine_tweets, 기능 (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())
나는 gettext에 (x)는 여전히 행운 :(위해 X $ gettext에()를 스왑. –
에게 dataframe 입력에 튜토리얼을 적용하는 방법에 대한 다른 지침을? –
을 당신이 그것을 바꿀 때 오류 메시지가 무엇입니까? –