public class NLClassifier extends BaseTaskApi

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

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

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 Class Summary

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

Protected Constructor Summary

NLClassifier(long nativeHandle)
Constructor to initialize the JNI with a pointer from C++.

Public Method Summary

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

Protected Method Summary

void
deinit(long nativeHandle)

Inherited Method Summary

Protected Constructors

protected NLClassifier (long nativeHandle)

Constructor to initialize the JNI with a pointer from C++.

Parameters
nativeHandle a pointer referencing memory allocated in C++.

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 (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 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 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

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

Protected Methods

protected void deinit (long nativeHandle)