Merge pull request 'Implement version 2.0.0' (#5) from develop into main
All checks were successful
Publish package / publish (push) Successful in 1m44s

Reviewed-on: #5
This commit is contained in:
Marvin Elsen 2024-10-07 18:54:45 +00:00
commit 4b11926fe3
10 changed files with 138 additions and 134 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

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

View File

@ -1,119 +0,0 @@
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

@ -0,0 +1,119 @@
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") {
}
})

View File

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