Skip to contents

Classify text using a Hugging Face model. Commonly used for sentiment analysis, topic classification, etc.

Usage

hf_classify(
  text,
  model = "distilbert/distilbert-base-uncased-finetuned-sst-2-english",
  token = NULL,
  ...
)

Arguments

text

Character vector of text(s) to classify.

model

Character string. Model ID from Hugging Face Hub. Default: "distilbert/distilbert-base-uncased-finetuned-sst-2-english" (sentiment analysis).

token

Character string or NULL. API token for authentication.

...

Additional arguments (currently unused).

Value

A tibble with columns: text, label, score

Examples

if (FALSE) { # \dontrun{
# Sentiment analysis
hf_classify("I love R programming!")

# Multiple texts
hf_classify(c("This is great!", "This is terrible."))

# Use in a pipeline
library(dplyr)
reviews |>
  mutate(sentiment = hf_classify(review_text)) |>
  unnest(sentiment)
} # }