Files
Athena-Deck/test_speech_text.py
T

60 lines
3.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import unittest
from speech_text import prepare_for_speech, expand_german_abbreviations
from tts_worker import spoken_chunks
class SpeechPreparationTests(unittest.TestCase):
def test_common_german_abbreviations(self):
cases={'u.a.':'unter anderem','u. a.':'unter anderem','u.\u00a0a.':'unter anderem',
'z.B.':'zum Beispiel','z. B.':'zum Beispiel','d.h.':'das heißt',
'i. d. R.':'in der Regel','u.U.':'unter Umständen','m.E.':'meines Erachtens',
'bzw.':'beziehungsweise','ggf.':'gegebenenfalls','evtl.':'eventuell',
'usw.':'und so weiter','inkl.':'inklusive','zzgl.':'zuzüglich',
'Nr.':'Nummer','Dr.':'Doktor','Prof.':'Professor'}
for raw,spoken in cases.items():
with self.subTest(raw=raw):self.assertEqual(expand_german_abbreviations(raw),spoken)
def test_abbreviations_preserve_identifiers_and_ambiguous_forms(self):
raw='https://example.org/u.a./Dr. /config/u.a.txt mike@bspw.de foo.bzw.txt St. S. min. KCSM-TV WITF KTEH'
self.assertEqual(expand_german_abbreviations(raw),raw)
def test_user_production_example(self):
raw='1. Produziert wurde u.a. von KCSM-TV, WITF (Pennsylvania) und KTEH San Jose – mitten im Silicon Valley, Cheifet Productions bis zum Schluss.'
spoken=prepare_for_speech(raw)
self.assertEqual(spoken,raw.replace('u.a.','unter anderem'))
self.assertNotIn('u.a.', ' '.join(spoken_chunks(dict(text=raw,language='German'))))
self.assertEqual(expand_german_abbreviations(spoken),spoken)
def test_weather_units_and_ranges(self):
text=prepare_for_speech('15°–23 °C, 93 %, ca. 12 mm, v.a. um 05–06 Uhr, Wind max. 16 km/h.')
for expected in ('15 bis 23 Grad','93 Prozent','circa 12 mm','vor allem','5 bis 6 Uhr','Wind maximal 16 Kilometer pro Stunde'):
self.assertIn(expected,text)
for original in ('29 km/h','29 KM / H','29 kmh'):
self.assertEqual(prepare_for_speech(original),'29 Kilometer pro Stunde')
def test_clock_is_distinct_from_aspect_ratio(self):
text=prepare_for_speech('Beginn um 16:09 Uhr, Fehler um 02:14; das Videoformat ist 16:9.')
self.assertIn('16 Uhr 9',text);self.assertNotIn('16 Uhr 9 Uhr',text)
self.assertIn('2 Uhr 14',text);self.assertIn('Videoformat ist 16 zu 9',text)
def test_dates_and_ip_addresses(self):
text=prepare_for_speech('full_kiosk (192.168.1.5): 2× Timeout seit 01.09. und vom 04.–05.09. sowie 04.09.–06.10.2026.')
for expected in ('full kiosk','192 Punkt 168 Punkt 1 Punkt 5','2 mal Timeout','1. September','4. bis 5. September','4. September bis 6. Oktober 2026'):
self.assertIn(expected,text)
def test_high_low_and_postcode(self):
text=prepare_for_speech('Rastatt (76437), 22° / 10°.')
self.assertIn('Postleitzahl sieben sechs vier drei sieben',text)
self.assertIn('Höchstwert 22 Grad, Tiefstwert 10 Grad',text)
def test_only_speech_is_rewritten(self):
original='## Wind\n- [Quelle](https://example.org): **29 km/h** 😊'
spoken=prepare_for_speech(original)
self.assertIn('29 Kilometer pro Stunde',spoken);self.assertNotIn('https:',spoken);self.assertNotIn('😊',spoken)
self.assertIn('29 km/h',original)
def test_other_languages_do_not_receive_german_words(self):
self.assertNotIn('Kilometer pro Stunde',prepare_for_speech('29 km/h at 02:14','English'))
def test_worker_prepares_before_sentence_splitting(self):
chunks=spoken_chunks(dict(text='Am 05.10.2026 um 02:14 Uhr: 29 km/h. Alles gut!',language='German'))
self.assertIn('5. Oktober 2026',chunks[0]);self.assertIn('2 Uhr 14',chunks[0])
self.assertIn('29 Kilometer pro Stunde',chunks[0]);self.assertEqual(chunks[-1],'Alles gut!')
def test_punctuation_and_hyphenated_words_keep_qwen_prosody(self):
self.assertEqual(prepare_for_speech('Docker-Container – wirklich? Ja!'),'Docker-Container – wirklich? Ja!')
with self.assertRaises(ValueError):spoken_chunks(dict(text='😊',language='German'))
if __name__=='__main__':unittest.main()