Upload create_train_from_sharc.py
Browse files- create_train_from_sharc.py +83 -0
create_train_from_sharc.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import argparse
|
3 |
+
|
4 |
+
|
5 |
+
def create_entailment_data(train_data):
|
6 |
+
entailment_data = [d for d in train_data if len(d['evidence']) > 0]
|
7 |
+
# entailment data
|
8 |
+
for d in entailment_data:
|
9 |
+
entailment_answer = d['answer'].lower()
|
10 |
+
if d['answer'].lower() not in ['yes', 'no']:
|
11 |
+
entailment_answer = 'maybe'
|
12 |
+
if len(d['history']) > 0:
|
13 |
+
# this means that not all the information needed to get to the answer were provided in the scenario
|
14 |
+
# (they were provided in the history). Therefore the entailment label should be 'maybe'
|
15 |
+
entailment_answer = 'maybe'
|
16 |
+
|
17 |
+
d['entailment_answer'] = entailment_answer
|
18 |
+
|
19 |
+
entailment_path = 'train_entailment_sharc.json'
|
20 |
+
with open(entailment_path, 'w') as f:
|
21 |
+
f.write(json.dumps(entailment_data, indent=True))
|
22 |
+
print('Wrote ShARC entailment data to ' + entailment_path)
|
23 |
+
|
24 |
+
return entailment_data
|
25 |
+
|
26 |
+
|
27 |
+
def filter_train_data(sharc_train_path, sharc_dev_path):
|
28 |
+
sharc_train_data = json.load(open(sharc_train_path))
|
29 |
+
sharc_dev_data = json.load(open(sharc_dev_path))
|
30 |
+
sharc_data = sharc_train_data + sharc_dev_data
|
31 |
+
|
32 |
+
train_utterance_ids = open('train_utterance_ids.txt').read().splitlines()
|
33 |
+
train_data = [d for d in sharc_data if d['utterance_id'] in train_utterance_ids]
|
34 |
+
return train_data
|
35 |
+
|
36 |
+
|
37 |
+
def create_qa_data(entailment_data):
|
38 |
+
qa_data = []
|
39 |
+
for d in entailment_data:
|
40 |
+
for e in d['evidence']:
|
41 |
+
q_key = 'follow_up_question'
|
42 |
+
a_key = 'follow_up_answer'
|
43 |
+
if 'follow_up_question' in e:
|
44 |
+
qa_data.append({
|
45 |
+
'utterance_id': d['utterance_id'],
|
46 |
+
'context': d['scenario'],
|
47 |
+
'question': e[q_key],
|
48 |
+
'answer': e[a_key].lower()
|
49 |
+
})
|
50 |
+
|
51 |
+
for h in d['history']:
|
52 |
+
qa_data.append({
|
53 |
+
'utterance_id': d['utterance_id'],
|
54 |
+
'context': d['scenario'],
|
55 |
+
'question': h['follow_up_question'],
|
56 |
+
'answer': 'maybe'
|
57 |
+
})
|
58 |
+
if d['answer'].lower() not in ['yes', 'no']:
|
59 |
+
qa_data.append({
|
60 |
+
'utterance_id': d['utterance_id'],
|
61 |
+
'context': d['scenario'],
|
62 |
+
'question': d['answer'],
|
63 |
+
'answer': 'maybe'
|
64 |
+
})
|
65 |
+
qa_path = 'train_qa_sharc.json'
|
66 |
+
with open(qa_path, 'w') as f:
|
67 |
+
f.write(json.dumps(entailment_data, indent=True))
|
68 |
+
print('Wrote ShARC QA data to ' + qa_path)
|
69 |
+
return qa_data
|
70 |
+
|
71 |
+
|
72 |
+
if __name__ == '__main__':
|
73 |
+
parser = argparse.ArgumentParser('Script for generating entailment and QA data from ShARC for training')
|
74 |
+
parser.add_argument('-sharc_train_path', type=str, default='sharc_train.json', help='path to ShARC train file')
|
75 |
+
parser.add_argument('-sharc_dev_path', type=str, default='sharc_dev.json', help='path to ShARC dev file')
|
76 |
+
|
77 |
+
args = parser.parse_args()
|
78 |
+
sharc_train_path = args.sharc_train_path
|
79 |
+
sharc_dev_path = args.sharc_dev_path
|
80 |
+
|
81 |
+
train_data = filter_train_data(sharc_train_path, sharc_dev_path)
|
82 |
+
entailment_data = create_entailment_data(train_data)
|
83 |
+
qa_data = create_qa_data(entailment_data)
|