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)
health.words = scan('C:\\Users\\Bacchus\\Desktop\\2018 spring\\special topic for CS\\week 4\\health_topic.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, health.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
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)

healthTweets = subset(topic.scores, score !=0)$text

corpus = Corpus(VectorSource(healthTweets))
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)
##       first       watch      pebble    smawatch     waiting     samsung 
##         380         182         159         154         128         124 
##         toq       wrist mydreamhome       dayin        tree         win 
##          99          96          86          84          84          84 
##       byfor      chance    qualcomm      fitbit      iwatch impressions 
##          82          82          79          45          44          44 
##         new       apple 
##          39          39