NextWordPredictor

class NextWordPredictor(context: Context)

Next-word prediction system which internally uses a prebuilt Markov-model to predict the next-word given the current word. Markov models could modelled as directed-graphs where nodes are the words from the training corpus (huge text dataset) and edges represent dependencies. If a directed-edge goes from word A to word B, then its weight is the frequency of word B, given that word A precedes it in the corpus. Once the graph is built, the top-3 words that precede any word 'w' are stored in a hashmap, where the key is the word 'w' and the value is List<String> that contains the top-3 words succeeding 'w'. These words are displayed to the user as suggestions. For more implementation details, see the rust branch.

The implementation of the graph is built in Rust, and is interfaced with this class through JNI. The .so libraries for various targets could be found in text-predictor/src/main/jniLibs. The JNI methods would be found on the rust branch in src/lib.rs script.

Example:

val nextWordPredictor = NextWordPredictor( context )
nextWordPredictor.load()

val input = "why"
nextWordPredictor.predict(
input ,
onResult = { results ->

} ,
onError = { error ->
println( error.message
}
)

Constructors

NextWordPredictor
Link copied to clipboard
fun NextWordPredictor(context: Context)

Types

Companion
Link copied to clipboard
object Companion

Functions

close
Link copied to clipboard
fun close()

Release the native resources acquired by the object. Internally, it deallocates the memory which was acquired with the createNativeInstance method.

predict
Link copied to clipboard
fun predict(word: String, onResult: (List<String>) -> Unit, onError: (TextPredictorError) -> Unit)

Predicts the next words, given the current word. If word="how", then this method will return [ are , you , should ] as a List<String> in the onResult callback.