Skip to content

dictate_polish

dictate_polish

Lightweight dictation polish (Diapason-inspired fillers / punctuation).

Functions:

strip_whisper_credits

strip_whisper_credits(text: str) -> str

Raye les génériques hallucinés, puis nettoie la ponctuation orpheline.

Source code in src/diapason/speech/dictate_polish.py
def strip_whisper_credits(text: str) -> str:
    """Raye les génériques hallucinés, puis nettoie la ponctuation orpheline."""
    nettoye = _WHISPER_CREDITS_RE.sub("", text or "")
    nettoye = re.sub(r"\s{2,}", " ", nettoye)
    return nettoye.strip(" \t\n,;")

reparer_homophones_de_commande

reparer_homophones_de_commande(text: str) -> str

Répare les homophones de verbes d'ordre en tête d'énoncé.

Source code in src/diapason/speech/dictate_polish.py
def reparer_homophones_de_commande(text: str) -> str:
    """Répare les homophones de verbes d'ordre en tête d'énoncé."""

    def _verbe(m: "re.Match[str]") -> str:
        verbe = "Mets" if m.group(2)[0].isupper() else "mets"
        return f"{m.group(1)}{verbe}{m.group(3)}"

    return _MAIS_METS_RE.sub(_verbe, text or "")

polish_dictation

polish_dictation(
    raw: str, *, aggressive: bool = True
) -> str

Clean raw STT text: drop fillers, fix spacing, capitalize sentences.

Source code in src/diapason/speech/dictate_polish.py
def polish_dictation(raw: str, *, aggressive: bool = True) -> str:
    """Clean raw STT text: drop fillers, fix spacing, capitalize sentences."""
    text = strip_whisper_credits(raw)
    if not text:
        return ""
    text = reparer_homophones_de_commande(text)

    if aggressive:
        text = _FILLER_RE.sub(" ", text)

    # "readme dot md" → "readme.md"
    text = _SPOKEN_DOT_EXT.sub(lambda m: f"{m.group(1)}.{m.group(2).lower()}", text)

    text = _MULTI_SPACE.sub(" ", text)
    text = _SPACE_BEFORE_PUNCT.sub(r"\1", text)
    text = text.strip(" ,;")

    # Sentence capitalization
    parts: List[str] = re.split(r"([.!?]\s+)", text)
    out: List[str] = []
    capitalize_next = True
    for part in parts:
        if not part:
            continue
        if re.fullmatch(r"[.!?]\s+", part):
            out.append(part)
            capitalize_next = True
            continue
        if capitalize_next and part:
            out.append(part[:1].upper() + part[1:])
            capitalize_next = False
        else:
            out.append(part)

    text = "".join(out).strip()
    if text and text[-1] not in ".!?":
        # Don't force period on short commands
        if len(text.split()) >= 4:
            text += "."
    return text

polish_pipeline

polish_pipeline(
    raw: str,
    *,
    polish: bool = True,
    use_dictionary: bool = True,
    llm_polish: bool = False,
    email_mode: bool = False,
    llm_timeout_ms: int = 2000,
    dictionary_path: Optional[str] = None,
) -> str

Local polish → dictionary → optional LLM. Safe for paste path only.

Source code in src/diapason/speech/dictate_polish.py
def polish_pipeline(
    raw: str,
    *,
    polish: bool = True,
    use_dictionary: bool = True,
    llm_polish: bool = False,
    email_mode: bool = False,
    llm_timeout_ms: int = 2000,
    dictionary_path: Optional[str] = None,
) -> str:
    """Local polish → dictionary → optional LLM. Safe for paste path only."""
    if not polish:
        return (raw or "").strip()

    from diapason.speech.dictation_dictionary import apply_dictionary

    text = polish_dictation(raw)
    if use_dictionary:
        text = apply_dictionary(text, path=dictionary_path or None)
    if llm_polish:
        from diapason.speech.llm_polish import llm_polish_text

        improved = llm_polish_text(
            text, email_mode=email_mode, timeout_ms=llm_timeout_ms
        )
        if improved:
            text = improved
    return text