Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class AutocompletionHandler(
return@Runnable
}

val completions = ime.getAutocompletions(currentWord, limit = 5)
val previousWord = ime.getPreviousWordBeforeCursor()
val completions = ime.getAutocompletions(currentWord, previousWord, limit = 5)

if (completions.isNotEmpty()) {
ime.updateAutocompleteSuggestions(completions)
Expand Down
15 changes: 14 additions & 1 deletion app/src/keyboards/java/be/scri/services/GeneralKeyboardIME.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1112,10 +1112,11 @@ abstract class GeneralKeyboardIME(
*/
fun getAutocompletions(
prefix: String,
previousWord: String? = null,
limit: Int = 3,
): List<String> {
if (this::nativeSuggestionEngine.isInitialized) {
val nativeCompletions = nativeSuggestionEngine.getAutocompletions(language, prefix, limit)
val nativeCompletions = nativeSuggestionEngine.getAutocompletions(language, prefix, previousWord, limit)
if (nativeCompletions.isNotEmpty()) {
return nativeCompletions
}
Expand Down Expand Up @@ -1156,6 +1157,18 @@ abstract class GeneralKeyboardIME(
*/
fun getLastWordBeforeCursor(): String? = getText()?.trim()?.split("\\s+".toRegex())?.lastOrNull()

/**
* Extracts the word immediately before the one currently being composed, i.e. the last
* completed word preceding the in-progress word at the cursor. Used to give the autocomplete
* engine sentence context so it can bias completions instead of scoring the prefix in isolation.
*
* @return The previous completed word as a [String], or null if there isn't one.
*/
fun getPreviousWordBeforeCursor(): String? {
val words = getText()?.trim()?.split("\\s+".toRegex()) ?: return null
return words.getOrNull(words.size - 2)
}

/**
* Retrieves the text immediately preceding the cursor.
*
Expand Down
Binary file removed app/src/main/assets/dicts/main_bg.dict
Binary file not shown.
Binary file removed app/src/main/assets/dicts/main_bn.dict
Binary file not shown.
Binary file modified app/src/main/assets/dicts/main_de.dict
Binary file not shown.
Binary file removed app/src/main/assets/dicts/main_el.dict
Binary file not shown.
Binary file removed app/src/main/assets/dicts/main_en-GB.dict
Binary file not shown.
Binary file modified app/src/main/assets/dicts/main_en-US.dict
Binary file not shown.
Binary file modified app/src/main/assets/dicts/main_es.dict
Binary file not shown.
Binary file modified app/src/main/assets/dicts/main_fr.dict
Binary file not shown.
Binary file removed app/src/main/assets/dicts/main_hu.dict
Binary file not shown.
Binary file modified app/src/main/assets/dicts/main_it.dict
Binary file not shown.
Binary file removed app/src/main/assets/dicts/main_nl.dict
Binary file not shown.
Binary file removed app/src/main/assets/dicts/main_pl.dict
Binary file not shown.
Binary file modified app/src/main/assets/dicts/main_pt-BR.dict
Binary file not shown.
Binary file removed app/src/main/assets/dicts/main_pt-PT.dict
Binary file not shown.
Binary file removed app/src/main/assets/dicts/main_ro.dict
Binary file not shown.
Binary file modified app/src/main/assets/dicts/main_ru.dict
Binary file not shown.
Binary file modified app/src/main/assets/dicts/main_sv.dict
Binary file not shown.
Binary file removed app/src/main/assets/dicts/main_tr.dict
Binary file not shown.
23 changes: 18 additions & 5 deletions app/src/main/java/be/scri/helpers/NativeSuggestionEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,38 @@ class NativeSuggestionEngine(private val context: Context) {
fun getAutocompletions(
language: String,
prefix: String,
previousWord: String? = null,
limit: Int = 3
): List<String> {
val dict = getDictionary(language) ?: return emptyList()
if (prefix.isBlank()) return emptyList()

return try {
val composedData = ComposedData.createForWord(prefix)
val ngramContext =
if (previousWord.isNullOrBlank()) {
NgramContext.EMPTY_PREV_WORDS_INFO
} else {
NgramContext(NgramContext.WordInfo(previousWord))
}
val suggestions = dict.getSuggestions(
composedData,
NgramContext.EMPTY_PREV_WORDS_INFO,
ngramContext,
dummyProximityInfo.nativeProximityInfo, // proximityInfoHandle
SettingsValuesForSuggestion(false, false),
SettingsValuesForSuggestion(true, false), // blockPotentiallyOffensive, spaceAwareGesture
1, // sessionId
1.0f, // weightForLocale
null // inOutWeightOfLangModelVsSpatialModel
)

val isCapitalized = StringUtils.isWordCapitalized(prefix)
suggestions?.map { it.mWord }
?.filter { it.isNotBlank() && it.lowercase(Locale.ROOT) != prefix.lowercase(Locale.ROOT) }
?.filter {
it.isNotBlank() &&
it.startsWith(prefix, ignoreCase = true) &&
it.lowercase(Locale.ROOT) != prefix.lowercase(Locale.ROOT)
}
?.map { if (isCapitalized) it.replaceFirstChar { c -> c.uppercaseChar() } else it }
?.take(limit)
?: emptyList()
} catch (e: Exception) {
Expand Down Expand Up @@ -162,14 +175,14 @@ class NativeSuggestionEngine(private val context: Context) {
composedData,
ngramContext,
dummyProximityInfo.nativeProximityInfo, // proximityInfoHandle
SettingsValuesForSuggestion(false, false),
SettingsValuesForSuggestion(true, false), // blockPotentiallyOffensive, spaceAwareGesture
1, // sessionId
1.0f, // weightForLocale
null // inOutWeightOfLangModelVsSpatialModel
)

suggestions?.map { it.mWord }
?.filter { it.isNotBlank() }
?.filter { it.isNotBlank() && it.lowercase(Locale.ROOT) != lastWord.lowercase(Locale.ROOT) }
?.take(limit)
?: emptyList()
} catch (e: Exception) {
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/be/scri/latin/common/ComposedData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/
package be.scri.latin.common

import kotlin.random.Random

/** An immutable class that encapsulates a snapshot of word composition data. */
class ComposedData(
@JvmField val mInputPointers: InputPointers,
Expand Down Expand Up @@ -48,10 +46,12 @@ class ComposedData(
companion object {
fun createForWord(word: String): ComposedData {
val codePoints = StringUtils.toCodePointArray(word)
val coordinates = CoordinateUtils.newCoordinateArray(codePoints.size)
for (i in codePoints.indices) {
CoordinateUtils.setXYInArray(coordinates, i, Random.nextBits(2), Random.nextBits(2))
}
val coordinates =
CoordinateUtils.newCoordinateArray(
codePoints.size,
Constants.NOT_A_COORDINATE,
Constants.NOT_A_COORDINATE,
)
val pointers = InputPointers(codePoints.size).apply {
for (i in codePoints.indices) {
addPointer(CoordinateUtils.xFromArray(coordinates, i), CoordinateUtils.yFromArray(coordinates, i), 0, 0)
Expand Down
215 changes: 0 additions & 215 deletions app/src/main/jni/Android.bp

This file was deleted.

Loading
Loading