Skip to content

Commit

Permalink
Improve Candidate and CandidateType; implement db.match(text:)
Browse files Browse the repository at this point in the history
  • Loading branch information
bingzheung committed Jun 30, 2024
1 parent bbce823 commit 226c7c3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
}
} else {
currentInputConnection.setComposingText(value, value.length)
candidates.value = db.shortcut(value)
candidates.value = (db.match(value) + db.shortcut(value)).distinct()
if (!isBuffering.value) {
isBuffering.value = true
}
Expand Down
19 changes: 17 additions & 2 deletions app/src/main/java/org/jyutping/jyutping/keyboard/Candidate.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
package org.jyutping.jyutping.keyboard

data class Candidate(
val type: CandidateType = CandidateType.CANTONESE,
val type: CandidateType = CandidateType.Cantonese,
val text: String,
val lexiconText: String = text,
val romanization: String,
val input: String,
val mark: String = input,
val order: Int = 0
)
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Candidate) return false
if (this.type != other.type) return false
if (this.type.isCantonese() && other.type.isCantonese()) {
return (this.text == other.text) && (this.romanization == other.romanization)
} else {
return this.text == other.text
}
}
override fun hashCode(): Int = when (type) {
CandidateType.Cantonese -> (text.hashCode() * 31 + romanization.hashCode())
else -> text.hashCode()
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.jyutping.jyutping.keyboard

enum class CandidateType {
CANTONESE,
TEXT,
EMOJI,
SYMBOL,
COMPOSE
Cantonese,
Text,
Emoji,
Symbol,
Compose
}

fun CandidateType.isCantonese(): Boolean = (this == CandidateType.Cantonese)
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,25 @@ class DatabaseHelper(context: Context, databaseName: String) : SQLiteOpenHelper(

fun shortcut(text: String): List<Candidate> {
val candidates: MutableList<Candidate> = mutableListOf()
val command = "SELECT rowid, word, romanization FROM lexicontable WHERE shortcut = ${text.charcode()} LIMIT 50;"
if (text.isBlank()) return candidates
val code: Int = text.charcode() ?: 0
val command = "SELECT rowid, word, romanization FROM lexicontable WHERE shortcut = $code LIMIT 50;"
val cursor = this.readableDatabase.rawQuery(command, null)
while (cursor.moveToNext()) {
val order = cursor.getInt(0)
val word = cursor.getString(1)
val romanization = cursor.getString(2)
val candidate = Candidate(text = word, romanization = romanization, input = text, order = order)
candidates.add(candidate)
}
cursor.close()
return candidates
}
fun match(text: String): List<Candidate> {
val candidates: MutableList<Candidate> = mutableListOf()
if (text.isBlank()) return candidates
val code: Int = text.hashCode()
val command = "SELECT rowid, word, romanization FROM lexicontable WHERE ping = ${code};"
val cursor = this.readableDatabase.rawQuery(command, null)
while (cursor.moveToNext()) {
val order = cursor.getInt(0)
Expand Down

0 comments on commit 226c7c3

Please sign in to comment.