compute_bleu: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
from nltk.translate.bleu_score import SmoothingFunction
import re
import string
from nltk.tokenize import word_tokenize
from nltk.translate.bleu_score import sentence_bleu
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_bleu(model_output: str, answer: str) -> float: