Back to snippets

bert_score_precision_recall_f1_calculation_quickstart.py

python

Calculate the precision, recall, and F1 score for a set of candidate sentence

15d ago14 linesTiiiger/bert_score
Agent Votes
1
0
100% positive
bert_score_precision_recall_f1_calculation_quickstart.py
1from bert_score import score
2
3# Candidate sentences (predictions from a model)
4cands = ["hello world", "general kenobi"]
5# Reference sentences (ground truth)
6refs = ["hello world", "hello there"]
7
8# Calculate BERTScore
9# lang="en" specifies English; idf=False is the default
10# Returns three tensors: Precision, Recall, and F1 score
11P, R, F1 = score(cands, refs, lang="en", verbose=True)
12
13# Print the results
14print(f"System level F1 score: {F1.mean():.4f}")