Certainly! Here's an R script that downloads a book from Project Gutenberg, processes its text, and analyzes its emotional trajectory over time using the Syuzhet package:
r
# Load necessary libraries
library(gutenbergr)
library(dplyr)
library(syuzhet)
library(ggplot2)
# Function to analyze a book by its Gutenberg ID
analyze_gutenberg_book <- function(book_id) {
# Download the book text
book_text <- gutenberg_download(book_id, strip = TRUE)
# Combine all lines into a single text
full_text <- paste(book_text$text, collapse = " ")
# Split the text into sentences
sentences <- get_sentences(full_text)
# Get the sentiment scores for each sentence
sentiment_scores <- get_sentiment(sentences, method = "syuzhet")
# Normalize the sentiment scores
normalized_scores <- rescale_x_2(sentiment_scores)
# Create a data frame for plotting
sentiment_df <- data.frame(
Sentence = 1:length(normalized_scores),
Sentiment = normalized_scores
)
# Plot the sentiment trajectory
ggplot(sentiment_df, aes(x = Sentence, y = Sentiment)) +
geom_line(color = "blue") +
labs(title = paste("Emotional Trajectory of Gutenberg Book ID", book_id),
x = "Sentence Number",
y = "Normalized Sentiment Score") +
theme_minimal()
}
# Example usage: Analyze "Pride and Prejudice" by Jane Austen (Gutenberg ID: 1342)
analyze_gutenberg_book(1342)
Explanation:
Load Libraries: The script loads the necessary libraries: gutenbergr for accessing Project Gutenberg texts, dplyr for data manipulation, syuzhet for sentiment analysis, and ggplot2 for plotting.
Function Definition: The analyze_gutenberg_book function takes a Gutenberg book ID as input.
Download Book: It downloads the book's text using gutenberg_download.
Process Text: The text is combined into a single string and split into sentences.
Sentiment Analysis: Each sentence's sentiment is analyzed using the Syuzhet package.
Normalization: The sentiment scores are normalized to ensure they are on a comparable scale.
Data Frame Creation: A data frame is created with sentence numbers and their corresponding sentiment scores.
Plotting: The emotional trajectory is plotted using ggplot2.
Usage:
Replace the book_id in the function call with the desired Gutenberg book ID. For example, to analyze "Pride and Prejudice" by Jane Austen, use the ID 1342.
Note:
Ensure that the gutenbergr and syuzhet packages are installed and that you have an active internet connection to download the book from Project Gutenberg.
User
have it then color the sentences of the text according to their sentiment
Assistant
To enhance the analysis of a Project Gutenberg book by visualizing its emotional progression and highlighting sentences based on their sentiment, you can utilize the sentimentr package in R. This package offers functions to compute sentiment scores and generate HTML documents with color-coded sentences, facilitating a more intuitive understanding of the text's emotional landscape.
Here's an R script that accomplishes this:
r
# Load necessary libraries
library(gutenbergr)
library(dplyr)
library(sentimentr)
library(ggplot2)
# Function to analyze a book by its Gutenberg ID
analyze_gutenberg_book <- function(book_id) {
# Download the book text
book_text <- gutenberg_download(book_id, strip = TRUE)
# Combine all lines into a single text
full_text <- paste(book_text$text, collapse = " ")
# Split the text into sentences
sentences <- get_sentences(full_text)
# Compute sentiment scores for each sentence
sentiment_scores <- sentiment(sentences)
# Aggregate sentiment scores by sentence
sentiment_by_sentence <- sentiment_by(sentences)
# Generate an HTML file with sentences highlighted by sentiment
highlight(sentiment_by_sentence, file = "sentiment_highlight.html", open = TRUE)
# Plot the sentiment trajectory
ggplot(sentiment_scores, aes(x = sentence_id, y = sentiment)) +
geom_line(color = "blue") +
labs(title = paste("Emotional Trajectory of Gutenberg Book ID", book_id),
x = "Sentence Number",
y = "Sentiment Score") +
theme_minimal()
}
# Example usage: Analyze "Pride and Prejudice" by Jane Austen (Gutenberg ID: 1342)
analyze_gutenberg_book(1342)
Explanation:
Load Libraries: The script loads the necessary libraries: for accessing Project Gutenberg texts, for data manipulation, for sentiment analysis and text highlighting, and for plotting.
gutenbergr
dplyr
sentimentr
ggplot2
Function Definition: The analyze_gutenberg_book function takes a Gutenberg book ID as input.
Download Book: It downloads the book's text using gutenberg_download.
Process Text: The text is combined into a single string and split into sentences using get_sentences from the sentimentr package.
Sentiment Analysis: Each sentence's sentiment is analyzed using the sentiment function from sentimentr.
Aggregate Sentiment: Sentiment scores are aggregated by sentence using sentiment_by.
Highlight Sentences: The highlight function generates an HTML file (sentiment_highlight.html) where sentences are color-coded based on their sentiment polarity: positive sentences are highlighted in green, and negative sentences in pink. This visual representation aids in quickly identifying the emotional tone of different parts of the text[1].
Plotting: The emotional trajectory is plotted using ggplot2, showing how sentiment evolves over the course of the text.
Usage:
Replace the book_id in the function call with the desired Gutenberg book ID. For example, to analyze "Pride and Prejudice" by Jane Austen, use the ID 1342.
Note:
Ensure that the gutenbergr, sentimentr, and ggplot2 packages are installed and that you have an active internet connection to download the book from Project Gutenberg. The highlight function will create an HTML file in your working directory and attempt to open it in your default web browser.