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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Extract cognate pairs from the ACD (Austronesian Comparative Dictionary) CLDF dataset.
|
| 3 |
+
|
| 4 |
+
Source: https://github.com/lexibank/acd (CC BY 4.0)
|
| 5 |
+
Citation: Blust, Trussel & Smith (2023), DOI: 10.5281/zenodo.7737547
|
| 6 |
+
Data files: data/training/raw/acd_cldf/{forms,languages,cognatesets}.csv
|
| 7 |
+
— Downloaded by scripts/ingest_acd.py via urllib from GitHub raw content
|
| 8 |
+
|
| 9 |
+
The ACD provides expert cognacy assignments for 146K+ Austronesian forms.
|
| 10 |
+
This script reads the Cognacy column from forms.csv and generates
|
| 11 |
+
cross-language pairwise cognate pairs within each cognacy group.
|
| 12 |
+
|
| 13 |
+
IPA handling:
|
| 14 |
+
- ACD provides NO IPA/Segments data. Forms are in Blust notation
|
| 15 |
+
(proto-forms) or orthographic form (modern languages).
|
| 16 |
+
- Proto-forms: converted via Blust (2009) notation → IPA mapping.
|
| 17 |
+
- Modern forms: lowercased orthography used as pseudo-IPA.
|
| 18 |
+
- Score = -1 sentinel for ALL pairs (no reliable IPA for SCA computation).
|
| 19 |
+
|
| 20 |
+
Iron Rule: All data read from external CSV files. No hardcoded word lists.
|
| 21 |
+
|
| 22 |
+
Output: staging/cognate_pairs/acd_cognate_pairs.tsv (14-column schema)
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
from __future__ import annotations
|
| 26 |
+
|
| 27 |
+
import csv
|
| 28 |
+
import io
|
| 29 |
+
import re
|
| 30 |
+
import sys
|
| 31 |
+
from collections import defaultdict
|
| 32 |
+
from itertools import combinations
|
| 33 |
+
from pathlib import Path
|
| 34 |
+
|
| 35 |
+
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
|
| 36 |
+
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding="utf-8")
|
| 37 |
+
|
| 38 |
+
ROOT = Path(__file__).resolve().parent.parent
|
| 39 |
+
ACD_DIR = ROOT / "data" / "training" / "raw" / "acd_cldf"
|
| 40 |
+
STAGING_DIR = ROOT / "staging" / "cognate_pairs"
|
| 41 |
+
STAGING_DIR.mkdir(parents=True, exist_ok=True)
|
| 42 |
+
|
| 43 |
+
HEADER = (
|
| 44 |
+
"Lang_A\tWord_A\tIPA_A\tLang_B\tWord_B\tIPA_B\tConcept_ID\t"
|
| 45 |
+
"Relationship\tScore\tSource\tRelation_Detail\tDonor_Language\t"
|
| 46 |
+
"Confidence\tSource_Record_ID\n"
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Blust notation → IPA mapping
|
| 50 |
+
# Reference: Blust (2009) "The Austronesian Languages", Chapter 2
|
| 51 |
+
BLUST_TO_IPA = {
|
| 52 |
+
"C": "ts", "N": "ŋ", "R": "ʀ", "S": "s", "Z": "z",
|
| 53 |
+
"H": "h", "L": "ɬ", "T": "t", "D": "d",
|
| 54 |
+
"ng": "ŋ", "ny": "ɲ", "nj": "ɲ",
|
| 55 |
+
"q": "ʔ", "e": "ə",
|
| 56 |
+
"₁": "", "₂": "", "₃": "", "₄": "", "₅": "",
|
| 57 |
+
"₆": "", "₇": "", "₈": "", "₉": "", "₀": "",
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def blust_to_ipa(form: str) -> str:
|
| 62 |
+
"""Convert Blust notation to approximate IPA.
|
| 63 |
+
|
| 64 |
+
Reference: Blust (2009) The Austronesian Languages, Chapter 2.
|
| 65 |
+
"""
|
| 66 |
+
form = form.lstrip("*")
|
| 67 |
+
form = re.sub(r"\([^)]+\)", "", form)
|
| 68 |
+
keys = sorted(BLUST_TO_IPA.keys(), key=len, reverse=True)
|
| 69 |
+
result = []
|
| 70 |
+
i = 0
|
| 71 |
+
while i < len(form):
|
| 72 |
+
matched = False
|
| 73 |
+
for key in keys:
|
| 74 |
+
if form[i:i + len(key)] == key:
|
| 75 |
+
result.append(BLUST_TO_IPA[key])
|
| 76 |
+
i += len(key)
|
| 77 |
+
matched = True
|
| 78 |
+
break
|
| 79 |
+
if not matched:
|
| 80 |
+
if form[i] not in "- ":
|
| 81 |
+
result.append(form[i])
|
| 82 |
+
i += 1
|
| 83 |
+
return "".join(result)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def clean_form(value: str) -> str:
|
| 87 |
+
"""Clean a form value for use as Word column."""
|
| 88 |
+
if not value:
|
| 89 |
+
return "-"
|
| 90 |
+
result = re.sub(r"\([^)]*\)", "", value)
|
| 91 |
+
result = re.sub(r"\[[^\]]*\]", "", result)
|
| 92 |
+
result = re.sub(r"^\*+", "", result)
|
| 93 |
+
result = result.strip().strip("-").strip()
|
| 94 |
+
return result if result else "-"
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def form_to_ipa(value: str, is_proto: bool) -> str:
|
| 98 |
+
"""Convert form value to IPA representation.
|
| 99 |
+
|
| 100 |
+
Proto-forms: Blust notation → IPA via cited mapping.
|
| 101 |
+
Modern forms: lowercased cleaned form (pseudo-IPA).
|
| 102 |
+
"""
|
| 103 |
+
cleaned = clean_form(value)
|
| 104 |
+
if cleaned == "-":
|
| 105 |
+
return "-"
|
| 106 |
+
if is_proto:
|
| 107 |
+
return blust_to_ipa(value)
|
| 108 |
+
return cleaned.lower()
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def main():
|
| 112 |
+
print("=" * 60)
|
| 113 |
+
print("ACD Cognate Extraction")
|
| 114 |
+
print("=" * 60)
|
| 115 |
+
print(f" Source: {ACD_DIR}")
|
| 116 |
+
|
| 117 |
+
# Step 1: Load languages (for ISO codes and proto status)
|
| 118 |
+
lang_map: dict[str, dict] = {}
|
| 119 |
+
lang_path = ACD_DIR / "languages.csv"
|
| 120 |
+
with open(lang_path, "r", encoding="utf-8") as f:
|
| 121 |
+
for row in csv.DictReader(f):
|
| 122 |
+
lid = row["ID"]
|
| 123 |
+
iso = row.get("ISO639P3code", "").strip()
|
| 124 |
+
name = row.get("Name", "")
|
| 125 |
+
is_proto = row.get("Is_Proto", "false").lower() == "true"
|
| 126 |
+
lang_map[lid] = {
|
| 127 |
+
"iso": iso if iso else "-",
|
| 128 |
+
"name": name,
|
| 129 |
+
"is_proto": is_proto,
|
| 130 |
+
}
|
| 131 |
+
print(f" Languages loaded: {len(lang_map)}")
|
| 132 |
+
proto_count = sum(1 for v in lang_map.values() if v["is_proto"])
|
| 133 |
+
modern_iso = sum(1 for v in lang_map.values()
|
| 134 |
+
if not v["is_proto"] and v["iso"] != "-")
|
| 135 |
+
print(f" Proto-languages: {proto_count}")
|
| 136 |
+
print(f" Modern with ISO: {modern_iso}")
|
| 137 |
+
|
| 138 |
+
# Step 2: Load forms grouped by cognacy
|
| 139 |
+
cognacy_groups: dict[str, list[dict]] = defaultdict(list)
|
| 140 |
+
forms_path = ACD_DIR / "forms.csv"
|
| 141 |
+
total_forms = 0
|
| 142 |
+
skipped_no_cognacy = 0
|
| 143 |
+
skipped_no_lang = 0
|
| 144 |
+
loan_flagged_forms = 0
|
| 145 |
+
|
| 146 |
+
with open(forms_path, "r", encoding="utf-8") as f:
|
| 147 |
+
for row in csv.DictReader(f):
|
| 148 |
+
total_forms += 1
|
| 149 |
+
cognacy = row.get("Cognacy", "").strip()
|
| 150 |
+
if not cognacy:
|
| 151 |
+
skipped_no_cognacy += 1
|
| 152 |
+
continue
|
| 153 |
+
|
| 154 |
+
lang_id = row.get("Language_ID", "").strip()
|
| 155 |
+
if lang_id not in lang_map:
|
| 156 |
+
skipped_no_lang += 1
|
| 157 |
+
continue
|
| 158 |
+
|
| 159 |
+
lang_info = lang_map[lang_id]
|
| 160 |
+
iso = lang_info["iso"]
|
| 161 |
+
is_proto = lang_info["is_proto"]
|
| 162 |
+
|
| 163 |
+
# Skip proto-languages (no ISO code, not real attestations)
|
| 164 |
+
if is_proto:
|
| 165 |
+
continue
|
| 166 |
+
|
| 167 |
+
# Skip languages without ISO codes (unidentifiable)
|
| 168 |
+
if iso == "-":
|
| 169 |
+
continue
|
| 170 |
+
|
| 171 |
+
value = row.get("Value", "").strip()
|
| 172 |
+
if not value:
|
| 173 |
+
continue
|
| 174 |
+
|
| 175 |
+
loan = row.get("Loan", "").strip()
|
| 176 |
+
doubt = row.get("Doubt", "").strip()
|
| 177 |
+
is_loan = loan.lower() == "true" if loan else False
|
| 178 |
+
is_doubtful = doubt.lower() == "true" if doubt else False
|
| 179 |
+
|
| 180 |
+
if is_loan:
|
| 181 |
+
loan_flagged_forms += 1
|
| 182 |
+
|
| 183 |
+
# Concept from Description or Parameter_ID
|
| 184 |
+
description = row.get("Description", "").strip()
|
| 185 |
+
param_id = row.get("Parameter_ID", "").strip()
|
| 186 |
+
concept = description if description else param_id
|
| 187 |
+
# Normalize concept: lowercase, replace spaces with underscore
|
| 188 |
+
if concept:
|
| 189 |
+
concept = concept.lower().replace(" ", "_")
|
| 190 |
+
else:
|
| 191 |
+
concept = "-"
|
| 192 |
+
|
| 193 |
+
word = clean_form(value)
|
| 194 |
+
ipa = form_to_ipa(value, is_proto=False)
|
| 195 |
+
|
| 196 |
+
cognacy_groups[cognacy].append({
|
| 197 |
+
"iso": iso,
|
| 198 |
+
"word": word,
|
| 199 |
+
"ipa": ipa,
|
| 200 |
+
"concept": concept,
|
| 201 |
+
"is_loan": is_loan,
|
| 202 |
+
"is_doubtful": is_doubtful,
|
| 203 |
+
"form_id": row.get("ID", ""),
|
| 204 |
+
})
|
| 205 |
+
|
| 206 |
+
print(f"\n Total forms: {total_forms:,}")
|
| 207 |
+
print(f" Skipped (no cognacy): {skipped_no_cognacy:,}")
|
| 208 |
+
print(f" Skipped (unknown language): {skipped_no_lang:,}")
|
| 209 |
+
print(f" Loan-flagged forms: {loan_flagged_forms:,}")
|
| 210 |
+
print(f" Cognacy groups with modern forms: {len(cognacy_groups):,}")
|
| 211 |
+
|
| 212 |
+
# Step 3: Deduplicate members within each cognacy group by (iso, word)
|
| 213 |
+
dedup_removed = 0
|
| 214 |
+
for cog, members in cognacy_groups.items():
|
| 215 |
+
seen: set[str] = set()
|
| 216 |
+
unique: list[dict] = []
|
| 217 |
+
for m in members:
|
| 218 |
+
key = f"{m['iso']}|{m['word']}"
|
| 219 |
+
if key not in seen:
|
| 220 |
+
seen.add(key)
|
| 221 |
+
unique.append(m)
|
| 222 |
+
else:
|
| 223 |
+
dedup_removed += 1
|
| 224 |
+
cognacy_groups[cog] = unique
|
| 225 |
+
print(f" Dedup removed: {dedup_removed:,}")
|
| 226 |
+
|
| 227 |
+
# Step 4: Generate cross-language pairs within each cognacy group
|
| 228 |
+
output_path = STAGING_DIR / "acd_cognate_pairs.tsv"
|
| 229 |
+
total_pairs = 0
|
| 230 |
+
loan_flagged_pairs = 0
|
| 231 |
+
doubtful_pairs = 0
|
| 232 |
+
|
| 233 |
+
with open(output_path, "w", encoding="utf-8") as f:
|
| 234 |
+
f.write(HEADER)
|
| 235 |
+
|
| 236 |
+
for cognacy_id, members in sorted(cognacy_groups.items()):
|
| 237 |
+
if len(members) < 2:
|
| 238 |
+
continue
|
| 239 |
+
|
| 240 |
+
for a, b in combinations(members, 2):
|
| 241 |
+
# Skip same-language pairs
|
| 242 |
+
if a["iso"] == b["iso"]:
|
| 243 |
+
continue
|
| 244 |
+
|
| 245 |
+
# Determine relation detail and confidence
|
| 246 |
+
if a["is_loan"] or b["is_loan"]:
|
| 247 |
+
relation_detail = "loan_flagged"
|
| 248 |
+
loan_flagged_pairs += 1
|
| 249 |
+
else:
|
| 250 |
+
relation_detail = "inherited"
|
| 251 |
+
|
| 252 |
+
if a["is_doubtful"] or b["is_doubtful"]:
|
| 253 |
+
confidence = "doubtful"
|
| 254 |
+
doubtful_pairs += 1
|
| 255 |
+
else:
|
| 256 |
+
confidence = "certain"
|
| 257 |
+
|
| 258 |
+
# Use the concept from form A (both should share concept
|
| 259 |
+
# within cognacy group, but take A's)
|
| 260 |
+
concept = a["concept"] if a["concept"] != "-" else b["concept"]
|
| 261 |
+
|
| 262 |
+
# Score = -1: ACD has no IPA, all forms are pseudo-IPA
|
| 263 |
+
score = -1
|
| 264 |
+
|
| 265 |
+
# Source_Record_ID: cognacy group from ACD forms.csv
|
| 266 |
+
source_record_id = f"acd_{cognacy_id}"
|
| 267 |
+
|
| 268 |
+
row = (
|
| 269 |
+
f"{a['iso']}\t{a['word']}\t{a['ipa']}\t"
|
| 270 |
+
f"{b['iso']}\t{b['word']}\t{b['ipa']}\t"
|
| 271 |
+
f"{concept}\texpert_cognate\t{score}\tacd\t"
|
| 272 |
+
f"{relation_detail}\t-\t{confidence}\t{source_record_id}\n"
|
| 273 |
+
)
|
| 274 |
+
f.write(row)
|
| 275 |
+
total_pairs += 1
|
| 276 |
+
|
| 277 |
+
if total_pairs % 500000 == 0:
|
| 278 |
+
print(f" ... {total_pairs:,} pairs written")
|
| 279 |
+
|
| 280 |
+
print(f"\n Total pairs: {total_pairs:,}")
|
| 281 |
+
print(f" Loan-flagged pairs: {loan_flagged_pairs:,}")
|
| 282 |
+
print(f" Doubtful pairs: {doubtful_pairs:,}")
|
| 283 |
+
print(f" Output: {output_path}")
|
| 284 |
+
print("=" * 60)
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
if __name__ == "__main__":
|
| 288 |
+
main()
|