File size: 10,112 Bytes
98e5288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Ingest Proto-Austronesian reconstructed forms from the ACD CLDF dataset.

Source: Austronesian Comparative Dictionary (ACD) — CLDF on GitHub
URL: https://github.com/lexibank/acd
License: CC BY 4.0
Citation: Blust, Trussel & Smith (2023), DOI: 10.5281/zenodo.7737547

The CLDF forms.csv contains reconstructed forms for 42 proto-languages.
Forms use Blust notation (not IPA) — requires transliteration.

Iron Rule: Data comes from downloaded CSV files. No hardcoded word lists.

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

from __future__ import annotations

import argparse
import csv
import io
import json
import logging
import re
import sys
import unicodedata
from pathlib import Path

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

logger = logging.getLogger(__name__)

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

ACD_DIR = RAW_DIR / "acd_cldf"
ACD_BASE = "https://raw.githubusercontent.com/lexibank/acd/main/cldf/"

# Blust notation → IPA mapping
# Reference: Blust (2009) The Austronesian Languages, Chapter 2
BLUST_TO_IPA = {
    # Capital letters = special proto-phonemes
    "C": "ts",      # *C — voiceless dental/alveolar affricate
    "N": "ŋ",       # *N — velar nasal (sometimes ñ)
    "R": "ʀ",       # *R — uvular trill or retroflex
    "S": "s",       # *S — voiceless sibilant
    "Z": "z",       # *Z — voiced sibilant
    "H": "h",       # *H — laryngeal
    "L": "ɬ",       # *L — lateral fricative
    "T": "t",       # *T — voiceless dental stop
    "D": "d",       # *D — voiced dental stop
    # Digraphs
    "ng": "ŋ",
    "ny": "ɲ",
    "nj": "ɲ",
    # Glottal
    "q": "ʔ",
    # Vowels with special values
    "e": "ə",       # Blust *e = schwa in PAN
    # Subscript digits (used for homonyms) — remove
    "₁": "", "₂": "", "₃": "", "₄": "", "₅": "",
    "₆": "", "₇": "", "₈": "", "₉": "", "₀": "",
}


def blust_to_ipa(form: str) -> str:
    """Convert Blust notation to approximate IPA."""
    # Remove reconstruction asterisk
    form = form.lstrip("*")
    # Remove parenthetical optional segments
    form = re.sub(r"\([^)]+\)", "", form)

    # Greedy longest-match transliteration
    keys = sorted(BLUST_TO_IPA.keys(), key=len, reverse=True)
    result = []
    i = 0
    while i < len(form):
        matched = False
        for key in keys:
            if form[i:i + len(key)] == key:
                result.append(BLUST_TO_IPA[key])
                i += len(key)
                matched = True
                break
        if not matched:
            if form[i] not in "- ":  # skip hyphens and spaces
                result.append(form[i])
            i += 1
    return "".join(result)


def download_if_needed():
    """Download ACD CLDF files if not cached."""
    import urllib.request

    ACD_DIR.mkdir(parents=True, exist_ok=True)
    for fname in ("forms.csv", "languages.csv", "cognatesets.csv"):
        local = ACD_DIR / fname
        if local.exists():
            logger.info("Cached: %s (%d bytes)", fname, local.stat().st_size)
            continue
        url = ACD_BASE + fname
        logger.info("Downloading %s ...", url)
        req = urllib.request.Request(url, headers={
            "User-Agent": "PhaiPhon/1.0 (ancient-scripts-datasets)"
        })
        with urllib.request.urlopen(req, timeout=120) as resp:
            data = resp.read()
        with open(local, "wb") as f:
            f.write(data)
        logger.info("Downloaded %s (%d bytes)", fname, len(data))


def load_proto_languages():
    """Load language metadata to identify proto-languages."""
    lang_path = ACD_DIR / "languages.csv"
    protos = {}
    with open(lang_path, "r", encoding="utf-8") as f:
        for row in csv.DictReader(f):
            name = row.get("Name", "")
            lid = row.get("ID", "")
            # Proto-languages have names starting with "Proto-"
            if name.startswith("Proto-"):
                protos[lid] = name
    return protos


def extract_proto_forms():
    """Extract reconstructed forms from ACD CLDF."""
    protos = load_proto_languages()
    logger.info("Found %d proto-languages in ACD", len(protos))

    forms_path = ACD_DIR / "forms.csv"
    entries = {}  # (proto_lang, form) -> {gloss, ...}

    with open(forms_path, "r", encoding="utf-8") as f:
        for row in csv.DictReader(f):
            lang_id = row.get("Language_ID", "")
            if lang_id not in protos:
                continue

            form = row.get("Form", "").strip()
            value = row.get("Value", "").strip()
            gloss = row.get("Description", "").strip()

            if not form:
                continue
            # Use Form (cleaned) rather than Value (has optional segments)
            word = form

            # Strip infix angle brackets: C<in>aliS → CinaliS
            word = re.sub(r"<([^>]+)>", r"\1", word)
            # Strip parenthetical optional segments: (q)uNah → uNah
            word = re.sub(r"\([^)]+\)", "", word)
            # Remove leading asterisk
            word = word.lstrip("*")
            # Remove subscript digits (homonym markers)
            word = re.sub(r"[₀₁₂₃₄₅₆₇₈₉]", "", word)
            # Remove tilde variants: keep only first form
            if " ~ " in word:
                word = word.split(" ~ ")[0]
            # Remove hyphens (prefix/suffix markers)
            word = word.strip("-").strip()

            # NFC normalize
            word = unicodedata.normalize("NFC", word)

            key = (lang_id, word)
            if key not in entries:
                entries[key] = {
                    "word": word,
                    "gloss": gloss,
                    "proto_lang": protos[lang_id],
                    "proto_lang_id": lang_id,
                }

    return entries


def load_existing_words(tsv_path: Path) -> set[str]:
    """Load existing Word column values."""
    existing = set()
    if tsv_path.exists():
        with open(tsv_path, "r", encoding="utf-8") as f:
            for line in f:
                if line.startswith("Word\t"):
                    continue
                word = line.split("\t")[0]
                existing.add(word)
    return existing


def main():
    parser = argparse.ArgumentParser(description="Ingest ACD Proto-Austronesian")
    parser.add_argument("--dry-run", action="store_true")
    args = parser.parse_args()

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

    download_if_needed()

    # We ingest all proto-forms into a single map.tsv (Proto-Austronesian family)
    tsv_path = LEXICON_DIR / "map.tsv"
    existing = load_existing_words(tsv_path)
    logger.info("Existing Proto-Austronesian entries: %d", len(existing))

    entries = extract_proto_forms()
    logger.info("ACD proto-forms: %d", len(entries))

    # Count by proto-language
    by_lang = {}
    for (lid, _), info in entries.items():
        name = info["proto_lang"]
        by_lang[name] = by_lang.get(name, 0) + 1
    for name, count in sorted(by_lang.items(), key=lambda x: -x[1])[:10]:
        logger.info("  %s: %d", name, count)

    # Process
    new_entries = []
    audit_trail = []
    skipped = 0

    for (lid, word), info in sorted(entries.items()):
        clean_word = word.strip()
        if not clean_word or len(clean_word) < 2 or len(clean_word) > 50:
            skipped += 1
            continue

        if clean_word in existing:
            skipped += 1
            continue

        # Convert Blust notation to IPA
        ipa = blust_to_ipa(word)
        if not ipa:
            ipa = clean_word

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

        new_entries.append({
            "word": clean_word,
            "ipa": ipa,
            "sca": sca,
        })
        existing.add(clean_word)

        audit_trail.append({
            "word": clean_word,
            "raw_form": word,
            "ipa": ipa,
            "gloss": info["gloss"],
            "proto_lang": info["proto_lang"],
            "source": "acd",
        })

    logger.info("New: %d, Skipped: %d", len(new_entries), skipped)

    if args.dry_run:
        print(f"\nDRY RUN: ACD Proto-Austronesian Ingestion:")
        print(f"  ACD proto-forms:    {len(entries)}")
        print(f"  Existing:           {len(existing) - len(new_entries)}")
        print(f"  New:                {len(new_entries)}")
        print(f"  Total:              {len(existing)}")
        return

    if new_entries:
        LEXICON_DIR.mkdir(parents=True, exist_ok=True)
        if not tsv_path.exists():
            with open(tsv_path, "w", encoding="utf-8") as f:
                f.write("Word\tIPA\tSCA\tSource\tConcept_ID\tCognate_Set_ID\n")

        with open(tsv_path, "a", encoding="utf-8") as f:
            for e in new_entries:
                f.write(f"{e['word']}\t{e['ipa']}\t{e['sca']}\tacd\t-\t-\n")

    if audit_trail:
        AUDIT_TRAIL_DIR.mkdir(parents=True, exist_ok=True)
        audit_path = AUDIT_TRAIL_DIR / "acd_ingest_map.jsonl"
        with open(audit_path, "w", encoding="utf-8") as f:
            for r in audit_trail:
                f.write(json.dumps(r, ensure_ascii=False) + "\n")

    print(f"\nACD Proto-Austronesian Ingestion:")
    print(f"  ACD proto-forms:    {len(entries)}")
    print(f"  Existing:           {len(existing) - len(new_entries)}")
    print(f"  New:                {len(new_entries)}")
    print(f"  Total:              {len(existing)}")


if __name__ == "__main__":
    main()