R Datei verarbeitet wird:Ich mache eine Analyse twitter Stimmung und nur einer der meine zieht von 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 Console:
> 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')
anzeigen Traceback
Rerun mit Debug Fehler in erbt (.data, "split"): Objekt 'americanair.text' nicht gefunden> Southwestair.scores = score.sentiment (Southwestair.text, pos.words, + neg.words, .progress = 'text')
anzeigen Traceback
Rerun mit Debug Fehler in inherits (.data "split"): Objekt 'Southwestair.text' nicht gefunden> vereint. Partituren = score.sentiment (united.text, pos.words, + neg.words, .progress = 'text') anzeigen Traceback
Rerun mit Debug Fehler in inherits (.data "split"): Objekt 'united.text' nicht gefunden> USAirways.scores = score.sentiment (USAirways.text, pos.words, + neg.words, .progress = 'text') Zeige Traceback
Rerun mit Debug Fehlern in inherits (.data "split"): Objekt 'USAirways.text' nicht
gefunden
Der positive/Negativ- Meinungslexika können hier heruntergeladen werden: https://www.cs.uic.edu/~liub/FBS/sentiment-analysis.html –