File size: 2,114 Bytes
c1d045b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""Turn a released museum transcript (PDF) into a Q/A transcript CuratorBot can analyse.

    python scripts/import_transcript_pdf.py private/RG-50.165.0093/transcript.pdf \
        -o private/RG-50.165.0093/transcript.txt

Prints the speakers it found and which one it took to be the interviewer.
If it guessed wrong, rerun with --interviewer WH. Check the .txt before analysing:
speaker codes in unusual layouts (several interviewers, OCR slips) may need a
quick manual fix. Released transcripts have no timestamps, so the brief cites
turn numbers instead of times.
"""
from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))

from curatorbot import transcript as tx  # noqa: E402
from curatorbot.importers import front_matter_fields, parse_speaker_transcript, pdf_text  # noqa: E402


def main() -> int:
    p = argparse.ArgumentParser()
    p.add_argument("source", help="Transcript PDF, or a .txt already extracted from one")
    p.add_argument("-o", "--output", required=True, help="Q/A .txt to write (a .json with metadata is written beside it)")
    p.add_argument("--interviewer", help="Speaker code of the interviewer, if the guess is wrong")
    a = p.parse_args()
    src = Path(a.source)
    text = pdf_text(src) if src.suffix.lower() == ".pdf" else src.read_text(encoding="utf-8")
    res = parse_speaker_transcript(text, interviewer=a.interviewer)
    out = Path(a.output)
    out.parent.mkdir(parents=True, exist_ok=True)
    out.write_text(tx.to_qa_text(res["segments"]), encoding="utf-8")
    meta = {"speakers": res["speakers"], "interviewer": res["interviewer"], **front_matter_fields(res["front_matter"])}
    out.with_suffix(".json").write_text(json.dumps({"meta": meta, "segments": res["segments"]}, ensure_ascii=False, indent=1), encoding="utf-8")
    print(f"{len(res['segments'])} turns. Speakers {res['speakers']}; interviewer taken as {res['interviewer']}.")
    print(f"Front matter: {meta}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())