-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_process_utils.py
219 lines (157 loc) · 6.48 KB
/
data_process_utils.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
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
import copy
import string
def make_aqua_x(tmp_js):
question = tmp_js['question']
options = tmp_js['options']
# print('options:{}'.format(options))
question = question.strip()
choice = "(" + "(".join(options)
choice = choice.replace("(", " (").replace(")", ") ")
choice = "Answer Choices:" + choice
# print('choice:{}'.format(choice))
question = question + " " + choice
return question
def make_gsm8k_answer(tmp_js):
answer = tmp_js["answer"].split("#### ")[-1]
return answer
def make_gsm8k_rationale(tmp_js):
rationale = tmp_js["answer"].split("#### ")[0]
rationale = list(filter(lambda x: len(x) > 0, rationale.strip().split('\n')))
rationale = list(map(lambda x: x + '.' if (x[-1] not in string.punctuation) else x, rationale))
rationale = ' '.join(rationale)
return rationale
def make_aqua_rationale(tmp_js):
rationale = tmp_js['rationale']
rationale = list(filter(lambda x: len(x) > 0, rationale.strip().split('\n')))[:-1]
rationale = list(map(lambda x: x + '.' if (x[-1] not in string.punctuation) else x, rationale))
rationale = ' '.join(rationale)
return rationale
def make_nli_question(tmp_js):
premise = tmp_js['premise']
hypothesis = tmp_js['hypothesis']
question = 'Premise:\n"{}"\nBased on this premise, can we conclude the hypothesis "{}" is true?\nOPTIONS:\n- yes\n- no\n- it is not possible to tell' \
.format(premise, hypothesis)
return question
def make_openbookqa_x(tmp_js):
q = tmp_js['question_stem'].strip()
choice = "Answer Choices:"
for c in tmp_js['choices']:
choice += " ("
choice += c["label"]
choice += ") "
choice += c["text"]
q = q + ' ' + choice
return q
def make_csqa_x(tmp_js):
q = tmp_js["question"]
choice = "Answer Choices:"
for c in tmp_js["choices"]:
choice += " ("
choice += c["label"]
choice += ") "
choice += c["text"]
q = q + ' ' + choice
return q
def create_single_demo(x, z, y, direct_answer_trigger_for_fewshot, cot_flag, dataset):
'''
:param x: question (maybe including options)
:param z: rationale
:param y: answer
:return:
'''
if 'nli' not in dataset:
assert not x.startswith('Q:')
x = 'Q: ' + x + '\n' + 'A:'
if cot_flag:
result = ' '.join([x, z, direct_answer_trigger_for_fewshot, y])
else:
result = ' '.join([x, direct_answer_trigger_for_fewshot, y])
else:
x = x + '\n' + 'A:'
if cot_flag:
result = ' '.join([x, z, direct_answer_trigger_for_fewshot, y])
else:
result = ' '.join([x, direct_answer_trigger_for_fewshot, y])
return result
def concat_demos(demos):
demos = copy.deepcopy(demos)
# result = ''
for i, demo in enumerate(demos):
if demo[-1] != '.':
demos[i] += '.'
demos[i] += '\n\n'
result = ''
for demo in demos:
result += demo
# result = ".\n\n".join(demos)
# result += ".\n\n"
return result
def transform_original_aqua_into_x_z_y(tmp_js, direct_answer_trigger_for_fewshot, cot_flag):
tmp_question = make_aqua_x(tmp_js)
if tmp_question.startswith('Q:'):
tmp_question = tmp_question[2:]
elif tmp_question.startswith('Q: '):
tmp_question = tmp_question[3:]
tmp_rationale = make_aqua_rationale(tmp_js)
tmp_answer = tmp_js['correct']
tmp_demo = create_single_demo(tmp_question, tmp_rationale, tmp_answer, direct_answer_trigger_for_fewshot, cot_flag,
'aqua')
tmp_demo_dict = {'demonstration': tmp_demo, 'question': tmp_question, 'rationale': tmp_rationale,
'answer': tmp_answer}
return tmp_demo_dict
def transform_original_strategyqa_into_x_z_y(tmp_js, direct_answer_trigger_for_fewshot, cot_flag):
tmp_question = tmp_js['question']
tmp_answer = 'yes' if tmp_js['answer'] else 'no'
tmp_rationale = ' '.join(tmp_js['facts'])
tmp_demo = create_single_demo(tmp_question, tmp_rationale, tmp_answer, direct_answer_trigger_for_fewshot,
cot_flag, 'strategyqa')
tmp_demo_dict = {'demonstration': tmp_demo, 'question': tmp_question, 'rationale': tmp_rationale,
'answer': tmp_answer}
return tmp_demo_dict
def transform_original_gsm8k_into_x_z_y(tmp_js, direct_answer_trigger_for_fewshot, cot_flag):
tmp_rationale = make_gsm8k_rationale(tmp_js)
tmp_answer = make_gsm8k_answer(tmp_js)
tmp_question = tmp_js['question']
tmp_demo = create_single_demo(tmp_question, tmp_rationale, tmp_answer, direct_answer_trigger_for_fewshot,
cot_flag, 'gsm8k')
tmp_demo_dict = {'demonstration': tmp_demo, 'question': tmp_question, 'rationale': tmp_rationale,
'answer': tmp_answer}
return tmp_demo_dict
def extract_premise_and_hypothesis(nli_question):
tmp_split = nli_question.split('\nBased on this premise, can we conclude the hypothesis ')
assert tmp_split[0].startswith('Premise:\n')
tmp_split[0] = tmp_split[0][len('Premise:\n'):]
tmp_split[0] = tmp_split[0][1:-1]
assert tmp_split[1].endswith(' is true?\nOPTIONS:\n- yes\n- no\n- it is not possible to tell\nA:')
tmp_split[1] = tmp_split[1][:len(' is true?\nOPTIONS:\n- yes\n- no\n- it is not possible to tell\nA:')]
tmp_split[1] = tmp_split[1][1:-1]
return {'premise': tmp_split[0], 'hypothesis': tmp_split[1]}
def make_drop_question(tmp_js):
passage = tmp_js['passage']
question = tmp_js['question']
# answers_spans = tmp_js['answers_spans']['spans']
# answer = ', '.join(answers_spans)
final_q = '{} {}'.format(passage, question)
return final_q
def make_drop_answer(tmp_js):
answers_spans = tmp_js['answers_spans']['spans']
answer = ', '.join(answers_spans)
return answer
def make_elementary_math_qa_question(tmp_js):
CHOICE_LABEL_S = "ABCDEFGHIJKLMN"
question = tmp_js['input']
target_scores = tmp_js['target_scores']
choice = "Answer Choices:"
for i, target_score in enumerate(target_scores):
choice += " ("
choice += CHOICE_LABEL_S[i]
choice += ") "
choice += target_score[0]
result = question + ' ' + choice
return result
def make_elementary_math_qa_answer(tmp_js):
CHOICE_LABEL_S = "ABCDEFGHIJKLMN"
target_scores = tmp_js['target_scores']
for i, target_score in enumerate(target_scores):
if target_score[1] == 1:
return CHOICE_LABEL_S[i]