File size: 10,627 Bytes
26786e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Assign cognate links from expert annotations and automated scoring.

1. Expert cognates from ABVD (Cognacy) and sinotibetan (COGID)
2. Concept-aligned pairs within families scored by Levenshtein
3. WOLD borrowing pairs

Outputs:
  data/training/cognate_pairs/cognate_pairs_inherited.tsv
  data/training/cognate_pairs/cognate_pairs_similarity.tsv
  data/training/cognate_pairs/cognate_pairs_borrowing.tsv
"""

from __future__ import annotations

import csv
import json
import sys
from collections import defaultdict
from pathlib import Path

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

from cognate_pipeline.cognate.baseline_levenshtein import normalised_similarity
from cognate_pipeline.normalise.sound_class import ipa_to_sound_class

LEXICONS_DIR = ROOT / "data" / "training" / "lexicons"
COGNATE_DIR = ROOT / "data" / "training" / "cognate_pairs"
SOURCES_DIR = ROOT / "sources"

FAMILY_MAP_PATH = ROOT / "cognate_pipeline" / "src" / "cognate_pipeline" / "cognate" / "family_map.json"

PAIR_HEADER = "Lang_A\tWord_A\tIPA_A\tLang_B\tWord_B\tIPA_B\tConcept_ID\tRelationship\tScore\tSource\n"


def load_json(path: Path) -> dict:
    if path.exists():
        with open(path, encoding="utf-8") as f:
            return json.load(f)
    return {}


def read_lexicon(path: Path) -> list[dict]:
    """Read a lexicon TSV file."""
    entries = []
    if not path.exists():
        return entries
    with open(path, encoding="utf-8", newline="") as f:
        reader = csv.DictReader(f, delimiter="\t")
        for row in reader:
            entries.append(row)
    return entries


def extract_expert_cognates() -> list[tuple]:
    """Extract expert cognate pairs from lexicon files (ABVD + sinotibetan)."""
    pairs = []

    # Group entries by cognate set ID across all lexicons
    cognate_sets: dict[str, list[tuple[str, str, str, str, str]]] = defaultdict(list)
    # (iso, word, ipa, sca, concept_id)

    for path in sorted(LEXICONS_DIR.glob("*.tsv")):
        iso = path.stem
        for entry in read_lexicon(path):
            cog_id = entry.get("Cognate_Set_ID", "-")
            if cog_id == "-" or not cog_id:
                continue
            source = entry.get("Source", "")
            if source not in ("abvd", "sinotibetan"):
                continue
            cognate_sets[cog_id].append((
                iso,
                entry.get("Word", ""),
                entry.get("IPA", ""),
                entry.get("SCA", ""),
                entry.get("Concept_ID", "-"),
            ))

    # Generate pairs within each cognate set
    for cog_id, members in cognate_sets.items():
        if len(members) < 2:
            continue
        for i in range(len(members)):
            for j in range(i + 1, len(members)):
                a = members[i]
                b = members[j]
                if a[0] == b[0]:  # skip same-language pairs
                    continue
                score = normalised_similarity(a[3], b[3])
                source = "abvd" if cog_id.startswith("abvd_") else "sinotibetan"
                pairs.append((
                    a[0], a[1], a[2],  # lang_a, word_a, ipa_a
                    b[0], b[1], b[2],  # lang_b, word_b, ipa_b
                    a[4],              # concept_id
                    "expert_cognate",
                    round(score, 4),
                    source,
                ))

    return pairs


def extract_concept_pairs(family_map: dict) -> tuple[list[tuple], list[tuple]]:
    """Extract concept-aligned pairs within families.

    Returns (inherited_pairs, similarity_pairs).
    """
    # Load all concept-annotated entries grouped by (family, concept)
    family_concept: dict[tuple[str, str], list[tuple[str, str, str, str]]] = defaultdict(list)
    # (iso, word, ipa, sca)

    for path in sorted(LEXICONS_DIR.glob("*.tsv")):
        iso = path.stem
        family = family_map.get(iso, "unknown")
        if family == "unknown":
            continue
        for entry in read_lexicon(path):
            concept = entry.get("Concept_ID", "-")
            if concept == "-" or not concept:
                continue
            sca = entry.get("SCA", "")
            if not sca:
                continue
            family_concept[(family, concept)].append((
                iso,
                entry.get("Word", ""),
                entry.get("IPA", ""),
                sca,
            ))

    inherited = []
    similarity = []

    for (family, concept), members in family_concept.items():
        if len(members) < 2:
            continue

        # Limit pairs per concept to avoid O(n^2) explosion
        # For large sets, sample representatives
        members_sample = members[:50]  # cap at 50 entries per concept

        for i in range(len(members_sample)):
            for j in range(i + 1, len(members_sample)):
                a = members_sample[i]
                b = members_sample[j]
                if a[0] == b[0]:  # skip same-language
                    continue

                score = normalised_similarity(a[3], b[3])

                if score >= 0.5:
                    inherited.append((
                        a[0], a[1], a[2],
                        b[0], b[1], b[2],
                        concept,
                        "cognate_inherited",
                        round(score, 4),
                        f"concept_align_{family}",
                    ))
                elif score >= 0.3:
                    similarity.append((
                        a[0], a[1], a[2],
                        b[0], b[1], b[2],
                        concept,
                        "similarity_only",
                        round(score, 4),
                        f"concept_align_{family}",
                    ))

    return inherited, similarity


def extract_wold_borrowings() -> list[tuple]:
    """Extract borrowing relationships from WOLD."""
    cldf_dir = SOURCES_DIR / "wold" / "cldf"
    if not cldf_dir.exists():
        return []

    # Use forms.csv Borrowed column to identify borrowed items
    return _extract_borrowings_from_forms(cldf_dir)


def _extract_borrowings_from_forms(cldf_dir: Path) -> list[tuple]:
    """Extract borrowing indicators from WOLD forms.csv."""
    forms_path = cldf_dir / "forms.csv"
    if not forms_path.exists():
        return []

    # Build language map
    wold_lang_map = {}
    for row in _read_csv(cldf_dir / "languages.csv"):
        iso = row.get("ISO639P3code", "")
        if iso:
            wold_lang_map[row["ID"]] = iso

    # Build parameter map
    param_map = {}
    for row in _read_csv(cldf_dir / "parameters.csv"):
        gloss = row.get("Concepticon_Gloss", row.get("Name", row["ID"]))
        param_map[row["ID"]] = gloss

    # Collect forms with borrowing annotations
    # Group by concept for cross-language pairing
    concept_forms: dict[str, list[tuple]] = defaultdict(list)

    for row in _read_csv(forms_path):
        lang_id = row.get("Language_ID", "")
        iso = wold_lang_map.get(lang_id)
        if not iso:
            continue

        borrowed = row.get("Borrowed", "").strip()
        if not borrowed or "no evidence" in borrowed or "very little" in borrowed:
            continue

        segments = row.get("Segments", "")
        if not segments:
            continue

        ipa = "".join(p for p in segments.split() if p not in ("^", "$", "+", "#", "_"))
        if not ipa:
            continue

        param_id = row.get("Parameter_ID", "")
        concept = param_map.get(param_id, param_id)
        word = row.get("Form", "")
        sca = ipa_to_sound_class(ipa)

        concept_forms[concept].append((iso, word, ipa, sca, borrowed))

    # Generate borrowing pairs (forms from different languages sharing a concept)
    pairs = []
    for concept, forms in concept_forms.items():
        if len(forms) < 2:
            continue
        for i in range(len(forms)):
            for j in range(i + 1, len(forms)):
                a = forms[i]
                b = forms[j]
                if a[0] == b[0]:
                    continue
                score = normalised_similarity(a[3], b[3])
                if score >= 0.3:
                    pairs.append((
                        a[0], a[1], a[2],
                        b[0], b[1], b[2],
                        concept,
                        "borrowing",
                        round(score, 4),
                        "wold",
                    ))

    return pairs


def _read_csv(path: Path) -> list[dict]:
    if not path.exists():
        return []
    with open(path, encoding="utf-8", newline="") as f:
        return list(csv.DictReader(f))


def write_pairs(path: Path, pairs: list[tuple]):
    """Write cognate pairs to TSV."""
    path.parent.mkdir(parents=True, exist_ok=True)
    with open(path, "w", encoding="utf-8", newline="") as f:
        f.write(PAIR_HEADER)
        for pair in pairs:
            f.write("\t".join(str(x) for x in pair) + "\n")


def main():
    print("=" * 80)
    print("Cognate Link Assignment")
    print("=" * 80)

    family_map = load_json(FAMILY_MAP_PATH)

    # Phase 1: Expert cognates
    print("\n  [Expert Cognates]")
    expert_pairs = extract_expert_cognates()
    print(f"  Expert cognate pairs: {len(expert_pairs):,}")

    # Phase 2: Concept-aligned pairs
    print("\n  [Concept-Aligned Pairs]")
    inherited, similarity = extract_concept_pairs(family_map)
    print(f"  Inherited pairs:  {len(inherited):,}")
    print(f"  Similarity pairs: {len(similarity):,}")

    # Phase 3: WOLD borrowings
    print("\n  [WOLD Borrowings]")
    borrowing_pairs = extract_wold_borrowings()
    print(f"  Borrowing pairs: {len(borrowing_pairs):,}")

    # Merge expert into inherited
    all_inherited = expert_pairs + inherited

    # Write output files
    COGNATE_DIR.mkdir(parents=True, exist_ok=True)
    write_pairs(COGNATE_DIR / "cognate_pairs_inherited.tsv", all_inherited)
    write_pairs(COGNATE_DIR / "cognate_pairs_similarity.tsv", similarity)
    write_pairs(COGNATE_DIR / "cognate_pairs_borrowing.tsv", borrowing_pairs)

    total_pairs = len(all_inherited) + len(similarity) + len(borrowing_pairs)

    print(f"\n{'=' * 80}")
    print(f"SUMMARY")
    print(f"{'=' * 80}")
    print(f"  Total cognate pairs: {total_pairs:,}")
    print(f"    Inherited:   {len(all_inherited):,}")
    print(f"    Similarity:  {len(similarity):,}")
    print(f"    Borrowing:   {len(borrowing_pairs):,}")
    print(f"\n  Output: {COGNATE_DIR}")
    print("Done!")


if __name__ == "__main__":
    main()