Back to Tensorflow

NLClassifier

tensorflow/lite/g3doc/api_docs/java/org/tensorflow/lite/task/text/nlclassifier/NLClassifier.html

2.21.011.9 KB
Original Source

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 | createFromBufferAndOptions(ByteBuffer modelBuffer, NLClassifier.NLClassifierOptions options) Creates NLClassifier with a model ByteBuffer and NLClassifier.NLClassifierOptions.

| | static NLClassifier | createFromFile(Context context, String modelPath) Creates NLClassifier from default NLClassifier.NLClassifierOptions.

| | static NLClassifier | createFromFile(File modelFile) Creates NLClassifier from default NLClassifier.NLClassifierOptions.

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

From class org.tensorflow.lite.task.core.BaseTaskApi

| synchronized void | close() Release the memory allocated from C++ and deregister the library from the static holder.

| | long | getNativeHandle() | | boolean | isClosed() |

From class java.lang.Object

| boolean | equals(Object arg0) | | final Class<?> | getClass() | | int | hashCode() | | final void | notify() | | final void | notifyAll() | | String | toString() | | final void | wait(long arg0, int arg1) | | final void | wait(long arg0) | | final void | wait() |

From interface java.io.Closeable

| abstract void | close() |

From interface java.lang.AutoCloseable

| abstract void | close() |

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)

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 |