UML_Final_Project

Author

Saisheel Gopularam

Published

April 30, 2024

suppressMessages({
  suppressPackageStartupMessages({
    library(tidyverse)
    library(tidytext) 
    library(gutenbergr)
    library(glmnet)
    library(textmineR)
    library(proxy)
    library(igraph)
    library(quanteda)
    library(igraph)
    library(ggraph)
    library(topicmodels)
    library(text2vec)
    library(stm)
    library(tm)
    library(readtext)
    library(topicmodels)
    library(reshape2)
    library(wordcloud2)
    library(wordcloud)
    library(useful)
    library(SnowballC)
    library(widyr)
    library(datawizard)
    library(textclean)
  })
})
Warning in .recacheSubclasses(def@className, def, env): undefined subclass
"ndiMatrix" of class "replValueSp"; definition not updated
poe=gutenberg_download(932)
Determining mirror for Project Gutenberg from https://www.gutenberg.org/robot/harvest
Using mirror http://aleph.gutenberg.org
doyle=gutenberg_download(2345)

mypoe=poe|> as.data.frame()|> 
  dplyr::mutate(Author="Poe",
                page=1:nrow(poe))|> dplyr::select(-gutenberg_id)

head(mypoe)
                                 text Author page
1      The Fall of the House of Usher    Poe    1
2                                        Poe    2
3                                        Poe    3
4     Son coeur est un luth suspendu;    Poe    4
5   Sitot qu'on le touche il resonne.    Poe    5
6                        DE BERANGER.    Poe    6
mydoyle=doyle|> as.data.frame()|>
  dplyr::mutate(Author="Doyle",
                page=1:nrow(doyle)) |> dplyr::select(-gutenberg_id)
poe=gutenberg_download(932)
doyle=gutenberg_download(2345)

mypoe=poe|> as.data.frame()|> 
  dplyr::mutate(Author="Poe",
                page=1:nrow(poe))|> dplyr::select(-gutenberg_id)
head(mypoe)
                                 text Author page
1      The Fall of the House of Usher    Poe    1
2                                        Poe    2
3                                        Poe    3
4     Son coeur est un luth suspendu;    Poe    4
5   Sitot qu'on le touche il resonne.    Poe    5
6                        DE BERANGER.    Poe    6
mydoyle=doyle|> as.data.frame()|>
  dplyr::mutate(Author="Doyle",
                page=1:nrow(doyle)) |> dplyr::select(-gutenberg_id)
head(mydoyle)
                             text Author page
1 The Adventure of the Red Circle  Doyle    1
2                                  Doyle    2
3                                  Doyle    3
4                              By  Doyle    4
5                                  Doyle    5
6          Sir Arthur Conan Doyle  Doyle    6
myfiles=bind_rows(mypoe,mydoyle)
head(myfiles)
                                 text Author page
1      The Fall of the House of Usher    Poe    1
2                                        Poe    2
3                                        Poe    3
4     Son coeur est un luth suspendu;    Poe    4
5   Sitot qu'on le touche il resonne.    Poe    5
6                        DE BERANGER.    Poe    6
mytext=myfiles|>unnest_tokens(word,text)|>
  anti_join(stop_words)
Joining with `by = join_by(word)`
mytext$word = textclean::strip(mytext$word,char.keep = "~~",
digit.remove = TRUE,
apostrophe.remove = TRUE,
lower.case = TRUE)
nrc_dict=get_sentiments("nrc")
head(nrc_dict)
# A tibble: 6 × 2
  word      sentiment
  <chr>     <chr>    
1 abacus    trust    
2 abandon   fear     
3 abandon   negative 
4 abandon   sadness  
5 abandoned anger    
6 abandoned fear     
table(nrc_dict$sentiment)

       anger anticipation      disgust         fear          joy     negative 
        1245          837         1056         1474          687         3316 
    positive      sadness     surprise        trust 
        2308         1187          532         1230 
se=nrc_dict|> dplyr::select(word,sentiment)
head(se)
# A tibble: 6 × 2
  word      sentiment
  <chr>     <chr>    
1 abacus    trust    
2 abandon   fear     
3 abandon   negative 
4 abandon   sadness  
5 abandoned anger    
6 abandoned fear     
table(se$sentiment)

       anger anticipation      disgust         fear          joy     negative 
        1245          837         1056         1474          687         3316 
    positive      sadness     surprise        trust 
        2308         1187          532         1230 
dim(se)
[1] 13872     2
mytext|>
  inner_join(se)|>
  group_by(sentiment)|>
  
  dplyr::count(word,sentiment,Author,sort = TRUE)|>
  ungroup()|>
  dplyr::filter(!sentiment%in% c("positive","negative"))|>
  dplyr::slice_max(n,n=20)|>
  mutate(word=reorder(word,n))|>
  ggplot(aes(sentiment,n))+
  geom_col(aes(fill=sentiment))+
  facet_wrap(~sentiment,scale="free_y")+
  coord_flip()+
  labs(title = "Emotional word count without +ve & -ve sentiment",subtitle = "Author Wise",caption = "Presented by Saisheel Gopularam")+
  theme(legend.position = "right")+
  facet_grid(~Author)
Joining with `by = join_by(word)`
Warning in inner_join(mytext, se): Detected an unexpected many-to-many relationship between `x` and `y`.
ℹ Row 1 of `x` matches multiple rows in `y`.
ℹ Row 13214 of `y` matches multiple rows in `x`.
ℹ If a many-to-many relationship is expected, set `relationship =
  "many-to-many"` to silence this warning.

mytext|>
  inner_join(se)|>
  group_by(sentiment)|>
  
  dplyr::count(word,sentiment,Author,sort = TRUE)|>
  ungroup()|>
  dplyr::filter(sentiment%in% c("positive","negative"))|>
  dplyr::slice_max(n,n=20)|>
  mutate(word=reorder(word,n))|>
  ggplot(aes(sentiment,n))+
  geom_col(aes(fill=sentiment))+
  facet_wrap(~sentiment,scale="free_y")+
  coord_flip()+
  labs(title = "Emotional Word count with only +ve and -ve sentiment",subtitle = "Author Wise",caption = "Presented by Saisheel Gopularam")+
  theme(legend.position = "right")+
  facet_grid(~Author)
Joining with `by = join_by(word)`
Warning in inner_join(mytext, se): Detected an unexpected many-to-many relationship between `x` and `y`.
ℹ Row 1 of `x` matches multiple rows in `y`.
ℹ Row 13214 of `y` matches multiple rows in `x`.
ℹ If a many-to-many relationship is expected, set `relationship =
  "many-to-many"` to silence this warning.