compute_diff:v0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import weave
import re
import string
import difflib
def normalize_text(text: str) -> str:
"""
Normalize the input text by lowercasing, removing punctuation, and extra whitespace.
Args:
text (str): The input text to normalize.
Returns:
str: The normalized text.
"""
# Convert to lowercase
if text is None:
return "no output"
text = text.lower()
text = re.sub(r"[^\w\s\d]", " ", text)
text = text.translate(str.maketrans("", "", string.punctuation))
text = re.sub(r"\s+", " ", text).strip()
return text
@weave.op()
def compute_diff(model_output: str, answer: str) -> float:
"""
Compute the similarity ratio between the normalized model output and the expected answer.