Emotional Words Analysis

Author

Satya Panda

FINAL Project

=======================================

# Emotional Analysis of “The Fall of the House of Usher” and “The Adventure of the Red Circle”

Download the following two books from Project Gutenberg: - “The Fall of the House of Usher” by Edgar Allan Poe (#932) - “The Adventure of the Red Circle” by Arthur Conan Doyle (#2345)

Use the NRC dictionary to conduct an analysis of emotional words in both texts.

Emotional Words without Positive and Negative Sentiment

# FINAL Project =======================================
# Download the following two books from project Gutenberg
# "The Fall of the House of Usher" by Edgar Allan Poe (#932)
# &
# "The Adventure of the Red Circle" by Arthur Conan Doyle (#2345)

# Use the nrc dictionary.

# conduct an analysis of emotional words in both texts
  # First, emotional words without positive and negative sentiment
  # Second, just positive and negative sentiment

# Provide your results in a Quarto Document; include visual displays.

# Submit an html version of your Quarto document.

library(quanteda)
Warning in .recacheSubclasses(def@className, def, env): undefined subclass
"ndiMatrix" of class "replValueSp"; definition not updated
Package version: 4.0.2
Unicode version: 14.0
ICU version: 71.1
Parallel computing: disabled
See https://quanteda.io for tutorials and examples.
library(tidytext)
library(gutenbergr)
library(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(tidyr)
library(ggplot2)
# Download the texts from Project Gutenberg
usher <- gutenberg_download(932)
Determining mirror for Project Gutenberg from https://www.gutenberg.org/robot/harvest
Using mirror http://aleph.gutenberg.org
red_circle <- gutenberg_download(2345)

str(usher)
tibble [789 × 2] (S3: tbl_df/tbl/data.frame)
 $ gutenberg_id: int [1:789] 932 932 932 932 932 932 932 932 932 932 ...
 $ text        : chr [1:789] "The Fall of the House of Usher" "" "" "  Son coeur est un luth suspendu;" ...
# Tokenize texts
usher_tokens <- usher %>%
  mutate(text = as.character(text)) %>%
  unnest_tokens(word, text)

# Tokenize texts
red_circle_tokens <- red_circle %>%
  mutate(text = as.character(text)) %>%
  unnest_tokens(word, text)
# Load NRC dictionary
nrc <- get_sentiments("nrc")
# Join NRC sentiment scores
usher_sentiments <- usher_tokens %>%
  inner_join(distinct(nrc), by = "word") %>%
  group_by(word) %>%
  summarise(sentiment = n_distinct(sentiment))
Warning in inner_join(., distinct(nrc), by = "word"): Detected an unexpected many-to-many relationship between `x` and `y`.
ℹ Row 2 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.
red_circle_sentiments <- red_circle_tokens %>%
  inner_join(distinct(nrc), by = "word") %>%
  group_by(word) %>%
  summarise(sentiment = n_distinct(sentiment))
Warning in inner_join(., distinct(nrc), by = "word"): Detected an unexpected many-to-many relationship between `x` and `y`.
ℹ Row 2 of `x` matches multiple rows in `y`.
ℹ Row 11348 of `y` matches multiple rows in `x`.
ℹ If a many-to-many relationship is expected, set `relationship =
  "many-to-many"` to silence this warning.
# Emotional words without positive and negative sentiment
usher_emotional <- usher_sentiments %>%
  filter(!sentiment %in% c("positive", "negative"))

red_circle_emotional <- red_circle_sentiments %>%
  filter(!sentiment %in% c("positive", "negative"))
# Positive and negative sentiment words
usher_pos_neg <- usher_sentiments %>%
  filter(sentiment %in% c("positive", "negative"))

red_circle_pos_neg <- red_circle_sentiments %>%
  filter(sentiment %in% c("positive", "negative"))
# Create visual displays
usher_emotional_plot <- ggplot(usher_emotional, aes(x = sentiment, fill = sentiment)) +
  geom_bar() +
  labs(title = "Emotional Words Distribution in 'The Fall of the House of Usher'",
       x = "Sentiment", y = "Count") +
  theme_minimal()

red_circle_emotional_plot <- ggplot(red_circle_emotional, aes(x = sentiment, fill = sentiment)) +
  geom_bar() +
  labs(title = "Emotional Words Distribution in 'The Adventure of the Red Circle'",
       x = "Sentiment", y = "Count") +
  theme_minimal()

usher_pos_neg_plot <- ggplot(usher_pos_neg, aes(x = sentiment, fill = sentiment)) +
  geom_bar() +
  labs(title = "Positive and Negative Sentiment Words Distribution in 'The Fall of the House of Usher'",
       x = "Sentiment", y = "Count") +
  theme_minimal()

red_circle_pos_neg_plot <- ggplot(red_circle_pos_neg, aes(x = sentiment, fill = sentiment)) +
  geom_bar() +
  labs(title = "Positive and Negative Sentiment Words Distribution in 'The Adventure of the Red Circle'",
       x = "Sentiment", y = "Count") +
  theme_minimal()
# Save plots
ggsave("usher_emotional_plot.png", usher_emotional_plot)
Saving 7 x 5 in image
Warning: The following aesthetics were dropped during statistical transformation: fill.
ℹ This can happen when ggplot fails to infer the correct grouping structure in
  the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
  variable into a factor?
ggsave("red_circle_emotional_plot.png", red_circle_emotional_plot)
Saving 7 x 5 in image
Warning: The following aesthetics were dropped during statistical transformation: fill.
ℹ This can happen when ggplot fails to infer the correct grouping structure in
  the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
  variable into a factor?
ggsave("usher_pos_neg_plot.png", usher_pos_neg_plot)
Saving 7 x 5 in image
ggsave("red_circle_pos_neg_plot.png", red_circle_pos_neg_plot)
Saving 7 x 5 in image

Emotional Words Distribution in ‘The Fall of the House of Usher’
knitr::include_graphics("red_circle_emotional_plot.png")

knitr::include_graphics("usher_pos_neg_plot.png")

knitr::include_graphics("red_circle_pos_neg_plot.png")