-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_vocab.py
40 lines (30 loc) · 920 Bytes
/
make_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
31
32
33
34
35
36
37
38
39
40
import torch
import os
import glob
import json
from utils import Vocab
def main(args = {}):
print(args)
# путь к директории с датасетом
dataPath = 'data'
# путь к словарю
vocabPath = 'vocab.json'
# загружаем/создаём словарь
try:
with open(vocabPath, 'r') as f:
data = json.load(f)
vocab = Vocab(data)
except (FileExistsError, FileNotFoundError):
vocab = Vocab()
files = glob.glob(dataPath + "/*.json")
for filename in files:
print("Loading", filename,"\r")
with open(filename, 'r') as f:
data = json.load(f)
text = data['story']
vocab.make(text)
with open(vocabPath, 'w') as f:
json.dump(vocab.vocab, f, ensure_ascii=False)
print(vocab)
if __name__ == '__main__':
main()