Add method to test whether a string is a valid pinyin with tone number syllable #1
@ -13,6 +13,9 @@ data class Syllable(
|
|||||||
private val zhuyinToPinyin = pinyinToZhuyin.entries.associate { it.value to it.key }
|
private val zhuyinToPinyin = pinyinToZhuyin.entries.associate { it.value to it.key }
|
||||||
private val zhuyinToneMarkRegex = """[ˊˇˋ˙]""".toRegex()
|
private val zhuyinToneMarkRegex = """[ˊˇˋ˙]""".toRegex()
|
||||||
|
|
||||||
|
fun isValidPinyinWithToneNumberSyllable(pinyinSyllable: String) =
|
||||||
|
pinyinSyllable.last().isDigit() && pinyinSyllable.last().digitToInt() in 1..5 && pinyinSyllable.lowercase() in pinyinToZhuyin
|
||||||
|
|
||||||
fun fromPinyinWithToneNumber(pinyinWithToneNumber: String): Syllable {
|
fun fromPinyinWithToneNumber(pinyinWithToneNumber: String): Syllable {
|
||||||
val pinyinWithoutNumber = pinyinWithToneNumber.substring(0, pinyinWithToneNumber.lastIndex)
|
val pinyinWithoutNumber = pinyinWithToneNumber.substring(0, pinyinWithToneNumber.lastIndex)
|
||||||
val lastCharacter = pinyinWithToneNumber.last()
|
val lastCharacter = pinyinWithToneNumber.last()
|
||||||
@ -42,7 +45,7 @@ data class Syllable(
|
|||||||
return Syllable(
|
return Syllable(
|
||||||
zhuyinToPinyin[zhuyinWithoutToneMark]!!,
|
zhuyinToPinyin[zhuyinWithoutToneMark]!!,
|
||||||
Tone.fromZhuyinToneMarkOrNull(zhuyin.last()) ?: Tone.fromZhuyinToneMarkOrNull(zhuyin.first())
|
Tone.fromZhuyinToneMarkOrNull(zhuyin.last()) ?: Tone.fromZhuyinToneMarkOrNull(zhuyin.first())
|
||||||
?: Tone.FIRST
|
?: Tone.FIRST
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user