Skip to contents

This function extracts the top terms for each topic in the LDA model and optionally prints them.

Usage

top_terms(lda_model, n = 10, verbose = TRUE)

Arguments

lda_model

An LDA model

n

The number of top terms to extract for each topic

verbose

Logical; if TRUE, print the top terms to the console (default is TRUE)

Value

A list of character vectors, each containing the top terms for a topic.

Examples

# \donttest{
# Create a temporary file with sample book IDs
temp_file <- tempfile(fileext = ".txt")
writeLines(c("1420", "2767052", "10210"), temp_file)

# Scrape reviews
reviews <- scrape_reviews(temp_file, num_reviews = 5, use_parallel = FALSE)
#> Total book IDs to process: 3
#> 2024-09-03 16:17:44.445879 scrape_goodreads_reviews: Completed! All book reviews extracted
#> Scraping run time = 6.86547303199768
#> Total books processed: 3

# Preprocess the reviews
preprocessed <- preprocess_reviews(reviews, english_only = TRUE)

# Fit LDA model
lda_model <- fit_lda(preprocessed$dtm, k = 2)

# Print top terms
top_terms(lda_model, n = 5)
#> Topic 1:  
#> love, jane, book, read, novel 
#> 
#> Topic 2:  
#> fuck, hamlet, get, katniss, watch 
#> 
#> $Topic_1
#> [1] "love"  "jane"  "book"  "read"  "novel"
#> 
#> $Topic_2
#> [1] "fuck"    "hamlet"  "get"     "katniss" "watch"  
#> 

# Clean up: remove the temporary file
file.remove(temp_file)
#> [1] TRUE
# }