|
| 1 | +import html |
| 2 | +import re |
| 3 | + |
| 4 | +from datasets import load_metric |
| 5 | + |
| 6 | + |
| 7 | +def general_detokenize(string): |
| 8 | + string = re.sub(r"\s+([.,;:!?)])", r"\1", string) |
| 9 | + string = re.sub(r"(\s+|^)\(\s+([^)]+)\s+\)", r"\1(\2)", string) |
| 10 | + string = re.sub(r"(\s+|^)\[\s+([^)]+)\s+\]", r"\1[\2]", string) |
| 11 | + string = re.sub(r'(\s+|^)"\s+([^"]+)\s+"', r'\1"\2"', string) |
| 12 | + string = re.sub(r"(\s+|^)'\s+([^']+)\s+'", r"\1'\2'", string) |
| 13 | + return string |
| 14 | + |
| 15 | + |
| 16 | +def process_doc(string): |
| 17 | + string = html.unescape(string) |
| 18 | + string = general_detokenize(string) |
| 19 | + return string |
| 20 | + |
| 21 | + |
| 22 | +def process_wic_docs(dataset): |
| 23 | + def _helper(doc): |
| 24 | + # there's some issues with the encoding on this one |
| 25 | + doc["sentence1"] = ( |
| 26 | + process_doc(doc["sentence1"]).encode("latin-1").decode("utf-8") |
| 27 | + ) |
| 28 | + doc["sentence2"] = ( |
| 29 | + process_doc(doc["sentence2"]).encode("latin-1").decode("utf-8") |
| 30 | + ) |
| 31 | + return doc |
| 32 | + |
| 33 | + return dataset.map(_helper) |
| 34 | + |
| 35 | + |
| 36 | +def coref_doc_to_text(x): |
| 37 | + def _span_in_context(span_index, span_text): |
| 38 | + span_start = span_index |
| 39 | + span_end = span_start + len(span_text.split(" ")) - 1 |
| 40 | + tokens[span_start] = f"*{tokens[span_start]}" |
| 41 | + tokens[span_end] = f"{tokens[span_end]}*" |
| 42 | + |
| 43 | + tokens = x["text"].split(" ") |
| 44 | + _span_in_context(x["span1_index"], x["span1_text"]) |
| 45 | + _span_in_context( |
| 46 | + x["span2_index"] - 1, x["span2_text"] |
| 47 | + ) # span1_index is 0-based but span2_index is 1-based ?? |
| 48 | + context = process_doc(" ".join(tokens)) |
| 49 | + span_1 = process_doc(x["span1_text"]) |
| 50 | + span_2 = process_doc(x["span2_text"]) |
| 51 | + text = ( |
| 52 | + f"Testua: {context}\n" |
| 53 | + + f'Galdera: Aurreko testuan, "*{span_1}*" eta "*{span_2}*" gauza bera dira?\n' |
| 54 | + + "Erantzuna:" |
| 55 | + ) |
| 56 | + return text |
| 57 | + |
| 58 | + |
| 59 | +# Measure F1 as in the benchmark repo: https://github.com/orai-nlp/BasqueGLUE/blob/main/eval_basqueglue.py |
| 60 | + |
| 61 | + |
| 62 | +def micro_f1_score(items): |
| 63 | + f1_metric = load_metric("f1") |
| 64 | + golds, preds = list(zip(*items)) |
| 65 | + f1_score = f1_metric.compute(references=golds, predictions=preds, average="micro")[ |
| 66 | + "f1" |
| 67 | + ] |
| 68 | + return f1_score |
| 69 | + |
| 70 | + |
| 71 | +def vaxx_f1_score(items): |
| 72 | + f1_metric = load_metric("f1") |
| 73 | + golds, preds = list(zip(*items)) |
| 74 | + f1_class = f1_metric.compute( |
| 75 | + references=golds, predictions=preds, labels=[0, 2], average=None |
| 76 | + )["f1"] |
| 77 | + f1_score = sum(f1_class) / len(f1_class) |
| 78 | + return f1_score |
0 commit comments