sft-6k / thinker /text_utils.py
VOLBEM's picture
Add files using upload-large-folder tool
ebcf321 verified
Raw
History Blame Contribute Delete
4.29 kB
"""
Text utilities for V4 multi-task evaluation.
Includes: WER/CER (from v3) + accuracy + F1 + simple BLEU.
"""
import re
import unicodedata
from collections import Counter
# =============================================================
# Language detection & normalization (from v3)
# =============================================================
def detect_language(text: str) -> str:
if not text: return "en"
cjk = sum(1 for c in text if '\u4e00' <= c <= '\u9fff')
return "zh" if cjk / max(len(text), 1) >= 0.3 else "en"
def normalize_text(text: str, lang: str = None) -> str:
if lang is None: lang = detect_language(text)
text = text.strip().lower()
text = unicodedata.normalize("NFKC", text)
text = re.sub(r'[^\w\s]', '', text)
if lang == "zh":
text = re.sub(r'\s+', '', text)
else:
text = re.sub(r'\s+', ' ', text).strip()
return text
# =============================================================
# WER / CER
# =============================================================
def _edit_distance(a, b):
m, n = len(a), len(b)
dp = list(range(n + 1))
for i in range(1, m + 1):
prev, dp[0] = dp[0], i
for j in range(1, n + 1):
temp = dp[j]
dp[j] = prev if a[i-1] == b[j-1] else 1 + min(dp[j], dp[j-1], prev)
prev = temp
return dp[n]
def compute_wer(ref: str, hyp: str) -> float:
ref_w = normalize_text(ref, "en").split()
hyp_w = normalize_text(hyp, "en").split()
if not ref_w: return 0.0 if not hyp_w else float(len(hyp_w))
return _edit_distance(ref_w, hyp_w) / len(ref_w)
def compute_cer(ref: str, hyp: str) -> float:
ref_c = list(normalize_text(ref, "zh"))
hyp_c = list(normalize_text(hyp, "zh"))
if not ref_c: return 0.0 if not hyp_c else float(len(hyp_c))
return _edit_distance(ref_c, hyp_c) / len(ref_c)
# =============================================================
# Accuracy (exact match after normalization)
# =============================================================
def compute_accuracy(refs: list, hyps: list) -> float:
if not refs: return 0.0
correct = 0
for r, h in zip(refs, hyps):
rn = normalize_text(r).strip()
hn = normalize_text(h).strip()
if rn == hn: correct += 1
return correct / len(refs)
# =============================================================
# F1 for comma-separated labels (audio events)
# =============================================================
def compute_label_f1(refs: list, hyps: list) -> float:
"""Macro-averaged F1 over samples. Each ref/hyp is comma-separated labels."""
if not refs: return 0.0
total_f1 = 0.0
for r, h in zip(refs, hyps):
ref_set = set(x.strip().lower() for x in r.split(",") if x.strip())
hyp_set = set(x.strip().lower() for x in h.split(",") if x.strip())
if not ref_set and not hyp_set:
total_f1 += 1.0; continue
if not ref_set or not hyp_set:
continue
tp = len(ref_set & hyp_set)
prec = tp / len(hyp_set) if hyp_set else 0
rec = tp / len(ref_set) if ref_set else 0
total_f1 += 2*prec*rec / (prec+rec) if (prec+rec) > 0 else 0
return total_f1 / len(refs)
# =============================================================
# Simple BLEU-4 (sentence level, for translation)
# =============================================================
def _ngrams(tokens, n):
return [tuple(tokens[i:i+n]) for i in range(len(tokens)-n+1)]
def compute_bleu4(refs: list, hyps: list) -> float:
if not refs: return 0.0
total = 0.0
for r, h in zip(refs, hyps):
ref_tok = normalize_text(r).split() or list(normalize_text(r))
hyp_tok = normalize_text(h).split() or list(normalize_text(h))
if not ref_tok or not hyp_tok: continue
bp = min(1.0, len(hyp_tok) / len(ref_tok)) if ref_tok else 0
score = bp
for n in range(1, 5):
ref_ng = Counter(_ngrams(ref_tok, n))
hyp_ng = Counter(_ngrams(hyp_tok, n))
matches = sum((hyp_ng & ref_ng).values())
total_hyp = max(sum(hyp_ng.values()), 1)
prec = matches / total_hyp
score *= max(prec, 1e-10) ** 0.25
total += score
return total / len(refs)