Unsupervised Final Project

Author

Dathan Chann

Download the Books

poe = gutenberg_download(932)
doyle = gutenberg_download(2345)

Poe Data Frame

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

Doyle Date Frame

my_doyle= doyle |> as.data.frame() |>
  dplyr::mutate(Author = "Doyle",
                page= 1:nrow(doyle)) |> dplyr::select(-gutenberg_id)

myfiles= bind_rows(my_poe,my_doyle)

mytext= myfiles |> unnest_tokens(word,text) |>
  anti_join(stop_words)

mytext$word = textclean:: strip(mytext$word,char.keep = "__",
                                digit.remove = T,
                                apostrophe.remove = T,
                                lower.case = T)

Word Contribution to Emotion

nrc_dict = get_sentiments("nrc")

se=nrc_dict %>% 
  dplyr::select(word, sentiment)

Graph without positive and negative sentiment

mytext%>%
  inner_join(se)%>%
  group_by(sentiment)%>%
  dplyr::count(word,sentiment,Author,sort=T) %>%
  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, scales = "free_y") +
  coord_flip()+
  labs(title="Emotional Content", subtitle = "Authors")+
  theme(legend.position= "none")+
  facet_grid(~Author)

Graph with positive and negative sentiment

mytext%>%
  inner_join(se)%>%
  group_by(sentiment)%>%
  dplyr::count(word,sentiment,Author,sort=T) %>%
  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, scales = "free_y") +
  coord_flip()+
  labs(title="Emotional Content", subtitle = "Authors")+
  theme(legend.position= "none")+
  facet_grid(~Author)