Create a word cloud to show what users talk about Zynga on Twitter.

library('tm')
## Loading required package: NLP
library('RColorBrewer')
library('wordcloud')
zynga<-readRDS("C:\\Users\\Bacchus\\Desktop\\2018 spring\\special topic for CS\\midterm test\\Zynga.RDS")
zynga_tweets<-zynga$text
clean.text = function(x)
{
  x = gsub("[^[:graph:]]", " ",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)
}
zynga_tweets = clean.text(zynga_tweets)
corpus = Corpus(VectorSource(zynga_tweets))
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) 
word_freqs = word_freqs[-(1:4)]
dm = data.frame(word=names(word_freqs), freq=word_freqs)
wordcloud(head(dm$word, 50), head(dm$freq, 50), scale=c(2, .9), random.order=FALSE, colors=brewer.pal(8, "Dark2"))

Create a histogram to show the distribution of Zynga users’ sentiment.

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)
## Loading required package: plyr
require(stringr)
## Loading required package: stringr
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(zynga_tweets, pos.words, neg.words, .progress='none')

score <- sentiment.scores$score

library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'ggplot2'
## The following object is masked from 'package:NLP':
## 
##     annotate
## 
## 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

Create a histogram to show Zynga users’ involvement on each weekday

zynga$days <- weekdays(as.POSIXlt(zynga$created))
day <- zynga$days
library(plotly)
p <- plot_ly(x = ~day, type = "histogram")
p

question 4: From the histogram which show the distribution of Zynga users’ sentiment. We can find that most users’ sentimental source just 0. It means that most users don’t pay attention on Zynga. They don’t care about Zynga is good or bad. I think company can use The “predictive practitioner.” stategy to collect users’ oppion about the Zynga.The Developer also can talk with users to get the idea to improve their product.

From the histogram of the Zynga users’ involvement on each weekday, we can find that on Tuesday, Wendnesday, and Thursday , users are unactive.So for increasing monthly active users. Zynga should hold the activities or events on these days. By using The “social media champion.” stategy, for example, Zynga can support some twitch liver to use Zygna on these days to attract users use Zygna on these days.