if (!require("sentimentr"))install.packages('sentimentr')
## Loading required package: sentimentr
if (!require("dplyr"))install.packages('dplyr')
## Loading required package: dplyr
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(sentimentr)
library(dplyr)

Get sentiment of the sentence using function sentiment() to understand polarity of text(if it is a positive or negative)

text<- "I am enjoying my coffee. I think it's a perfect day today."

sentiment(text)
sentiment_by(text)
emotion(text)

Get aggregate of sentiment usign sentiment_by() function to understand the total polarity of text

stext<- "I am feeling terrible. Why does this happen to only me?"

sentiment(stext)
sentiment_by(stext)
emotion(stext)

Get emotion of the sentence using function emotion() to understand how the writer/author of sentence feels based on text

stext2<- "I am feeling terrible. Hope it gets better tomorrow."
sentiment(stext2)
sentiment_by(stext2)
emotion(stext2)

Detect profanity if text using function profanity()

ptext<- "The movie is just ridiculous!. I think it's shitty writing."
sentiment(ptext)
profanity(ptext)

Get Sentences from a given text as list using get_sentences() method

sent <- get_sentences(text)
sent
## [[1]]
## [1] "I am enjoying my coffee."          "I think it's a perfect day today."
## 
## attr(,"class")
## [1] "get_sentences"           "get_sentences_character"
## [3] "list"
  1. How many kinds of lexicons does ‘sentimentr’ package have? How are they different from each other?


nrc
bing
afinn

Difference in Lexicons
Difference in Lexicons
  1. Consider the ‘nrc’ package of ‘sentimentr’ package and display the count of the number of words of each kind of sentiment.
library(tidytext)
if(!require(textdata))install.packages('textdata')
## Loading required package: textdata
words_nrc<-get_sentiments("nrc")
words_nrc
words_nrc %>% group_by(sentiment)%>%
  count()

10.c Apply the ‘sentiment’function to ‘I am not very successful. He is successful’ and displays the result.

c= "I am not very successful. He is successful"
sentiment(c)
  1. What’s the use of ‘extract_sentiment_terms’ function? Consider the following sentence and apply this function. ‘My life has become terrible since I met you and lost money. But I still have got a little hope left in me’.

extract_sentiment_terms

d <- "My life has become terrible since I met you and lost money. But I still have got a little hope left in me"

sentences_d <- get_sentences(d)
sentences_d
## [[1]]
## [1] "My life has become terrible since I met you and lost money."
## [2] "But I still have got a little hope left in me"              
## 
## attr(,"class")
## [1] "get_sentences"           "get_sentences_character"
## [3] "list"
pol_d <-extract_sentiment_terms(sentences_d)
pol_d
#pol$sentence
#pol$neutral
#pol$positive
#pol$negative

data.table::as.data.table(pol_d)
sentiment_counts_d<-attributes(pol_d)$counts
sentiment_counts_d
sentiment_elements_d<-attributes(pol_d)$elements
sentiment_elements_d

Build a word cloud after fetching the positive words using extract_sentiment_terms()

if(!require(data.table))install.packages('data.table')
## Loading required package: data.table
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
library(wordcloud)
## Loading required package: RColorBrewer
library(data.table)

sentiment_counts_d[polarity > 0,]
par(mfrow = c(1, 3), mar = c(0, 0, 0, 0))
## Positive Words
with(
    sentiment_counts_d[polarity > 0,],
    wordcloud(words = words, freq = n, min.freq = 1,
          max.words = 200, random.order = FALSE, rot.per = 0.35,
          colors = brewer.pal(8, "Dark2"), scale = c(3, .75)
    )
)
mtext("Positive Words", side = 4, padj = 5)

## Negative Words
sentiment_counts_d[polarity < 0,]
with(
    sentiment_counts_d[polarity < 0,],
    wordcloud(words = words, freq = n, min.freq = 1,
          max.words = 200, random.order = FALSE, rot.per = 0.35,
          colors = brewer.pal(8, "Dark2"), scale = c(3, .75)
    )
)
mtext("Negative Words", side = 2, padj = 5)

## Neutral Words
sentiment_counts_d[polarity == 0,]
with(
    sentiment_counts_d[polarity == 0,],
    wordcloud(words = words, freq = n, min.freq = 1,
          max.words = 200, random.order = FALSE, rot.per = 0.35,
          colors = brewer.pal(8, "Dark2"), scale = c(3, .75)
    )
)
mtext("Neutral Words", side = 2, padj = 5)

  1. Consider the following sentences and highlight the positive and negative sentences in different colors. Also, plot a graph displaying the variation of sentiments of the given sentences. ‘Rekha is an obedient daughter’,’I have lost hope in my life’, the fan has stopped moving’,’Riya looks beautiful’.
e<-"Rekha is an obedient daughter.I have lost hope in my life. the fan has stopped moving.Riya looks beautiful"
sentences_e <-get_sentences(e)
sentences_e
## [[1]]
## [1] "Rekha is an obedient daughter." "I have lost hope in my life."  
## [3] "the fan has stopped moving."    "Riya looks beautiful"          
## 
## attr(,"class")
## [1] "get_sentences"           "get_sentences_character"
## [3] "list"

Sentiment of 4 sentences are as follows

if(!require('dplyr'))install.packages('dplyr')
library(dplyr)
sentiment_e<-sentiment(sentences_e)
sentiment_e
sentiment_e_tbl<-data.table::as.data.table(sentiment_e)

Extracting sentiment terms for a given a list of sentences.

pol_e <-extract_sentiment_terms(sentences_e)
pol_e
sentiment_counts_e<-attributes(pol_e)$counts
sentiment_elements_e<-attributes(pol_e)$elements
pol_e_tbl<-data.table::as.data.table(sentiment_elements_e)
pol_e_tbl

Positive and Negative list of sentences filtered

positive_sen <-sentiment_e_tbl %>%filter(sentiment>0) 
negative_sen <-sentiment_e_tbl %>%filter(sentiment<0) 

Highlighted Sentences Based on Sentiment

if(!require('ggplot2'))install.packages('ggplot2')
## Loading required package: ggplot2
library(ggplot2)

sentences_e_tbl <-data.table::as.data.table(sentences_e)
sentences_e_tbl %>%
  get_sentences()%>%
  sentiment_by()%>%
  highlight()
## Saved in /var/folders/q1/tpbkm_bj6dv12y3kmgdzq8280000gn/T//Rtmp5IoVyf/polarity.html
## Opening /var/folders/q1/tpbkm_bj6dv12y3kmgdzq8280000gn/T//Rtmp5IoVyf/polarity.html ...

highlight() function generates below image on a browser and I have saved that image for reference purpose only.


Highlighted Sentences Based on Sentiment

Plot the sentiment analysis

The plot method for the class sentiment uses syuzhet’s get_transformed_values combined with ggplot2 to make a reasonable, smoothed plot for the duration of the text based on percentage, allowing for comparison between plots of different texts. This plot gives the overall shape of the text’s sentiment.

sentiment_counts_e
(out <- with(
    sentences_e_tbl, 
    sentiment_by(
        get_sentences(sentences_e_tbl) 
    )
))
plot(uncombine(out))

sentences_e_tbl
library(ggplot2)
if(!require('tidytext'))install.packages('tidytext')
if(!require('tidyr'))install.packages('tidyr')
## Loading required package: tidyr
library(tidytext)
library(tidyr)
bigram <- sentences_e_tbl %>%
  unnest_tokens(bigram, V1, token = "ngrams", n = 2) %>%
  filter(!is.na(bigram))

trigram <- sentences_e_tbl %>%
  unnest_tokens(trigram, V1, token = "ngrams", n = 3) %>%
  filter(!is.na(trigram))

bigrams_separated <- bigram %>%
  separate(bigram, c("word1", "word2"), sep = " ")
bigrams_separated
AFINN <- get_sentiments("afinn")
bigram_w <- bigrams_separated %>%
  filter(word1 != "not") %>%
  inner_join(AFINN, by = c(word2 = "word")) %>%
  count(word2, value, sort = TRUE)

bigram_w %>%
  mutate(contribution = n * value) %>%
  arrange(desc(abs(contribution))) %>%
  head(20) %>%
  mutate(word2 = reorder(word2, contribution)) %>%
  ggplot(aes(n * value, word2, fill = n * value > 0)) +
  geom_col(show.legend = FALSE) +
  labs(x = "Sentiment value * number of occurrences",
       y = "Words preceded by \"not\"")

bigrams_united <- bigrams_separated %>%
  unite(bigram, word1, word2, sep = " ")
bigrams_united
if(!require('igraph'))install.packages('igraph')
## Loading required package: igraph
## 
## Attaching package: 'igraph'
## The following object is masked from 'package:tidyr':
## 
##     crossing
## The following objects are masked from 'package:dplyr':
## 
##     as_data_frame, groups, union
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
library(igraph)
bigram_counts <- bigrams_separated %>% 
  count(word1, word2, sort = TRUE) 
bigram_counts
bigram_graph <- bigram_counts %>%
  graph_from_data_frame()
bigram_graph
## IGRAPH b0f7236 DN-- 20 16 -- 
## + attr: name (v/c), n (e/n)
## + edges from b0f7236 (vertex names):
##  [1] an      ->obedient  fan     ->has       has     ->stopped  
##  [4] have    ->lost      hope    ->in        i       ->have     
##  [7] in      ->my        is      ->an        looks   ->beautiful
## [10] lost    ->hope      my      ->life      obedient->daughter 
## [13] rekha   ->is        riya    ->looks     stopped ->moving   
## [16] the     ->fan
if(!require('ggraph'))install.packages('ggraph')
## Loading required package: ggraph
library(ggraph)
set.seed(222)

ggraph(bigram_graph, layout = "kk") +
  geom_edge_link() +
  geom_node_point() +
  geom_node_text(aes(label = name), vjust = 1, hjust = 0.5)

set.seed(2020)

a <- grid::arrow(type = "closed", length = unit(.15, "inches"))


ggraph(bigram_graph, layout = "kk") +
  geom_edge_link(aes(edge_alpha = n), show.legend = FALSE,
                 arrow = a, end_cap = circle(.07, 'inches')) +
  geom_node_point(color = "lightblue", size = 5) +
  geom_node_text(aes(label = name), vjust = 1.5, hjust = 0.5) +
  theme_void()