-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_char_vocab.py
27 lines (23 loc) · 930 Bytes
/
get_char_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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
import json
def get_char_vocab(input_filenames, output_filename):
vocab = set()
for filename in input_filenames:
with open(filename) as f:
for line in f.readlines():
for sentence in json.loads(line)["sentences"]:
for word in sentence:
vocab.update(word)
vocab = sorted(list(vocab))
with open(output_filename, "w") as f:
for char in vocab:
f.write(u"{}\n".format(char).encode("utf8"))
print("Wrote {} characters to {}".format(len(vocab), output_filename))
def get_char_vocab_language(language):
get_char_vocab(["{}.{}.jsonlines".format(partition, language) for partition in ("train", "dev", "test")], "char_vocab.{}.txt".format(language))
#get_char_vocab_language("english")
#get_char_vocab_language("chinese")
get_char_vocab_language("arabic")