Skip to content

Commit

Permalink
Implement Structure Reverse Lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
bingzheung committed Jul 7, 2024
1 parent 70d0260 commit 912caef
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.jyutping.jyutping.keyboard.KeyboardCase
import org.jyutping.jyutping.keyboard.KeyboardForm
import org.jyutping.jyutping.keyboard.Segmentor
import org.jyutping.jyutping.keyboard.isUppercased
import org.jyutping.jyutping.keyboard.length
import org.jyutping.jyutping.keyboard.transformed
import org.jyutping.jyutping.utilities.DatabaseHelper
import org.jyutping.jyutping.utilities.DatabasePreparer
Expand Down Expand Up @@ -134,9 +135,32 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
'r' -> {}
'v' -> {}
'x' -> {}
'q' -> {}
'q' -> {
if (value.length < 2) {
currentInputConnection.setComposingText(value, value.length)
} else {
val text = value.drop(1)
val segmentation = Segmentor.segment(text, db)
val tailMark: String = run {
val bestScheme = segmentation.firstOrNull()
val leadingLength: Int = bestScheme?.length() ?: 0
val leadingText: String = bestScheme?.joinToString(separator = String.space) { it.text } ?: String.empty
when (leadingLength) {
0 -> text
text.length -> leadingText
else -> (leadingText + String.space + text.drop(leadingLength))
}
}
val mark = "q $tailMark"
currentInputConnection.setComposingText(mark, mark.length)
val suggestions = Engine.structureReverseLookup(text, segmentation, db)
candidates.value = suggestions.map { it.transformed(characterStandard.value) }.distinct()
}
if (isBuffering.value.not()) {
isBuffering.value = true
}
}
else -> {
currentInputConnection.setComposingText(value, value.length)
val segmentation = Segmentor.segment(value, db)
val suggestions = Engine.suggest(text = value, segmentation = segmentation, db = db)
val mark: String = run {
Expand Down Expand Up @@ -173,7 +197,22 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
}
fun select(candidate: Candidate) {
currentInputConnection.commitText(candidate.text, candidate.text.length)
bufferText = bufferText.drop(candidate.input.length)
when (bufferText.firstOrNull()) {
null -> {}
'q' -> {
val inputLength = candidate.input.length
val leadingLength = inputLength + 1
if (bufferText.length > leadingLength) {
val tail = bufferText.drop(leadingLength)
bufferText = "q$tail"
} else {
bufferText = String.empty
}
}
else -> {
bufferText = bufferText.drop(candidate.input.length)
}
}
}
fun backspace() {
if (bufferText.isEmpty()) {
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/org/jyutping/jyutping/keyboard/Engine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,26 @@ object Engine {
}
return candidates.sortedWith(comparator)
}

fun structureReverseLookup(text: String, segmentation: Segmentation, db: DatabaseHelper): List<Candidate> {
return structureProcess(text, segmentation, db)
.map { item ->
db.reverseLookup(item.text)
.map { romanization -> Candidate(text = item.text, romanization = romanization, input = text, mark = romanization.filter { it.isLetter() }) }
}
.flatten()
}
private fun structureProcess(text: String, segmentation: Segmentation, db: DatabaseHelper): List<Candidate> {
val fullMatched = db.structureMatch(text)
val textLength = text.length
val schemes = segmentation.filter { it.length() == textLength }
if (schemes.maxLength() < 1) return fullMatched
val matches: MutableList<List<Candidate>> = mutableListOf()
for (scheme in schemes) {
val pingText = scheme.joinToString(separator = String.empty) { it.origin }
val matched = db.structureMatch(pingText)
matches.add(matched)
}
return fullMatched + matches.flatten()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.jyutping.jyutping.utilities
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import androidx.compose.runtime.MutableState
import org.jyutping.jyutping.extensions.charcode
import org.jyutping.jyutping.extensions.convertedS2T
import org.jyutping.jyutping.extensions.intercode
Expand Down Expand Up @@ -396,4 +397,32 @@ class DatabaseHelper(context: Context, databaseName: String) : SQLiteOpenHelper(
cursor.close()
return token
}

fun reverseLookup(text: String): List<String> {
if (text.isBlank()) return emptyList()
val romanizations: MutableList<String> = mutableListOf()
val command = "SELECT romanization FROM lexicontable WHERE word = '${text}';"
val cursor = this.readableDatabase.rawQuery(command, null)
while (cursor.moveToNext()) {
val romanization = cursor.getString(0)
romanizations.add(romanization)
}
cursor.close()
return romanizations
}
fun structureMatch(text: String): List<Candidate> {
if (text.isBlank()) return emptyList()
val candidates: MutableList<Candidate> = mutableListOf()
val code = text.hashCode()
val command = "SELECT word, romanization FROM structuretable WHERE ping = ${code};"
val cursor = this.readableDatabase.rawQuery(command, null)
while (cursor.moveToNext()) {
val word = cursor.getString(0)
val romanization = cursor.getString(1)
val instance = Candidate(text = word, romanization = romanization, input = text)
candidates.add(instance)
}
cursor.close()
return candidates
}
}

0 comments on commit 912caef

Please sign in to comment.