-
Notifications
You must be signed in to change notification settings - Fork 4
/
eval.py
86 lines (74 loc) · 2.35 KB
/
eval.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import datasets
from jiwer import wer, cer
from aaas.audio_utils.asr import inference_asr
import re
from text_to_num import alpha2digit
from tqdm.auto import tqdm
from unidecode import unidecode
import diff_match_patch as dmp_module
base = []
predicted = []
dmp = dmp_module.diff_match_patch()
ds = datasets.load_dataset(
"common_voice",
"de",
split="test",
).cast_column("audio", datasets.features.Audio(sampling_rate=16000, decode=False))
ds = (
ds.filter(lambda x: x["down_votes"] == 0)
.filter(lambda x: x["up_votes"] >= 2)
.filter(lambda x: len(x["sentence"]) >= 16)
.sort("up_votes", reverse=False)
.cast_column("audio", datasets.features.Audio(sampling_rate=16000, decode=True))
)
for d in tqdm(ds):
# normalize base transcription
base_str_orig = unidecode(alpha2digit(d["sentence"], "de"))
base_str = (
re.sub(r"[^\w\s]", "", base_str_orig.strip())
.lower()
.replace("-", " ")
.replace(",", "")
.replace(" ", " ")
.replace("ph", "f")
.replace("ß", "ss")
)
try:
# load audio
audio_data = d["audio"]["array"]
# normalize prediction
pred_str_orig = unidecode(
alpha2digit(inference_asr(audio_data, "german", "large"), "de")
)
pred_str = (
re.sub(r"[^\w\s]", "", pred_str_orig.strip())
.lower()
.replace("-", " ")
.replace(",", "")
.replace(" ", " ")
.replace("ph", "f")
.replace("ß", "ss")
)
diff_score = 0
diff = dmp.diff_main(base_str.replace(" ", ""), pred_str.replace(" ", ""))
dmp.diff_cleanupSemantic(diff)
for d in diff:
if d[0] != 0:
if len(d[1]) > 1:
diff_score += 1
# append to lists
base.append(pred_str if diff_score == 0 else base_str)
predicted.append(pred_str)
# print results
if diff_score != 0:
diff = dmp.diff_main(base_str, pred_str)
dmp.diff_cleanupSemantic(diff)
print()
print(diff)
print("normalized", wer(base, predicted) * 100, cer(base, predicted) * 100)
print()
except Exception as e:
print(e)
if len(base) > 100000:
break
print(wer(base, predicted) * 100, cer(base, predicted) * 100)