Cotton code
69 lines · 2.6 KB · PythonRaw
#!/usr/bin/env python3"""A synthetic meeting's .txt as the transcript the Mac lands.    python3 -I scripts/meetings/synthetic/transcript.py <meeting.txt> [--out <meeting.json>]The .txt is front matter (`# title:`, `# started_at:`, `# speaker S1: name`,`# truth: S2 | words | YYYY-MM-DD`, `# not: words`) and one `S1 [lang]: text`per line. Lines are timed as talk is: 2.2 words a second, half a secondbetween turns. The JSON is { meeting, speakers, lines, truth, not }: themeeting row's fields, the speakers' rows, and the lines in the transcript'sshape (CORE.md), which `meetings-summarise --file` reads."""import argparseimport jsonimport reimport sysLINE = re.compile(r"^(S\d+)(?:\s*\[(\w+)\])?:\s*(.+)$")def parse(text: str) -> dict:    meeting, speakers, lines, truth, nots = {}, [], [], [], []    at = 0    for raw in text.splitlines():        raw = raw.rstrip()        if not raw:            continue        if raw.startswith("#"):            body = raw[1:].strip()            if body.startswith("title:"):                meeting["title"] = body[6:].strip()            elif body.startswith("started_at:"):                meeting["started_at"] = body[11:].strip()            elif body.startswith("speaker "):                label, _, name = body[8:].partition(":")                speakers.append({"label": label.strip(), "name": name.strip() or None})            elif body.startswith("truth:"):                who, words, due = [p.strip() for p in (body[6:] + " | ").split("|")[:3]]                truth.append({"who": None if who == "-" else who, "words": words, "due": due or None})            elif body.startswith("not:"):                nots.append(body[4:].strip())            continue        m = LINE.match(raw)        if not m:            sys.exit(f"not a line: {raw}")        speaker, lang, said = m.group(1), m.group(2) or "en", m.group(3)        ms = int(len(said.split()) / 2.2 * 1000) + 400        lines.append({"speaker": speaker, "start_ms": at, "end_ms": at + ms, "text": said, "language": lang})        at += ms + 500    meeting["duration_ms"] = at    return {"meeting": meeting, "speakers": speakers, "lines": lines, "truth": truth, "not": nots}def main():    ap = argparse.ArgumentParser()    ap.add_argument("txt")    ap.add_argument("--out")    a = ap.parse_args()    with open(a.txt, encoding="utf-8") as fh:        body = json.dumps(parse(fh.read()), ensure_ascii=False, indent=1)    if a.out:        with open(a.out, "w", encoding="utf-8") as fh:            fh.write(body)    else:        print(body)if __name__ == "__main__":    main()