~chambln/steno

ref: 5814208515590ec8c50792bb42d3a97550d2ac0a steno/steno -rwxr-xr-x 2.6 KiB
58142085Gregory Chamberlain Represent keyboard chords as sets instead of lists 3 months ago
                                                                                
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
#!/usr/bin/env python3

from sys import stdin

MACHINE = (
    # Onset.
    (
        [
            ('s', 'q'), ('z', 'a'),
            ('k', 'w'), ('g', 's'),
            ('t', 'e'), ('d', 'd'),
            ('r', 'r'), ('j', 'f')
        ],
        {
            "sg": "v",
            "sj": "ʃ",
            "zk": "f",
            "zg": "n",
            "zgd": "m",
            "zj": "ʒ",
            "kg": "h",
            "kt": "p",
            "kd": "ð",
            "gt": "θ",
            "gd": "b",
            "td": "l",
            "tj": "tʃ",
            "dj": "dʒ",
            "rj": "w",
        }
    ),

    # Nucleus.
    (
        [
            ('a', 'c'), ('e', 'v'),
            ('*', 'tgyh'),
            ('I', 'n'), ('U', 'm')
        ],
        {
            "a*ɪ": "aɪə",
            "e*ɪ": "eɪə",
            "ae*ɪ": "ɔɪə",
            "*ɪ": "ɪə",
            "aeɪʊ": "ɔ",
            "aeɪ": "ɔɪ",
            "aeʊ": "əʊ",
            "aɪʊ": "ɑ",
            "eɪʊ": "ɜ",
            "ae": "ɒ",
            "eʊ": "u",
            "ɪʊ": "i",
        }
    ),

    # Coda.
    (
        [
            ('l', 'u'), ('n', 'j'),
            ('t', 'i'), ('d', 'k'),
            ('k', 'o'), ('g', 'l'),
            ('s', 'p'), ('z', ['semicolon']),
            ('t', ['bracketleft']), ('d', ['apostrophe'])
        ],
        {
            "ln": "s",
            "ng": "ŋ",
            "nk": "ŋk",
            "tdk": "mp",
            "td": "m",
            "tk": "p",
            "dg": "b",
            "dkg": "dʒ",
            "kg": "ʃ",
            "kz": "f",
            "gs": "v",
        }
    )
)

def read_chords(lines):
    def read_xev(lines):
        for line in lines:
            words = line.split()
            if words and words[0] in ("KeyPress", "KeyRelease"):
                event = words[0]
            elif len(words) >= 5 and words[4].strip("(),") == "keysym":
                keysym = words[6].strip("(),")
                yield event, keysym

    released, pressed = set(), set()
    for event, keysym in read_xev(lines):
        if event == "KeyPress":
            pressed.add(keysym)
        else:
            pressed.discard(keysym)
            released.add(keysym)
        if released and not pressed:
            yield released
            released.clear()

def transcribe(chord):
    return [
        [c for c, ks in keys if any([k in chord for k in ks])]
        for keys, clusters in MACHINE
    ]

def main():
    strokes = (transcribe(chord) for chord in read_chords(stdin))
    for stroke in strokes:
        print(stroke)

if __name__ == '__main__':
    main()