Skip to content

Evaluation scripts for the Open TTS Leaderboard: https://huggingface.co/spaces/hf-audio/open_tts_leaderboard

Python
2.4 KB
Raw Blame
import re

import num2words
from normalizer import EnglishTextNormalizer, BasicMultilingualTextNormalizer
from normalizer.arabic import fold_arabic
from normalizer.persian import PersianTextNormalizer
from normalizer.hindi import normalize_hi_numbers_after, normalize_hi_numbers_before


# num2words's code where it differs from ours (Norwegian Bokmål is `no` there).
_NUM2WORDS_CODE = {"nb": "no"}


class MultilingualNormalizer(BasicMultilingualTextNormalizer):
    """BasicMultilingualTextNormalizer with optional number normalization.

    Pass lang= to also convert digits to words via num2words.
    """

    def _normalize_numbers(self, text, lang):
        # Join space-separated thousand groups (e.g. "10 000" -> "10000")
        text = re.sub(r"(\d)\s+(\d{3})\b", r"\1\2", text)
        # Convert remaining digit sequences to words
        lang = _NUM2WORDS_CODE.get(lang, lang)
        def _replace(m):
            try:
                return num2words.num2words(int(m.group()), lang=lang)
            except Exception:
                return m.group()
        return re.sub(r"\d+", _replace, text)

    @staticmethod
    def _normalize_vi_numbers(text):
        # Vietnamese conventions, applied before punctuation is stripped: "." groups thousands
        # ("10.000"), "," is the decimal separator ("3,5" -> "3 phẩy 5", leading zeros read as
        # "không"), and "%" is read "phần trăm". Otherwise "3,5%" would become "ba năm".
        text = re.sub(r"(?<=\d)\.(?=\d{3}\b)", "", text)
        def _decimal(m):
            frac = m.group(2)
            zeros = len(frac) - len(frac.lstrip("0"))
            return " ".join([m.group(1), "phẩy"] + ["không"] * zeros + ([frac[zeros:]] if frac[zeros:] else []))
        text = re.sub(r"(\d+),(\d+)", _decimal, text)
        return text.replace("%", " phần trăm")

    def __call__(self, s, lang=None):
        if lang == "fa":
            return persian_normalizer(s)
        if lang == "vi":
            s = self._normalize_vi_numbers(s)
        elif lang == "hi":
            s = normalize_hi_numbers_before(s)
        s = super().__call__(s)
        if lang == "ar":
            s = fold_arabic(s)
        if lang == "hi":
            s = normalize_hi_numbers_after(s)
        elif lang is not None:
            s = self._normalize_numbers(s, lang)
        return s


normalizer = EnglishTextNormalizer()

persian_normalizer = PersianTextNormalizer()

ml_normalizer = MultilingualNormalizer(remove_diacritics=False)