|
| 1 | +import re |
| 2 | +import sys |
| 3 | +import unicodedata |
1 | 4 | from functools import partial
|
2 | 5 |
|
| 6 | +from lmms_eval.filters.extraction import RegexFilter |
| 7 | + |
3 | 8 | choices = [
|
4 | 9 | "A",
|
5 | 10 | "B",
|
@@ -58,3 +63,100 @@ def process_docs(dataset, subject):
|
58 | 63 | process_philosophy = partial(process_docs, subject="philosophy")
|
59 | 64 | process_physics = partial(process_docs, subject="physics")
|
60 | 65 | process_psychology = partial(process_docs, subject="psychology")
|
| 66 | + |
| 67 | + |
| 68 | +class MultiChoiceRegexFilter(RegexFilter): |
| 69 | + """ """ |
| 70 | + |
| 71 | + def __init__( |
| 72 | + self, |
| 73 | + regex_pattern: str = r"#### (\-?[0-9\.\,]+)", |
| 74 | + group_select=0, |
| 75 | + fallback: str = "[invalid]", |
| 76 | + ignore_case=False, |
| 77 | + ignore_punctuation=False, |
| 78 | + regexes_to_ignore=None, |
| 79 | + ) -> None: |
| 80 | + """ |
| 81 | + regex_pattern: The basic regex pattern to use. If fails to match, we will use the customized match procedure |
| 82 | + - step 1 : We parse the choices between ([A-Z])s then try to find these choices in the response. |
| 83 | + - step 2 : We parse the choice with regex :[\s]*([A-?]), where ? varies by number of choices. |
| 84 | + group_select: Selects the (group_select)th match from the findall result. |
| 85 | + ignore_case: Ignores the case during step 1 matching |
| 86 | + ignore_punctuation: Remove the punctuation during step 1 matching |
| 87 | + regexes_to_ignore: Remove these regexes during step 1 matching |
| 88 | + """ |
| 89 | + super().__init__(regex_pattern, group_select, fallback) |
| 90 | + self.ignore_case = ignore_case |
| 91 | + self.ignore_punctuation = ignore_punctuation |
| 92 | + self.regexes_to_ignore = regexes_to_ignore |
| 93 | + |
| 94 | + def apply(self, resps, docs): |
| 95 | + # here, we assume we have a list, in which each element is |
| 96 | + # a list of model responses for some particular input/target pair. |
| 97 | + # so we process each of these (same input/target response sets) |
| 98 | + # independently (and keep them a list.) |
| 99 | + |
| 100 | + def find_match(regex, resp, convert_dict={}): |
| 101 | + match = regex.findall(resp) |
| 102 | + if match: |
| 103 | + match = match[self.group_select] |
| 104 | + if isinstance(match, tuple): |
| 105 | + match = [m for m in match if m][0] |
| 106 | + match = match.strip() |
| 107 | + if match and match in convert_dict: |
| 108 | + match = convert_dict[match] |
| 109 | + return match |
| 110 | + |
| 111 | + punct_tbl = dict.fromkeys(i for i in range(sys.maxunicode) if unicodedata.category(chr(i)).startswith("P")) |
| 112 | + |
| 113 | + def filter_ignores(st): |
| 114 | + if self.regexes_to_ignore is not None: |
| 115 | + for s in self.regexes_to_ignore: |
| 116 | + st = re.sub(s, "", st) |
| 117 | + |
| 118 | + if self.ignore_case: |
| 119 | + st = st.lower() |
| 120 | + |
| 121 | + if self.ignore_punctuation: |
| 122 | + # https://stackoverflow.com/a/266162 |
| 123 | + st = st.translate(punct_tbl) |
| 124 | + return st |
| 125 | + |
| 126 | + filtered_resps = [] |
| 127 | + |
| 128 | + for r, doc in zip(resps, docs): |
| 129 | + fallback_regexes = [] |
| 130 | + choice_to_alpha = {} |
| 131 | + next_alpha = "A" |
| 132 | + |
| 133 | + without_paren_fallback_regexes = [] |
| 134 | + without_paren_to_target = {} |
| 135 | + |
| 136 | + choices = doc["options"] |
| 137 | + for c in choices: |
| 138 | + m = filter_ignores(c.strip()) |
| 139 | + fallback_regexes.append(f"{re.escape(m)}") |
| 140 | + choice_to_alpha[m] = f"({next_alpha})" |
| 141 | + |
| 142 | + without_paren_fallback_regexes.append(next_alpha) |
| 143 | + without_paren_to_target[next_alpha] = f"({next_alpha})" |
| 144 | + |
| 145 | + next_alpha = chr(ord(next_alpha) + 1) |
| 146 | + fallback_regex = re.compile("|".join(fallback_regexes)) |
| 147 | + without_paren_fallback_regex = "|".join(without_paren_fallback_regexes) |
| 148 | + without_paren_fallback_regex = re.compile(f":[\s]*({without_paren_fallback_regex})") |
| 149 | + |
| 150 | + filtered = [] |
| 151 | + for resp in r: |
| 152 | + match = find_match(self.regex, resp) |
| 153 | + if not match: |
| 154 | + match = find_match(fallback_regex, filter_ignores(resp), choice_to_alpha) |
| 155 | + if not match: |
| 156 | + match = find_match(without_paren_fallback_regex, resp, without_paren_to_target) |
| 157 | + if not match: |
| 158 | + match = self.fallback |
| 159 | + filtered.append(match) |
| 160 | + filtered_resps.append(filtered) |
| 161 | + |
| 162 | + return filtered_resps |
0 commit comments