-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnerd-dictation.py
283 lines (223 loc) · 7.12 KB
/
nerd-dictation.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# User configuration file typically located at `~/.config/nerd-dictation/nerd-dictation.py`
import re
# -----------------------------------------------------------------------------
# Replace Multiple Words
TEXT_REPLACE_REGEX = (
("\\b" "data type" "\\b", "data-type"),
("\\b" "copy on write" "\\b", "copy-on-write"),
("\\b" "key word" "\\b", "keyword"),
("\\b" "x ray" "\\b", "x-ray"),
("\\b" "be factor" "\\b", "refactor"),
("\\b" "pre factor" "\\b", "refactor"),
("\\b" "a wait" "\\b", "await"),
("\\b" "awaits" "\\b", "await"),
("\\b" "a sink" "\\b", "async"),
("\\b" "it a raider" "\\b", "iterator"),
("\\b" "localhost" "\\b", "localhost"),
("\\b" "hyper core" "\\b", "hypercore"),
("\\b" "get hub" "\\b", "Github"),
("\\b" "git hub" "\\b", "Github"),
("\\b" "good have" "\\b", "Github"),
("\\b" "get have" "\\b", "Github"),
("\\b" "fedor verse" "\\b", "Fediverse"),
("\\b" "fed averse" "\\b", "Fediverse"),
("\\b" "activity pub" "\\b", "ActivityPub"),
("\\b" "web recorder" "\\b", "Webrecorder"),
("\\b" "sadie panel" "\\b", "Sutty panel"),
("\\b" "sorry panel" "\\b", "Sutty panel"),
("\\b" "saudi panel" "\\b", "Sutty panel"),
("\\b" "hi fi" "\\b", "Hypha"),
("\\b" "agora core" "\\b", "Agregore"),
("\\b" "agora gore" "\\b", "Agregore"),
("\\b" "agricole" "\\b", "Agregore"),
("\\b" "agricola" "\\b", "Agregore"),
("\\b" "agra court" "\\b", "Agregore"),
("\\b" "agra gore" "\\b", "Agregore"),
("\\b" "been to" "\\b", "Ubuntu"),
("\\b" "debbie in" "\\b", "Debian"),
("\\b" "a p eyes" "\\b", "APIs"),
("\\b" "a p i" "\\b", "API"),
("\\b" "cp you" "\\b", "CPU"),
("\\b" "w three see" "\\b", "W3C"),
("\\b" "I pfs" "\\b", "IPFS"),
("\\b" "I pianos" "\\b", "IPNS"),
("\\b" "see id" "\\b", "cid"),
("\\b" "you are a?l" "\\b", "URL"),
("\\b" "u r l" "\\b", "URL"),
("\\b" "you are else" "\\b", "URLs"),
("\\b" "probably tree" "\\b", "prolly tree"),
("\\b" "m o g" "\\b", "emoji"),
# these make it easier to program in go lang
("\\b" "go error" "\\b", "err"),
("\\b" "go format" "\\b", "go fmt"),
("\\b" "\s?equals\s?" "\\b", "="),
("\\b" "\s?plus\s?" "\\b", "+"),
("\\b" "\s?hyphen\s?" "\\b", "-"),
("\\b" "\s?dash\s?" "\\b", "-"),
("\\b" "\s?asterisk\s?" "\\b", "*"),
("\\b" "\s?tilda\s?" "\\b", "~"),
("\\b" "\s?dot\s?" "\\b", "."),
("\\b" "\s?under\s?score\s?" "\\b", "_"),
("\\b" "\s?hash\s?tag\s?" "\\b", "#"),
("\\b" "\s?at\s?sign\s?" "\\b", "@"),
("\\b" "\s?dollar\s?sign\s?" "\\b", "$"),
("\\b" "\s?back\s?slash\s?" "\\b", "\\\\"),
("\\b" "\s?slash\s?" "\\b", "/"),
("\\b" "\s?vertical bar\s?" "\\b", "|"),
("\\b" "\s?back\s?tick\s?" "\\b", "`"),
("\\b" "\s?new\s?line\s?" "\\b", "\n"),
)
TEXT_REPLACE_REGEX = tuple(
(re.compile(match), replacement)
for (match, replacement) in TEXT_REPLACE_REGEX
)
# -----------------------------------------------------------------------------
# Replace Single Words
# VOSK-API doesn't use capitals anywhere so they have to be explicitly added in.
WORD_REPLACE = {
"i": "I",
"api": "API",
"linux": "Linux",
"jason": "JSON",
"haifa": "Hypha",
"vex": "Vex",
"vax": "Vex",
"okay": "ok",
"maine": "main",
# It's also possible to ignore words entirely.
"um": "",
"huh": "",
}
# Regular expressions allow partial words to be replaced.
WORD_REPLACE_REGEX = (
("^i'(.*)", "I'\\1"),
)
WORD_REPLACE_REGEX = tuple(
(re.compile(match), replacement)
for (match, replacement) in WORD_REPLACE_REGEX
)
# -----------------------------------------------------------------------------
# Add Punctuation
CLOSING_PUNCTUATION = {
"period": ".",
"comma": ",",
"karma": ",",
"calmer": ",",
"question mark": "?",
"exclamation mark": '!',
"colin": ":",
"semi colon": ";",
"close quote": '"',
"close bracket": ')',
"close brace": '}',
"close square": ']',
"close carrot": ">",
"close tick": "'",
}
OPENING_PUNCTUATION = {
"open quote": '"',
"open bracket": '(',
"open brace": '{',
"open square": '[',
"open carrot": '<',
"close tick": "'",
}
SPACE_REGEX = re.compile("\\b" "\s?space bar\s?" "\\b")
# -----------------------------------------------------------------------------
# Type individual characters
NATO_ALPHABET = {
"alpha": "a",
"bravo": "b",
"charlie": "c",
"delta": "d",
"echo": "e",
"foxtrot": "f",
"golf": "g",
"hotel": "h",
"india": "i",
"juliet": "j",
"kilo": "k",
"lima": "l",
"mike": "m",
"november": "n",
"oscar": "o",
"papa": "p",
"quebec": "q",
"romeo": "r",
"sierra": "s",
"tango": "t",
"uniform": "u",
"victor": "v",
"whiskey": "w",
"x-ray": "x",
"yankee": "y",
"zulu": "z"
}
def make_tokens(text):
for match, replacement in TEXT_REPLACE_REGEX:
text = match.sub(replacement, text)
for match, replacement in CLOSING_PUNCTUATION.items():
text = text.replace(" " + match, replacement)
text = text.replace(match, replacement)
for match, replacement in OPENING_PUNCTUATION.items():
text = text.replace(match + " ", replacement)
text = text.replace(match, replacement)
words = text.split(" ")
for i, w in enumerate(words):
w_init = w
w_test = WORD_REPLACE.get(w)
if w_test is not None:
w = w_test
if w_init == w:
for match, replacement in WORD_REPLACE_REGEX:
w_test = match.sub(replacement, w)
if w_test != w:
w = w_test
break
if w in NATO_ALPHABET:
w = NATO_ALPHABET[w]
words[i] = w
# Strip any words that were replaced with empty strings.
words[:] = [w for w in words if w]
return words
def word_typing(text):
return SPACE_REGEX.sub(" ", " ".join(make_tokens(text)))
def word_scream(text):
return word_typing(text).upper()
def word_snake(text):
return "_".join(make_tokens(text))
def word_scream_snake(text):
return word_snake(text).upper()
def word_camel(text):
tokens = make_tokens(text)
if not len(tokens):
return ""
first = tokens.pop(0)
capped = [word.capitalize() for word in tokens]
return first + "".join(capped)
def word_dictate(text):
return "".join(make_tokens(text))
def word_proper_camel(text):
camelized = word_camel(text)
proper = camelized[:1].upper() + camelized[1:]
return proper
text_mode = "speak"
modes = {
"speak": word_typing,
"cap": word_scream,
"snake": word_snake,
"screaming snake": word_scream_snake,
"camel": word_camel,
"proper camel": word_proper_camel,
"dictate": word_dictate
}
# -----------------------------------------------------------------------------
# Main Processing Function
def nerd_dictation_process(text):
global text_mode
for mode in modes:
activator = "mode "+mode
if activator in text:
text = text.replace(activator, "")
text_mode = mode
return modes[text_mode](text)