-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapping_test_dictate.py
More file actions
executable file
·120 lines (97 loc) · 2.86 KB
/
wrapping_test_dictate.py
File metadata and controls
executable file
·120 lines (97 loc) · 2.86 KB
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
#!/usr/bin/env python3
"""Test dictate.py speech recognition functionality - simplified version."""
import os
import subprocess
import sys
import time
import pyautogui
import yaml
from box import Box
from multi_dictate.kbd_utils import (for_typewrite,
get_current_keyboard_layout, kbd_cfg)
FIFO_PATH = "/tmp/dictate_test_trigger"
dictate_proc = None
cfg = None
errors = 0
def init():
global dictate_proc, cfg
# Allow X11 connections
subprocess.run(["xhost", "+"], capture_output=True, check=False)
dictate_proc = subprocess.Popen(
["python3", "-m", "multi_dictate.dictate", "--no-echo", "--trigger", FIFO_PATH],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
for _ in range(30):
if os.path.exists(FIFO_PATH):
break
time.sleep(0.1)
else:
dictate_proc.terminate()
sys.exit(1)
y = {}
try:
with open("dictate.yaml", "r", encoding="utf-8") as f:
y = yaml.safe_load(f) or {}
except Exception:
pass
cfg = Box(y, default_box=True)
time.sleep(1)
def check_result(expected, result, case_sensitive=True):
if result.strip().lower() == expected.lower():
print("pass")
else:
print(f"Expected: '{expected}' ", end="")
print(f"fail, got: '{result}'")
global errors
errors += 1
def send_cmd(cmd):
fd = os.open(FIFO_PATH, os.O_WRONLY | os.O_NONBLOCK)
os.write(fd, f"{cmd}\n".encode())
os.close(fd)
def play_audio(text, lang="en"):
os.system(f"gtts-cli '{text}' -l {lang} | play -q -v 0.1 -t mp3 -")
def test_typewrite(sample):
to_type = for_typewrite(kl, sample)
pyautogui.typewrite(to_type + "\n")
time.sleep(0.5)
check_result(sample, input())
def test_dictate(sample, lang="en"):
send_cmd("record")
time.sleep(1)
play_audio(sample, lang)
time.sleep(2.5)
send_cmd("stop")
time.sleep(2)
pyautogui.typewrite("\n") # Press enter to submit
check_result(sample, input())
init()
time.sleep(0.5)
kl = get_current_keyboard_layout()
print(f"Current keyboard layout: {kl}")
# Test typewrite with some sample text
test_samples = {
"us": ["Hello", "Test"],
"de": ["Hallo", "Straße"],
"ru": ["Привет", "эхо"],
"es": ["Hola", "niño"],
"fr": ["Bonjour", "café"],
"it": ["Ciao", "città"],
}
lang_code = kbd_cfg.layouts[kl].tts or kl
# Test typewrite
if kl in test_samples:
print("\nTesting typewrite:")
for sample in test_samples[kl]:
test_typewrite(sample)
else:
print("\nTesting typewrite with default:")
test_typewrite("Test")
# Test dictation with a simple phrase
print("\nTesting dictation:")
test_phrase = test_samples.get(kl, ["Hello"])[0]
test_dictate(test_phrase, lang_code)
dictate_proc.terminate()
dictate_proc.wait()
print(f"Errors: {errors}")
sys.exit(errors)