66 lines
3.1 KiB
Python
66 lines
3.1 KiB
Python
"""Transparent STT round-trip distance, not a human naturalness or TTS-only score."""
|
|
import json
|
|
import re
|
|
import sys
|
|
import unicodedata
|
|
import os
|
|
from pathlib import Path
|
|
ROOT = Path(__file__).resolve().parent
|
|
search = ([Path(os.environ['DECK_SOURCE'])] if os.environ.get('DECK_SOURCE') else [])
|
|
search += [path for parent in ROOT.parents for path in (parent, parent/'repo')]
|
|
source = next(path for path in search if (path/'speech_text.py').is_file())
|
|
sys.path.insert(0, str(source))
|
|
from speech_text import prepare_for_speech
|
|
|
|
def de_number(n):
|
|
basic=['null','eins','zwei','drei','vier','fünf','sechs','sieben','acht','neun',
|
|
'zehn','elf','zwölf','dreizehn','vierzehn','fünfzehn','sechzehn','siebzehn','achtzehn','neunzehn']
|
|
if n < 20:
|
|
return basic[n]
|
|
if n < 100:
|
|
tens=['','','zwanzig','dreißig','vierzig','fünfzig','sechzig','siebzig','achtzig','neunzig']
|
|
return (('ein' if n%10 == 1 else basic[n%10])+'und' if n%10 else '')+tens[n//10]
|
|
if n < 1000:
|
|
return ('ein' if n//100 == 1 else basic[n//100])+'hundert'+(de_number(n%100) if n%100 else '')
|
|
return str(n)
|
|
|
|
def tokens(text):
|
|
text=prepare_for_speech(text,'German')
|
|
text=re.sub(r'\b\d{1,3}\b',lambda m:de_number(int(m[0])),text)
|
|
text=unicodedata.normalize('NFC',text).casefold().replace('ß','ss')
|
|
return re.findall(r'[^\W_]+',text,re.UNICODE)
|
|
|
|
def distance(a,b):
|
|
previous=list(range(len(b)+1))
|
|
for i,x in enumerate(a,1):
|
|
current=[i]
|
|
for j,y in enumerate(b,1):
|
|
current.append(min(current[-1]+1,previous[j]+1,previous[j-1]+(x!=y)))
|
|
previous=current
|
|
return previous[-1]
|
|
|
|
cases={x['id']:x for x in json.loads((ROOT/'cases.json').read_text())}
|
|
rows=[]
|
|
for filename in ['moss-stt.json','neutts-stt.json']:
|
|
p=ROOT/filename
|
|
if not p.exists():continue
|
|
for row in json.loads(p.read_text()):
|
|
if filename.startswith('neutts') and row['arm']!='neutts':continue
|
|
ident=row['file'].rsplit('-',1)[0]
|
|
a=tokens(cases[ident]['prepared']);b=tokens(row.get('text',''))
|
|
edits=distance(a,b)
|
|
rows.append(dict(arm=row['arm'],file=row['file'],case_id=ident,
|
|
reference_words=len(a),edits=edits,wer=edits/len(a),
|
|
recognized_words=len(b),stt_text=row.get('text'),
|
|
error_type=row.get('error_type')))
|
|
summary={}
|
|
for arm in sorted({x['arm'] for x in rows}):
|
|
summary[arm]={}
|
|
for group in ['all','plain','technical']:
|
|
selected=[x for x in rows if x['arm']==arm and (group=='all' or (
|
|
x['case_id'] in ['story','long'] if group=='plain' else x['case_id'] in ['onset','previous-failure','technical']))]
|
|
words=sum(x['reference_words'] for x in selected);edits=sum(x['edits'] for x in selected)
|
|
summary[arm][group]=dict(cases=len(selected),words=words,edits=edits,wer=edits/words if words else None)
|
|
(ROOT/'roundtrip-scores.json').write_text(json.dumps(dict(method='Same German preparation, number expansion <=999, case and punctuation normalized. Combined TTS+STT error, not a pure TTS rate.',summary=summary,rows=rows),ensure_ascii=False,indent=2))
|
|
print(json.dumps(summary,indent=2))
|