# -*- coding: utf-8 -*- """Pasa los numeros de una transcripcion a palabras, para poder compararla con el guion (que escribe todo en letras).""" import re U = ["cero", "uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", "once", "doce", "trece", "catorce", "quince", "dieciseis", "diecisiete", "dieciocho", "diecinueve", "veinte", "veintiuno", "veintidos", "veintitres", "veinticuatro", "veinticinco", "veintiseis", "veintisiete", "veintiocho", "veintinueve"] D = {3: "treinta", 4: "cuarenta", 5: "cincuenta", 6: "sesenta", 7: "setenta", 8: "ochenta", 9: "noventa"} C = {1: "ciento", 2: "doscientos", 3: "trescientos", 4: "cuatrocientos", 5: "quinientos", 6: "seiscientos", 7: "setecientos", 8: "ochocientos", 9: "novecientos"} def _lt1000(n): if n < 30: return U[n] if n < 100: d, u = divmod(n, 10) return D[d] + (" y " + U[u] if u else "") if n == 100: return "cien" c, r = divmod(n, 100) return C[c] + (" " + _lt1000(r) if r else "") def spell_int(n): if n < 1000: return _lt1000(n) if n < 1000000: m, r = divmod(n, 1000) pre = "mil" if m == 1 else _lt1000(m) + " mil" return pre + (" " + _lt1000(r) if r else "") m, r = divmod(n, 1000000) pre = "un millon" if m == 1 else _lt1000(m) + " millones" return pre + (" " + spell_int(r) if r else "") def _tok(m): s = m.group(0) s = re.sub(r"\.(?=\d{3}\b)", "", s) # 384.000 -> 384000 if "," in s: a, b = s.split(",", 1) ent = spell_int(int(a or 0)) dec = " ".join(U[int(c)] for c in b if c.isdigit()) return f"{ent} coma {dec}" if "." in s: # 0.5 estilo ingles a, b = s.split(".", 1) return f"{spell_int(int(a or 0))} coma " + " ".join(U[int(c)] for c in b if c.isdigit()) return spell_int(int(s)) def expand(text): t = text.replace("%", " por ciento ").replace("ยบ", "") t = re.sub(r"\d+(?:[.,]\d+)*", _tok, t) return re.sub(r"\s+", " ", t).strip()