MediaPipeTasksText Framework Reference

TextClassifier

class TextClassifier : NSObject

@brief Performs classification on text.

This API expects a TFLite model with (optional) TFLite Model Metadatathat 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 xbert_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 xmax_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 AssociatedFiles with type TENSOR_AXIS_LABELS, containing one label per line. The first such AssociatedFile (if any) is used to fill the categoryName field of the results. The displayName field is filled from the AssociatedFile (if any) whose locale matches the displayNamesLocale field of the MPPTextClassifierOptions 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.
  • Creates a new instance of MPPTextClassifier from an absolute path to a TensorFlow Lite model file stored locally on the device and the default MPPTextClassifierOptions.

    Declaration

    Swift

    convenience init(modelPath: String) throws

    Parameters

    modelPath

    An absolute path to a TensorFlow Lite model file stored locally on the device.

    error

    An optional error parameter populated when there is an error in initializing the text classifier.

    Return Value

    A new instance of MPPTextClassifier with the given model path. nil if there is an error in initializing the text classifier.

  • Creates a new instance of MPPTextClassifier from the given MPPTextClassifierOptions.

    Declaration

    Swift

    init(options: TextClassifierOptions) throws

    Parameters

    options

    The options of type MPPTextClassifierOptions to use for configuring the MPPTextClassifier.

    error

    An optional error parameter populated when there is an error in initializing the text classifier.

    Return Value

    A new instance of MPPTextClassifier with the given options. nil if there is an error in initializing the text classifier.

  • Performs classification on the input text.

    Declaration

    Swift

    func classify(text: String) throws -> TextClassifierResult

    Parameters

    text

    The NSString on which classification is to be performed.

    error

    An optional error parameter populated when there is an error in performing classification on the input text.

    Return Value

    A MPPTextClassifierResult object that contains a list of text classifications.

  • Undocumented

  • Undocumented