File size: 2,281 Bytes
d4101a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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()