Clean data
library(readr)
## Warning: package 'readr' was built under R version 3.4.3
gear <- read.csv("C:\\Users\\Xiayang Xiao\\Downloads\\Samsung Tweets(1).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)
Sentiment Analysis
pos.words = scan('C:\\Users\\Xiayang Xiao\\Downloads\\positive-words.txt', what='character', comment.char=';')
neg.words = scan('C:\\Users\\Xiayang Xiao\\Downloads\\negative-words.txt', what='character', comment.char=';')
neg.words = c(neg.words, 'wtf', 'fail')
require(plyr)
## Loading required package: plyr
## Warning: package 'plyr' was built under R version 3.4.3
require(stringr)
## Loading required package: stringr
## Warning: package 'stringr' was built under R version 3.4.3
require(stringi)
## Loading required package: 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(geartweets, pos.words, neg.words, .progress='none')
score <- sentiment.scores$score
library(plotly)
## Warning: package 'plotly' was built under R version 3.4.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.4.3
##
## 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
## Warning: package 'bindrcpp' was built under R version 3.4.3
pie chart
positive = subset(sentiment.scores, score > 0)
negtive = subset(sentiment.scores, score < 0)
neutral = subset(sentiment.scores, score = 0)
No.positive = nrow(positive)
No.negtive = nrow(negtive)
No.neutral= nrow(neutral)
dftemp=data.frame(topic=c("positive", "negtive","neutral"),
number=c(No.positive,No.negtive,No.neutral))
library(plotly)
p <- plot_ly(data=dftemp, labels = ~topic, values = ~number, type = 'pie') %>%
layout(title = 'Pie Chart of Tweets opinion',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
p