TextClassifier

public final class TextClassifier

Performs classification on text.

This API expects a TFLite model with (optional) TFLite Model Metadata that contains the mandatory (described below) input tensors, output tensor, and the optional (but recommended) label items as AssociatedFiles with type TENSOR_AXIS_LABELS per output classification tensor.

Metadata is required for models with int32 input tensors because it contains the input process unit for the model's Tokenizer. No metadata is required for models with string input tensors.

  • Input tensors
    • Three input tensors (kTfLiteInt32) of shape [batch_size x bert_max_seq_len] representing the input ids, mask ids, and segment ids. This input signature requires a Bert Tokenizer process unit in the model metadata.
    • Or one input tensor (kTfLiteInt32) of shape [batch_size x max_seq_len] representing the input ids. This input signature requires a Regex Tokenizer process unit in the model metadata.
    • Or one input tensor (kTfLiteString) that is shapeless or has shape [1] containing the input string.
  • At least one output tensor (kTfLiteFloat32/kBool) with:
    • N classes and shape [1 x N]
    • optional (but recommended) label map(s) as AssociatedFile-s with type TENSOR_AXIS_LABELS, containing one label per line. The first such AssociatedFile (if any) is used to fill the class_name field of the results. The display_name field is filled from the AssociatedFile (if any) whose locale matches the display_names_locale field of the TextClassifierOptions used at creation time ("en" by default, i.e. English). If none of these are available, only the index field of the results will be filled.

Nested Classes

class TextClassifier.TextClassifierOptions Options for setting up a TextClassifier

Public Methods

TextClassifierResult
classify(String inputText)
Performs classification on the input text.
void
close()
Closes and cleans up the TextClassifier.
static TextClassifier
createFromFile(Context context, String modelPath)
Creates a TextClassifier instance from a model file and the default TextClassifier.TextClassifierOptions.
static TextClassifier
createFromFile(Context context, File modelFile)
Creates a TextClassifier instance from a model file and the default TextClassifier.TextClassifierOptions.
static TextClassifier

Inherited Methods

Public Methods

public TextClassifierResult classify (String inputText)

Performs classification on the input text.

Parameters
inputText a String for processing.

public void close ()

Closes and cleans up the TextClassifier.

public static TextClassifier createFromFile (Context context, String modelPath)

Creates a TextClassifier instance from a model file and the default TextClassifier.TextClassifierOptions.

Parameters
context an Android ERROR(/Context).
modelPath path to the text model with metadata in the assets.
Throws
if there is is an error during TextClassifier creation.

public static TextClassifier createFromFile (Context context, File modelFile)

Creates a TextClassifier instance from a model file and the default TextClassifier.TextClassifierOptions.

Parameters
context an Android ERROR(/Context).
modelFile the text model File instance.
Throws
IOException if an I/O error occurs when opening the tflite model file.
if there is an error during TextClassifier creation.

public static TextClassifier createFromOptions (Context context, TextClassifier.TextClassifierOptions options)

Parameters
context an Android ERROR(/Context).
options a TextClassifier.TextClassifierOptions instance.
Throws
if there is an error during TextClassifier creation.