forked from 2d6/talon_german
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgerman.py
422 lines (361 loc) · 11.1 KB
/
german.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import unicodedata
from talon import Context, Module, actions, settings, speech_system
from talon.grammar import Phrase
from .clipscanner import ClipScanner
mod = Module()
mod.mode("german", desc="german language mode")
ctx = Context()
ctx.matches = """
mode: user.german
language: /.*/
"""
ctx.settings = {
'speech.engine': 'vosk',
'speech.language': 'de_DE',
'speech.timeout': 0.3
}
ctx = Context()
ctx.matches = """
mode: user.german
language: de_DE
"""
setting_context_sensitive_dictation_german = mod.setting(
"context_sensitive_dictation_german",
type=bool,
default=False,
desc="Look at surrounding text to improve auto-capitalization/spacing in dictation mode. By default, this works by selecting that text & copying it to the clipboard, so it may be slow or fail in some applications.",
)
phrase_stack = []
def on_pre_phrase(d): phrase_stack.append(d)
def on_post_phrase(d): phrase_stack.pop()
speech_system.register('pre:phrase', on_pre_phrase)
speech_system.register('post:phrase', on_post_phrase)
mod.setting("german_unicode",
type=int,
default=1,
desc="Enable proper unicode punctuation")
letters = {
"alt": "a",
"bett": "b",
"kap": "c",
"kapp": "c",
"cap": "c",
"cup": "c",
"drum": "d",
"echt": "e",
"fein": "f",
"feind": "f",
"gast": "g",
"hab": "h",
"hat": "h",
"hart": "h",
"ich": "i",
"j": "j",
"kennt": "k",
"kent": "k",
"kind": "k",
"look": "l",
"luck": "l",
"met": "m",
"mit": "m",
"not": "n",
"oft": "o",
"pitt": "p",
"kuh": "q",
"rad": "r",
"rat": "r",
"rate": "r",
"raten": "r",
"sinn": "s",
"tipp": "t",
"ulf": "u",
"von": "v",
"wall": "w",
"plex": "x",
"chunky": "y",
"junkie": "y",
"tanki": "y",
"z": "z",
"ähnlich": "ä",
"öl": "ö",
"übel": "ü",
"üben": "ü",
"s z": "ß",
}
letters.update({v: v for v in letters.values()})
ctx.lists["user.letter"] = letters
ctx.lists["user.number_key"] = {
"null": "0",
"eins": "1",
"zwei": "2",
"drei": "3",
"vier": "4",
"fünf": "5",
"sechs": "6",
"sieben": "7",
"acht": "8",
"neun": "9",
"komma": ",",
"punkt": ".",
}
ctx.lists["user.punctuation"] = {
"leerzeichen": "␣", # will become spaces after all substitutions
"blank": "␣",
"blink": "␣",
"plenk": "␣",
"planck": "␣",
"beistrich": ",", # komma is often confused with komme
"bei strich": ",",
"bei stich": ",",
"bei scheck": ",",
"beiß dich": ",",
"beißt nicht": ",",
"beistriche": ",",
"punkt": ".",
"ellipse": "...",
"semikolon": ";",
"doppelpunkt": ":",
"schrägstrich": "/",
"fragezeichen": "?",
"ausrufezeichen": "!",
"sternchen": "*",
"bindestrich": "-",
"gedankenstrich": "–",
"unterstrich": "_",
"raute": "#",
"prozent": "%",
"at zeichen": "@",
"klammeraffe": "@",
"und zeichen": "&",
"dollar zeichen": "$",
"dollarzeichen": "$",
"pfund zeichen": "£",
"pfundzeichen": "£",
"euro zeichen": "€",
"eurozeichen": "€",
}
ctx.lists["user.symbol_key"] = {
"leerzeichen": "␣", # will become spaces after all substitutions
"blank": "␣",
"blink": "␣",
"plenk": "␣",
"planck": "␣",
"beistrich": ",", # komma is often confused with komme
"bei strich": ",", # komma is often confused with komme
"beistriche": ",", # komma is often confused with komme
"punkt": ".",
"semikolon": ";",
"doppelpunkt": ":",
"schrägstrich": "/",
"fragezeichen": "?",
"ausrufezeichen": "!",
"sternchen": "*",
"bindestrich": "-",
"gedankenstrich": "–",
"unterstrich": "_",
"raute": "#",
"prozent": "%",
"at zeichen": "@",
"klammeraffe": "@",
"und zeichen": "&",
"dollar zeichen": "$",
"pfund zeichen": "£",
"euro zeichen": "€",
"backslash": "\\",
"senkrecht strich": "|",
"zitat": '„',
"zitat ende": '“',
"halbes zitat": '‚',
"halbes zitat ende": '‘',
"apostroph": "’",
"klammer auf": "(",
"klammer zu": ")",
"eckige klammer auf": "[",
"eckige klammer zu": "]",
"geschweifte klammer auf": "{",
"geschweifte klammer zu": "}",
"kleiner zeichen": "<",
"größer zeichen": ">",
"ist gleich zeichen": "=",
"tilde": "~",
"zirkumflex": "^",
}
mod.list("number", desc="Words for a positive nonzero number")
number_words = {
"eins": "1",
"zwei": "2",
"drei": "3",
"vier": "4",
"fünf": "5",
"sechs": "6",
"sieben": "7",
"acht": "8",
"neun": "9",
"zehn": "10",
}
number_words.update({str(i): str(i) for i in range(10)})
ctx.lists["self.number"] = number_words
@mod.capture
def number_small(m) -> int:
"""A small number"""
@ctx.capture(
"user.number_small", rule=f"({'|'.join(number_words.keys())})"
)
def number_small(m) -> int:
return int(number_words[str(m)])
mod.list("count", desc="Words for a positive nonzero count of actions")
count_words = {
"einfach": "1",
"zweifach": "2",
"dreifach": "3",
"vierfach": "4",
"fünffach": "5",
"sechsfach": "6",
"siebenfach": "7",
"achtfach": "8",
"neunfach": "9",
"zehnfach": "10",
}
ctx.lists["self.count"] = count_words
@mod.capture
def count_small(m) -> int:
"""A small count of instances of actions"""
@ctx.capture(
"user.count_small", rule=f"({'|'.join(count_words.keys())})"
)
def count_small(m) -> int:
return int(count_words[str(m)])
_space_after = ".,!?:;)]}–“‘$£€"
_no_space_before = ".,-!?:;)]}␣“‘’$£€"
_ascii_replace = {'–': '-', '„': '"', '“': '"', "‚": "'", "‘": "'", "’": "'"}
_capitalize_after = ".!?"
mod.list("modifier", desc="Modifiers for upper casement")
ctx.lists["self.modifier"] = {
"schiff": "CAP", # groß often becomes große/großer/großes
"schiffs": "CAP",
"schifft": "CAP",
"holzschiff": "ALLCAPS", # hold shift
"zwerg": "LOWER",
}
@mod.capture
def vocabulary_german(m: str) -> str:
"""user vocabulary"""
@mod.capture
def wort(m: str) -> str:
"""word or spelled word or number, inserts space in the end"""
@mod.capture
def gk_wort(m: str) -> str:
"""potentially upper case word"""
@mod.capture
def satzglied(m: str) -> str:
"""word or symbol"""
@mod.capture
def satz(m: str) -> str:
"""sentence"""
@mod.capture
def weg(m: str, count: str) -> str:
"""capture multiple "weg"s"""
@mod.capture
def acronym(m: str) -> str:
"""an acronym composed of multiple letters"""
@mod.action_class
class Actions:
def smart_insertion(txt: str) -> str:
"""handles context-aware insertion"""
text = ""
before, after = actions.user.dictation_peek(left=True, right=True)
# the following checks only use the first char
before = before[-1] if len(before) > 0 else before
after = after[0] if len(after) > 0 else after
squeeze_into_word = before != "" and unicodedata.category(before)[0] == 'L' \
and after != "" and unicodedata.category(after)[0] == 'L'
if before != "" \
and (unicodedata.category(before)[0] == 'L' or before in _space_after) \
and txt[0] not in _no_space_before \
and not squeeze_into_word:
text += " "
if before in _capitalize_after or before == "":
txt = txt[0].upper() + txt[1:]
if settings.get("user.german_unicode") == 0:
ascii = txt
for c in _ascii_replace:
ascii = ascii.replace(c, _ascii_replace[c])
text += ascii
else:
text += txt
if (
after != ""
and (
txt[-1] in _space_after
or unicodedata.category(txt[-1])[0] == 'L'
)
and after not in ' \n\t'
and after not in _no_space_before
and not squeeze_into_word
):
text += " "
return text
def insert(txt: str):
"""text insertion"""
# delete whatever is currently selected
# note: this behavior leads to missing spaces in vim as a delete in vim sometimes takes multiple
# spaces at once (-> investigate!)
#actions.key(" ")
#actions.key("backspace")
text = txt
if setting_context_sensitive_dictation_german.get():
text = actions.user.smart_insertion(text)
actions.user.add_phrase_to_history(text)
actions.insert(text)
def smart_delete(txt: str, count: str):
"""delete word and optionally space"""
with ClipScanner() as clip:
for i in range(len(str(txt).split())):
for j in range(int(count)):
# first just delete all spaces until next word
clip.clear()
actions.edit.extend_word_left()
before = clip.get_selection()
if before != '' and before[-1] in [" ", "\n"]:
actions.edit.extend_word_right()
actions.key("backspace")
continue
# if there were none, delete next word
actions.key("backspace")
# delete spaces before that as well
clip.clear()
actions.edit.extend_left()
before = clip.get_selection()
if before in [" ", "\n"]:
actions.key("backspace")
elif before != '':
actions.edit.extend_right()
def vosk_recognize_german(phrase: Phrase):
"""Replay speech from last phrase into vosk"""
# NOTE: this is pretty much all considered an experimental API
# and this script is just for demo purposes, for the beta only
current_phrase = phrase_stack[-1]
ts = current_phrase['_ts']
start = phrase.words[0].start - ts
# NOTE: might have to tweak this depending on engine / model if words
# get lost or parts of your prefix appear as word (for example getting
# "an ..." when saying "german ..." given prefix "german")
tweak_offset = -0.1
start = max(0, start + tweak_offset)
end = phrase.words[-1].end - ts
samples = current_phrase['samples']
pstart = int(start * 16_000)
pend = int(end * 16_000)
samples = samples[pstart:pend]
actions.mode.enable("user.german")
try:
# NOTE: the following API is completely private and subject to change with no notice
speech_system._on_audio_frame(samples)
# Change command history entry
german_text = actions.user.history_get(0)
phrase_text = actions.user.history_transform_phrase_text(phrase.words)
command = actions.user.history_get(1).removesuffix(phrase_text)
actions.user.history_set(1, command + german_text)
actions.user.history_set(0, None)
finally:
actions.mode.disable("user.german")