Compare commits

...

3 Commits

Author SHA1 Message Date
e1dbdb37eb Merge pull request 'Add method to test whether a string is a valid pinyin with tone number syllable' (#1) from add-is-valid-method into main
All checks were successful
Publish package / publish (push) Successful in 1m33s
Reviewed-on: #1
2024-09-20 10:58:27 +00:00
ba16e25917
Fix gradle issues
All checks were successful
Pull Request / build (pull_request) Successful in 1m57s
2024-09-20 12:55:41 +02:00
e897dd3c82
Add method to test whether a string is a valid pinyin with tone number syllable
Some checks failed
Pull Request / build (pull_request) Failing after 1m19s
2024-09-20 12:51:31 +02:00

View File

@ -2,6 +2,7 @@ package com.marvinelsen.chinese.transliteration
import java.io.InputStream import java.io.InputStream
@Suppress("MagicNumber", "MaximumLineLength", "MaxLineLength")
data class Syllable( data class Syllable(
val pinyinSyllableWithoutTone: String, val pinyinSyllableWithoutTone: String,
val tone: Tone, val tone: Tone,
@ -13,6 +14,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()
@ -20,7 +24,6 @@ data class Syllable(
require(lastCharacter.isDigit()) { require(lastCharacter.isDigit()) {
"'$pinyinWithToneNumber' is not a valid Pinyin with tone number syllable. Expected the last character to be a digit, but was '${pinyinWithToneNumber.last()}'" "'$pinyinWithToneNumber' is not a valid Pinyin with tone number syllable. Expected the last character to be a digit, but was '${pinyinWithToneNumber.last()}'"
} }
@Suppress("MagicNumber")
require(lastCharacter.digitToInt() in 1..5) { require(lastCharacter.digitToInt() in 1..5) {
"'$pinyinWithToneNumber' is not a valid Pinyin with tone number syllable. Expected the tone number 'n' to be in range 1 <= n <= 5, but was '${pinyinWithToneNumber.last()}'" "'$pinyinWithToneNumber' is not a valid Pinyin with tone number syllable. Expected the tone number 'n' to be in range 1 <= n <= 5, but was '${pinyinWithToneNumber.last()}'"
} }