forked from mohammadhashemii/FFC-RNN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetric.py
More file actions
33 lines (27 loc) · 1.33 KB
/
metric.py
File metadata and controls
33 lines (27 loc) · 1.33 KB
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
32
33
from jiwer import wer
import jiwer
class HandwrittenRecognitionMetrics:
def __init__(self):
self.wer, self.ser, self.cer = 0, 0, 0
self.n_smaples = 0
self.n_words = 0
self.n_chars = 0
def update_metric(self, sample: str, ground_truth: str, char_based=False):
if not char_based:
measures = jiwer.compute_measures(ground_truth, sample)
word_errors = self.wer * self.n_words + (
measures['substitutions'] + measures['deletions'] + measures['insertions'])
ser_sum = self.ser * self.n_smaples + int(ground_truth != sample)
self.n_smaples += 1
self.n_words += len(ground_truth.split(" "))
self.wer = word_errors / self.n_words
self.ser = ser_sum / self.n_smaples
else:
measures = jiwer.compute_measures(ground_truth, sample)
word_errors = self.wer * self.n_words + (
measures['substitutions'] + measures['deletions'] + measures['insertions'])
cer_sum = self.cer * self.n_chars + jiwer.cer(ground_truth, sample) * len(list(ground_truth))
self.n_chars += len(list(ground_truth))
self.n_words += len(ground_truth.split(" "))
self.wer = word_errors / self.n_words
self.cer = cer_sum / self.n_chars