File size: 8,787 Bytes
59d97af |
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
#!/usr/bin/env python
# coding=utf-8
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
from abc import ABC, abstractmethod
import jsonlines
import json
import copy
import glob
import random
import fire
class Converter(ABC):
def __init__(self, filepath) -> None:
super().__init__()
self.filepath = filepath
def convert(self):
"""
Implement your convert logics in this function
"""
self.start()
self.process()
self.end()
pass
def start(self):
print(f'Start processing {self.__class__.__name__} at {self.filepath}')
def end(self):
print(
f'Finish processing {self.__class__.__name__} at {self.filepath}')
@abstractmethod
def process(self):
"""
Implement your convert logics in this function
"""
class DSTC7Converter(Converter):
'''
Converter class for DSTC7 Grounded response generation
'''
def process(self):
convs = open(self.filepath)
examples = []
for conv in convs:
_, c_id, score, facts, context, response = conv.split('\t')
example = {}
if context.strip() == 'START':
continue
context = context.replace('START EOS TIL ', '')
example['Context'] = context.strip()
example['Knowledge'] = facts.replace(
' < p > ', '').replace(' < /p > ', '').strip()
example['Response'] = response.strip()
examples.append(copy.deepcopy(example))
with jsonlines.open('../data/dstc7.jsonl', mode='w') as writer:
for i in examples:
writer.write(i)
return
class MSMARCOConverter(Converter):
'''
Converter class for MS MARCO
'''
def process(self):
train_data = json.load(open(self.filepath))
examples = []
for ids in train_data['query'].keys():
query, answer, passage = train_data['query'][ids], train_data['answers'][ids], train_data['passages'][ids]
knowledge = [i['passage_text']
for i in passage if i['is_selected']]
example = {}
example['Context'] = query.strip()
example['Knowledge'] = ' '.join(knowledge)
example['Response'] = ' '.join(answer).strip()
examples.append(copy.deepcopy(example))
with jsonlines.open('../data/msmarco.jsonl', mode='w') as writer:
for i in examples:
writer.write(i)
return
class UnifiedQAConverter(Converter):
def process(self):
examples = []
for fname in glob.glob(f'{self.filepath}/*/*'):
if 'train.tsv' in fname or 'test.tsv' in fname:
data = open(fname)
for line in data:
line = line.strip()
try:
question, answer = line.split('\t')
question, story = question.split('\\n')
example = {}
example['Context'] = question
example['Response'] = answer
example['Knowledge'] = story
examples.append(copy.deepcopy(example))
k += 1
except:
pass
train_writer = jsonlines.open('../data/unifiedqa.jsonl', mode='w')
for i in examples:
train_writer.write(i)
return
class SGDConverter(Converter):
'''
Converter class for SGD dataset
'''
def process(self):
examples = []
for split in ['train', 'dev', 'test']:
schema_info = json.load(
open(f'{self.filepath}/{split}/schema.json'))
schema_info = dict([(i['service_name'], i) for i in schema_info])
for file in glob.glob(f'{self.filepath}/{split}/dialogues_*.json'):
data = json.load(open(file))
for dialogue in data:
dialogue_id = dialogue['dialogue_id']
services = dialogue['services'][0]
schema = schema_info[services]
description = schema['description']
task_slots = [s['name'] for s in schema['slots']]
task_intents = [s['name'] for s in schema['intents']]
task_intents_description = [
s['description'] for s in schema['intents']]
turns = dialogue['turns']
history = []
example = {}
for idx, turn in enumerate(turns):
if idx == 0:
assert turn['speaker'] == 'USER'
frame = turn['frames'][0]
service = turn['frames'][0]['service'].split('_')[
0].lower()
if turn['speaker'] == 'USER':
user_utter = turn['utterance']
history.append(f'{user_utter}')
belief_slot_values = frame['state']['slot_values']
slot_values_list = []
for slot_value in belief_slot_values.items():
slot, values = slot_value
value = values[0]
slot_values_list.append(f'{slot} = {value}')
slot_values_str = ' ; '.join(slot_values_list)
else:
sys_utter = copy.copy(turn['utterance'])
slot_values_str = f'belief : {service} {slot_values_str}'
slots = frame['slots']
offset = 0
len_ = len(sys_utter)
candidates = []
for idx, slot_info in enumerate(slots):
start, end, slot_name = slot_info['start'], slot_info['exclusive_end'], slot_info['slot']
sys_utter = sys_utter[:start+offset] + str(
idx) * (end - start) + sys_utter[end+offset:]
candidates.append(
(slot_name, str(idx) * (end - start)))
for idx, info in enumerate(candidates):
slotname, target = info
sys_utter = sys_utter.replace(
target, f'[{slotname}]')
reply = f'{sys_utter}'
example['Context'] = ' EOS '.join(history)
example['Knowledge'] = slot_values_str
example['Response'] = reply
examples.append(copy.deepcopy(example))
history.append(reply)
train_writer = jsonlines.open('../data/sgd.jsonl', mode='w')
for i in examples:
train_writer.write(i)
return
def merge_and_split():
examples = []
filepath = '../data/dstc7.jsonl'
with open(filepath, "r", encoding="utf-8") as reader:
for item in jsonlines.Reader(reader):
examples.append(item)
filepath = '../data/msmarco.jsonl'
with open(filepath, "r", encoding="utf-8") as reader:
for item in jsonlines.Reader(reader):
examples.append(item)
filepath = '../data/sgd.jsonl'
with open(filepath, "r", encoding="utf-8") as reader:
for item in jsonlines.Reader(reader):
examples.append(item)
filepath = '../data/unifiedqa.jsonl'
with open(filepath, "r", encoding="utf-8") as reader:
for item in jsonlines.Reader(reader):
examples.append(item)
random.seed(2021)
train_writer = jsonlines.open(
'../data/grounded_data_train.jsonl', mode='w')
valid_writer = jsonlines.open(
'../data/grounded_data_valid.jsonl', mode='w')
for i in examples:
if random.random() < 0.01:
valid_writer.write(i)
else:
train_writer.write(i)
print('Done!')
def process(
msmarco_path,
sgd_path,
dstc7_path,
unified_qa_path
):
MSMARCOConverter(f'{msmarco_path}/train_v2.1.json').convert()
SGDConverter(f'{sgd_path}').convert()
DSTC7Converter(f'{dstc7_path}').convert()
UnifiedQAConverter(unified_qa_path).convert()
def main():
fire.Fire(process)
# merge generated data and split it into train and valid
merge_and_split()
if __name__ == '__main__':
main()
|