Working with Current & Next Word Prediction

Among a few features offered by Fleksy Core SDK, current-word prediction is one of them. This feature predicts the word the user is typing or might have intended to, thus offering a chance to fix it quickly. Having equal importance, the next-word prediction feature predicts what the user might want to enter next. This lets the developer offer suggestions within his app to increase the user’s typing speed, among other benefits.

For this purpose, the SDK offers the currentWordPrediction and the nextWordPrediction methods. These overloaded methods offer multiple choices that can be divided into two areas depending on the type of app the SDK is being integrated into.

  • An app having access to the layout points (X and Y coordinates of the interaction made by the user while typing on the input method editor),
  • Any other app accepting text input (not having access to the layout points).

The methods accepting the layout point as parameters offer better predictions based on the layout of the keyboard, timestamps, and the coordinates the user touched compared to the other methods. However, access to layout points is restricted to IME/Keyboard apps.

This guide aims to show how any app can take benefit of the currentWordPrediction and the nextWordPrediction using the non-layout point methods.

To use this feature, you first need to set up and instantiate an instance of the FleksyLib. Check out the instructions for Android or iOS guide as per your platform.

Predicting Current Words

Android and iOS offer an overloaded version of the currentWordPrediction method that accepts non-layout point parameters and returns the possible suggestions for the typed word.

Examples

For Android:

For Android, the currentWordPrediction method accepts TypingContext as a parameter. An instance of the TypingContext requires String as context. The cursor’s position can be additionally given to improve the prediction if available.

// Set listener for auto-correction button
binding.btAutocorrect.setOnClickListener {
    // Launch a new coroutine
    launch {
        val result =
            fleksyLib.currentWordPrediction(
                TypingContext(
                    binding.editTextInput.text.toString(),
                    binding.editTextInput.cursortStart,
                    binding.editTextInput.cursorEnd
                )
            )

        // Show the list of predicted words to the user
        // if result is a success, error otherwise
        if (result.isSuccess) {
            showCandidateList(result.getOrNull())

        } else {
            showError(result.exceptionOrNull()?.message)
        }
    }
}

For iOS:

On iOS, the overloaded currentWordPrediction methods accept either a String as a parameter directly or an instance of TypingContext. Both overloaded methods further offer two more choices in parameters, i.e., having an inline function to call or return the Result obtained as per the developer’s convenience.

func currentWordPrediction(_ text: String, start: UInt, end: UInt) {
    // Get an instance of the FleksyLib from the class
    guard let fleksyLib = fleksyLib else {
        return
    }

    // Get the predictions and update the class variable
    Task.detached(priority: .userInitiated) {
        let context = TypingContext(context: text, cursorStart: start, cursorEnd: end)
        let result = await fleksyLib.currentWordPrediction(context)
        self.candidateSubject.send(result)
    }
}

Predicting Next Words

Unlike the currentWordPrediction methods, the nextWordPrediction choices don’t depend on layout points. This results in the Android counterpart offering one choice while

The method suggests both, the next words, and emojis a user might want to enter next.

Examples

For Android:

The nextWordPrediction method accepts TypingContext as a parameter similar as before.

// Set listener for next-word prediction button
binding.btSuggestion.setOnClickListener {
    // Launch a new coroutine
    launch {
        val result =
            fleksyLib.nextWordPrediction(
                TypingContext(
                    binding.editTextInput.text.toString(),
                    binding.editTextInput.cursortStart,
                    binding.editTextInput.cursorEnd
                )
            )

        // Show the list of predicted words to the user
        // if result is a success, error otherwise
        if (result.isSuccess) {
            showCandidateList(result.getOrNull())
        } else {
            showError(result.exceptionOrNull()?.message)
        }
    }
}

For iOS:

Just as before, on iOS, the overloaded nextWordPrediction methods again offer two choices, i.e., having an inline function to call or return the Result obtained while accepting String as a parameter directly or an instance of TypingContext.

func nextWordPrediction(_ text: String, start: UInt, end: UInt) {
    // Get an instance of the FleksyLib from the class
    guard let fleksyLib = fleksyLib else {
        return
    }

    // Get the predictions and update the class variable
    Task.detached(priority: .userInitiated) {
        let context = TypingContext(context: text, cursorStart: start, cursorEnd: end)
        let result = await fleksyLib.nextWordPrediction(context)
        self.candidateSubject.send(result)
    }
}

Additional Resources

For more information on working with Fleksy Core SDK, consider checking out the examples and the API reference documentation for the respective platforms that documents all the available methods.


If something needs to be added or if you find an error in our documentation, please let us know either on our GitHub or Discord.

Last updated on February 20, 2024