-
Notifications
You must be signed in to change notification settings - Fork 2
/
prepare_data.py
69 lines (52 loc) · 2 KB
/
prepare_data.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
import os
import json
from parlai.core.params import ParlaiParser
from parlai.agents.fixed_response.fixed_response import FixedResponseAgent
from parlai.core.worlds import create_task
from parlai.utils.misc import TimeLogger
def prepare_dataset(opt):
task = opt['task']
dt = opt['datatype'].split(':')[0]
conv_idx, utter_idx = 0, 0
agent = FixedResponseAgent(opt)
world = create_task(opt, agent)
log_timer = TimeLogger()
file_root_dir = os.path.join(opt['prepare_data_dir'], task)
os.makedirs(file_root_dir, exist_ok=True)
cnt = 0
f = open(os.path.join(file_root_dir, f'{dt}.jsonl'), 'w', encoding='utf-8')
while not world.epoch_done():
cnt += 1
world.parley()
context = world.acts[0].get('text')
response = world.acts[0].get('labels', world.acts[0].get('eval_labels'))[0]
episode_done = world.acts[0].get('episode_done')
emotion = world.acts[0].get('emotion')
situation = world.acts[0].get('situation')
obj1 = json.dumps({'conv_idx': conv_idx, 'utter_idx': utter_idx, 'utterance': context, 'emotion': emotion, 'situation': situation})
f.write(obj1 + '\n')
utter_idx += 1
obj2 = json.dumps({'conv_idx': conv_idx, 'utter_idx': utter_idx, 'utterance': response, 'emotion': emotion, 'situation': situation})
f.write(obj2 + '\n')
utter_idx += 1
if episode_done:
conv_idx += 1
utter_idx = 0
if log_timer.time() > opt['log_every_n_secs']:
text, _log = log_timer.log(world.total_parleys, world.num_examples())
print(text)
f.close()
if __name__ == '__main__':
parser = ParlaiParser()
parser.add_argument(
"--prepare-data-dir",
default=None,
type=str,
help='Save to the prepared dataset'
)
opt = parser.parse_args()
# Common options
opt['log_every_n_secs'] = 2
opt['fixed_response'] = None
# prepare dataset
prepare_dataset(opt)