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.
- 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> | |
static NLClassifier | |
static NLClassifier |
createFromFile(Context context, String modelPath)
Creates
NLClassifier from default NLClassifier.NLClassifierOptions . |
static NLClassifier | |
static NLClassifier |
createFromFileAndOptions(File modelFile, NLClassifier.NLClassifierOptions options)
Creates
NLClassifier from NLClassifier.NLClassifierOptions . |
static NLClassifier |
createFromFileAndOptions(Context context, String modelPath, NLClassifier.NLClassifierOptions options)
Creates
NLClassifier from NLClassifier.NLClassifierOptions . |
Inherited Methods
Public Methods
public List<Category> classify (String text)
Performs classification on a string input, returns classified Category
s.
Parameters
text | input text to the model |
---|
Returns
- a list of Category results
public static NLClassifier createFromBufferAndOptions (ByteBuffer modelBuffer, NLClassifier.NLClassifierOptions options)
Creates NLClassifier
with a model ByteBuffer
and NLClassifier.NLClassifierOptions
.
Parameters
modelBuffer | a direct ByteBuffer or a MappedByteBuffer of the
classification model |
---|---|
options | configurations for the model |
Returns
NLClassifier
instance
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)
Creates NLClassifier
from default NLClassifier.NLClassifierOptions
.
Parameters
context | Android context |
---|---|
modelPath | path to the classification model relative to asset dir |
Returns
- an
NLClassifier
instance
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)
Creates NLClassifier
from default NLClassifier.NLClassifierOptions
.
Parameters
modelFile | the classification model File instance |
---|
Returns
- an
NLClassifier
instance
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)
Creates NLClassifier
from NLClassifier.NLClassifierOptions
.
Parameters
modelFile | the classification model File instance |
---|---|
options | configurations for the model |
Returns
- an
NLClassifier
instance
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)
Creates NLClassifier
from NLClassifier.NLClassifierOptions
.
Parameters
context | Android context |
---|---|
modelPath | path to the classification model relative to asset dir |
options | configurations for the model. |
Returns
- an
NLClassifier
instance
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 |