-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
31 lines (25 loc) · 1015 Bytes
/
Copy pathutils.py
File metadata and controls
31 lines (25 loc) · 1015 Bytes
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
29
30
31
import Levenshtein
def calculate_CER(reference, hypothesis):
"""
Calculate Character Error Rate (CER) between two strings.
CER = (substitutions + deletions + insertions) / length of reference
"""
if len(reference) == 0:
return float('inf') # avoid division by zero
distance = Levenshtein.distance(reference, hypothesis)
return distance / len(reference)
def calculate_MinCER(references, hypothesis):
"""
Calculate Character Error Rate (CER) between two strings.
CER = (substitutions + deletions + insertions) / length of reference
"""
cers = [calculate_CER(reference, hypothesis) for reference in references]
return min(cers)
def calculate_accuracy_at1(references, attempt):
"""
Calculate accuracy of attempts against references.
Accuracy = correct / total
references: list[str], reference string (multiple reference strings can be provided)
attempt: str, best attempt string
"""
return 1 if attempt in references else 0