length(apple)
class(apple)
head(apple)
tweet.apple <- apple[[1]]
tweet.apple$getId()
tweet.apple$favoriteCount
tweet.apple$getScreenName() # ID확인
tweet.apple$text #내용.
apple.text <- laply(apple, function(t) t$getText())
head(apple.text)
pos.word <- scan("positive-words.txt",what="character",comment.char = ";")
neg.word <- scan("negative-words.txt",what="character",comment.char = ";")
pos.word <- c(pos.word,"upgrade")
neg.word <- c(neg.word,"wait","waiting")
score.sentiment = function(sentences, pos.words, neg.words, .progress='none')
{
require(plyr)
require(stringr)
# we got a vector of sentences. plyr will handle a list or a vector as an "l" for us
# we want a simple array ("a") of scores back, so we use
# "l" + "a" + "ply" = "laply":
scores = laply(sentences, function(sentence, pos.words, neg.words) {
# clean up sentences with R's regex-driven global substitute, gsub():
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)
# match() returns 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)
# 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)
}
apple.text <- apple.text[!Encoding(apple.text)=="UTF-8"]
apple.score <- score.sentiment(apple.text,pos.word,neg.word,.progress = 'text')
hist(apple.score$score)
'STUDY > Summer Break' 카테고리의 다른 글
4. RFacebook [ 데이터 가지고 오기 ] (0) | 2016.06.25 |
---|---|
4. RFacebook [ Api Key 가지고 오기 ] (0) | 2016.06.25 |
2. TwitterR [ R : 한글 ] (0) | 2016.06.24 |
1. TwitterR [ API Key 가지고 오기 ] (0) | 2016.06.24 |