-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython_vocabs.py
38 lines (33 loc) · 1.06 KB
/
python_vocabs.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
def create_value_vocab(data: dict) -> dict:
"""
Creates a vocabulary of terminals from a file in the format of: {terminal: index}
:param data:
:return: dict of terminals and their indices.
"""
vocab = {"": 0}
counter = 1
for rep, name in data.items():
if rep:
for val1, _, val2 in rep:
if val1 not in vocab:
vocab[val1] = counter
counter += 1
if val2 not in vocab:
vocab[val2] = counter
counter += 1
if name not in vocab:
vocab[name] = counter
counter += 1
return vocab
def create_path_vocab(data: dict) -> dict:
vocab = {"": 0}
counter = 1
for rep, name in data.items():
if rep:
for val1, path, val2 in rep:
if path not in vocab:
vocab[path] = counter
counter += 1
return vocab
def create_tag_vocab(data: dict) -> dict:
return {val: i for i, val in enumerate(list(set(data.values())))}