-
Notifications
You must be signed in to change notification settings - Fork 1
/
easy_Korean_steno_chorded.py
189 lines (176 loc) · 4.63 KB
/
easy_Korean_steno_chorded.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# You need to use this with the Korean keyboard layout on your computer enabled
import re
LONGEST_KEY = 1
# list of keys for consonants and vowels
starting_consonants = {
"regular": {
# consonants
"K": "r", # ㄱ
"TKPW": "r", # ㄱ
"TPH": "s", # ㄴ
"TK": "e", # ㄷ
"R": "f", # ㄹ
"HR": "f", # ㄹ
"PH": "a", # ㅁ
"PW": "q", # ㅂ
"S": "t", # ㅅ
"W": "d", # ㅇ
"SKWR": "w", # ㅈ
"KH": "c", # ㅊ
"KP": "z", # ㅋ
"T": "x", # ㅌ
"P": "v", # ㅍ
"H": "g", # ㅎ
# extra
"TH": "E", # ㄸ
"": "d", # ㅇ
},
"tense": {
# tense consonants (add *)
"K": "R", # ㄲ
"TKPW": "R", # ㄲ
"TK": "E", # ㄸ
"PW": "Q", # ㅃ
"SKWR": "W", # ㅉ
"S": "T", # ㅆ
},
"special": {
# adds "y" to vowels
"KW": "r", # ㄱ
"TKPWR": "r", # ㄱ
"TPWH": "s", # ㄴ
"TKW": "e", # ㄷ
"WR": "f", # ㄹ
"WHR": "f", # ㄹ
"KPWHR": "a", # ㅁ
"KPWR": "q", # ㅂ
"SH": "t", # ㅅ
"KWR": "d", # ㅇ
"SKW": "w", # ㅈ
"KWH": "c", # ㅊ
"KPW": "z", # ㅋ
"TKWR": "x", # ㅌ
"KPR": "v", # ㅍ
"WH": "g", # ㅎ
"STKPWHR": "", # empty
},
}
vowels = {
# vowels
"A": "k", # ㅏ
"U": "j", # ㅓ
"O": "h", # ㅗ
"AO": "n", # ㅜ
"AOU": "m", # ㅡ
"AU": "m", # ㅡ
"EU": "l", # ㅣ
# complex vowels
"AEU": "o", # ㅐ
"AE": "p", # ㅔ
# extra/compound
"E": "ml", # ㅢ
"OEU": "hl", # ㅚ
"AOU": "nj", # ㅝ
"OU": "hk", # ㅘ
"AOEU": "nl", # ㅟ
"": "", # empty
}
y_vowels = {
# "y" vowels
"A": "i", # ㅑ
"U": "u", # ㅕ
"O": "y", # ㅛ
"OE": "y", # ㅛ
"AOU": "b", # ㅠ
"AO": "b", # ㅠ
"AEU": "O", # ㅒ
"AE": "P", # ㅖ
"EU": "l", # ㅣ
"": "", # empty
}
ending_consonants = {
# consonants
"G": "r", # ㄱ
"PB": "s", # ㄴ
"D": "e", # ㄷ
"R": "f", # ㄹ
"L": "f", # ㄹ
"PL": "a", # ㅁ
"B": "q", # ㅂ
"S": "t", # ㅅ
"PBG": "d", # ㅇ
"PBLG": "w", # ㅈ
"FP": "c", # ㅊ
"BG": "z", # ㅋ
"T": "x", # ㅌ
"P": "v", # ㅍ
"F": "g", # ㅎ
# tense consonants (add -R)
"RG": "R", # ㄲ
"RD": "E", # ㄸ
"RB": "Q", # ㅃ
"RPBLG": "W", # ㅉ
"RS": "T", # ㅆ
"Z": "T", # ㅆ
"": "", # empty
}
def lookup(chord):
stroke = chord[0]
if len(chord) != 1:
raise KeyError
assert len(chord) <= LONGEST_KEY
# backspacing
if stroke == "*":
return "{#left}{#right}{#backspace}"
# the regex decomposes a stroke into the following groups/variables:
# start consonants #STKPWHR
# vowel 1 AO
# stress start consonants */-
# vowel 2 EU
# end consonants FRPBLGTSDZ
match = re.fullmatch(r"([#STKPWHR]*)([AO]*)([*-]?)([EU]*)([FRPBLGTSDZ]*)", stroke)
if match is None:
raise KeyError
(
start_consonant,
vowel1,
stress,
vowel2,
end_consonant,
) = match.groups()
# get start consonant
add_y = False
# detect stress
if stress == "*" and start_consonant in starting_consonants["tense"]:
start_final = starting_consonants["tense"][start_consonant]
elif start_consonant in starting_consonants["regular"]:
start_final = starting_consonants["regular"][start_consonant]
elif start_consonant in starting_consonants["special"]:
add_y = True
start_final = starting_consonants["special"][start_consonant]
else:
raise KeyError
# get vowel
vowel = vowel1 + vowel2
if vowel not in vowels and vowel not in y_vowels:
raise KeyError
if start_consonant in starting_consonants["special"] and not vowel:
raise KeyError
if add_y:
vowel_final = y_vowels[vowel]
else:
vowel_final = vowels[vowel]
# only vowel output using *
if stress == "*" and start_consonant == "":
start_final = ""
# get end consonant
if end_consonant not in ending_consonants:
raise KeyError
# only end output
if end_consonant and start_consonant == "":
start_final = ""
# combine output
output = (
"{^}" + start_final + vowel_final + ending_consonants[end_consonant] + "{^}"
)
return output