Skip to content

Predict Text Label Using Flair Classifier

Usage

predict_label(text, classifier, sentence = NULL)

Arguments

text

A character string containing the text to be labeled

classifier

A Flair TextClassifier object for making predictions

sentence

Optional Flair Sentence object. If NULL, one will be created from text

Value

A list containing:

label

Character string of predicted label

score

Numeric confidence score from classifier

token_number

Integer count of tokens in input text

Examples

if (FALSE) { # \dontrun{
# Example 1: Using text input
classifier <- flair_models()$TextClassifier$load('stance-classifier')
result1 <- predict_label(
  text = "I strongly support this policy",
  classifier = classifier
)

# Example 2: Using pre-created and tagged sentence
sent <- Sentence("I love Berlin and New York.")
tagger <- flair_models()$SequenceTagger$load('pos')
tagger$predict(sent)
print(sent)  # Shows tokens with POS tags

result2 <- predict_label(
  text = NULL,
  classifier = classifier,
  sentence = sent
)
} # }