Back to Tensorflow

TextSearcher

tensorflow/lite/g3doc/api_docs/java/org/tensorflow/lite/task/text/searcher/TextSearcher.html

2.21.09.8 KB
Original Source

public final class TextSearcher

Performs similarity search on text string.

The API expects a TFLite model with optional, but strongly recommended, TFLite Model Metadata..

The API expects a TFLite model with metadata populated. The metadata should contain the following information:

  • For Bert based TFLite model:

    • 3 input tensors of type kTfLiteString with names "ids", "mask" and "segment_ids".
    • input_process_units for Wordpiece/Sentencepiece Tokenizer
    • exactly one output tensor of type kTfLiteFloat32
  • For Regex based TFLite model:

  • 1 input tensor.

  • input_process_units for RegexTokenizer Tokenizer

  • exactly one output tensor of type kTfLiteFloat32

  • For Universal Sentence Encoder based TFLite model:

  • 3 input tensors with names "inp_text", "res_context" and "res_text"

  • 2 output tensors with names "query_encoding" and "response_encoding" of type kTfLiteFloat32

TODO(b/180502532): add pointer to example model.

TODO(b/222671076): add factory create methods without options, such as createFromFile, once the single file format (index file packed in the model) is supported.

Nested Classes

| class | TextSearcher.TextSearcherOptions | Options for setting up an TextSearcher. |

Public Methods

| static TextSearcher | createFromBufferAndOptions(ByteBuffer modelBuffer, TextSearcher.TextSearcherOptions options) Creates an TextSearcher instance with a model buffer and TextSearcher.TextSearcherOptions.

| | static TextSearcher | createFromBufferAndOptionsImpl(ByteBuffer modelBuffer, TextSearcher.TextSearcherOptions options, int indexFd) | | static TextSearcher | createFromFileAndOptions(File modelFile, TextSearcher.TextSearcherOptions options) Creates an TextSearcher instance.

| | static TextSearcher | createFromFileAndOptions(Context context, String modelPath, TextSearcher.TextSearcherOptions options) Creates an TextSearcher instance from TextSearcher.TextSearcherOptions.

| | List<NearestNeighbor> | search(String text) Performs embedding extraction on the provided string input, followed by nearest-neighbor search in the index.

|

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 static TextSearcher createFromBufferAndOptions (ByteBuffer modelBuffer, TextSearcher.TextSearcherOptions options)

Creates an TextSearcher instance with a model buffer and TextSearcher.TextSearcherOptions.

Parameters

| modelBuffer | a direct ByteBuffer or a MappedByteBuffer of the search model | | options | |

Throws

| IllegalArgumentException | if the model buffer is not a direct ByteBuffer or a MappedByteBuffer | | IOException | if an I/O error occurs when loading the index file | | IllegalStateException | if there is an internal error | | RuntimeException | if there is an otherwise unspecified error |

public static TextSearcher createFromBufferAndOptionsImpl (ByteBuffer modelBuffer, TextSearcher.TextSearcherOptions options, int indexFd)

Parameters

| modelBuffer | | | options | | | indexFd | |

public static TextSearcher createFromFileAndOptions (File modelFile, TextSearcher.TextSearcherOptions options)

Creates an TextSearcher instance.

Parameters

| modelFile | the search model File instance | | options | |

Throws

| IOException | if an I/O error occurs when loading the tflite model or the index file | | IllegalArgumentException | if an argument is invalid | | IllegalStateException | if there is an internal error | | RuntimeException | if there is an otherwise unspecified error |

public static TextSearcher createFromFileAndOptions (Context context, String modelPath, TextSearcher.TextSearcherOptions options)

Creates an TextSearcher instance from TextSearcher.TextSearcherOptions.

Parameters

| context | | | modelPath | path of the search model with metadata in the assets | | options | |

Throws

| IOException | if an I/O error occurs when loading the tflite model or the index file | | IllegalArgumentException | if an argument is invalid | | IllegalStateException | if there is an internal error | | RuntimeException | if there is an otherwise unspecified error |

public List<NearestNeighbor> search (String text)

Performs embedding extraction on the provided string input, followed by nearest-neighbor search in the index.

Parameters

| text | input text query to the model |