Compare commits

..

No commits in common. "be194b20e164c54ee574929c8c3b58970a3f9d55" and "fffda1ce62942ff6888c472c917587fcdd64251a" have entirely different histories.

10 changed files with 134 additions and 138 deletions

View File

@ -5,7 +5,7 @@ plugins {
}
group = "com.marvinelsen"
version = "2.0.0"
version = "1.0.1"
repositories {
mavenCentral()

View File

@ -1,9 +1,9 @@
package com.marvinelsen.chinese.transliteration.api
package com.marvinelsen.chinese.transliteration
import java.io.InputStream
@Suppress("MagicNumber", "MaximumLineLength", "MaxLineLength")
data class PinyinSyllable(
data class Syllable(
val pinyinSyllableWithoutTone: String,
val tone: Tone,
) {
@ -19,7 +19,7 @@ data class PinyinSyllable(
.substring(0, pinyinSyllable.lastIndex)
.lowercase() in pinyinToZhuyin
fun fromPinyinWithToneNumber(pinyinWithToneNumber: String): PinyinSyllable {
fun fromPinyinWithToneNumber(pinyinWithToneNumber: String): Syllable {
val pinyinWithoutNumber = pinyinWithToneNumber.substring(0, pinyinWithToneNumber.lastIndex)
val lastCharacter = pinyinWithToneNumber.last()
@ -33,18 +33,18 @@ data class PinyinSyllable(
pinyinWithoutNumber.lowercase() in pinyinToZhuyin
) { "'$pinyinWithoutNumber' is not a valid Pinyin syllable." }
return PinyinSyllable(
return Syllable(
pinyinSyllableWithoutTone = pinyinWithoutNumber,
tone = Tone.fromDigit(lastCharacter)
)
}
fun fromZhuyin(zhuyin: String): PinyinSyllable {
fun fromZhuyin(zhuyin: String): Syllable {
val zhuyinWithoutToneMark = zhuyin.replace(zhuyinToneMarkRegex, "")
require(zhuyinWithoutToneMark in zhuyinToPinyin) { "'$zhuyin' is not a valid Zhuyin syllable." }
return PinyinSyllable(
return Syllable(
zhuyinToPinyin[zhuyinWithoutToneMark]!!,
Tone.fromZhuyinToneMarkOrNull(zhuyin.last()) ?: Tone.fromZhuyinToneMarkOrNull(zhuyin.first())
?: Tone.FIRST

View File

@ -1,4 +1,4 @@
package com.marvinelsen.chinese.transliteration.api
package com.marvinelsen.chinese.transliteration
@Suppress("MagicNumber")
enum class Tone {

View File

@ -1,4 +1,4 @@
package com.marvinelsen.chinese.transliteration.api
package com.marvinelsen.chinese.transliteration
enum class TransliterationSystem {
ZHUYIN, PINYIN_WITH_TONE_NUMBERS, PINYIN_WITH_TONE_MARKS

View File

@ -0,0 +1,5 @@
package com.marvinelsen.chinese.transliteration
object Zhuyin {
const val SEPARATOR = " "
}

View File

@ -1,5 +0,0 @@
package com.marvinelsen.chinese.transliteration.api
object Zhuyin {
const val SEPARATOR = " "
}

View File

@ -203,9 +203,6 @@ lüe ㄌㄩㄝ
lu:n ㄌㄩㄣ
lvn ㄌㄩㄣ
lün ㄌㄩㄣ
lu:an ㄌㄩㄢ
lvan ㄌㄩㄢ
lüan ㄌㄩㄢ
m ㄇ
ma ㄇㄚ
mai ㄇㄞ
@ -384,7 +381,6 @@ xuan ㄒㄩㄢ
xue ㄒㄩㄝ
xun ㄒㄩㄣ
ya ㄧㄚ
yai ㄧㄞ
yan ㄧㄢ
yang ㄧㄤ
yao ㄧㄠ

1 a
203 lu:n ㄌㄩㄣ
204 lvn ㄌㄩㄣ
205 lün ㄌㄩㄣ
lu:an ㄌㄩㄢ
lvan ㄌㄩㄢ
lüan ㄌㄩㄢ
206 m
207 ma ㄇㄚ
208 mai ㄇㄞ
381 xue ㄒㄩㄝ
382 xun ㄒㄩㄣ
383 ya ㄧㄚ
yai ㄧㄞ
384 yan ㄧㄢ
385 yang ㄧㄤ
386 yao ㄧㄠ

View File

@ -0,0 +1,119 @@
package com.marvinelsen.chinese.transliteration
import io.kotest.core.spec.style.ShouldSpec
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe
class SyllableTest : ShouldSpec({
context("from pinyin with tone numbers") {
withData(
nameFn = { "'${it.first}' -> ${it.second}" },
"sheng1" to Syllable("sheng", Tone.FIRST),
"zhi2" to Syllable("zhi", Tone.SECOND),
"ka3" to Syllable("ka", Tone.THIRD),
"yao4" to Syllable("yao", Tone.FORTH),
"me5" to Syllable("me", Tone.FIFTH),
"Me5" to Syllable("Me", Tone.FIFTH),
"nv3" to Syllable("nv", Tone.THIRD),
"nü3" to Syllable("", Tone.THIRD),
"nu:3" to Syllable("nu:", Tone.THIRD),
"r5" to Syllable("r", Tone.FIFTH),
"R5" to Syllable("R", Tone.FIFTH),
"er2" to Syllable("er", Tone.SECOND),
"Er2" to Syllable("Er", Tone.SECOND),
) { (pinyinWithNumber, expectedSyllable) ->
Syllable.fromPinyinWithToneNumber(pinyinWithNumber) shouldBe expectedSyllable
}
}
context("from zhuyin") {
withData(
nameFn = { "'${it.first}' -> ${it.second}" },
"ㄕㄥ" to Syllable("sheng", Tone.FIRST),
"ㄓˊ" to Syllable("zhi", Tone.SECOND),
"ㄎㄚˇ" to Syllable("ka", Tone.THIRD),
"ㄧㄠˋ" to Syllable("yao", Tone.FORTH),
"ㄇㄜ˙" to Syllable("me", Tone.FIFTH),
"˙ㄇㄜ" to Syllable("me", Tone.FIFTH),
"ㄋㄩˇ" to Syllable("", Tone.THIRD),
) { (zhuyin, expectedSyllable) ->
Syllable.fromZhuyin(zhuyin) shouldBe expectedSyllable
}
}
context("from invalid pinyin with tone numbers") {
}
context("from invalid zhuyin") {
}
context("format to zhuyin") {
withData(
nameFn = { "${it.first.format(TransliterationSystem.PINYIN_WITH_TONE_NUMBERS)} -> ${it.second}" },
Syllable("sheng", Tone.FIRST) to "ㄕㄥ",
Syllable("zhi", Tone.SECOND) to "ㄓˊ",
Syllable("ka", Tone.THIRD) to "ㄎㄚˇ",
Syllable("yao", Tone.FORTH) to "ㄧㄠˋ",
Syllable("me", Tone.FIFTH) to "˙ㄇㄜ",
Syllable("", Tone.THIRD) to "ㄋㄩˇ",
Syllable("nu:", Tone.THIRD) to "ㄋㄩˇ",
Syllable("nv", Tone.THIRD) to "ㄋㄩˇ",
Syllable("r", Tone.FIFTH) to "˙ㄦ",
Syllable("R", Tone.FIFTH) to "˙ㄦ",
Syllable("er", Tone.SECOND) to "ㄦˊ",
Syllable("Er", Tone.SECOND) to "ㄦˊ",
) { (syllable, expectedZhuyin) ->
syllable.format(TransliterationSystem.ZHUYIN) shouldBe expectedZhuyin
}
}
context("format to pinyin with tone numbers") {
withData(
nameFn = { "${it.first.format(TransliterationSystem.PINYIN_WITH_TONE_NUMBERS)} -> ${it.second}" },
Syllable("sheng", Tone.FIRST) to "sheng1",
Syllable("zhi", Tone.SECOND) to "zhi2",
Syllable("ka", Tone.THIRD) to "ka3",
Syllable("yao", Tone.FORTH) to "yao4",
Syllable("me", Tone.FIFTH) to "me5",
Syllable("", Tone.THIRD) to "nü3",
Syllable("nu:", Tone.THIRD) to "nu:3",
Syllable("nv", Tone.THIRD) to "nv3",
Syllable("r", Tone.FIFTH) to "r5",
Syllable("R", Tone.FIFTH) to "R5",
Syllable("er", Tone.SECOND) to "er2",
Syllable("Er", Tone.SECOND) to "Er2",
) { (syllable, expectedPinyinWithToneNumbers) ->
syllable.format(TransliterationSystem.PINYIN_WITH_TONE_NUMBERS) shouldBe expectedPinyinWithToneNumbers
}
}
context("format to pinyin with tone marks") {
withData(
nameFn = { "${it.first.format(TransliterationSystem.PINYIN_WITH_TONE_NUMBERS)} -> ${it.second}" },
Syllable("sheng", Tone.FIRST) to "shēng",
Syllable("zhi", Tone.SECOND) to "zhí",
Syllable("ka", Tone.THIRD) to "kǎ",
Syllable("yao", Tone.FORTH) to "yào",
Syllable("me", Tone.FIFTH) to "me",
Syllable("zhui", Tone.FIRST) to "zhuī",
Syllable("liu", Tone.FIRST) to "liū",
Syllable("", Tone.THIRD) to "nǚ",
Syllable("nu:", Tone.THIRD) to "nǚ",
Syllable("nv", Tone.THIRD) to "nǚ",
Syllable("r", Tone.FIFTH) to "r",
Syllable("er", Tone.SECOND) to "ér",
Syllable("Er", Tone.SECOND) to "Ér",
) { (syllable, expectedPinyinWithToneMarks) ->
syllable.format(TransliterationSystem.PINYIN_WITH_TONE_MARKS) shouldBe expectedPinyinWithToneMarks
}
}
context("format to zhuyin with invalid pinyin syllable") {
}
context("format to pinyin with tone diacritics with invalid pinyin syllable") {
}
context("format to pinyin with tone numbers with invalid pinyin syllable") {
}
})

View File

@ -1,4 +1,4 @@
package com.marvinelsen.chinese.transliteration.api
package com.marvinelsen.chinese.transliteration
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.ShouldSpec

View File

@ -1,119 +0,0 @@
package com.marvinelsen.chinese.transliteration.api
import io.kotest.core.spec.style.ShouldSpec
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe
class PinyinSyllableTest : ShouldSpec({
context("from pinyin with tone numbers") {
withData(
nameFn = { "'${it.first}' -> ${it.second}" },
"sheng1" to PinyinSyllable("sheng", Tone.FIRST),
"zhi2" to PinyinSyllable("zhi", Tone.SECOND),
"ka3" to PinyinSyllable("ka", Tone.THIRD),
"yao4" to PinyinSyllable("yao", Tone.FORTH),
"me5" to PinyinSyllable("me", Tone.FIFTH),
"Me5" to PinyinSyllable("Me", Tone.FIFTH),
"nv3" to PinyinSyllable("nv", Tone.THIRD),
"nü3" to PinyinSyllable("", Tone.THIRD),
"nu:3" to PinyinSyllable("nu:", Tone.THIRD),
"r5" to PinyinSyllable("r", Tone.FIFTH),
"R5" to PinyinSyllable("R", Tone.FIFTH),
"er2" to PinyinSyllable("er", Tone.SECOND),
"Er2" to PinyinSyllable("Er", Tone.SECOND),
) { (pinyinWithNumber, expectedSyllable) ->
PinyinSyllable.fromPinyinWithToneNumber(pinyinWithNumber) shouldBe expectedSyllable
}
}
context("from zhuyin") {
withData(
nameFn = { "'${it.first}' -> ${it.second}" },
"ㄕㄥ" to PinyinSyllable("sheng", Tone.FIRST),
"ㄓˊ" to PinyinSyllable("zhi", Tone.SECOND),
"ㄎㄚˇ" to PinyinSyllable("ka", Tone.THIRD),
"ㄧㄠˋ" to PinyinSyllable("yao", Tone.FORTH),
"ㄇㄜ˙" to PinyinSyllable("me", Tone.FIFTH),
"˙ㄇㄜ" to PinyinSyllable("me", Tone.FIFTH),
"ㄋㄩˇ" to PinyinSyllable("", Tone.THIRD),
) { (zhuyin, expectedSyllable) ->
PinyinSyllable.fromZhuyin(zhuyin) shouldBe expectedSyllable
}
}
context("from invalid pinyin with tone numbers") {
}
context("from invalid zhuyin") {
}
context("format to zhuyin") {
withData(
nameFn = { "${it.first.format(TransliterationSystem.PINYIN_WITH_TONE_NUMBERS)} -> ${it.second}" },
PinyinSyllable("sheng", Tone.FIRST) to "ㄕㄥ",
PinyinSyllable("zhi", Tone.SECOND) to "ㄓˊ",
PinyinSyllable("ka", Tone.THIRD) to "ㄎㄚˇ",
PinyinSyllable("yao", Tone.FORTH) to "ㄧㄠˋ",
PinyinSyllable("me", Tone.FIFTH) to "˙ㄇㄜ",
PinyinSyllable("", Tone.THIRD) to "ㄋㄩˇ",
PinyinSyllable("nu:", Tone.THIRD) to "ㄋㄩˇ",
PinyinSyllable("nv", Tone.THIRD) to "ㄋㄩˇ",
PinyinSyllable("r", Tone.FIFTH) to "˙ㄦ",
PinyinSyllable("R", Tone.FIFTH) to "˙ㄦ",
PinyinSyllable("er", Tone.SECOND) to "ㄦˊ",
PinyinSyllable("Er", Tone.SECOND) to "ㄦˊ",
) { (syllable, expectedZhuyin) ->
syllable.format(TransliterationSystem.ZHUYIN) shouldBe expectedZhuyin
}
}
context("format to pinyin with tone numbers") {
withData(
nameFn = { "${it.first.format(TransliterationSystem.PINYIN_WITH_TONE_NUMBERS)} -> ${it.second}" },
PinyinSyllable("sheng", Tone.FIRST) to "sheng1",
PinyinSyllable("zhi", Tone.SECOND) to "zhi2",
PinyinSyllable("ka", Tone.THIRD) to "ka3",
PinyinSyllable("yao", Tone.FORTH) to "yao4",
PinyinSyllable("me", Tone.FIFTH) to "me5",
PinyinSyllable("", Tone.THIRD) to "nü3",
PinyinSyllable("nu:", Tone.THIRD) to "nu:3",
PinyinSyllable("nv", Tone.THIRD) to "nv3",
PinyinSyllable("r", Tone.FIFTH) to "r5",
PinyinSyllable("R", Tone.FIFTH) to "R5",
PinyinSyllable("er", Tone.SECOND) to "er2",
PinyinSyllable("Er", Tone.SECOND) to "Er2",
) { (syllable, expectedPinyinWithToneNumbers) ->
syllable.format(TransliterationSystem.PINYIN_WITH_TONE_NUMBERS) shouldBe expectedPinyinWithToneNumbers
}
}
context("format to pinyin with tone marks") {
withData(
nameFn = { "${it.first.format(TransliterationSystem.PINYIN_WITH_TONE_NUMBERS)} -> ${it.second}" },
PinyinSyllable("sheng", Tone.FIRST) to "shēng",
PinyinSyllable("zhi", Tone.SECOND) to "zhí",
PinyinSyllable("ka", Tone.THIRD) to "kǎ",
PinyinSyllable("yao", Tone.FORTH) to "yào",
PinyinSyllable("me", Tone.FIFTH) to "me",
PinyinSyllable("zhui", Tone.FIRST) to "zhuī",
PinyinSyllable("liu", Tone.FIRST) to "liū",
PinyinSyllable("", Tone.THIRD) to "nǚ",
PinyinSyllable("nu:", Tone.THIRD) to "nǚ",
PinyinSyllable("nv", Tone.THIRD) to "nǚ",
PinyinSyllable("r", Tone.FIFTH) to "r",
PinyinSyllable("er", Tone.SECOND) to "ér",
PinyinSyllable("Er", Tone.SECOND) to "Ér",
) { (syllable, expectedPinyinWithToneMarks) ->
syllable.format(TransliterationSystem.PINYIN_WITH_TONE_MARKS) shouldBe expectedPinyinWithToneMarks
}
}
context("format to zhuyin with invalid pinyin syllable") {
}
context("format to pinyin with tone diacritics with invalid pinyin syllable") {
}
context("format to pinyin with tone numbers with invalid pinyin syllable") {
}
})