Skip to content

Commit

Permalink
Dictionary: Check whether Phase list is empty when loop through each
Browse files Browse the repository at this point in the history
  • Loading branch information
1552980358 committed Jan 3, 2024
1 parent 114f5c5 commit 1ea0912
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
17 changes: 7 additions & 10 deletions src/main/kotlin/org/ks/chan/c2pinyin/StringDecode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@ internal fun String.decodeToPinyinList(
dictionary: Dictionary,
indexArray: IntArray = IntArray(length) { INDEX_INVALID }
): List<String> {
// Find and set phases in dictionary
if (dictionary.hasPhase) {
// Loop through each phase
dictionary.forEachPhase { phase ->
// Check if any phase exists in string
validate(phase = phase.text, indexArray = indexArray) { startIndex ->
// Replace all index
phase.forEachIndexed { index, word ->
indexArray[startIndex + index] = word.tableIndex
}
// Loop through each phase
dictionary.allPhases { phase ->
// Check if any phase exists in string
validate(phase = phase.text, indexArray = indexArray) { startIndex ->
// Replace all index
phase.forEachIndexed { index, word ->
indexArray[startIndex + index] = word.tableIndex
}
}
}
Expand Down
20 changes: 9 additions & 11 deletions src/main/kotlin/org/ks/chan/c2pinyin/dictionary/Dictionary.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.ks.chan.c2pinyin.decode.INDEX_INVALID
import org.ks.chan.c2pinyin.decode.INDEX_JUMP
import org.ks.chan.c2pinyin.decode.PinyinTable
import java.io.File
import kotlin.jvm.functions.FunctionN

/**
* [Dictionary]
Expand Down Expand Up @@ -46,19 +47,16 @@ class Dictionary {
private val wordList = ArrayList<Word>()

/**
* [Dictionary.hasPhase]
* @return [Boolean]
* [Dictionary.allPhases]
* @param action [kotlin.jvm.functions.Function1]
*
* Checks if [Dictionary.phaseList] is empty or not
* Loop through all [phaseList] and perform [action]
**/
val hasPhase: Boolean
get() = phaseList.isNotEmpty()

/**
*
**/
fun forEachPhase(action: (Phase) -> Unit) =
phaseList.forEach(action)
fun allPhases(action: (Phase) -> Unit) {
if (phaseList.isNotEmpty()) {
phaseList.forEach(action)
}
}

/**
* [Dictionary.findWord]
Expand Down

0 comments on commit 1ea0912

Please sign in to comment.