Skip to contents

This function takes the output from scrape_reviews and performs sentiment analysis, including basic negation scope detection.

Usage

analyze_sentiment(reviews_df, lexicon = "afinn")

Arguments

reviews_df

A data frame containing the output from scrape_reviews.

lexicon

The sentiment lexicon to use. Options are "afinn", "bing", or "nrc".

Value

A data frame with sentiment scores for each review.

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-09-03 16:15:17.520288 scrape_goodreads_reviews: Completed! All book reviews extracted
#> Scraping run time = 7.43408608436584
#> 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    416390      Paul Bryant   "The Skinhead Hamlet - S…                 11
#> 5 1420    10171516    jessica       "shakespeare when pitchi…                 44
#> 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
# }