MediaPipeTasksText Framework Reference

MPPTextClassifier


@interface MPPTextClassifier : 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

    Objective-C

    - (nullable instancetype)initWithModelPath:(nonnull NSString *)modelPath
                                         error:(NSError *_Nullable *_Nullable)error;

    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

    Objective-C

    - (nullable instancetype)initWithOptions:
                                 (nonnull MPPTextClassifierOptions *)options
                                       error:(NSError *_Nullable *_Nullable)error;

    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

    Objective-C

    - (nullable MPPTextClassifierResult *)
        classifyText:(nonnull NSString *)text
               error:(NSError *_Nullable *_Nullable)error;

    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

    Declaration

    Objective-C

    - (instancetype)init NS_UNAVAILABLE;
  • Undocumented

    Declaration

    Objective-C

    + (instancetype)new NS_UNAVAILABLE;