|
| 1 | +package org.fossify.keyboard.helpers |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.os.Handler |
| 5 | +import android.os.Looper |
| 6 | +import org.fossify.commons.helpers.ensureBackgroundThread |
| 7 | +import org.json.JSONArray |
| 8 | +import org.json.JSONException |
| 9 | +import java.io.IOException |
| 10 | +import java.util.Collections |
| 11 | +import kotlin.math.abs |
| 12 | +import kotlin.math.min |
| 13 | + |
| 14 | +private const val DEBOUNCE_DELAY_MS = 100L |
| 15 | +private const val MAX_SUGGESTIONS = 3 |
| 16 | +private const val MAX_LENGTH_DIFF = 2 |
| 17 | +private const val MAX_EDIT_DISTANCE = 2 |
| 18 | +private const val MIN_WORD_LENGTH = 2 |
| 19 | +private const val CACHE_SIZE = 20 |
| 20 | + |
| 21 | +class SpellChecker(context: Context) { |
| 22 | + @Volatile |
| 23 | + private var dictionary: HashMap<String, Int>? = null |
| 24 | + private val mainHandler = Handler(Looper.getMainLooper()) |
| 25 | + private var pendingRunnable: Runnable? = null |
| 26 | + @Suppress("MagicNumber") |
| 27 | + private val cache: MutableMap<String, List<String>> = Collections.synchronizedMap( |
| 28 | + object : LinkedHashMap<String, List<String>>(CACHE_SIZE, 0.75f, true) { |
| 29 | + override fun removeEldestEntry(eldest: MutableMap.MutableEntry<String, List<String>>) = size > CACHE_SIZE |
| 30 | + } |
| 31 | + ) |
| 32 | + |
| 33 | + var onSuggestionsReady: ((List<String>) -> Unit)? = null |
| 34 | + |
| 35 | + init { |
| 36 | + ensureBackgroundThread { |
| 37 | + dictionary = loadDictionary(context) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + fun checkWord(word: String) { |
| 42 | + pendingRunnable?.let { mainHandler.removeCallbacks(it) } |
| 43 | + |
| 44 | + pendingRunnable = Runnable { |
| 45 | + ensureBackgroundThread { |
| 46 | + val suggestions = findSuggestions(word) |
| 47 | + mainHandler.post { |
| 48 | + onSuggestionsReady?.invoke(suggestions) |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + mainHandler.postDelayed(pendingRunnable!!, DEBOUNCE_DELAY_MS) |
| 54 | + } |
| 55 | + |
| 56 | + fun clear() { |
| 57 | + pendingRunnable?.let { mainHandler.removeCallbacks(it) } |
| 58 | + mainHandler.post { |
| 59 | + onSuggestionsReady?.invoke(emptyList()) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + fun isValidWord(word: String): Boolean { |
| 64 | + return dictionary?.containsKey(word.lowercase()) ?: true |
| 65 | + } |
| 66 | + |
| 67 | + fun destroy() { |
| 68 | + pendingRunnable?.let { mainHandler.removeCallbacks(it) } |
| 69 | + onSuggestionsReady = null |
| 70 | + } |
| 71 | + |
| 72 | + private fun loadDictionary(context: Context): HashMap<String, Int> { |
| 73 | + val map = HashMap<String, Int>() |
| 74 | + try { |
| 75 | + val inputStream = context.assets.open("dictionaries/en_US.json") |
| 76 | + val jsonString = inputStream.bufferedReader().use { it.readText() } |
| 77 | + val jsonArray = JSONArray(jsonString) |
| 78 | + |
| 79 | + for (i in 0 until jsonArray.length()) { |
| 80 | + val entry = jsonArray.getJSONArray(i) |
| 81 | + val word = entry.getString(0) |
| 82 | + val rank = jsonArray.length() - i |
| 83 | + map[word] = rank |
| 84 | + } |
| 85 | + } catch (_: IOException) { |
| 86 | + } catch (_: JSONException) { |
| 87 | + } |
| 88 | + return map |
| 89 | + } |
| 90 | + |
| 91 | + private fun findSuggestions(input: String): List<String> { |
| 92 | + val dict = dictionary ?: return emptyList() |
| 93 | + if (input.length < MIN_WORD_LENGTH) return emptyList() |
| 94 | + |
| 95 | + val inputLower = input.lowercase() |
| 96 | + |
| 97 | + cache[inputLower]?.let { return it } |
| 98 | + |
| 99 | + val prefixMatches = dict.keys |
| 100 | + .filter { word -> word.startsWith(inputLower) && word != inputLower } |
| 101 | + .sortedByDescending { dict[it] ?: 0 } |
| 102 | + .take(MAX_SUGGESTIONS) |
| 103 | + |
| 104 | + if (prefixMatches.isNotEmpty()) { |
| 105 | + cache[inputLower] = prefixMatches |
| 106 | + return prefixMatches |
| 107 | + } |
| 108 | + |
| 109 | + val firstChar = inputLower[0] |
| 110 | + val inputLen = inputLower.length |
| 111 | + |
| 112 | + val levenshteinMatches = dict.keys |
| 113 | + .filter { word -> |
| 114 | + word[0] == firstChar && abs(word.length - inputLen) <= MAX_LENGTH_DIFF |
| 115 | + } |
| 116 | + .map { word -> |
| 117 | + word to levenshteinDistance(inputLower, word) |
| 118 | + } |
| 119 | + .filter { it.second <= MAX_EDIT_DISTANCE } |
| 120 | + .sortedWith(compareBy({ it.second }, { -(dict[it.first] ?: 0) })) |
| 121 | + .take(MAX_SUGGESTIONS) |
| 122 | + .map { it.first } |
| 123 | + |
| 124 | + cache[inputLower] = levenshteinMatches |
| 125 | + return levenshteinMatches |
| 126 | + } |
| 127 | + |
| 128 | + private fun levenshteinDistance(a: String, b: String, maxDistance: Int = MAX_EDIT_DISTANCE): Int { |
| 129 | + val m = a.length |
| 130 | + val n = b.length |
| 131 | + val dp = Array(m + 1) { IntArray(n + 1) } |
| 132 | + |
| 133 | + for (i in 0..m) dp[i][0] = i |
| 134 | + for (j in 0..n) dp[0][j] = j |
| 135 | + |
| 136 | + for (i in 1..m) { |
| 137 | + var rowMin = Int.MAX_VALUE |
| 138 | + for (j in 1..n) { |
| 139 | + val cost = if (a[i - 1] == b[j - 1]) 0 else 1 |
| 140 | + dp[i][j] = min( |
| 141 | + min(dp[i - 1][j] + 1, dp[i][j - 1] + 1), |
| 142 | + dp[i - 1][j - 1] + cost |
| 143 | + ) |
| 144 | + rowMin = min(rowMin, dp[i][j]) |
| 145 | + } |
| 146 | + if (rowMin > maxDistance) return maxDistance + 1 |
| 147 | + } |
| 148 | + |
| 149 | + return dp[m][n] |
| 150 | + } |
| 151 | +} |
0 commit comments