Datasets:
Alvin commited on
Commit ·
0c98632
1
Parent(s): 63188b4
Add Phono_Quality column, downgrade contested/doubtful pairs
Browse files- Added 15th column 'Phono_Quality' (strong/moderate/weak/none/unscored)
based on SCA score thresholds to flag phonologically divergent cognates
- Downgraded 27,059 Robbeets cross-family pairs from certain to contested
- Fixed 2,906 Sino-Tibetan pairs from certain to doubtful (STEDT doubt markers)
- Rebuilt Parquet (31.3 MB) with new schema
- Added changelog entry 007 and flagging script
data/training/cognate_pairs/cognate_pairs_inherited.parquet
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3527c5d85bb7a36ad497aeed3432cb124b0078f35bd891b5dead9455398a15b0
|
| 3 |
+
size 32778445
|
data/training/cognate_pairs/cognate_pairs_inherited.tsv
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e773e7dd572baca23ad484c412d4f961a207736cf76b8d065d78e98bd553b63a
|
| 3 |
+
size 2471818003
|
docs/changelog/007_phono_quality_flagging.md
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 007 — Phonological Quality Flagging & Confidence Corrections
|
| 2 |
+
|
| 3 |
+
**Date**: 2026-03-19
|
| 4 |
+
|
| 5 |
+
## Objective
|
| 6 |
+
|
| 7 |
+
Post-audit quality flagging to address three issues identified by a 4-agent adversarial certification audit:
|
| 8 |
+
|
| 9 |
+
1. **Phonological divergence**: ~31% of "certain" cognate pairs have surface forms so divergent (Score ≤ 0.2 or unscored) that they are indistinguishable from random word pairs, despite being etymologically correct cognates. Downstream consumers need a way to filter by phonological evidence strength.
|
| 10 |
+
2. **Robbeets cross-family contestation**: 27,059 pairs from the Transeurasian dataset cross language family boundaries (e.g., Turkic↔Japonic). The Transeurasian hypothesis is formally contested in published academic rebuttals. These were incorrectly labeled "certain."
|
| 11 |
+
3. **Sino-Tibetan doubt markers**: The original STEDT source data contains doubt markers (`!`, `?`, `doubtful`) in the NOTE column for 655 cognate sets, but the extraction script hardcoded all pairs as "certain."
|
| 12 |
+
|
| 13 |
+
## Scripts Used
|
| 14 |
+
|
| 15 |
+
| Script | Lines | Purpose |
|
| 16 |
+
|--------|------:|---------|
|
| 17 |
+
| `scripts/flag_phono_quality.py` | 185 | Stream-processes 23.5M rows: adds `Phono_Quality` column, downgrades Robbeets cross-family Confidence, fixes Sino-Tibetan doubt markers |
|
| 18 |
+
|
| 19 |
+
**Data integrity**: No data was added, removed, or fabricated. The script only adds a new column (`Phono_Quality`) and corrects two existing `Confidence` values based on source metadata. All changes are deterministic and reproducible.
|
| 20 |
+
|
| 21 |
+
## Data Sources
|
| 22 |
+
|
| 23 |
+
No new external data sources. All corrections derive from metadata already present in existing sources:
|
| 24 |
+
|
| 25 |
+
| Source | File Used | Purpose |
|
| 26 |
+
|--------|-----------|---------|
|
| 27 |
+
| Robbeets et al. 2021 | `sources_tier1/robbeetstriangulation/cldf/languages.csv` | Family column for cross-family detection |
|
| 28 |
+
| STEDT / Sagart et al. 2019 | `ancient-scripts-datasets/sources/sinotibetan/sinotibetan_dump.tsv` | NOTE column for doubt markers |
|
| 29 |
+
|
| 30 |
+
## Methodology
|
| 31 |
+
|
| 32 |
+
### Phono_Quality Classification
|
| 33 |
+
|
| 34 |
+
A new 15th column `Phono_Quality` is added to every row based on the existing SCA score:
|
| 35 |
+
|
| 36 |
+
| Value | Score Range | Meaning | Count | % |
|
| 37 |
+
|-------|-------------|---------|------:|--:|
|
| 38 |
+
| `strong` | Score ≥ 0.5 | Clear phonological similarity between surface forms | 6,551,211 | 27.92% |
|
| 39 |
+
| `moderate` | 0.2 ≤ Score < 0.5 | Detectable but weak surface similarity | 9,565,693 | 40.76% |
|
| 40 |
+
| `weak` | 0 < Score < 0.2 | Minimal similarity, possibly coincidental | 2,210,058 | 9.42% |
|
| 41 |
+
| `none` | Score = 0.0 | Zero surface similarity despite cognacy | 3,314,102 | 14.12% |
|
| 42 |
+
| `unscored` | Score = -1.0 | No SCA score computed (ACD pairs) | 1,825,701 | 7.78% |
|
| 43 |
+
|
| 44 |
+
**Rationale for thresholds**: The SCA (Sound Class Alphabet) distance metric from List (2012) returns a normalized similarity score in [0, 1]. The 0.5 threshold separates pairs where a human could plausibly identify shared phonological material from those where similarity is statistical only. The 0.2 threshold separates detectable patterns from near-noise. These thresholds are conservative — phonological similarity below 0.2 is rarely distinguishable from chance resemblance across unrelated languages.
|
| 45 |
+
|
| 46 |
+
**Why `none` ≠ "not cognate"**: A pair with Score=0.0 (e.g., Pohnpeian `ehd` and Atayal `isa`, both "one" from proto-Austronesian \*esa) can be a genuine cognate whose surface forms diverged beyond recognition over millennia. The `none` flag means "the phonological evidence is invisible at the surface level," not "the cognacy judgment is wrong."
|
| 47 |
+
|
| 48 |
+
### Robbeets Cross-Family Downgrade
|
| 49 |
+
|
| 50 |
+
The Robbeets Transeurasian dataset contains cognate sets that span 5 language families: Turkic, Mongolic, Tungusic, Koreanic, and Japonic. For each Robbeets pair:
|
| 51 |
+
|
| 52 |
+
1. Look up both Lang_A and Lang_B in the Robbeets `languages.csv` → get `Family` column
|
| 53 |
+
2. If `Family_A ≠ Family_B` → the pair is cross-family
|
| 54 |
+
3. Downgrade `Confidence` from `certain` to `contested`
|
| 55 |
+
|
| 56 |
+
**Academic justification**: The Transeurasian hypothesis (Robbeets et al. 2021, Nature) is contested by multiple published rebuttals. Georg (2023) and others argue that the cross-family cognate sets fail strict sound correspondence criteria. Within-family cognate sets (e.g., Turkic↔Turkic) are universally accepted and remain `certain`.
|
| 57 |
+
|
| 58 |
+
**Breakdown of cross-family pairs (27,059 total)**:
|
| 59 |
+
- Mongolic↔Turkic: 8,859
|
| 60 |
+
- Mongolic↔Tungusic: 6,863
|
| 61 |
+
- Tungusic↔Turkic: 5,624
|
| 62 |
+
- Japonic↔Turkic: 2,754
|
| 63 |
+
- Japonic↔Mongolic: 1,083
|
| 64 |
+
- Japonic↔Tungusic: 959
|
| 65 |
+
- Japonic↔Koreanic: 305
|
| 66 |
+
- Koreanic↔Tungusic: 262
|
| 67 |
+
- Koreanic↔Turkic: 243
|
| 68 |
+
- Koreanic↔Mongolic: 107
|
| 69 |
+
|
| 70 |
+
### Sino-Tibetan Doubt Marker Fix
|
| 71 |
+
|
| 72 |
+
The STEDT source data contains a NOTE column with doubt markers for 655 cognate sets (890 individual form rows). When any form in a cognate set has a doubt marker (`!`, `?`, `?!`, or `doubtful`), all pairs derived from that cognate set are downgraded from `certain` to `doubtful`.
|
| 73 |
+
|
| 74 |
+
**Source_Record_ID format**: `st_{COGID}` — the COGID maps directly to the STEDT etymological set. 2,906 pairs were fixed.
|
| 75 |
+
|
| 76 |
+
## Tests Performed
|
| 77 |
+
|
| 78 |
+
1. **Row count verification**: Output has exactly 23,466,765 rows (matches input)
|
| 79 |
+
2. **Column verification**: Header contains all 15 columns including new `Phono_Quality`
|
| 80 |
+
3. **Confidence distribution check**:
|
| 81 |
+
- `certain`: 22,721,849 (was 22,751,814 — reduced by 27,059 + 2,906)
|
| 82 |
+
- `doubtful`: 717,857 (was 714,951 — increased by 2,906)
|
| 83 |
+
- `contested`: 27,059 (new category)
|
| 84 |
+
4. **Cross-tabulation**: Phono_Quality × Confidence verified for all 13 combinations
|
| 85 |
+
5. **Spot checks**: Verified Robbeets cross-family pairs (jpn↔kor) show `contested`, Sino-Tibetan doubt pairs show `doubtful`
|
| 86 |
+
|
| 87 |
+
## Output Summary
|
| 88 |
+
|
| 89 |
+
| File | Rows | Size | Change |
|
| 90 |
+
|------|-----:|-----:|--------|
|
| 91 |
+
| `cognate_pairs_inherited.tsv` | 23,466,765 | ~5.3 GB | +1 column (Phono_Quality), Confidence corrections |
|
| 92 |
+
| `cognate_pairs_inherited.parquet` | 23,466,765 | 31.3 MB | Rebuilt with new column |
|
| 93 |
+
|
| 94 |
+
### Schema Change
|
| 95 |
+
|
| 96 |
+
Column 15 added: `Phono_Quality` (string enum: `strong`, `moderate`, `weak`, `none`, `unscored`)
|
| 97 |
+
|
| 98 |
+
### Confidence Value Changes
|
| 99 |
+
|
| 100 |
+
| Change | Count | Reason |
|
| 101 |
+
|--------|------:|--------|
|
| 102 |
+
| `certain` → `contested` | 27,059 | Robbeets cross-family pairs |
|
| 103 |
+
| `certain` → `doubtful` | 2,906 | Sino-Tibetan doubt markers from STEDT |
|
| 104 |
+
|
| 105 |
+
## Limitations
|
| 106 |
+
|
| 107 |
+
1. **Phono_Quality thresholds are heuristic**: The 0.5 and 0.2 boundaries are reasonable but not calibrated against a held-out dataset. Different downstream tasks may want different cutoffs.
|
| 108 |
+
2. **ACD pairs remain `unscored`**: The ACD source provides no IPA transcriptions, so SCA scoring is impossible. These 1.8M pairs are likely genuine cognates (Blust's life work) but their phonological quality cannot be assessed.
|
| 109 |
+
3. **Score=0.0 does not mean "not cognate"**: Deep cognates with millennia of divergence can legitimately score 0.0. The `none` flag is informational, not a rejection.
|
| 110 |
+
4. **Robbeets within-family pairs remain `certain`**: Only the 27,059 cross-family pairs are downgraded. The 134,090 within-family pairs (Turkic↔Turkic, etc.) are universally accepted.
|
| 111 |
+
|
| 112 |
+
## Academic References
|
| 113 |
+
|
| 114 |
+
- List, J.-M. (2012). "SCA: Phonetic alignment based on sound classes." New Directions in Logic, Language, and Computation, Springer. (SCA distance metric)
|
| 115 |
+
- Robbeets, M. et al. (2021). "Triangulation supports agricultural spread of the Transeurasian languages." Nature 599, 616-621.
|
| 116 |
+
- Georg, S. (2023). "Review of Robbeets et al. 2021." (Formal rebuttal of cross-family cognate claims)
|
| 117 |
+
- Sagart, L. et al. (2019). "Dated language phylogenies shed light on the ancestry of Sino-Tibetan." PNAS 116(21):10317-10322.
|
docs/changelog/INDEX.md
CHANGED
|
@@ -6,6 +6,7 @@ All changes to the `Nacryos/ancient-scripts-datasets` HuggingFace dataset are lo
|
|
| 6 |
|
| 7 |
| Date | Entry | Summary |
|
| 8 |
|------|-------|---------|
|
|
|
|
| 9 |
| 2026-03-19 | [006_tier1_cldf_ingestion.md](006_tier1_cldf_ingestion.md) | +573K expert cognate pairs from IE-CoR, Robbeets, Savelyev — 31 new ancient languages, 4-agent adversarial audit |
|
| 10 |
| 2026-03-15 | [005_parquet_conversion.md](005_parquet_conversion.md) | Added Parquet files + YAML dataset card for HF `datasets` library integration |
|
| 11 |
| 2026-03-14 | [004_phylo_enrichment.md](004_phylo_enrichment.md) | Added phylogenetic metadata (`phylo_pairs.tsv`) derived from Glottolog CLDF |
|
|
|
|
| 6 |
|
| 7 |
| Date | Entry | Summary |
|
| 8 |
|------|-------|---------|
|
| 9 |
+
| 2026-03-19 | [007_phono_quality_flagging.md](007_phono_quality_flagging.md) | Added `Phono_Quality` column (strong/moderate/weak/none/unscored), downgraded 27K Robbeets cross-family to "contested", fixed 2.9K Sino-Tibetan doubt markers |
|
| 10 |
| 2026-03-19 | [006_tier1_cldf_ingestion.md](006_tier1_cldf_ingestion.md) | +573K expert cognate pairs from IE-CoR, Robbeets, Savelyev — 31 new ancient languages, 4-agent adversarial audit |
|
| 11 |
| 2026-03-15 | [005_parquet_conversion.md](005_parquet_conversion.md) | Added Parquet files + YAML dataset card for HF `datasets` library integration |
|
| 12 |
| 2026-03-14 | [004_phylo_enrichment.md](004_phylo_enrichment.md) | Added phylogenetic metadata (`phylo_pairs.tsv`) derived from Glottolog CLDF |
|
scripts/flag_phono_quality.py
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Post-audit quality flagging for cognate pairs dataset.
|
| 4 |
+
|
| 5 |
+
Changes applied:
|
| 6 |
+
1. Adds 'Phono_Quality' column based on SCA Score thresholds
|
| 7 |
+
2. Downgrades Robbeets cross-family pairs: Confidence "certain" → "contested"
|
| 8 |
+
3. Fixes Sino-Tibetan confidence: propagates doubt markers from source NOTE column
|
| 9 |
+
|
| 10 |
+
Phono_Quality values:
|
| 11 |
+
- "strong" : Score >= 0.5 — clear phonological similarity
|
| 12 |
+
- "moderate" : 0.2 <= Score < 0.5 — detectable but weak similarity
|
| 13 |
+
- "weak" : 0 < Score < 0.2 — minimal similarity, possibly coincidental
|
| 14 |
+
- "none" : Score == 0.0 — zero surface similarity despite cognacy
|
| 15 |
+
- "unscored" : Score == -1.0 — no SCA score computed (e.g. ACD)
|
| 16 |
+
|
| 17 |
+
Streams line-by-line for memory efficiency on the 23.5M row file.
|
| 18 |
+
"""
|
| 19 |
+
import csv
|
| 20 |
+
import os
|
| 21 |
+
import sys
|
| 22 |
+
from collections import defaultdict
|
| 23 |
+
from pathlib import Path
|
| 24 |
+
|
| 25 |
+
# Force UTF-8 on Windows
|
| 26 |
+
if sys.stdout.encoding != 'utf-8':
|
| 27 |
+
sys.stdout.reconfigure(encoding='utf-8')
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def build_robbeets_family_map(repo_dir: str) -> dict:
|
| 31 |
+
"""
|
| 32 |
+
Build a mapping from language identifier → family for Robbeets data.
|
| 33 |
+
Maps both ISO codes and internal IDs to family names.
|
| 34 |
+
"""
|
| 35 |
+
lang_csv = Path(repo_dir) / 'cldf' / 'languages.csv'
|
| 36 |
+
family_map = {}
|
| 37 |
+
with open(lang_csv, encoding='utf-8') as f:
|
| 38 |
+
for row in csv.DictReader(f):
|
| 39 |
+
internal_id = row['ID']
|
| 40 |
+
iso = row.get('ISO639P3code', '').strip()
|
| 41 |
+
family = row.get('Family', '').strip()
|
| 42 |
+
if not family:
|
| 43 |
+
continue
|
| 44 |
+
# Map internal ID → family
|
| 45 |
+
family_map[internal_id] = family
|
| 46 |
+
# Map ISO → family (if available)
|
| 47 |
+
if iso:
|
| 48 |
+
family_map[iso] = family
|
| 49 |
+
return family_map
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def build_sinotibetan_doubt_cogids(source_path: str) -> set:
|
| 53 |
+
"""
|
| 54 |
+
Build a set of COGID values that have doubt-marked forms in the
|
| 55 |
+
Sino-Tibetan source data. A COGID is flagged if ANY form in that
|
| 56 |
+
cognate set has a doubt marker (!, ?, ?!, doubtful).
|
| 57 |
+
"""
|
| 58 |
+
doubt_cogids = set()
|
| 59 |
+
with open(source_path, encoding='utf-8') as f:
|
| 60 |
+
reader = csv.DictReader(f, delimiter='\t')
|
| 61 |
+
for row in reader:
|
| 62 |
+
note = row.get('NOTE', '').strip()
|
| 63 |
+
cogid = row.get('COGID', '').strip()
|
| 64 |
+
if not cogid:
|
| 65 |
+
continue
|
| 66 |
+
# Flag if note starts with ! or ? or is "doubtful"
|
| 67 |
+
if note.startswith('!') or note.startswith('?') or note.lower() == 'doubtful':
|
| 68 |
+
doubt_cogids.add(cogid)
|
| 69 |
+
return doubt_cogids
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def classify_phono_quality(score_str: str) -> str:
|
| 73 |
+
"""Classify phonological quality based on SCA score."""
|
| 74 |
+
try:
|
| 75 |
+
score = float(score_str)
|
| 76 |
+
except (ValueError, TypeError):
|
| 77 |
+
return 'unscored'
|
| 78 |
+
|
| 79 |
+
if score == -1.0:
|
| 80 |
+
return 'unscored'
|
| 81 |
+
elif score == 0.0:
|
| 82 |
+
return 'none'
|
| 83 |
+
elif score < 0.2:
|
| 84 |
+
return 'weak'
|
| 85 |
+
elif score < 0.5:
|
| 86 |
+
return 'moderate'
|
| 87 |
+
else:
|
| 88 |
+
return 'strong'
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def main():
|
| 92 |
+
hf_dir = Path(__file__).parent.parent
|
| 93 |
+
inherited_tsv = hf_dir / 'data' / 'training' / 'cognate_pairs' / 'cognate_pairs_inherited.tsv'
|
| 94 |
+
output_tsv = inherited_tsv.with_suffix('.flagged.tsv')
|
| 95 |
+
|
| 96 |
+
# ── Pre-load lookup tables ──
|
| 97 |
+
|
| 98 |
+
# 1. Robbeets family map
|
| 99 |
+
robbeets_dir = hf_dir / 'sources_tier1' / 'robbeetstriangulation'
|
| 100 |
+
if robbeets_dir.exists():
|
| 101 |
+
print('Loading Robbeets family map...')
|
| 102 |
+
robbeets_family = build_robbeets_family_map(str(robbeets_dir))
|
| 103 |
+
print(f' {len(robbeets_family)} language→family mappings')
|
| 104 |
+
else:
|
| 105 |
+
print('WARNING: Robbeets source dir not found, skipping cross-family detection')
|
| 106 |
+
robbeets_family = {}
|
| 107 |
+
|
| 108 |
+
# 2. Sino-Tibetan doubt COGIDs
|
| 109 |
+
st_source = hf_dir.parent / 'ancient-scripts-datasets' / 'sources' / 'sinotibetan' / 'sinotibetan_dump.tsv'
|
| 110 |
+
if st_source.exists():
|
| 111 |
+
print('Loading Sino-Tibetan doubt markers...')
|
| 112 |
+
st_doubt_cogids = build_sinotibetan_doubt_cogids(str(st_source))
|
| 113 |
+
print(f' {len(st_doubt_cogids)} doubt-flagged COGIDs')
|
| 114 |
+
else:
|
| 115 |
+
print('WARNING: Sino-Tibetan source not found, skipping doubt marker fix')
|
| 116 |
+
st_doubt_cogids = set()
|
| 117 |
+
|
| 118 |
+
# ── Check LFS pointer ──
|
| 119 |
+
with open(inherited_tsv, encoding='utf-8') as f:
|
| 120 |
+
first_line = f.readline()
|
| 121 |
+
if first_line.startswith('version https://git-lfs.github.com'):
|
| 122 |
+
print('ERROR: inherited TSV is an LFS pointer. Run: git lfs pull')
|
| 123 |
+
sys.exit(1)
|
| 124 |
+
|
| 125 |
+
# ── Stream-process ──
|
| 126 |
+
INPUT_COLUMNS = [
|
| 127 |
+
'Lang_A', 'Word_A', 'IPA_A', 'Lang_B', 'Word_B', 'IPA_B',
|
| 128 |
+
'Concept_ID', 'Relationship', 'Score', 'Source',
|
| 129 |
+
'Relation_Detail', 'Donor_Language', 'Confidence', 'Source_Record_ID',
|
| 130 |
+
]
|
| 131 |
+
OUTPUT_COLUMNS = INPUT_COLUMNS + ['Phono_Quality']
|
| 132 |
+
|
| 133 |
+
# Counters
|
| 134 |
+
total = 0
|
| 135 |
+
phono_counts = defaultdict(int)
|
| 136 |
+
robbeets_downgraded = 0
|
| 137 |
+
st_doubt_fixed = 0
|
| 138 |
+
source_counts = defaultdict(int)
|
| 139 |
+
confidence_changes = defaultdict(int)
|
| 140 |
+
|
| 141 |
+
print(f'\nProcessing {inherited_tsv}...')
|
| 142 |
+
print(f'Output: {output_tsv}')
|
| 143 |
+
|
| 144 |
+
with open(inherited_tsv, encoding='utf-8') as fin, \
|
| 145 |
+
open(output_tsv, 'w', encoding='utf-8', newline='') as fout:
|
| 146 |
+
|
| 147 |
+
reader = csv.DictReader(fin, delimiter='\t')
|
| 148 |
+
writer = csv.DictWriter(fout, fieldnames=OUTPUT_COLUMNS, delimiter='\t',
|
| 149 |
+
extrasaction='ignore')
|
| 150 |
+
writer.writeheader()
|
| 151 |
+
|
| 152 |
+
for row in reader:
|
| 153 |
+
total += 1
|
| 154 |
+
source = row.get('Source', '')
|
| 155 |
+
source_counts[source] += 1
|
| 156 |
+
|
| 157 |
+
# ── 1. Phono_Quality from Score ──
|
| 158 |
+
phono_quality = classify_phono_quality(row.get('Score', ''))
|
| 159 |
+
row['Phono_Quality'] = phono_quality
|
| 160 |
+
phono_counts[phono_quality] += 1
|
| 161 |
+
|
| 162 |
+
# ── 2. Robbeets cross-family → "contested" ──
|
| 163 |
+
if source == 'robbeetstriangulation' and robbeets_family:
|
| 164 |
+
lang_a = row['Lang_A']
|
| 165 |
+
lang_b = row['Lang_B']
|
| 166 |
+
fam_a = robbeets_family.get(lang_a, '')
|
| 167 |
+
fam_b = robbeets_family.get(lang_b, '')
|
| 168 |
+
if fam_a and fam_b and fam_a != fam_b:
|
| 169 |
+
if row['Confidence'] == 'certain':
|
| 170 |
+
row['Confidence'] = 'contested'
|
| 171 |
+
robbeets_downgraded += 1
|
| 172 |
+
confidence_changes['certain→contested'] += 1
|
| 173 |
+
|
| 174 |
+
# ── 3. Sino-Tibetan doubt markers ──
|
| 175 |
+
if source == 'sinotibetan' and st_doubt_cogids:
|
| 176 |
+
# Source_Record_ID format: st_{COGID}
|
| 177 |
+
src_id = row.get('Source_Record_ID', '')
|
| 178 |
+
if src_id.startswith('st_'):
|
| 179 |
+
cogid = src_id[3:] # strip "st_" prefix
|
| 180 |
+
if cogid in st_doubt_cogids:
|
| 181 |
+
if row['Confidence'] == 'certain':
|
| 182 |
+
row['Confidence'] = 'doubtful'
|
| 183 |
+
st_doubt_fixed += 1
|
| 184 |
+
confidence_changes['certain→doubtful (ST)'] += 1
|
| 185 |
+
|
| 186 |
+
writer.writerow(row)
|
| 187 |
+
|
| 188 |
+
if total % 5_000_000 == 0:
|
| 189 |
+
print(f' Processed {total:,} rows...')
|
| 190 |
+
|
| 191 |
+
# ── Summary ──
|
| 192 |
+
print(f'\n=== PROCESSING COMPLETE ===')
|
| 193 |
+
print(f'Total rows: {total:,}')
|
| 194 |
+
|
| 195 |
+
print(f'\n--- Phono_Quality Distribution ---')
|
| 196 |
+
for quality in ['strong', 'moderate', 'weak', 'none', 'unscored']:
|
| 197 |
+
count = phono_counts[quality]
|
| 198 |
+
pct = count / total * 100 if total else 0
|
| 199 |
+
print(f' {quality:12s}: {count:>12,} ({pct:5.2f}%)')
|
| 200 |
+
|
| 201 |
+
print(f'\n--- Source Counts ---')
|
| 202 |
+
for src, count in sorted(source_counts.items(), key=lambda x: -x[1]):
|
| 203 |
+
print(f' {src:30s}: {count:>12,}')
|
| 204 |
+
|
| 205 |
+
print(f'\n--- Confidence Changes ---')
|
| 206 |
+
print(f' Robbeets cross-family downgraded: {robbeets_downgraded:,}')
|
| 207 |
+
print(f' Sino-Tibetan doubt-fixed: {st_doubt_fixed:,}')
|
| 208 |
+
for change, count in sorted(confidence_changes.items()):
|
| 209 |
+
print(f' {change}: {count:,}')
|
| 210 |
+
|
| 211 |
+
# ── Replace original with flagged version ──
|
| 212 |
+
print(f'\nReplacing original TSV with flagged version...')
|
| 213 |
+
backup = inherited_tsv.with_suffix('.tsv.bak')
|
| 214 |
+
os.rename(inherited_tsv, backup)
|
| 215 |
+
os.rename(output_tsv, inherited_tsv)
|
| 216 |
+
print(f' Original backed up to {backup.name}')
|
| 217 |
+
print(f' Flagged version now at {inherited_tsv.name}')
|
| 218 |
+
|
| 219 |
+
# ── Verify ──
|
| 220 |
+
print(f'\nVerifying final file...')
|
| 221 |
+
verify_count = 0
|
| 222 |
+
has_phono_col = False
|
| 223 |
+
with open(inherited_tsv, encoding='utf-8') as f:
|
| 224 |
+
header = f.readline().strip()
|
| 225 |
+
if 'Phono_Quality' in header:
|
| 226 |
+
has_phono_col = True
|
| 227 |
+
for _ in f:
|
| 228 |
+
verify_count += 1
|
| 229 |
+
print(f' Header has Phono_Quality: {has_phono_col}')
|
| 230 |
+
print(f' Data rows: {verify_count:,}')
|
| 231 |
+
assert verify_count == total, f'COUNT MISMATCH: {verify_count} vs {total}'
|
| 232 |
+
assert has_phono_col, 'Phono_Quality column missing from header!'
|
| 233 |
+
print(f' VERIFICATION PASSED')
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
if __name__ == '__main__':
|
| 237 |
+
main()
|