Improve find characters query to only show characters with the same pronunciation
Some checks failed
Pull Request / build (pull_request) Failing after 2m42s

This commit is contained in:
Marvin Elsen 2024-10-10 23:17:13 +02:00
parent af3a291536
commit ea1973a3cf
Signed by: marvinelsen
GPG Key ID: 820672408CC318C2

View File

@ -78,10 +78,11 @@ class SqliteDictionary(private val connection: Connection) : Dictionary {
}
private val findCharacters = """
WITH cte(id, character) AS (VALUES ?)
WITH cte(id, character, syllable) AS (VALUES ?)
SELECT traditional, simplified, pinyin_with_tone_marks, pinyin_with_tone_numbers, zhuyin, cedict_definitions, cross_straits_definitions, moe_definitions
FROM entry INNER JOIN cte
ON cte.character = entry.traditional OR cte.character = entry.simplified
WHERE cte.syllable = entry.pinyin_with_tone_numbers
ORDER BY cte.id
""".trimIndent()
@ -105,13 +106,24 @@ class SqliteDictionary(private val connection: Connection) : Dictionary {
}
override fun findCharacters(entry: DictionaryEntry): List<DictionaryEntry> {
val characterList = entry.traditional
val pinyinSyllablesWithToneNumbers = entry.pinyinWithToneNumbers
.lowercase()
.split(" ")
.filter { it.isNotBlank() }
.mapIndexed { index, s -> "($index, '$s')" }
val characters = entry.traditional
.split("")
.filter { it.isNotBlank() }
.filter { it != "" }
.filter { it != "·" }
val charactersWithSyllables = characters.zip(pinyinSyllablesWithToneNumbers)
val queryInput = charactersWithSyllables
.mapIndexed { index, s -> "($index, '${s.first}', '${s.second}')" }
.joinToString(",")
val query = findCharacters.replace("?", characterList)
val query = findCharacters.replace("?", queryInput)
val resultSet: ResultSet = connection.createStatement().executeQuery(query)