Skip to content

⚡️ Speed up function eval_answer by 158% #53

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions evaluation/benchmarks/toolqa/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,18 @@ def encode_question(question):
# imported from https://github.com/night-chen/ToolQA/tree/main/benchmark/ReAct/code/agents_chatgpt.py
def normalize_answer(s):
def remove_articles(text):
return re.sub(r'\b(a|an|the|usd)\b', ' ', text)
return re.sub(r'\b(?:a|an|the|usd)\b', ' ', text) # Use non-capturing group

def white_space_fix(text):
return ' '.join(text.split())

def remove_punc(text):
exclude = set(string.punctuation)
return ''.join(ch for ch in text if ch not in exclude)
return text.translate(str.maketrans('', '', string.punctuation)) # Use str.translate & maketrans for efficiency

def lower(text):
return text.lower()

# Function composition: remove redundant variable assignments by composing functions
return white_space_fix(remove_articles(remove_punc(lower(s))))


Expand All @@ -127,4 +127,7 @@ def eval_answer(pred, answer):
match = re.search(pattern, pred)
if match:
pred = match.group(1)
return normalize_answer(pred) == normalize_answer(answer)
# Avoid recalculating normalize_answer multiple times by storing results
norm_pred = normalize_answer(pred)
norm_answer = normalize_answer(answer)
return norm_pred == norm_answer