Datasets:
File size: 13,245 Bytes
f2ad62a |
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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
from evaluation_utils import quac_correct_retrieved_instance_idx_list
from evaluation_utils import unanswerable_keyphrases
import json
from metrics import F1Metric
import copy
import re
def compute_f1_score(predicted_answers, groundtruth_answer, exp_name="default"):
"""Evaluating F1 Score"""
print(len(predicted_answers), len(groundtruth_answer))
if len(predicted_answers) != len(groundtruth_answer):
groundtruth_answer = groundtruth_answer[:len(predicted_answers)]
guess_list = []
for guess in predicted_answers:
guess = guess.strip()
if "</s>" in guess:
guess = guess.replace("</s>", "")
guess_list.append(guess)
answer_list = []
for answer in groundtruth_answer:
answer_list.append(answer)
assert len(guess_list) == len(answer_list), \
"lengths of guess and answer are different!"
precision, recall, f1 = F1Metric.compute_all_pairs(guess_list, answer_list)
print('Method: %s; Precision: %.4f; recall: %.4f; f1: %.4f' % (\
exp_name, precision, recall, f1))
def load_groundtruth_file(data_file):
with open(data_file, "r") as f:
examples = json.load(f)
data = []
for instance in examples:
if "answers" in instance:
answers = instance["answers"]
elif "answer" in instance:
if type(instance["answer"]) is str:
answers = [instance["answer"]]
elif type(instance["answer"]) is list:
answers = instance["answer"]
else:
answers = [str(instance["answer"])]
else:
raise ValueError("need to have answer or answers")
data.append(answers)
return data
def load_prediction(data_file):
data = []
with open(data_file, "r") as f:
for line in f.readlines():
data.append(line.strip())
return data
def evaluate_f1(ground_truth_file, prediction_file):
groundtruth_answers = load_groundtruth_file(ground_truth_file)
if "inscit" in ground_truth_file:
groundtruth_answers_update = []
for answers in groundtruth_answers:
answers_update = []
for ans in answers:
## this answer is additionally added to the answer_list for inscit dataset, needs to remove
if ans != "Sorry. I cannot find the answer based on the context.":
answers_update.append(ans)
assert len(answers_update) > 0
groundtruth_answers_update.append(copy.deepcopy(answers_update))
groundtruth_answers = groundtruth_answers_update
predicted_answers = load_prediction(prediction_file)
if "quac" in prediction_file or "doqa" in prediction_file:
predicted_answers_new = []
for pred in predicted_answers:
pred = pred.lower()
for keyphrase in unanswerable_keyphrases:
if keyphrase in pred:
pred = "Sorry. I cannot find the answer based on the context."
break
predicted_answers_new.append(pred)
predicted_answers = predicted_answers_new
compute_f1_score(predicted_answers, groundtruth_answers)
def separate_cannot_answer(ground_truth_file, prediction_file):
# load ground truth
with open(ground_truth_file, "r") as f:
groundtruth_answers = json.load(f)
# load prediction
predicted_answers = load_prediction(prediction_file)
print(len(predicted_answers), len(groundtruth_answers))
if len(predicted_answers) != len(groundtruth_answers):
groundtruth_answers = groundtruth_answers[:len(predicted_answers)]
if "quac" in prediction_file:
"""
For answerable cases, we want to make sure the retrieved context list contains the gold chunk.
For QuAC dataset, we use top-5 retrieved contexts as inputs, quac_correct_retrieved_instance_idx_list
is the index list where the top-5 retrieved context contains the gold answer
"""
answerable_instance_idx_list = quac_correct_retrieved_instance_idx_list
else:
answerable_instance_idx_list = None
predicted_answers_new = []
for pred in predicted_answers:
pred = pred.lower()
for keyphrase in unanswerable_keyphrases:
if keyphrase in pred:
pred = "Sorry. I cannot find the answer based on the context."
break
predicted_answers_new.append(pred)
predicted_answers = predicted_answers_new
cannot_answer_idx_list = []
answerable_idx_list = []
if answerable_instance_idx_list:
count_idx = 0
for idx, item in enumerate(groundtruth_answers):
if 'answers' in item:
answer = item["answers"][0]
else:
answer = item['answer']
noanswer_response = "Sorry. I cannot find the answer based on the context."
if answer == noanswer_response:
cannot_answer_idx_list.append(idx)
continue
if answerable_instance_idx_list:
if count_idx in answerable_instance_idx_list:
answerable_idx_list.append(idx)
count_idx += 1
else:
answerable_idx_list.append(idx)
print("number of cannot answer cases: %d (out of %d)" % (len(cannot_answer_idx_list), len(groundtruth_answers)))
print("number of answerable cases: %d (out of %d)" % (len(answerable_idx_list), len(groundtruth_answers)))
return predicted_answers, cannot_answer_idx_list, answerable_idx_list
def get_cannot_answer_and_answerable_acc(predicted_answers, cannot_answer_idx_list, answerable_idx_list):
# cannot answer
noanswer_count = 0
for idx in cannot_answer_idx_list:
prediction = predicted_answers[idx]
prediction = prediction.lower()
# print(prediction)
if "sorry" in prediction and "cannot find the answer" in prediction:
# print(prediction)
noanswer_count += 1
cannot_answer_acc = noanswer_count / len(cannot_answer_idx_list)
print("accuracy of cannot answer cases: %.4f" % cannot_answer_acc)
# answerable
answerable_count = 0
for idx in answerable_idx_list:
prediction = predicted_answers[idx]
prediction = prediction.lower()
if "sorry" in prediction and "cannot find the answer" in prediction:
# print(prediction)
continue
answerable_count += 1
answerable_acc = answerable_count / len(answerable_idx_list)
print("accuracy of answerable cases: %.4f" % answerable_acc)
def evaluate_cannot_answer_acc(ground_truth_file, prediction_file):
predicted_answers, cannot_answer_idx_list, answerable_idx_list = \
separate_cannot_answer(ground_truth_file, prediction_file)
get_cannot_answer_and_answerable_acc(predicted_answers, cannot_answer_idx_list, answerable_idx_list)
def evaluate_convfinqa(ground_truth_file, prediction_file):
"""
Since the model will give a long answer output, while the gold answer for ConvFinQA are either
a arithmetic formula or a final executed number.
We consider the output containing either the executed number or the arithmetic formula as correct.
This script is to measure the proportion of the outputs containing these elements.
"""
def _is_float(string):
try:
float(string)
return True
except ValueError:
return False
with open(ground_truth_file, "r") as f:
gold_list = json.load(f)
groundtruth_answers = [item['exe_answer'] for item in gold_list]
groundtruth_answers_formula = [item['answers'][0] for item in gold_list]
## last turn question_list
question_list = [item['messages'][-1]['content'] for item in gold_list]
predicted_answers = load_prediction(prediction_file)
print(len(predicted_answers), len(groundtruth_answers))
if len(predicted_answers) != len(groundtruth_answers):
groundtruth_answers = groundtruth_answers[:len(predicted_answers)]
count_exact_match = 0
for question, pred, gold, gold_formula in zip(question_list, predicted_answers, groundtruth_answers, groundtruth_answers_formula):
original_pred = pred
## convert 1,000,000 into 1000000
original_pred = original_pred.replace(",", "")
## convert $10 million + $20 million into 10 + 20
original_pred = original_pred.replace("$", "").replace("million", "").replace("billion", "")
## convert 10 (2017) + 20 (2018) into 10 + 20
pattern = r'\((\b\w+\b)\)'
original_pred = re.sub(pattern, '', original_pred)
## make sure it each token only has one space in between
original_pred = " ".join(original_pred.split())
if str(gold) in original_pred:
count_exact_match += 1
elif str(gold_formula) in original_pred:
count_exact_match += 1
elif _is_float(gold) and (str(round(float(gold), 3)) in original_pred or str(round(float(gold), 2)) in original_pred):
count_exact_match += 1
elif "percent" in question and (str(float(gold)*100) in original_pred or str(round(float(gold)*100, 1)) in original_pred or str(round(float(gold)*100, 2)) in original_pred):
count_exact_match += 1
elif str(gold).endswith(".0") and str(int(gold)) in original_pred:
## gold is a integer like 80.0 then convert it into 80
count_exact_match += 1
elif "decrease" in original_pred and _is_float(gold) and gold < 0 and (str(-1 * gold) in original_pred):
## for the case where model generates something like a decrese of 10 million, while gold is -10.
count_exact_match += 1
print("accuracy of exact match: %.4f" % (count_exact_match/len(predicted_answers)))
def main():
## doc2dial
prediction_file = "PATH_TO_THE_GENERATED_OUTPUT" # e.g., outputs/doc2idal_output.txt
ground_truth_file = "PATH_TO_THE_TEST_DATA" # e.g., data/doc2dial/test.json
print("-"*80)
print(prediction_file)
print(ground_truth_file)
evaluate_f1(ground_truth_file, prediction_file)
## quac
prediction_file = "PATH_TO_THE_GENERATED_OUTPUT"
ground_truth_file = "PATH_TO_THE_TEST_DATA"
print("-"*80)
print(prediction_file)
print(ground_truth_file)
evaluate_f1(ground_truth_file, prediction_file)
evaluate_cannot_answer_acc(ground_truth_file, prediction_file)
## qrecc
prediction_file = "PATH_TO_THE_GENERATED_OUTPUT"
ground_truth_file = "PATH_TO_THE_TEST_DATA"
print("-"*80)
print(prediction_file)
print(ground_truth_file)
evaluate_f1(ground_truth_file, prediction_file)
## topiocqa
prediction_file = "PATH_TO_THE_GENERATED_OUTPUT"
ground_truth_file = "PATH_TO_THE_TEST_DATA"
print("-"*80)
print(prediction_file)
print(ground_truth_file)
evaluate_f1(ground_truth_file, prediction_file)
## inscit
prediction_file = "PATH_TO_THE_GENERATED_OUTPUT"
ground_truth_file = "PATH_TO_THE_TEST_DATA"
print("-"*80)
print(prediction_file)
print(ground_truth_file)
evaluate_f1(ground_truth_file, prediction_file)
## coqa
prediction_file = "PATH_TO_THE_GENERATED_OUTPUT"
ground_truth_file = "PATH_TO_THE_TEST_DATA"
print("-"*80)
print(prediction_file)
print(ground_truth_file)
evaluate_f1(ground_truth_file, prediction_file)
## hybridial
prediction_file = "PATH_TO_THE_GENERATED_OUTPUT"
ground_truth_file = "PATH_TO_THE_TEST_DATA"
print("-"*80)
print(prediction_file)
print(ground_truth_file)
evaluate_f1(ground_truth_file, prediction_file)
## sqa
prediction_file = "PATH_TO_THE_GENERATED_OUTPUT"
ground_truth_file = "PATH_TO_THE_TEST_DATA"
print("-"*80)
print(prediction_file)
print(ground_truth_file)
evaluate_f1(ground_truth_file, prediction_file)
## doqa_cooking
prediction_file = "PATH_TO_THE_GENERATED_OUTPUT"
ground_truth_file = "PATH_TO_THE_TEST_DATA"
print("-"*80)
print(prediction_file)
print(ground_truth_file)
evaluate_f1(ground_truth_file, prediction_file)
evaluate_cannot_answer_acc(ground_truth_file, prediction_file)
## doqa_travel
prediction_file = "PATH_TO_THE_GENERATED_OUTPUT"
ground_truth_file = "PATH_TO_THE_TEST_DATA"
print("-"*80)
print(prediction_file)
print(ground_truth_file)
evaluate_f1(ground_truth_file, prediction_file)
evaluate_cannot_answer_acc(ground_truth_file, prediction_file)
## doqa_movies
prediction_file = "PATH_TO_THE_GENERATED_OUTPUT"
ground_truth_file = "PATH_TO_THE_TEST_DATA"
print("-"*80)
print(prediction_file)
print(ground_truth_file)
evaluate_f1(ground_truth_file, prediction_file)
evaluate_cannot_answer_acc(ground_truth_file, prediction_file)
## convfinqa
prediction_file = "PATH_TO_THE_GENERATED_OUTPUT"
ground_truth_file = "PATH_TO_THE_TEST_DATA"
print("-"*80)
print(prediction_file)
print(ground_truth_file)
evaluate_convfinqa(ground_truth_file, prediction_file)
if __name__ == "__main__":
main()
|