|
| 1 | +import re |
| 2 | +import string |
| 3 | + |
| 4 | +from default import ( |
| 5 | + TOP1_OUTPUT_IGNORE_REGEX, |
| 6 | + TOPK_OUTPUT_IGNORE_REGEX, |
| 7 | + CoT_OUTPUT_IGNORE_REGEX, |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +def normalize_em_triviaqa(s: str) -> str: |
| 12 | + def remove_articles(text): |
| 13 | + return re.sub(r"\b(a|an|the)\b", " ", text) |
| 14 | + |
| 15 | + def white_space_fix(text): |
| 16 | + return " ".join(text.split()) |
| 17 | + |
| 18 | + def handle_punc(text): |
| 19 | + exclude = set(string.punctuation + "".join(["‘", "’", "´", "`"])) |
| 20 | + return "".join(ch if ch not in exclude else " " for ch in text) |
| 21 | + |
| 22 | + def lower(text): |
| 23 | + return text.lower() |
| 24 | + |
| 25 | + def replace_underscore(text): |
| 26 | + return text.replace("_", " ") |
| 27 | + |
| 28 | + return white_space_fix( |
| 29 | + remove_articles(handle_punc(lower(replace_underscore(s)))) |
| 30 | + ).strip() |
| 31 | + |
| 32 | + |
| 33 | +def process_output_top1_triviaqa(output: str) -> str: |
| 34 | + output = TOP1_OUTPUT_IGNORE_REGEX.sub("", output) |
| 35 | + output = normalize_em_triviaqa(output) |
| 36 | + return output |
| 37 | + |
| 38 | + |
| 39 | +def process_output_topk_triviaqa(output: str) -> str: |
| 40 | + output = TOPK_OUTPUT_IGNORE_REGEX.sub("", output) |
| 41 | + output = normalize_em_triviaqa(output) |
| 42 | + return output |
| 43 | + |
| 44 | + |
| 45 | +def process_output_cot_triviaqa(output: str) -> str: |
| 46 | + output = CoT_OUTPUT_IGNORE_REGEX.sub("", output) |
| 47 | + output = normalize_em_triviaqa(output) |
| 48 | + return output |
0 commit comments