This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipa_chording.py
206 lines (150 loc) · 4.33 KB
/
ipa_chording.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from plover.steno import Stroke
from plover.system import KEYS
from collections import defaultdict
from enum import Enum
from typing import TYPE_CHECKING
if TYPE_CHECKING:
KEYS: tuple[str] = ()
class Modifier(Enum):
EXIT = 0
_CHORDS = {
"STKPWH": ("ʒ", ()),
"SKWR": ("d͡ʒ", ()),
"SH": ("ʃ", ()),
"TH": ("θ", ()),
"KH": ("t͡ʃ", ()),
"R": ("ɹ", ()),
"TWH": ("ð", ()),
"A": ("æ", ()),
"AO": ("ʊ", ()),
"AOE": ("iː", ()),
"AOEU": ("aɪ", ()),
"AE": ("ə", ()),
"AEU": ("eɪ", ()),
"AER": ("ɚ", ()),
"AU": ("ɔ", ()),
"AR": ("ɑɹ", ()),
"O": ("ɑ", ()),
"O*": ("ɒ", ()),
"OE": ("oʊ", ()),
"OEU": ("ɔɪ", ()),
"OU": ("aʊ", ()),
"OR": ("ɔr", ()),
"E": ("ɛ", ()),
"EU": ("ɪ", ()),
"ER": ("ɝ", ()),
"U": ("ʌ", ()),
"UR": ("ɚ", ()),
"EFRPB": ("ɝt͡ʃ", ()),
"UFRPB": ("ɚt͡ʃ", ()),
"-FRPBLG": ("nt͡ʃ", ()),
"-FRPB": ("ɹt͡ʃ", ()),
"-FP": ("t͡ʃ", ()),
"-FPB": ("ʃ", ()),
"-RB": ("ʒ", ()),
"-R": ("ɹ", ()),
"-PBLG": ("d͡ʒ", ()),
"-PBG": ("ŋ", ()),
"EFRPBLG": ("ɛnt͡ʃ", ()),
"UFRPBLG": ("ʌnt͡ʃ", ()),
"S": ("s", ()),
"T": ("t", ()),
"K": ("k", ()),
"P": ("p", ()),
"W": ("w", ()),
"H": ("h", ()),
"STKPW": ("z", ()),
"SKWR": ("j", ()),
"SR": ("v", ()),
"TK": ("d", ()),
"TKPW": ("g", ()),
"TP": ("f", ()),
"TPH": ("n", ()),
"KWR": ("j", ()),
"PW": ("b", ()),
"PH": ("m", ()),
"HR": ("l", ()),
"-F": ("f", ()),
"-P": ("p", ()),
"-B": ("b", ()),
"-L": ("l", ()),
"-G": ("g", ()),
"-T": ("t", ()),
"-S": ("s", ()),
"-D": ("d", ()),
"-Z": ("z", ()),
"-FB": ("v", ()),
"-PB": ("n", ()),
"-PL": ("m", ()),
"-BG": ("k", ()),
}
# Entries which are translated normally
_SPECIAL_ENTRIES = {
"TPWOPBG": "{plover:end_solo_dict}{^}/",
"#STRES": "{^}ˈ{^}",
"#STR*ES": "{^}ˌ{^}",
"#EPLT": "{^}◌{^}",
"#HROPBG": "{^}ː{^}",
"#STOP": "{^}ʔ{^}",
"SKWR*": "ʤ",
"TH*": "ð",
"*T": "ð",
"KH*": "ʧ",
"H*": "ʰ",
"TPH*": "n̩",
"S-P": "{^ ^}",
}
# Change control chords and special entries depending on whether an extended stenotype system is being used
# # For regular extended stenotype
# if "^-" in KEYS and "_" not in KEYS and "&-" not in KEYS:
# For custom extended stenotype
if "^-" in KEYS and "_" in KEYS and "&-" in KEYS:
_CHORDS.update({
"&": ("", (Modifier.EXIT,)),
})
_SPECIAL_ENTRIES.update({
"&": "{#}{plover:end_solo_dict}",
"_": "{^ ^}",
})
# # For regular English stenotype and other systems
# else:
# Build the dictionary which is used to identify chords in a stroke
_CHORD_RESULT = "chord" # arbitrary string
_construct_trie = lambda: defaultdict(_construct_trie)
_trie = _construct_trie()
for chord, entry in _CHORDS.items():
current_dict = _trie
for key in Stroke.from_steno(chord).keys():
current_dict = current_dict[key]
current_dict[_CHORD_RESULT] = entry
_UNDO_STROKE = "*"
#region Exports
LONGEST_KEY = 1
def lookup(strokes_steno: tuple[str]) -> str:
steno = strokes_steno[0]
if steno == _UNDO_STROKE: raise KeyError
if steno in _SPECIAL_ENTRIES:
return _SPECIAL_ENTRIES[steno]
out = ""
modifiers: set[Modifier] = set()
keys: tuple[str] = Stroke.from_steno(strokes_steno[0]).keys()
chord_start_index = 0
while chord_start_index < len(keys):
current_trie = _trie
longest_chord_found, longest_chord_modifiers = ("", ())
longest_chord_end_index = chord_start_index
for seek_index in range(chord_start_index, len(keys)):
key = keys[seek_index]
if key not in current_trie: break
current_trie = current_trie[key]
if _CHORD_RESULT in current_trie:
longest_chord_found, longest_chord_modifiers = current_trie[_CHORD_RESULT]
longest_chord_end_index = seek_index
out += longest_chord_found
modifiers.update(longest_chord_modifiers)
chord_start_index = longest_chord_end_index + 1
translation = f"{{&{out}}}" if len(out) > 0 else ""
if Modifier.EXIT in modifiers:
translation += "{plover:end_solo_dict}"
return translation
#endregion