-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtokenize_generate_text.py
32 lines (27 loc) · 992 Bytes
/
tokenize_generate_text.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
from utils.utils import load_json, save_json
from tqdm import tqdm
text_path = 'generate_example_test_t5_small_rl_v5.json'
data = load_json(text_path)
punctuation = [',', '.', '?', '!']
for dial in tqdm(data):
for turn in dial['log']:
# print(turn['user'])
new_user_ids = []
user_ids = turn['user'].split()
for token in user_ids:
if token[-1] in punctuation:
new_user_ids.append(token[:-1])
new_user_ids.append(token[-1])
else:
new_user_ids.append(token)
turn['user'] = ' '.join(new_user_ids)
new_sys_ids = []
sys_ids = turn['sys'].split()
for token in sys_ids:
if token[-1] in punctuation:
new_sys_ids.append(token[:-1])
new_sys_ids.append(token[-1])
else:
new_sys_ids.append(token)
turn['sys'] = ' '.join(new_sys_ids)
save_json(data, text_path[:-5] + '_fixed.json')