Skip to content

Commit

Permalink
feat: implement decompose()
Browse files Browse the repository at this point in the history
  • Loading branch information
WieeRd committed Feb 17, 2024
1 parent 7cfe1d2 commit a83da91
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions ricecake/hangul/compose.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
"""(De)composition of Hangul Jamo and Syllable."""

from . import offset
from . import offset as o

__all__ = ["compose", "decompose"]


# FEAT: LATER: compose syllable compat jamo
# | - provide separate function
# | - boolean flag parameter
# | - normalize the input every time
def compose(cho: str, jung: str, jong: str | None) -> str:
"""Composes Choseong, Jungseong, and an optional Jongseong into a Syllable.
Raises:
ValueError: If the characters are not appropriate Hangul Jamos.
"""
return chr(
offset.modern_choseong_offset(cho) * offset.CHOSEONG_COEF
+ offset.modern_jongseong_offset(jung) * offset.JUNGSEONG_COEF
+ (offset.modern_jongseong_offset(jong) if jong else 0)
+ offset.SYLLABLE_BASE
o.modern_choseong_offset(cho) * o.CHOSEONG_COEF
+ o.modern_jongseong_offset(jung) * o.JUNGSEONG_COEF
+ (o.modern_jongseong_offset(jong) if jong else 0)
+ o.SYLLABLE_BASE
)


def decompose(syl: str) -> tuple[str, str, str | None]:
"""Decomposes a Syllable into Choseong, Jungseong, and Jongseong."""
raise NotImplementedError
def decompose(c: str, /) -> tuple[str, str, str | None]:
"""Decomposes a Syllable into Choseong, Jungseong, and an optional Jongseong.
Raises:
ValueError: If the character is not a Hangul Syllable.
"""
syl = o.syllable_offset(c)

cho = syl // o.CHOSEONG_COEF
jung = syl % (o.CHOSEONG_COEF) // o.JUNGSEONG_COEF
jong = syl % o.JUNGSEONG_COEF

return (
chr(cho + o.MODERN_CHOSEONG_BASE),
chr(jung + o.MODERN_JUNGSEONG_BASE),
chr(jong + o.MODERN_JONGSEONG_BASE - 1) if jong else None,
)


# FEAT: decompose composite Jaum and Moum into tuple of str
Expand Down

0 comments on commit a83da91

Please sign in to comment.