Skip to content

Commit

Permalink
Use PresetString
Browse files Browse the repository at this point in the history
  • Loading branch information
bingzheung committed Aug 27, 2024
1 parent 99b5583 commit 50b51aa
Show file tree
Hide file tree
Showing 21 changed files with 91 additions and 108 deletions.
14 changes: 10 additions & 4 deletions app/src/main/java/org/jyutping/jyutping/AppContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import org.jyutping.jyutping.ui.about.*
import org.jyutping.jyutping.ui.cantonese.*
import org.jyutping.jyutping.ui.home.*
import org.jyutping.jyutping.ui.romanization.*
import org.jyutping.jyutping.ui.about.AboutScreen
import org.jyutping.jyutping.ui.cantonese.CantoneseScreen
import org.jyutping.jyutping.ui.cantonese.ConfusionScreen
import org.jyutping.jyutping.ui.cantonese.ExpressionsScreen
import org.jyutping.jyutping.ui.home.HomeScreen
import org.jyutping.jyutping.ui.home.IntroductionsScreen
import org.jyutping.jyutping.ui.romanization.JyutpingFinalsScreen
import org.jyutping.jyutping.ui.romanization.JyutpingInitialsScreen
import org.jyutping.jyutping.ui.romanization.JyutpingScreen
import org.jyutping.jyutping.ui.romanization.JyutpingTonesScreen

@Composable
fun AppContent(navController: NavHostController) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import androidx.compose.ui.platform.AbstractComposeView
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import org.jyutping.jyutping.keyboard.KeyboardForm
import org.jyutping.jyutping.keyboard.AlphabeticKeyboard
import org.jyutping.jyutping.keyboard.CandidateBoard
import org.jyutping.jyutping.keyboard.CangjieKeyboard
import org.jyutping.jyutping.keyboard.CantoneseNumericKeyboard
import org.jyutping.jyutping.keyboard.CantoneseSymbolicKeyboard
import org.jyutping.jyutping.keyboard.EditingPanel
import org.jyutping.jyutping.keyboard.EmojiBoard
import org.jyutping.jyutping.keyboard.KeyboardForm
import org.jyutping.jyutping.keyboard.NumericKeyboard
import org.jyutping.jyutping.keyboard.QwertyForm
import org.jyutping.jyutping.keyboard.SettingsScreen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ import androidx.savedstate.SavedStateRegistry
import androidx.savedstate.SavedStateRegistryController
import androidx.savedstate.SavedStateRegistryOwner
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
import org.jyutping.jyutping.extensions.empty
import org.jyutping.jyutping.extensions.isReverseLookupTrigger
import org.jyutping.jyutping.extensions.separator
import org.jyutping.jyutping.extensions.separatorChar
import org.jyutping.jyutping.extensions.space
import org.jyutping.jyutping.extensions.toneConverted
import org.jyutping.jyutping.keyboard.Candidate
import org.jyutping.jyutping.keyboard.Cangjie
Expand All @@ -41,6 +37,7 @@ import org.jyutping.jyutping.keyboard.Stroke
import org.jyutping.jyutping.keyboard.Structure
import org.jyutping.jyutping.keyboard.length
import org.jyutping.jyutping.keyboard.transformed
import org.jyutping.jyutping.presets.PresetCharacter
import org.jyutping.jyutping.presets.PresetColor
import org.jyutping.jyutping.presets.PresetString
import org.jyutping.jyutping.utilities.DatabaseHelper
Expand Down Expand Up @@ -160,7 +157,7 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
if (isBuffering.value) {
val shouldKeepBuffer: Boolean = (keyboardForm.value == KeyboardForm.Alphabetic) || (keyboardForm.value == KeyboardForm.CandidateBoard)
if (!shouldKeepBuffer) {
bufferText = String.empty
bufferText = PresetString.EMPTY
}
}
keyboardForm.value = destination
Expand Down Expand Up @@ -242,7 +239,7 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
val candidateState: MutableIntState by lazy { mutableIntStateOf(1) }
val candidates: MutableState<List<Candidate>> by lazy { mutableStateOf(listOf()) }
private val db by lazy { DatabaseHelper(this, DatabasePreparer.databaseName) }
private var bufferText: String = String.empty
private var bufferText: String = PresetString.EMPTY
set(value) {
candidates.value = emptyList()
field = value
Expand Down Expand Up @@ -276,11 +273,11 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
} else {
val bestScheme = segmentation.firstOrNull()
val leadingLength: Int = bestScheme?.map { it.length }?.fold(0) { acc, i -> acc + i } ?: 0
val leadingText: String = bestScheme?.joinToString(separator = String.space) ?: String.empty
val leadingText: String = bestScheme?.joinToString(separator = PresetString.SPACE) ?: PresetString.EMPTY
when (leadingLength) {
0 -> text
text.length -> leadingText
else -> (leadingText + String.space + text.drop(leadingLength))
else -> (leadingText + PresetString.SPACE + text.drop(leadingLength))
}
}
}
Expand Down Expand Up @@ -346,11 +343,11 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
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
val leadingText: String = bestScheme?.joinToString(separator = PresetString.SPACE) { it.text } ?: PresetString.EMPTY
when (leadingLength) {
0 -> text
text.length -> leadingText
else -> (leadingText + String.space + text.drop(leadingLength))
else -> (leadingText + PresetString.SPACE + text.drop(leadingLength))
}
}
val mark = "q $tailMark"
Expand Down Expand Up @@ -389,7 +386,7 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
}
val isBuffering: MutableState<Boolean> by lazy { mutableStateOf(false) }
fun clearBuffer() {
bufferText = String.empty
bufferText = PresetString.EMPTY
}
fun process(text: String) {
when (inputMethodMode.value) {
Expand Down Expand Up @@ -419,13 +416,13 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
val tail = bufferText.drop(leadingLength)
bufferText = "${firstChar}${tail}"
} else {
bufferText = String.empty
bufferText = PresetString.EMPTY
}
}
else -> {
val inputLength: Int = candidate.input.replace(Regex("([456])"), "RR").length
var tail = bufferText.drop(inputLength)
while (tail.startsWith(Char.separatorChar)) {
while (tail.startsWith(PresetCharacter.SEPARATOR)) {
tail = tail.drop(1)
}
bufferText = tail
Expand All @@ -442,7 +439,7 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
fun performReturn() {
if (isBuffering.value) {
currentInputConnection.commitText(bufferText, bufferText.length)
bufferText = String.empty
bufferText = PresetString.EMPTY
} else {
sendDefaultEditorAction(true)
}
Expand All @@ -452,20 +449,20 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
val firstCandidate = candidates.value.firstOrNull()
if (firstCandidate == null) {
currentInputConnection.commitText(bufferText, bufferText.length)
bufferText = String.empty
bufferText = PresetString.EMPTY
} else {
select(firstCandidate)
}
} else {
currentInputConnection.commitText(String.space, String.space.length)
currentInputConnection.commitText(PresetString.SPACE, PresetString.SPACE.length)
}
}
fun dismissKeyboard() {
requestHideSelf(InputMethodManager.HIDE_NOT_ALWAYS)
}
fun leftKey() {
if (isBuffering.value) {
bufferText += String.separator
bufferText += PresetString.SEPARATOR
} else {
val text: String = when (inputMethodMode.value) {
InputMethodMode.Cantonese -> ""
Expand All @@ -476,7 +473,7 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
}
fun rightKey() {
if (isBuffering.value) {
bufferText += String.separator
bufferText += PresetString.SEPARATOR
} else {
val text: String = when (inputMethodMode.value) {
InputMethodMode.Cantonese -> ""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package org.jyutping.jyutping.extensions

val Char.Companion.separatorChar: Char
get() = '\''
import org.jyutping.jyutping.presets.PresetCharacter

val Char.Companion.spaceChar: Char
get() = ' '
fun Char.isSeparatorChar(): Boolean = (this == PresetCharacter.SEPARATOR)

fun Char.isSeparatorChar(): Boolean = this == Char.separatorChar

fun Char.isReverseLookupTrigger(): Boolean = reverseLookupTriggers.contains(this)
private val reverseLookupTriggers: Set<Char> = setOf('r', 'v', 'x', 'q')
fun Char.isReverseLookupTrigger(): Boolean = PresetCharacter.reverseLookupTriggers.contains(this)

/// is CJKV character
fun Char.isIdeographic(): Boolean = when (this.code) {
fun Char.isIdeographicChar(): Boolean = when (this.code) {
0x3007 -> true
in 0x4E00..0x9FFF -> true
in 0x3400..0x4DBF -> true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ import android.icu.text.Transliterator
fun String.convertedS2T(): String = Transliterator.getInstance("Simplified-Traditional").transliterate(this)
fun String.convertedT2S(): String = Transliterator.getInstance("Traditional-Simplified").transliterate(this)

val String.Companion.empty: String
get() = ""

val String.Companion.space: String
get() = " "

val String.Companion.separator: String
get() = "'"

/**
* Convert v/x/q to the tone digits
* @return Converted text with digital tones
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/org/jyutping/jyutping/keyboard/Candidate.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jyutping.jyutping.keyboard

import org.jyutping.jyutping.CharacterStandard
import org.jyutping.jyutping.extensions.space
import org.jyutping.jyutping.presets.PresetString
import org.jyutping.jyutping.utilities.DatabaseHelper
import org.jyutping.jyutping.utilities.HongKongVariantConverter
import org.jyutping.jyutping.utilities.Simplifier
Expand Down Expand Up @@ -32,9 +32,9 @@ data class Candidate(
}
operator fun plus(another: Candidate): Candidate {
val newText = this.text + another.text
val newRomanization = this.romanization + String.space + another.romanization
val newRomanization = this.romanization + PresetString.SPACE + another.romanization
val newInput = this.input + another.input
val newMark = this.mark + String.space + another.mark
val newMark = this.mark + PresetString.SPACE + another.mark
return Candidate(text = newText, romanization = newRomanization, input = newInput, mark = newMark)
}
}
Expand Down
31 changes: 15 additions & 16 deletions app/src/main/java/org/jyutping/jyutping/keyboard/Engine.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.jyutping.jyutping.keyboard

import org.jyutping.jyutping.extensions.empty
import org.jyutping.jyutping.extensions.isSeparatorChar
import org.jyutping.jyutping.extensions.separatorChar
import org.jyutping.jyutping.extensions.space
import org.jyutping.jyutping.presets.PresetCharacter
import org.jyutping.jyutping.presets.PresetString
import org.jyutping.jyutping.utilities.DatabaseHelper

object Engine {
Expand All @@ -19,7 +18,7 @@ object Engine {
}
}
private fun dispatch(text: String, segmentation: Segmentation, db: DatabaseHelper): List<Candidate> {
val hasSeparators: Boolean = text.contains(Char.separatorChar)
val hasSeparators: Boolean = text.contains(PresetCharacter.SEPARATOR)
val hasTones: Boolean = text.contains(Regex("[1-6]"))
return when {
hasSeparators && hasTones -> {
Expand Down Expand Up @@ -112,7 +111,7 @@ object Engine {
}
private fun processWithSeparators(text: String, segmentation: Segmentation, db: DatabaseHelper): List<Candidate> {
val separatorCount = text.count { it.isSeparatorChar() }
val textParts = text.split(Char.separatorChar).filter { it.isNotEmpty() }
val textParts = text.split(PresetCharacter.SEPARATOR).filter { it.isNotEmpty() }
val isHeadingSeparator: Boolean = text.firstOrNull()?.isSeparatorChar() ?: false
val isTrailingSeparator: Boolean = text.lastOrNull()?.isSeparatorChar() ?: false
val rawText = text.filter { !(it.isSeparatorChar()) }
Expand All @@ -138,13 +137,13 @@ object Engine {
when (syllables.size) {
1 -> {
if (item.input != textParts.firstOrNull()) continue
val combinedInput: String = item.input + Char.separatorChar
val combinedInput: String = item.input + PresetCharacter.SEPARATOR
val newItem = Candidate(text = item.text, romanization = item.romanization, input = combinedInput)
qualified.add(newItem)
}
2 -> {
if (syllables.firstOrNull() != textParts.firstOrNull()) continue
val combinedInput: String = item.input + Char.separatorChar
val combinedInput: String = item.input + PresetCharacter.SEPARATOR
val newItem = Candidate(text = item.text, romanization = item.romanization, input = combinedInput)
qualified.add(newItem)
}
Expand All @@ -155,7 +154,7 @@ object Engine {
when (syllables.size) {
1 -> {
if (item.input != textParts.firstOrNull()) continue
val combinedInput: String = item.input + Char.separatorChar
val combinedInput: String = item.input + PresetCharacter.SEPARATOR
val newItem = Candidate(text = item.text, romanization = item.romanization, input = combinedInput)
qualified.add(newItem)
}
Expand Down Expand Up @@ -223,8 +222,8 @@ object Engine {
val tail = text.drop(scheme.length())
val lastAnchor = tail.firstOrNull() ?: continue
val schemeAnchors = scheme.mapNotNull { it.text.firstOrNull() }
val anchors: String = (schemeAnchors + lastAnchor).joinToString(separator = String.empty)
val text2mark = scheme.joinToString(separator = String.space) { it.text } + String.space + tail
val anchors: String = (schemeAnchors + lastAnchor).joinToString(separator = PresetString.EMPTY)
val text2mark = scheme.joinToString(separator = PresetString.SPACE) { it.text } + PresetString.SPACE + tail
val shortcut = db.shortcut(anchors, limit)
.filter { candidate -> candidate.romanization.filter { it.isDigit().not() }.startsWith(text2mark) }
.map { Candidate(text = it.text, romanization = it.romanization, input = text, mark = text2mark) }
Expand Down Expand Up @@ -265,9 +264,9 @@ object Engine {
for (scheme in perfectSchemes) {
for (number in scheme.indices) {
val slice = scheme.dropLast(number)
val pingText = slice.joinToString(separator = String.empty) { it.origin }
val inputText = slice.joinToString(separator = String.empty) { it.text }
val text2mark = slice.joinToString(separator = String.space) { it.text }
val pingText = slice.joinToString(separator = PresetString.EMPTY) { it.origin }
val inputText = slice.joinToString(separator = PresetString.EMPTY) { it.text }
val text2mark = slice.joinToString(separator = PresetString.SPACE) { it.text }
val matched = db.match(text = pingText, input = inputText, mark = text2mark, limit = limit)
matches.add(matched)
}
Expand All @@ -276,9 +275,9 @@ object Engine {
} else {
val matches: MutableList<List<Candidate>> = mutableListOf()
for (scheme in segmentation) {
val pingText = scheme.joinToString(separator = String.empty) { it.origin }
val inputText = scheme.joinToString(separator = String.empty) { it.text }
val text2mark = scheme.joinToString(separator = String.space) { it.text }
val pingText = scheme.joinToString(separator = PresetString.EMPTY) { it.origin }
val inputText = scheme.joinToString(separator = PresetString.EMPTY) { it.text }
val text2mark = scheme.joinToString(separator = PresetString.SPACE) { it.text }
val matched = db.match(text = pingText, input = inputText, mark = text2mark, limit = limit)
matches.add(matched)
}
Expand Down
Loading

0 comments on commit 50b51aa

Please sign in to comment.