R檔從Twitter的我再換一個:我做一個Twitter情感分析,只有正在處理
# Install and Activate Packages
# install.packages("twitteR", "RCurl", "RJSONIO", "stringr", "ROAuth")
library(twitteR)
library(RCurl)
library(RJSONIO)
library(stringr)
library(ROAuth)
library(plyr)
# Declare Twitter API Credentials
api_key <- "MLTS91hQlJaPOPpdDTOeNAaHU" # From dev.twitter.com
api_secret <- "bUdEdspvjOfQ192INw48K84lZulRBLgOMqu0ukr6FRyysaW95f" # From dev.twitter.com
token <- "336148817-VQgCSg9KDng71zNOpsbkFhHSPImaO1nFvJTd4HmK" # From dev.twitter.com
token_secret <- "WAGmLbICos6M2QQaWJWGyhKPKCPqyE5pVV1FPNLMg5tkI" # From dev.twitter.com
# Create Twitter Connection
setup_twitter_oauth(api_key, api_secret, token, token_secret)
delta.tweets = searchTwitter('@delta', n=1500)
#delta.tweets
americanair.tweets = searchTwitter('@americanair', n=1500)
#americanair.tweets
#JetBlueAirlines.tweets = searchTwitter('@jetblueairlines', n=1500)
SouthwestAir.tweets = searchTwitter('@SouthwestAir', n=1500)
united.tweets = searchTwitter('@united', n=1500)
USAirways.tweets = searchTwitter('@USAirways', n=1500)
#loading opinion lexicon
setwd('C:/Users/SRC/Downloads')
hu.liu.pos <- scan('positive-words.txt',
what='character', comment.char=';')
hu.liu.neg = scan('negative-words.txt',
what='character', comment.char=';')
pos.words = c(hu.liu.pos, 'upgrade')
neg.words = c(hu.liu.neg, 'wtf', 'wait',
'waiting', 'epicfail', 'mechanical')
hu.liu.pos
hu.liu.neg
#Sentiment analysis code
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 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)
}
delta.scores = score.sentiment(delta.text, pos.words,
neg.words, .progress='text')
hist(delta.scores$score)
americainair.scores = score.sentiment(americanair.text, pos.words,
neg.words, .progress='text')
Southwestair.scores = score.sentiment(Southwestair.text, pos.words,
neg.words, .progress='text')
united.scores = score.sentiment(united.text, pos.words,
neg.words, .progress='text')
USAirways.scores = score.sentiment(USAirways.text, pos.words,
neg.words, .progress='text')
RStudio控制檯:
> delta.scores = score.sentiment(delta.text, pos.words,
+ neg.words, .progress='text')
|======================================================================================================| 100%
> hist(delta.scores$score)
> americainair.scores = score.sentiment(americanair.text, pos.words,
+ neg.words, .progress='text')
顯示回溯
重新運行調試 繼承錯誤(.data,「split」):object'americanair.text'not found> Southwestair.scores = score.sentiment(Southwestair.text,pos.words, 個+ neg.words,.progress =在繼承 '文本')
顯示回溯
重新運行與調試 誤差(。數據, 「分裂」):對象 'Southwestair.text' 未找到>聯合起來。分數= score.sentiment(united.text,pos.words, + neg.words,.progress = '文本') 顯示回溯
重新運行在繼承調試 誤差(。數據, 「分裂」):對象'united.text'找不到> USAirways.scores = score.sentiment(USAirways.text,pos.words, + neg.words,.progress ='text') Show Traceback
重新運行與調試 錯誤在繼承(。數據,「分裂」):對象「USAirways.text」未找到
正/負意見詞典可以在這裏下載:https://www.cs.uic.edu/~liub/FBS/sentiment-analysis.html –