-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpre_tokenize.py
129 lines (96 loc) · 3.59 KB
/
pre_tokenize.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
import sys
import time
import math
import random
from pytorch_pretrained_bert.tokenization import BertTokenizer
def simple_prep(sent, bert_tokenizer):
return ' '.join(bert_tokenizer.tokenize(sent.strip()))
def list_to_txt(samples, wpath):
with open(wpath, 'wt') as f:
for s in samples:
f.write('\t'.join(s)+'\n')
def text_to_bert(rpath, wpath, bert_model="bert-base-uncased", do_shuffle=False):
wdir = os.path.dirname(wpath)
if not os.path.exists(wdir):
os.mkdir(wdir)
bert_model = bert_model.strip()
if bert_model.endswith('uncased'):
do_lower_case = True
else:
do_lower_case = False
bert_tokenizer = BertTokenizer.from_pretrained(
bert_model, do_lower_case=do_lower_case)
samples = []
start = time.time()
with open(rpath, 'rt') as f:
for index, line in enumerate(f):
if (index+1) % 100000 == 0:
print('{:}\t{:}\t{:.1f}min'.format(os.path.basename(rpath),
index, (time.time() - start) / 60))
sys.stdout.flush()
label, text = line.strip().split('\t')
samples.append(('', label,
simple_prep(text, bert_tokenizer), 'mono'))
sys.stdout.flush()
if do_shuffle:
random.shuffle(samples)
list_to_txt(samples, wpath)
def dial_to_bert(rpath, wpath, bert_model="bert-base-uncased", do_shuffle=False):
wdir = os.path.dirname(wpath)
if not os.path.exists(wdir):
os.mkdir(wdir)
bert_model = bert_model.strip()
if bert_model.endswith('uncased'):
do_lower_case = True
else:
do_lower_case = False
tokenizer = BertTokenizer.from_pretrained(
bert_model, do_lower_case=do_lower_case)
samples = []
start = time.time()
with open(rpath, 'rt') as f:
for index, line in enumerate(f):
if (index+1) % 100000 == 0:
print('{:}\t{:}\t{:.1f}min'.format(os.path.basename(rpath),
index, (time.time() - start) / 60))
sys.stdout.flush()
src, label, tgt = line.strip().split('\t')
samples.append((simple_prep(src, tokenizer), label,
simple_prep(tgt, tokenizer), 'dial'))
sys.stdout.flush()
if do_shuffle:
random.shuffle(samples)
list_to_txt(samples, wpath)
def run_to_bert_file():
rpath = sys.argv[1]
wpath = sys.argv[2]
datatype, _ = os.path.basename(rpath).split('.')
assert datatype in ['dial', 'text']
if datatype == 'dial':
dial_to_bert(rpath, wpath)
else:
text_to_bert(rpath, wpath)
def run_to_bert_dir():
from multiprocessing import Pool
rdir = sys.argv[1]
wdir = sys.argv[2]
p = Pool(4)
for data_type in ['dial', 'text']:
for label in ['train', 'valid', 'test']:
target = "{:}.{:}".format(data_type, label)
assert os.path.exists(os.path.join(rdir, target))
print(os.path.join(rdir, target))
if data_type == 'dial':
p.apply_async(dial_to_bert, args=(os.path.join(rdir, target),
os.path.join(wdir, target)))
elif data_type == 'text':
p.apply_async(text_to_bert, args=(os.path.join(rdir, target),
os.path.join(wdir, target)))
else:
raise ValueError
print('\n')
p.close()
p.join()
if __name__ == '__main__':
run_to_bert_dir()