LuaNovaV1 / code /tests /test_text.py
oProfM's picture
Lua Nova V1 trained on oProfM/LuaNovaDataset: weights, ONNX, eval, code
d4101a0 verified
Raw History Blame Contribute Delete
2.28 kB
"""Tests for PT-BR text normalization, segmentation, and phonetic tokenization."""
from __future__ import annotations
import shutil
import unittest
from lua_nova_v1.text import normalize_ptbr_text, phonemize
from lua_nova_v1.text.phonemizer import SYMBOL_TO_ID, chunks, tokenize
from lua_nova_v1.utils.audio import finished, sentences, shorter
class TextPipelineTests(unittest.TestCase):
def test_ptbr_normalizer_formats_common_expressions(self) -> None:
raw = "Dia 22/03 às 14h30 custou R$ 45,90."
norm = normalize_ptbr_text(raw)
self.assertIn("vinte e dois de março", norm)
self.assertIn("quatorze horas e trinta minutos", norm)
self.assertIn("quarenta e cinco reais e noventa centavos", norm)
def test_segmentation_handles_abbreviations_and_clauses(self) -> None:
text = "Dr. Silva chegou cedo, tomou café; depois saiu."
sents = sentences(text)
self.assertEqual(len(sents), 1)
self.assertEqual(finished("Olá"), "Olá.")
self.assertEqual(finished("Tudo bem?"), "Tudo bem?")
parts = shorter("um, dois, três")
self.assertEqual(len(parts), 3)
def test_pure_tokenizer_maps_specials_and_stress(self) -> None:
ph = tokenize([(False, [[("k", 0), ("a", 1), ("z", 0), ("ɐ", 0)]]), (True, "?")])
self.assertEqual(ph.symbols[0], "<bos>")
self.assertEqual(ph.symbols[-1], "<eos>")
self.assertIn("?", ph.symbols)
self.assertEqual(ph.ids[0], SYMBOL_TO_ID["<bos>"])
self.assertIn(1, ph.stress)
self.assertEqual(ph.unknown, ())
def test_chunks_preserve_abbreviations_and_decimals(self) -> None:
out = list(chunks("Dr. Silva pagou 3.14 reais, e saiu!"))
text_pieces = [val for is_p, val in out if not is_p]
self.assertIn("Dr. Silva pagou 3.14 reais", text_pieces)
@unittest.skipUnless(shutil.which("espeak-ng") is not None, "espeak-ng binary not on PATH")
def test_espeak_phonemizer_smoke(self) -> None:
ph = phonemize("Olá, mundo!")
self.assertGreater(len(ph), 4)
self.assertEqual(ph.symbols[0], "<bos>")
self.assertEqual(ph.symbols[-1], "<eos>")
if __name__ == "__main__":
unittest.main()