BertNLClassifier

public class BertNLClassifier

Classifier API for NLClassification tasks with Bert models, categorizes string into different classes. The API expects a Bert based TFLite model with metadata populated.

The metadata should contain the following information:

  • 1 input_process_unit for Wordpiece/Sentencepiece Tokenizer.
  • 3 input tensors with names "ids", "mask" and "segment_ids".
  • 1 output tensor of type float32[1, 2], with a optionally attached label file. If a label file is attached, 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.

Nested Classes

class BertNLClassifier.BertNLClassifierOptions Options to configure BertNLClassifier. 

Public Methods

List<Category>
classify(String text)
Performs classification on a string input, returns classified Categorys.
static BertNLClassifier
createFromBuffer(ByteBuffer modelBuffer)
Creates BertNLClassifier with a model buffer and default BertNLClassifier.BertNLClassifierOptions.
static BertNLClassifier
static BertNLClassifier
createFromFile(Context context, String modelPath)
Creates BertNLClassifier from a model file with metadata and default BertNLClassifier.BertNLClassifierOptions.
static BertNLClassifier
createFromFile(File modelFile)
Creates BertNLClassifier from a File object with metadata and default BertNLClassifier.BertNLClassifierOptions.
static BertNLClassifier
static BertNLClassifier
createFromFileAndOptions(Context context, String modelPath, BertNLClassifier.BertNLClassifierOptions options)
Creates BertNLClassifier from a model file with metadata and BertNLClassifier.BertNLClassifierOptions.

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 BertNLClassifier createFromBuffer (ByteBuffer modelBuffer)

Creates BertNLClassifier with a model buffer and default BertNLClassifier.BertNLClassifierOptions.

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

public static BertNLClassifier createFromBufferAndOptions (ByteBuffer modelBuffer, BertNLClassifier.BertNLClassifierOptions options)

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

public static BertNLClassifier createFromFile (Context context, String modelPath)

Creates BertNLClassifier from a model file with metadata and default BertNLClassifier.BertNLClassifierOptions.

Parameters
context Android context
modelPath Path to the classification 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 BertNLClassifier createFromFile (File modelFile)

Creates BertNLClassifier from a File object with metadata and default BertNLClassifier.BertNLClassifierOptions.

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 BertNLClassifier createFromFileAndOptions (File modelFile, BertNLClassifier.BertNLClassifierOptions options)

Creates BertNLClassifier from a File object with metadata and BertNLClassifier.BertNLClassifierOptions.

Parameters
modelFile The classification model File instance
options to configure the classifier
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 BertNLClassifier createFromFileAndOptions (Context context, String modelPath, BertNLClassifier.BertNLClassifierOptions options)

Creates BertNLClassifier from a model file with metadata and BertNLClassifier.BertNLClassifierOptions.

Parameters
context Android context.
modelPath Path to the classification model
options to configure the classifier
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