library(readr)
gear <- read.csv("C:\\Users\\xiayangxiao\\Downloads\\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\\xiayangxiao\\Desktop\\health.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
topic.mentioned = subset(topic.scores, score !=0)
N= nrow(topic.scores)
Nmentioned = nrow(topic.mentioned)
dftemp=data.frame(topic=c("Mentioned", "Not Mentioned"),
number=c(Nmentioned,N-Nmentioned))
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(data=dftemp, labels = ~topic, values = ~number, type = 'pie') %>%
layout(title = 'Pie Chart of Tweets Mentioning Sports',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
p
wordLengths=c(0,Inf)
require(tm)
## Loading required package: tm
## Loading required package: NLP
##
## Attaching package: 'NLP'
## The following object is masked from 'package:ggplot2':
##
## annotate
require(wordcloud)
## Loading required package: wordcloud
## Loading required package: RColorBrewer
require(RColorBrewer)
sportsTweets = subset(topic.scores, score !=0)$text
corpus = Corpus(VectorSource(sportsTweets))
tdm = TermDocumentMatrix(
corpus,
control = list(
wordLengths=c(3,20),
removePunctuation = TRUE,
stopwords = c("the", "a", stopwords("english")),
removeNumbers = TRUE, tolower = TRUE) )
tdm = as.matrix(tdm)
word_freqs = sort(rowSums(tdm), decreasing=TRUE)
dm = data.frame(word=names(word_freqs), freq=word_freqs)
wordcloud(head(dm$word, 50), head(dm$freq, 50), random.order=FALSE, colors=brewer.pal(8, "Dark2"))

head(word_freqs, 20)
## watch apple pebble samsung new iwatch fitbit force
## 3285 2856 2695 2679 2615 2559 2394 2173
## smawatch galaxy sma toq sony qualcomm win just
## 2103 1349 1093 1089 1031 1028 863 843
## fitness iphone ipad next
## 803 768 725 672