cantone / jyutping.py
AlienKevin's picture
Add app.py and model
b256b6f
def extract_tone(syllable: str) -> int:
return int(syllable[-1]) - 1
jyutping_initials = ['βˆ…', 'ng', 'gw', 'kw', 'b', 'p', 'm', 'f', 'd', 't', 'n', 'l', 'g', 'k', 'h', 'w', 'z', 'c', 's', 'j']
def extract_initial(syllable: str) -> (int, str):
for i, initial in enumerate(jyutping_initials):
if syllable.startswith(initial):
return (i, initial)
return (0, '')
def inflate_initial(initial: int) -> str:
return jyutping_initials[initial] if initial != 0 else ''
jyutping_nuclei = ['βˆ…', 'aa', 'yu', 'eo', 'oe', 'a', 'i', 'u', 'e', 'o']
def extract_nucleus(syllable: str, initial: str) -> int:
syllable = syllable[len(initial):]
for i, nucleus in enumerate(jyutping_nuclei):
if syllable.startswith(nucleus):
return (i, nucleus)
return (0, '')
def inflate_nucleus(nucleus: int) -> str:
return jyutping_nuclei[nucleus] if nucleus != 0 else ''
jyutping_codas = ['βˆ…', 'ng', 'p', 't', 'k', 'm', 'n', 'i', 'u']
def extract_coda(syllable: str, initial: str, nucleus: str) -> int:
syllable = syllable[len(initial) + len(nucleus):]
for i, coda in enumerate(jyutping_codas):
if syllable.startswith(coda):
return (i, coda)
return (0, '')
def inflate_coda(coda: int) -> str:
return jyutping_codas[coda] if coda != 0 else ''
syllable = 'neoi5'
initial_int, initial = extract_initial(syllable)
nucleus_int, nucleus = extract_nucleus(syllable, initial)
coda_int, coda = extract_coda(syllable, initial, nucleus)
assert(initial == 'n' and initial_int == 10)
assert(nucleus == 'eo' and nucleus_int == 3)
assert(coda == 'i' and coda_int == 7)
syllable = 'gwok3'
initial_int, initial = extract_initial(syllable)
nucleus_int, nucleus = extract_nucleus(syllable, initial)
coda_int, coda = extract_coda(syllable, initial, nucleus)
assert(initial == 'gw' and initial_int == 2)
assert(nucleus == 'o' and nucleus_int == 9)
assert(coda == 'k' and coda_int == 4)
syllable = 'oi3'
initial_int, initial = extract_initial(syllable)
nucleus_int, nucleus = extract_nucleus(syllable, initial)
coda_int, coda = extract_coda(syllable, initial, nucleus)
assert(initial == '' and initial_int == 0)
assert(nucleus == 'o' and nucleus_int == 9)
assert(coda == 'i' and coda_int == 7)
syllable = 'ng4'
initial_int, initial = extract_initial(syllable)
nucleus_int, nucleus = extract_nucleus(syllable, initial)
coda_int, coda = extract_coda(syllable, initial, nucleus)
assert(initial == 'ng' and initial_int == 1)
assert(nucleus == '' and nucleus_int == 0)
assert(coda == '' and coda_int == 0)
syllable = 'ngo5'
initial_int, initial = extract_initial(syllable)
nucleus_int, nucleus = extract_nucleus(syllable, initial)
coda_int, coda = extract_coda(syllable, initial, nucleus)
assert(initial == 'ng' and initial_int == 1)
assert(nucleus == 'o' and nucleus_int == 9)
assert(coda == '' and coda_int == 0)
syllable = 'a3'
initial_int, initial = extract_initial(syllable)
nucleus_int, nucleus = extract_nucleus(syllable, initial)
coda_int, coda = extract_coda(syllable, initial, nucleus)
assert(initial == '' and initial_int == 0)
assert(nucleus == 'a' and nucleus_int == 5)
assert(coda == '' and coda_int == 0)
syllable = 'aa3'
initial_int, initial = extract_initial(syllable)
nucleus_int, nucleus = extract_nucleus(syllable, initial)
coda_int, coda = extract_coda(syllable, initial, nucleus)
assert(initial == '' and initial_int == 0)
assert(nucleus == 'aa' and nucleus_int == 1)
assert(coda == '' and coda_int == 0)
syllable = 'ngaang6'
initial_int, initial = extract_initial(syllable)
nucleus_int, nucleus = extract_nucleus(syllable, initial)
coda_int, coda = extract_coda(syllable, initial, nucleus)
assert(initial == 'ng' and initial_int == 1)
assert(nucleus == 'aa' and nucleus_int == 1)
assert(coda == 'ng' and coda_int == 1)
def extract_jyutping(syllable: str) -> (int, int, int, int):
initial_int, initial = extract_initial(syllable)
nucleus_int, nucleus = extract_nucleus(syllable, initial)
coda_int, _ = extract_coda(syllable, initial, nucleus)
tone = extract_tone(syllable)
return (initial_int, nucleus_int, coda_int, tone)
def inflate_jyutping(initial: int, nucleus: int, coda: int, tone: int) -> str:
return f"{inflate_initial(initial)}{inflate_nucleus(nucleus)}{inflate_coda(coda)}{tone + 1}"