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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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#!/usr/bin/env python3
"""Ingest Ancient South Arabian word data from Wiktionary.

Primary source: DASI (dasi.cnr.it) — blocked by Anubis bot protection.
Fallback source: Wiktionary categories:
  - Old_South_Arabian_lemmas (128 entries, lang code sem-srb)
  - Sabaean_lemmas (32 entries, lang code xsa)

Strategy:
  1. Fetch all page titles from Wiktionary categories
  2. Batch-fetch page wikitext
  3. Extract transliteration from {{head|...|tr=...}} templates
  4. For pages without tr=, convert Musnad script to Latin transliteration
     using the standard Unicode character names
  5. Apply ASA transliteration map for IPA
  6. Write xsa.tsv

Iron Rule: All words come from HTTP API responses. No hardcoded word lists.

Usage:
    python scripts/ingest_dasi.py [--dry-run]
"""

from __future__ import annotations

import argparse
import io
import json
import logging
import re
import sys
import time
import unicodedata
import urllib.error
import urllib.request
from pathlib import Path
from urllib.parse import urlencode

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding="utf-8")

ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT / "cognate_pipeline" / "src"))
sys.path.insert(0, str(ROOT / "scripts"))

from cognate_pipeline.normalise.sound_class import ipa_to_sound_class  # noqa: E402
from transliteration_maps import transliterate  # noqa: E402

logger = logging.getLogger(__name__)

LEXICON_DIR = ROOT / "data" / "training" / "lexicons"
AUDIT_TRAIL_DIR = ROOT / "data" / "training" / "audit_trails"
RAW_DIR = ROOT / "data" / "training" / "raw" / "dasi"

API_URL = "https://en.wiktionary.org/w/api.php"
USER_AGENT = "PhaiPhon/1.0 (ancient-scripts-datasets; academic research)"

# ---- Wiktionary categories to fetch ----
# Old South Arabian (sem-srb) is the umbrella; Sabaean (xsa) is a sub-variety.
# Both are pooled into xsa.tsv since the script/phonology is identical.
CATEGORIES = [
    ("Old_South_Arabian_lemmas", "sem-srb"),
    ("Sabaean_lemmas", "xsa"),
    # Non-lemma forms for additional coverage
    ("Old_South_Arabian_nouns", "sem-srb"),
    ("Old_South_Arabian_verbs", "sem-srb"),
    ("Sabaean_nouns", "xsa"),
    ("Sabaean_verbs", "xsa"),
]

# ---------------------------------------------------------------------------
# Musnad (Old South Arabian) script to Latin transliteration
# Based on Unicode character names (standard Semitist letter names)
# This maps from the Musnad Unicode block (U+10A60-U+10A7C) to the standard
# scholarly transliteration used in ASA studies.
# ---------------------------------------------------------------------------
MUSNAD_TO_LATIN: dict[str, str] = {
    "\U00010A60": "h",    # HE
    "\U00010A61": "l",    # LAMEDH
    "\U00010A62": "\u1e25",  # HETH -> ḥ
    "\U00010A63": "m",    # MEM
    "\U00010A64": "q",    # QOPH
    "\U00010A65": "w",    # WAW
    "\U00010A66": "s\u00b2",  # SHIN -> s² (lateral fricative in ASA)
    "\U00010A67": "r",    # RESH
    "\U00010A68": "b",    # BETH
    "\U00010A69": "t",    # TAW
    "\U00010A6A": "s\u00b9",  # SAT -> s¹ (standard s in ASA)
    "\U00010A6B": "k",    # KAPH
    "\U00010A6C": "n",    # NUN
    "\U00010A6D": "\u1e2b",  # KHETH -> ḫ
    "\U00010A6E": "\u1e63",  # SADHE -> ṣ
    "\U00010A6F": "s",    # SAMEKH -> s (= s¹ in many systems)
    "\U00010A70": "f",    # FE
    "\U00010A71": "\u02be",  # ALEF -> ʾ (glottal stop)
    "\U00010A72": "\u02bf",  # AYN -> ʿ (pharyngeal)
    "\U00010A73": "\u1e93",  # DHADHE -> ẓ (emphatic interdental)
    "\U00010A74": "g",    # GIMEL
    "\U00010A75": "d",    # DALETH
    "\U00010A76": "\u0121",  # GHAYN -> ġ
    "\U00010A77": "\u1e6d",  # TETH -> ṭ (emphatic t)
    "\U00010A78": "z",    # ZAYN
    "\U00010A79": "\u1e0f",  # DHALETH -> ḏ (interdental d)
    "\U00010A7A": "y",    # YODH
    "\U00010A7B": "\u1e6f",  # THAW -> ṯ (interdental t)
    "\U00010A7C": "s\u00b3",  # THETH -> s³ (ts affricate in ASA)
}


def musnad_to_latin(text: str) -> str:
    """Convert Musnad script text to Latin scholarly transliteration.

    Characters not in the Musnad block are passed through.
    Word separators (|) in Musnad are converted to spaces.
    """
    result = []
    for ch in text:
        if ch in MUSNAD_TO_LATIN:
            result.append(MUSNAD_TO_LATIN[ch])
        elif ch == "\U00010A7F":  # NUMERIC INDICATOR — skip
            continue
        elif ch == "\U00010A7D" or ch == "\U00010A7E":  # numbers — skip
            continue
        elif ch == "|" or ch == "\u00b7":  # word separator
            result.append(" ")
        else:
            result.append(ch)
    return "".join(result)


def _api_request(params: dict, retries: int = 3) -> dict:
    """Make a Wiktionary API request with retries."""
    url = f"{API_URL}?" + urlencode(params)
    req = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})

    for attempt in range(retries):
        try:
            with urllib.request.urlopen(req, timeout=30) as resp:
                return json.loads(resp.read().decode("utf-8"))
        except (urllib.error.URLError, urllib.error.HTTPError, OSError) as exc:
            if attempt < retries - 1:
                wait = 2 * (attempt + 1)
                logger.warning("Retry %d/%d after error: %s", attempt + 1, retries, exc)
                time.sleep(wait)
            else:
                logger.error("FAILED after %d retries: %s", retries, exc)
                raise
    return {}  # unreachable


def fetch_category_members(category: str, namespace: int = 0) -> list[str]:
    """Fetch all page titles from a Wiktionary category."""
    members = []
    params = {
        "action": "query",
        "list": "categorymembers",
        "cmtitle": f"Category:{category}",
        "cmtype": "page",
        "cmnamespace": str(namespace),
        "cmlimit": "500",
        "format": "json",
    }

    page_num = 0
    while True:
        page_num += 1
        data = _api_request(params)

        for m in data.get("query", {}).get("categorymembers", []):
            members.append(m["title"])

        cont = data.get("continue", {})
        if "cmcontinue" in cont:
            params["cmcontinue"] = cont["cmcontinue"]
            logger.info("  Page %d: %d members so far...", page_num, len(members))
            time.sleep(0.5)
        else:
            break

    return members


def fetch_page_contents(titles: list[str]) -> dict[str, str]:
    """Batch-fetch wikitext for multiple pages (max 50 per request)."""
    all_contents: dict[str, str] = {}

    for i in range(0, len(titles), 50):
        batch = titles[i:i + 50]
        params = {
            "action": "query",
            "titles": "|".join(batch),
            "prop": "revisions",
            "rvprop": "content",
            "rvslots": "main",
            "format": "json",
        }
        data = _api_request(params)
        pages = data.get("query", {}).get("pages", {})
        for pid, page in pages.items():
            title = page.get("title", "")
            revs = page.get("revisions", [])
            if revs:
                content = revs[0].get("slots", {}).get("main", {}).get("*", "")
                all_contents[title] = content

        if i + 50 < len(titles):
            time.sleep(1.0)  # Be respectful

    return all_contents


def extract_entry_from_wikitext(title: str, wikitext: str) -> list[dict]:
    """Extract transliteration, POS, and gloss from wikitext.

    Returns a list of entry dicts with keys: word, translit, pos, gloss, lang_code.
    A single page can have multiple language sections.
    """
    entries = []

    # Extract all {{head|LANG|POS|tr=TRANSLIT}} patterns
    head_pattern = re.compile(
        r"\{\{head\|([^|}]+)\|([^|}]+)"  # lang, pos
        r"(?:\|[^|}]*)*?"  # optional params
        r"(?:\|tr=([^|}]+))?"  # optional tr=
        r"[^}]*\}\}",
        re.DOTALL,
    )

    # Extract glosses: lines starting with # (not ## which are sub-glosses)
    gloss_pattern = re.compile(r"^#\s+(.+)", re.MULTILINE)

    for match in head_pattern.finditer(wikitext):
        lang_code = match.group(1).strip()
        pos = match.group(2).strip()
        tr = match.group(3).strip() if match.group(3) else None

        # Only accept ASA-related language codes
        if lang_code not in ("sem-srb", "xsa", "inm", "xqt", "xhd"):
            continue

        # Find the gloss nearest after this head template
        after_pos = match.end()
        gloss_match = gloss_pattern.search(wikitext[after_pos:after_pos + 500])
        gloss = ""
        if gloss_match:
            gloss = gloss_match.group(1).strip()
            # Clean wiki markup from gloss
            gloss = re.sub(r"\[\[([^\]|]*\|)?([^\]]*)\]\]", r"\2", gloss)
            gloss = re.sub(r"\{\{[^}]*\}\}", "", gloss)
            gloss = gloss.strip(" ,;.")

        # Determine transliteration
        if tr:
            translit = tr.strip()
        else:
            # Convert Musnad script title to Latin
            translit = musnad_to_latin(title)

        entries.append({
            "musnad": title,
            "word": translit,
            "pos": pos,
            "gloss": gloss,
            "lang_code": lang_code,
        })

    return entries


def is_valid_entry(entry: dict) -> bool:
    """Validate an entry for inclusion."""
    word = entry.get("word", "")
    if not word or len(word) > 60:
        return False
    # Skip single-character entries (just letters)
    if len(word) <= 1:
        return False
    # Skip if the word is entirely digits or punctuation
    if all(c.isdigit() or c in ".-_/|" for c in word):
        return False
    # Skip entries that are just letter names
    pos = entry.get("pos", "")
    if pos == "letter":
        return False
    return True


def main():
    parser = argparse.ArgumentParser(
        description="Ingest Ancient South Arabian from Wiktionary (DASI fallback)"
    )
    parser.add_argument("--dry-run", action="store_true",
                        help="Show what would be extracted without writing files")
    args = parser.parse_args()

    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s %(levelname)s: %(message)s",
        datefmt="%H:%M:%S",
    )

    # Ensure directories exist
    LEXICON_DIR.mkdir(parents=True, exist_ok=True)
    RAW_DIR.mkdir(parents=True, exist_ok=True)
    AUDIT_TRAIL_DIR.mkdir(parents=True, exist_ok=True)

    # ---- Step 1: Collect all unique titles from all categories ----
    logger.info("=" * 60)
    logger.info("Ancient South Arabian ingestion from Wiktionary")
    logger.info("=" * 60)

    all_titles: set[str] = set()
    category_counts: dict[str, int] = {}

    for cat_name, lang_code in CATEGORIES:
        logger.info("Fetching category: %s (%s)", cat_name, lang_code)
        titles = fetch_category_members(cat_name)
        new_count = len(titles - all_titles) if isinstance(titles, set) else len(set(titles) - all_titles)
        all_titles.update(titles)
        category_counts[cat_name] = len(titles)
        logger.info("  %s: %d titles (%d new)", cat_name, len(titles), new_count)
        time.sleep(1.0)

    logger.info("Total unique titles: %d", len(all_titles))

    # ---- Step 2: Fetch page contents ----
    title_list = sorted(all_titles)
    logger.info("Fetching page contents for %d titles...", len(title_list))
    page_contents = fetch_page_contents(title_list)
    logger.info("Got content for %d pages", len(page_contents))

    # Save raw content for audit trail
    raw_path = RAW_DIR / "wiktionary_pages.json"
    with open(raw_path, "w", encoding="utf-8") as f:
        json.dump(page_contents, f, ensure_ascii=False, indent=2)
    logger.info("Saved raw content to %s", raw_path)

    # ---- Step 3: Extract entries ----
    all_entries: list[dict] = []
    seen_words: set[str] = set()
    lang_code_counts: dict[str, int] = {}
    pos_counts: dict[str, int] = {}
    tr_from_template = 0
    tr_from_musnad = 0
    skipped = 0

    for title, wikitext in page_contents.items():
        entries = extract_entry_from_wikitext(title, wikitext)
        if not entries:
            skipped += 1
            continue

        for entry in entries:
            if not is_valid_entry(entry):
                skipped += 1
                continue

            word = entry["word"]
            # Normalize
            word = unicodedata.normalize("NFC", word.strip())

            # Deduplicate by word form
            if word in seen_words:
                continue
            seen_words.add(word)

            # Track source of transliteration
            # If the entry came from a {{head}} with tr=, it was from template
            # Check if the Musnad title and word differ (meaning tr= was used)
            musnad_derived = musnad_to_latin(entry["musnad"])
            if word != musnad_derived:
                tr_from_template += 1
            else:
                tr_from_musnad += 1

            # Apply transliteration map for IPA
            try:
                ipa = transliterate(word, "xsa")
            except Exception:
                ipa = word

            if not ipa:
                ipa = word

            # SCA
            try:
                sca = ipa_to_sound_class(ipa)
            except Exception:
                sca = ""

            # Determine source label
            lc = entry["lang_code"]
            lang_code_counts[lc] = lang_code_counts.get(lc, 0) + 1
            pos = entry["pos"]
            pos_counts[pos] = pos_counts.get(pos, 0) + 1

            source_label = f"wiktionary:{lc}"

            all_entries.append({
                "word": word,
                "ipa": ipa,
                "sca": sca,
                "source": source_label,
                "musnad": entry["musnad"],
                "gloss": entry.get("gloss", ""),
                "pos": pos,
            })

    # Sort by word for reproducibility
    all_entries.sort(key=lambda e: e["word"])

    # ---- Step 4: Report ----
    logger.info("")
    logger.info("=" * 60)
    logger.info("EXTRACTION RESULTS")
    logger.info("=" * 60)
    logger.info("Total entries:       %d", len(all_entries))
    logger.info("Skipped:             %d", skipped)
    logger.info("Transliteration from {{head}} tr=: %d", tr_from_template)
    logger.info("Transliteration from Musnad decode: %d", tr_from_musnad)
    logger.info("")
    logger.info("By language code:")
    for lc, count in sorted(lang_code_counts.items(), key=lambda x: -x[1]):
        name = {
            "sem-srb": "Old South Arabian (general)",
            "xsa": "Sabaean",
            "inm": "Minaic",
            "xqt": "Qatabanic",
            "xhd": "Hadramitic",
        }.get(lc, lc)
        logger.info("  %-35s %d", name, count)
    logger.info("")
    logger.info("By POS:")
    for pos, count in sorted(pos_counts.items(), key=lambda x: -x[1]):
        logger.info("  %-20s %d", pos, count)

    # Identity rate
    identity_count = sum(1 for e in all_entries if e["word"] == e["ipa"])
    identity_pct = (identity_count / len(all_entries) * 100) if all_entries else 0
    logger.info("")
    logger.info("Identity rate (word == IPA): %d/%d (%.1f%%)",
                identity_count, len(all_entries), identity_pct)

    # Sample entries
    logger.info("")
    logger.info("Sample entries:")
    logger.info("  %-20s %-25s %-10s %s", "Word", "IPA", "SCA", "Gloss")
    logger.info("  " + "-" * 70)
    for e in all_entries[:20]:
        logger.info("  %-20s %-25s %-10s %s",
                     e["word"], e["ipa"], e["sca"], e.get("gloss", "")[:30])

    # ---- Step 5: Write output ----
    if args.dry_run:
        logger.info("")
        logger.info("DRY RUN — not writing files")
        return

    # Write TSV
    tsv_path = LEXICON_DIR / "xsa.tsv"
    with open(tsv_path, "w", encoding="utf-8") as f:
        f.write("Word\tIPA\tSCA\tSource\tConcept_ID\tCognate_Set_ID\n")
        for e in all_entries:
            f.write(
                f"{e['word']}\t{e['ipa']}\t{e['sca']}\t"
                f"{e['source']}\t-\t-\n"
            )
    logger.info("Wrote %d entries to %s", len(all_entries), tsv_path)

    # Write audit trail
    audit_path = AUDIT_TRAIL_DIR / "xsa_audit.json"
    audit_data = {
        "source": "Wiktionary (DASI blocked by Anubis)",
        "categories": [c[0] for c in CATEGORIES],
        "date": time.strftime("%Y-%m-%d"),
        "total_entries": len(all_entries),
        "lang_code_counts": lang_code_counts,
        "pos_counts": pos_counts,
        "identity_rate": round(identity_pct, 1),
        "tr_from_template": tr_from_template,
        "tr_from_musnad": tr_from_musnad,
        "entries": [
            {
                "word": e["word"],
                "musnad": e["musnad"],
                "ipa": e["ipa"],
                "gloss": e.get("gloss", ""),
                "pos": e["pos"],
                "source": e["source"],
            }
            for e in all_entries
        ],
    }
    with open(audit_path, "w", encoding="utf-8") as f:
        json.dump(audit_data, f, ensure_ascii=False, indent=2)
    logger.info("Wrote audit trail to %s", audit_path)


if __name__ == "__main__":
    main()