#!/usr/bin/env python3
"""Stop Slop dictation cleaner: removes spoken filler, nothing else.

Usage:
  python3 filler_clean.py input.txt > cleaned.txt
  python3 filler_clean.py input.txt -o cleaned.txt --report
  cat input.txt | python3 filler_clean.py -

What it does (and only this):
  1. Deletes filler: "like," "you know," "I mean," "bro," "LOL" (with their commas).
  2. Swaps swear words for mild ones (optional table below; --keep-swears to skip).
  3. Fixes the capital letter at the start of a sentence after a filler is removed.
It never rewords, reorders, merges, or "polishes" a sentence. That is the point:
your words, in your order, stay yours. Standard library only; Python 3.8+.
"""
import argparse, re, sys

# Filler phrases. Only comma-bounded "like," is removed, so "I like pie" is kept.
FILLERS = [
    r"you know,\s*like,", r"like,\s*you know,", r"you know,", r"I mean,", r"like,", r"bro,",
    r"\bLOL\b[.!]*", r"\blol\b[.!]*",
]
# Mild swaps. Edit to taste. Whole words only, case-insensitive.
SWEARS = {
    "motherfucker": "my friend", "motherfuckers": "my friends",
    "fucking": "freaking", "fuck": "forget", "shit": "stuff", "damn": "darn",
}

def clean(text, keep_swears=False):
    counts = {}
    for pat in FILLERS:
        rx = re.compile(r"(?<![\w'])" + pat + r"\s*", re.I)
        text, n = rx.subn("", text)
        if n:
            counts[pat] = counts.get(pat, 0) + n
    if not keep_swears:
        for bad, good in SWEARS.items():
            rx = re.compile(r"\b" + bad + r"\b", re.I)
            text, n = rx.subn(good, text)
            if n:
                counts[bad] = n
    # tidy spacing left behind
    text = re.sub(r"[ \t]{2,}", " ", text)
    text = re.sub(r"\s+([,.!?;:])", r"\1", text)
    text = re.sub(r",\s*([.!?])", r"\1", text)
    text = re.sub(r"^[ \t]+", "", text, flags=re.M)
    # capital at sentence start and line start
    text = re.sub(r"(^|[.!?]\s+)([a-z])", lambda m: m.group(1) + m.group(2).upper(), text, flags=re.M)
    return text.strip() + "\n", counts

def main():
    ap = argparse.ArgumentParser(description="Remove spoken filler from a dictation, keep your wording.")
    ap.add_argument("input", help="text file, or - for stdin")
    ap.add_argument("-o", "--output", help="write here instead of stdout")
    ap.add_argument("--keep-swears", action="store_true", help="do not swap swear words")
    ap.add_argument("--report", action="store_true", help="print what was removed to stderr")
    a = ap.parse_args()
    src = sys.stdin.read() if a.input == "-" else open(a.input, encoding="utf-8").read()
    out, counts = clean(src, a.keep_swears)
    if a.output:
        open(a.output, "w", encoding="utf-8").write(out)
    else:
        sys.stdout.write(out)
    if a.report:
        w0, w1 = len(src.split()), len(out.split())
        print(f"words: {w0} -> {w1}", file=sys.stderr)
        for k, v in counts.items():
            print(f"  {v}x {k}", file=sys.stderr)

if __name__ == "__main__":
    main()
