Spaces:
Runtime error
Runtime error
File size: 744 Bytes
10b912d |
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 |
from collections import Counter
from rouge_score import rouge_scorer
ROUGE_TYPES = ["rouge1", "rouge2", "rougeL"]
rouge_scorer = rouge_scorer.RougeScorer(
ROUGE_TYPES,
use_stemmer=True
)
def compute_token_f1(tgt_tokens, pred_tokens, use_counts=True):
if not use_counts:
tgt_tokens = set(tgt_tokens)
pred_tokens = set(pred_tokens)
tgt_counts = Counter(tgt_tokens)
pred_counts = Counter(pred_tokens)
overlap = 0
for t in (set(tgt_tokens) | set(pred_tokens)):
overlap += min(tgt_counts[t], pred_counts[t])
p = overlap / len(pred_tokens) if overlap > 0 else 0.
r = overlap / len(tgt_tokens) if overlap > 0 else 0.
f1 = (2 * p * r) / (p + r) if min(p, r) > 0 else 0.
return f1
|