ML Community Day is November 9! Join us for updates from TensorFlow, JAX, and more Learn more

NLClassifier

public class NLClassifier

Classifier API for natural language classification tasks, categorizes string into different classes.

The API expects a TFLite model with the following input/output tensor:

  • Input tensor (kTfLiteString)
    • input of the model, accepts a string.
  • Output score tensor (kTfLiteUInt8/kTfLiteInt8/kTfLiteInt16/kTfLiteFloat32/kTfLiteFloat64/kTfLiteBool)
    • output scores for each class, if type is one of the Int types, dequantize it, if it is Bool type, convert the values to 0.0 and 1.0 respectively.
    • can have an optional associated file in metadata for labels, the file should be a plain text file with one label per line, the number of labels should match the number of categories the model outputs. Output label tensor: optional (kTfLiteString) - output classname for each class, should be of the same length with scores. If this tensor is not present, the API uses score indices as classnames. - will be ignored if output score tensor already has an associated label file.
  • Optional Output label tensor (kTfLiteString/kTfLiteInt32)
    • output classname for each class, should be of the same length with scores. If this tensor is not present, the API uses score indices as classnames.
    • will be ignored if output score tensor already has an associated labe file.

By default the API tries to find the input/output tensors with default configurations in NLClassifier.NLClassifierOptions, with tensor name prioritized over tensor index. The option is configurable for different TFLite models.

Nested Classes

class NLClassifier.NLClassifierOptions Options to identify input and output tensors of the model. 

Public Methods

List<Category>
classify(String text)
Performs classification on a string input, returns classified Categorys.
static NLClassifier
static NLClassifier
createFromFile(Context context, String modelPath)
static NLClassifier
static NLClassifier
static NLClassifier

Inherited Methods

Public Methods

public List<Category> classify (String text)

Performs classification on a string input, returns classified Categorys.

Parameters
text input text to the model
Returns
  • a list of Category results

public static NLClassifier createFromBufferAndOptions (ByteBuffer modelBuffer, NLClassifier.NLClassifierOptions options)

Parameters
modelBuffer a direct ByteBuffer or a MappedByteBuffer of the classification model
options configurations for the model
Returns
Throws
IllegalStateException if there is an internal error
RuntimeException if there is an otherwise unspecified error
IllegalArgumentException if the model buffer is not a direct ByteBuffer or a MappedByteBuffer

public static NLClassifier createFromFile (Context context, String modelPath)

Parameters
context Android context
modelPath path to the classification model relative to asset dir
Returns
Throws
IOException if model file fails to load
IllegalArgumentException if an argument is invalid
IllegalStateException if there is an internal error
RuntimeException if there is an otherwise unspecified error

public static NLClassifier createFromFile (File modelFile)

Parameters
modelFile the classification model File instance
Returns
Throws
IOException if model file fails to load
IllegalArgumentException if an argument is invalid
IllegalStateException if there is an internal error
RuntimeException if there is an otherwise unspecified error

public static NLClassifier createFromFileAndOptions (File modelFile, NLClassifier.NLClassifierOptions options)

Parameters
modelFile the classification model File instance
options configurations for the model
Returns
Throws
IOException if model file fails to load
IllegalArgumentException if an argument is invalid
IllegalStateException if there is an internal error
RuntimeException if there is an otherwise unspecified error

public static NLClassifier createFromFileAndOptions (Context context, String modelPath, NLClassifier.NLClassifierOptions options)

Parameters
context Android context
modelPath path to the classification model relative to asset dir
options configurations for the model.
Returns
Throws
IOException if model file fails to load
IllegalArgumentException if an argument is invalid
IllegalStateException if there is an internal error
RuntimeException if there is an otherwise unspecified error