library(readr)
gear <- read.csv("C:\\Users\\Bacchus\\Desktop\\2018 spring\\special topic for CS\\week 4\\Samsung Tweets.csv", row.names=1, sep=";")
geartweets <- gear$tweettext
clean.text = function(x)
{
x = tolower(x)
x = gsub("rt", "", x)
x = gsub("@\\w+", "", x)
x = gsub("[[:punct:]]", "", x)
x = gsub("[[:digit:]]", "", x)
x = gsub("http\\w+", "", x)
x = gsub("[ |\t]{2,}", "", x)
x = gsub("^ ", "", x)
x = gsub(" $", "", x)
return(x)
}
geartweets = clean.text(geartweets)
sports.words = scan('C:\\Users\\Bacchus\\Desktop\\2018 spring\\special topic for CS\\week 5\\Sports_Word.txt', what='character', comment.char=';')
score.topic = function(sentences, dict, .progress='none')
{
require(plyr)
require(stringr)
require(stringi)
scores = laply(sentences, function(sentence, dict) {
sentence = gsub('[[:punct:]]', '', sentence)
sentence = gsub('[[:cntrl:]]', '', sentence)
sentence = gsub('\\d+', '', sentence)
sentence = tolower(sentence)
word.list = str_split(sentence, '\\s+')
words = unlist(word.list)
topic.matches = match(words, dict)
topic.matches = !is.na(topic.matches)
score = sum(topic.matches)
return(score)
}, dict, .progress=.progress )
topicscores.df = data.frame(score=scores, text=sentences)
return(topicscores.df)
}
topic.scores= score.topic(geartweets, sports.words, .progress='none')
## Loading required package: plyr
## Loading required package: stringr
## Loading required package: stringi
sportsTweets = subset(topic.scores, score !=0)$text
pos.words = scan('C:\\Users\\Bacchus\\Desktop\\2018 spring\\special topic for CS\\week 5\\positive-words.txt', what='character', comment.char=';')
neg.words = scan('C:\\Users\\Bacchus\\Desktop\\2018 spring\\special topic for CS\\week 5\\negative-words.txt', what='character', comment.char=';')
neg.words = c(neg.words, 'wtf', 'fail')
require(plyr)
require(stringr)
require(stringi)
score.sentiment = function(sentences, pos.words, neg.words, .progress='none')
{
scores = laply(sentences, function(sentence, pos.words, neg.words) {
sentence = gsub('[[:punct:]]', '', sentence)
sentence = gsub('[[:cntrl:]]', '', sentence)
sentence = gsub('\\d+', '', sentence)
sentence = tolower(sentence)
word.list = str_split(sentence, '\\s+')
words = unlist(word.list)
pos.matches = match(words, pos.words)
neg.matches = match(words, neg.words)
pos.matches = !is.na(pos.matches)
neg.matches = !is.na(neg.matches)
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)
}
sentiment.scores= score.sentiment(sportsTweets, pos.words, neg.words, .progress='none')
score <- sentiment.scores$score
library(plotly)
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following objects are masked from 'package:plyr':
##
## arrange, mutate, rename, summarise
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
p <- plot_ly(x = ~score, type = "histogram")
p
topic.pos = subset(sentiment.scores, score > 0)
topic.neg = subset(sentiment.scores, score < 0)
topic.neu = subset(sentiment.scores, score == 0)
negN = nrow(topic.neg)
posN = nrow(topic.pos)
neuN = nrow(topic.neu)
dftemp=data.frame(topic=c("Postive tweets", "Negative tweets" , "Neutral tweets"),
number=c(posN, negN, neuN))
library(plotly)
p <- plot_ly(data=dftemp, labels = ~topic, values = ~number, type = 'pie') %>%
layout(title = 'Pie Chart of the percentage of Positive, Negative and Neutral tweets',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
p