Perform sentiment analysis on book reviews with negation handling
Source:R/sentiment_analysis.R
analyze_sentiment.Rd
This function takes the output from scrape_reviews and performs sentiment analysis, including basic negation scope detection.
Examples
# \donttest{
# Create a temporary file with sample book IDs
temp_file <- tempfile(fileext = ".txt")
writeLines(c("1420", "2767052", "10210"), temp_file)
# Run the scrape_reviews function
reviews <- scrape_reviews(temp_file, num_reviews = 5, use_parallel = FALSE)
#> Total book IDs to process: 3
#> 2024-10-25 03:00:41.793132 scrape_goodreads_reviews: Completed! All book reviews extracted
#> Scraping run time = 8.62044811248779
#> Total books processed: 3
# Check if reviews were successfully scraped
if (nrow(reviews) > 0) {
# Perform sentiment analysis
sentiment_results <- analyze_sentiment(reviews, lexicon = "afinn")
# Display the first few rows of the results
print(head(sentiment_results))
} else {
cat("No reviews found. Cannot perform sentiment analysis.\n")
}
#> # A tibble: 6 × 9
#> book_id reviewer_id reviewer_name review_content reviewer_followers
#> <chr> <chr> <chr> <chr> <dbl>
#> 1 1420 91434473 daph pink ♡ "if you don't ship Hamra… 3
#> 2 1420 83582 Bill Kerwin "I don't have any earth-… NA
#> 3 1420 44531801 Nayra.Hassan "متردد في قراءة هاملت..س… 6
#> 4 1420 10171516 jessica "shakespeare when pitchi… 45
#> 5 1420 416390 Paul Bryant "The Skinhead Hamlet - S… 11
#> 6 2767052 3672777 Nataliya "Suzanne Collins has bal… 14
#> # ℹ 4 more variables: reviewer_total_reviews <dbl>, review_date <chr>,
#> # review_rating <dbl>, sentiment_score <dbl>
# Clean up: remove the temporary file
file.remove(temp_file)
#> [1] TRUE
# }