-
Notifications
You must be signed in to change notification settings - Fork 5
/
vocab.py
30 lines (23 loc) · 847 Bytes
/
vocab.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
import os
import json
from talon.voice import Context
from talon import resource
from . import config
from .text import shrink
vocab_alternate = config.load_config_json("vocab_alternate.json", dict)
vocab_alternate.update({f"shrink {k}": v for k, v in shrink.shrink_map.items()})
ctx = Context("vocab")
try:
vocab_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "vocab.json")
with resource.open(vocab_path) as fh:
vocab = json.load(fh)
except FileNotFoundError:
vocab = []
def add_vocab(words):
global vocab
vocab += [re.sub("[^a-zA-Z0-9]+", "", w) for w in words]
vocab = sorted(list(set(vocab)))
with open(vocab_path, "w") as f:
json.dump(vocab, f, indent=0)
ctx.vocab = vocab + list(vocab_alternate.keys())
ctx.vocab_remove = config.load_config_json("vocab_remove.json", list)