File size: 4,316 Bytes
b256b6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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}"