File size: 1,278 Bytes
5f9d9e3 |
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 |
""" Extract the prediction samples from the raw prediction file.
i.e., ${data}/test_raw.jsonl to ${data}/test.jsonl
"""
import json
from jsonargparse import CLI
def context2input(l, start=0):
""" Given a list of context, just following UniCRS to format the context.
"""
context = ''
for i, utt in enumerate(l):
if utt == '' or utt == ' ':
continue
if i % 2 == start:
context += '\nUser: '
else:
context += '\nSystem: '
context += utt
return context + '\nSystem: '
def main(from_json: str = None, to_json: str = None, start: int = 0):
with open(from_json) as fr, open(to_json, 'w') as fw:
for l in fr:
l = json.loads(l)
if l['is_user'] == 0 and len(l['rec']) > 0:
l['input'] = context2input(l['context'], start=start)
fw.write(json.dumps(l) + '\n')
if __name__ == '__main__':
# python extract_prediction.py --from_json=inspired/test_raw.jsonl --to_json=inspired/test.jsonl --start 0
# python extract_prediction.py --from_json=redial/test_raw.jsonl --to_json=redial/test.jsonl --start 1
# python extract_prediction.py --from_json=reddit/test_raw.jsonl --to_json=reddit/test.jsonl --start 1
CLI(main) |