Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: WebSRC should be token-level F1 NOT character-level #70

Merged
merged 1 commit into from
May 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions lmms_eval/tasks/websrc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def websrc_process_results(doc, results):
"websrc_squad_f1": websrc_ans,
"submission": {
websrc_ans['question_id']: pred,
},
} if 'question_id' in websrc_ans else None
}


Expand Down Expand Up @@ -122,27 +122,39 @@ def _normalize_str(string):
# lower it
string = string.lower()

# strip non-alphanumeric characters
string = re.sub(r"[^a-zA-Z0-9]", "", string)

# strip leading and trailing whitespaces
string = string.strip()

return string

def _tokenize(text):
# Regex pattern to match words and isolate punctuation
pattern = r'\w+|[^\w\s]'
tokens = re.findall(pattern, text)
return tokens

def _compute_f1(sa, sb):
sa = _normalize_str(sa)
sb = _normalize_str(sb)

sa = _tokenize(sa)
sb = _tokenize(sb)

sa = set(sa)
sb = set(sb)

if len(sa) == 0 or len(sb) == 0:
return 0.0

comm = sa.intersection(sb)
prec = len(comm) / len(sb)
rec = len(comm) / len(sa)
f1 = 2 * prec * rec / (prec + rec) if prec + rec > 0 else 0
return f1

judge_list = []
for sample in samples:
gold_i = set(_normalize_str(sample["answer"]))
pred_i = set(_normalize_str( sample["parsed_pred"]))
if len(pred_i) == 0:
judge_list.append(0.0)
continue

comm_i = gold_i.intersection(pred_i)
prec_i = len(comm_i) / len(pred_i)
rec_i = len(comm_i) / len(gold_i)
f1_i = 2 * prec_i * rec_i / (prec_i + rec_i) if prec_i + rec_i > 0 else 0
judge_list.append(f1_i)
judge_list.append(_compute_f1(sample["answer"], sample["parsed_pred"]))

f1 = np.mean(judge_list)
return judge_list, {"f1": f1}
Loading