File size: 11,975 Bytes
bf38a2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env python3
"""Parse ~2,000 Audrey Tang transcript markdown files into structured JSONL.

Reads from: /Users/au/w/transcript/*.md
Writes to:  dataset/data/turns.jsonl  (one JSON object per speaker turn)
            dataset/data/metadata.json (statistics)
"""

from __future__ import annotations

import json
import glob
import html
import os
import re
import sys
from collections import Counter
from pathlib import Path
from typing import Optional

TRANSCRIPT_DIR = "/Users/au/w/transcript"
OUTPUT_DIR = Path(__file__).resolve().parent.parent / "data"

SKIP_FILES = {"lexicon.md", "z.md"}

# Audrey's names across languages
AUDREY_NAMES = {"audrey tang", "唐鳳"}

# Regex patterns
HEADER_RE = re.compile(r"^#\s+(\d{4}-\d{2}-\d{2})\s+(.+)$")
HEADER_YEAR_ONLY_RE = re.compile(r"^#\s+(\d{4})年(.+)$")
SPEAKER_RE = re.compile(r"^###\s+(.+?)\s*[::]\s*$")
STAGE_DIR_RE = re.compile(r"^>\s*(.+)$")
HTML_TAG_RE = re.compile(r"<[^>]+>")
IFRAME_BLOCK_RE = re.compile(r"<iframe[\s\S]*?</iframe>", re.IGNORECASE)


def is_cjk(char: str) -> bool:
    cp = ord(char)
    return 0x4E00 <= cp <= 0x9FFF


def detect_language(text: str) -> str:
    """Return 'zh' if >30% CJK characters, else 'en'."""
    chars = [c for c in text if not c.isspace()]
    if not chars:
        return "en"
    cjk_count = sum(1 for c in chars if is_cjk(c))
    return "zh" if cjk_count / len(chars) > 0.30 else "en"


def normalize_speaker(name: str) -> str:
    """Normalize speaker name: strip extra whitespace."""
    return " ".join(name.split())


def is_audrey(speaker: str) -> bool:
    return speaker.lower().strip() in AUDREY_NAMES


def extract_stage_directions(text: str) -> list[str]:
    """Extract parenthetical stage directions like (laughter), (笑)from text."""
    directions = []
    # Match both ASCII and fullwidth parens
    for m in re.finditer(r"[((]([^))]+)[))]", text):
        directions.append(f"({m.group(1)})")
    return directions


def clean_text(text: str) -> str:
    """Clean turn text: decode HTML entities, strip HTML tags, normalize whitespace."""
    text = html.unescape(text)
    text = IFRAME_BLOCK_RE.sub("", text)
    text = HTML_TAG_RE.sub("", text)
    # Remove markdown image/link artifacts that are just URLs
    # Keep link text but remove URL: [text](url) -> text
    text = re.sub(r"\[([^\]]*)\]\([^)]+\)", r"\1", text)
    # Normalize whitespace within paragraphs but preserve paragraph breaks
    lines = text.split("\n")
    cleaned_lines = []
    for line in lines:
        stripped = line.strip()
        if stripped:
            cleaned_lines.append(stripped)
        else:
            cleaned_lines.append("")
    text = "\n".join(cleaned_lines)
    # Collapse multiple blank lines into one
    text = re.sub(r"\n{3,}", "\n\n", text)
    return text.strip()


def parse_file(filepath: str) -> dict | None:
    """Parse a single transcript .md file.

    Returns dict with keys: date, title, source_file, turns, language, skip_reason
    or None if the file should be skipped.
    """
    filename = os.path.basename(filepath)

    if filename in SKIP_FILES:
        return {"skip_reason": f"excluded file: {filename}", "source_file": filename}

    with open(filepath, "r", encoding="utf-8") as f:
        content = f.read()

    if not content.strip():
        return {"skip_reason": "empty file", "source_file": filename}

    lines = content.split("\n")

    # Parse H1 header
    date = None
    title = None
    header_line_idx = None

    for i, line in enumerate(lines):
        stripped = line.strip()
        if not stripped:
            continue
        m = HEADER_RE.match(stripped)
        if m:
            date = m.group(1)
            title = m.group(2).strip()
            header_line_idx = i
            break
        m2 = HEADER_YEAR_ONLY_RE.match(stripped)
        if m2:
            # e.g. "# 1999年全國司法改革會議" - has year but no full date
            date = None
            title = stripped.lstrip("# ").strip()
            header_line_idx = i
            break
        # If first non-empty line is not a header, check if it's a speaker line
        if stripped.startswith("###"):
            break  # No H1 header
        if not stripped.startswith("#"):
            break  # Not a header line
        break

    # Try to extract date from filename if not from header
    if date is None:
        fm = re.match(r"(\d{4}-\d{2}-\d{2})", filename)
        if fm:
            date = fm.group(1)
            if title is None:
                # Derive title from filename
                title_part = filename[11:]  # Skip "YYYY-MM-DD-"
                title_part = title_part.rsplit(".md", 1)[0]
                title = title_part.replace("-", " ")

    if date is None:
        return {"skip_reason": "no date found in header or filename", "source_file": filename}

    # Parse content into turns
    body_start = (header_line_idx + 1) if header_line_idx is not None else 0
    body_lines = lines[body_start:]

    turns = []
    current_speaker = None
    current_paragraphs = []
    current_stage_dirs = []
    has_speaker_blocks = False

    def flush_turn():
        nonlocal current_speaker, current_paragraphs, current_stage_dirs
        if current_speaker and current_paragraphs:
            text = "\n\n".join(current_paragraphs)
            text = clean_text(text)
            if text:
                turns.append({
                    "speaker": current_speaker,
                    "text": text,
                    "stage_directions": current_stage_dirs[:],
                })
        current_paragraphs = []
        current_stage_dirs = []

    for line in body_lines:
        stripped = line.strip()

        # Check for speaker header
        sm = SPEAKER_RE.match(stripped)
        if sm:
            has_speaker_blocks = True
            new_speaker = normalize_speaker(sm.group(1))
            if new_speaker == current_speaker:
                # Same speaker continues - we'll add a paragraph break
                # but keep accumulating under the same turn
                if current_paragraphs:
                    current_paragraphs.append("")  # blank separator
            else:
                flush_turn()
                current_speaker = new_speaker
            continue

        # Check for stage direction (blockquote)
        sd = STAGE_DIR_RE.match(stripped)
        if sd:
            direction_text = sd.group(1).strip()
            # Extract parenthetical directions
            dirs = extract_stage_directions(direction_text)
            if dirs:
                current_stage_dirs.extend(dirs)
            # Don't add blockquote text to the turn content
            continue

        # Regular text line
        if current_speaker:
            if stripped:
                current_paragraphs.append(stripped)
            elif current_paragraphs and current_paragraphs[-1] != "":
                current_paragraphs.append("")  # paragraph break

    flush_turn()

    # Handle files with no speaker blocks - treat as monologue by Audrey Tang
    if not has_speaker_blocks:
        # Check if there's meaningful text content
        text_lines = []
        for line in body_lines:
            stripped = line.strip()
            if not stripped:
                continue
            sd = STAGE_DIR_RE.match(stripped)
            if sd:
                continue
            # Skip lines that are just links/embeds
            if stripped.startswith("http") or stripped.startswith("<"):
                continue
            text_lines.append(stripped)

        if not text_lines:
            return {"skip_reason": "no meaningful content", "source_file": filename}

        full_text = clean_text("\n\n".join(text_lines))
        if not full_text:
            return {"skip_reason": "no meaningful content after cleaning", "source_file": filename}

        # Detect if this is CJK to pick the right name
        lang = detect_language(full_text)
        speaker = "唐鳳" if lang == "zh" else "Audrey Tang"
        turns = [{
            "speaker": speaker,
            "text": full_text,
            "stage_directions": [],
        }]

    if not turns:
        return {"skip_reason": "no turns extracted", "source_file": filename}

    # Detect language from all turn text
    all_text = " ".join(t["text"] for t in turns)
    language = detect_language(all_text)

    return {
        "date": date,
        "title": title,
        "source_file": filename,
        "turns": turns,
        "language": language,
        "skip_reason": None,
    }


def make_turn_id(source_file: str, turn_index: int) -> str:
    """Create a turn ID from source file and index."""
    base = source_file.rsplit(".md", 1)[0]
    return f"{base}/{turn_index:03d}"


def main():
    OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

    md_files = sorted(glob.glob(os.path.join(TRANSCRIPT_DIR, "*.md")))
    print(f"Found {len(md_files)} .md files")

    all_turns = []
    skipped = []
    parsed_count = 0
    speaker_counter = Counter()
    lang_counter = Counter()
    dates = []

    for filepath in md_files:
        result = parse_file(filepath)
        if result is None:
            continue

        if result.get("skip_reason"):
            skipped.append({
                "file": result["source_file"],
                "reason": result["skip_reason"],
            })
            continue

        parsed_count += 1
        turns = result["turns"]
        total_turns = len(turns)
        lang_counter[result["language"]] += 1
        dates.append(result["date"])

        for i, turn in enumerate(turns):
            speaker = turn["speaker"]
            speaker_counter[speaker] += 1
            turn_obj = {
                "id": make_turn_id(result["source_file"], i),
                "date": result["date"],
                "title": result["title"],
                "source_file": result["source_file"],
                "speaker": speaker,
                "text": turn["text"],
                "turn_index": i,
                "is_audrey": is_audrey(speaker),
                "language": result["language"],
                "stage_directions": turn["stage_directions"],
                "total_turns": total_turns,
            }
            all_turns.append(turn_obj)

    # Write turns.jsonl
    turns_path = OUTPUT_DIR / "turns.jsonl"
    with open(turns_path, "w", encoding="utf-8") as f:
        for turn in all_turns:
            f.write(json.dumps(turn, ensure_ascii=False) + "\n")

    # Compute stats
    audrey_turns = sum(1 for t in all_turns if t["is_audrey"])
    sorted_dates = sorted(dates) if dates else []
    top_speakers = speaker_counter.most_common(20)

    metadata = {
        "total_files_found": len(md_files),
        "total_files_parsed": parsed_count,
        "total_files_skipped": len(skipped),
        "total_turns": len(all_turns),
        "total_audrey_turns": audrey_turns,
        "language_distribution": dict(lang_counter),
        "date_range": {
            "earliest": sorted_dates[0] if sorted_dates else None,
            "latest": sorted_dates[-1] if sorted_dates else None,
        },
        "top_speakers": [{"speaker": s, "count": c} for s, c in top_speakers],
        "skipped_files": skipped,
    }

    meta_path = OUTPUT_DIR / "metadata.json"
    with open(meta_path, "w", encoding="utf-8") as f:
        json.dump(metadata, f, ensure_ascii=False, indent=2)

    # Print summary
    print(f"\nParsed {parsed_count} files, skipped {len(skipped)}")
    print(f"Total turns: {len(all_turns)}")
    print(f"Audrey turns: {audrey_turns}")
    print(f"Language distribution: {dict(lang_counter)}")
    if sorted_dates:
        print(f"Date range: {sorted_dates[0]} to {sorted_dates[-1]}")
    print(f"Top 10 speakers:")
    for speaker, count in top_speakers[:10]:
        print(f"  {speaker}: {count}")
    print(f"\nOutput: {turns_path}")
    print(f"Metadata: {meta_path}")


if __name__ == "__main__":
    main()