Datasets:
Alvin commited on
Commit ·
f304b7a
1
Parent(s): b37727e
Add Linear A Phonotactics Validation dataset
Browse files1,414,143 gold-standard cognate pairs between 360 languages that
phonotactically resemble Linear A (CV open syllables, no clusters,
final vowels). Every pair has Confidence=certain and Phono_Quality=strong
(SCA Score >= 0.5). Threshold: Linear_A_Score >= 0.80 for both languages.
Includes typological analysis script and full 1,047-language ranking.
README.md
CHANGED
|
@@ -32,6 +32,10 @@ configs:
|
|
| 32 |
data_files:
|
| 33 |
- split: train
|
| 34 |
path: data/training/cognate_pairs/cognate_pairs_phono_filtered.parquet
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
- config_name: phylo_pairs
|
| 36 |
data_files:
|
| 37 |
- split: train
|
|
|
|
| 32 |
data_files:
|
| 33 |
- split: train
|
| 34 |
path: data/training/cognate_pairs/cognate_pairs_phono_filtered.parquet
|
| 35 |
+
- config_name: linear_a_phonotactics_validation
|
| 36 |
+
data_files:
|
| 37 |
+
- split: train
|
| 38 |
+
path: data/training/cognate_pairs/linear_a_phonotactics_validation.parquet
|
| 39 |
- config_name: phylo_pairs
|
| 40 |
data_files:
|
| 41 |
- split: train
|
analysis/typology_linear_a.tsv
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:968bd1db0295b36c908d6e40f92b06fc14dc8acc51de61bafe7901aab29b1ff9
|
| 3 |
+
size 84296
|
data/training/cognate_pairs/linear_a_phonotactics_validation.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3c81dc14b52c3b3b394cbaa64885f38cc35ea60004deaba4e0dfd8cc20a1e8c8
|
| 3 |
+
size 4918434
|
scripts/analyze_typology_linear_a.py
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Typological analysis: rank languages by similarity to Linear A phonotactic profile.
|
| 4 |
+
|
| 5 |
+
Linear A characteristics (from syllabary structure and linguistic analysis):
|
| 6 |
+
1. Open syllables (CV, V) — the syllabary is fundamentally CV-based
|
| 7 |
+
2. Limited/no consonant clusters — syllabary cannot represent CC sequences
|
| 8 |
+
3. Likely agglutinative morphology — observed prefix/suffix patterns
|
| 9 |
+
4. Simple vowel system — Linear B (descended from A) has 5 vowels (a,e,i,o,u)
|
| 10 |
+
5. Moderate consonant inventory
|
| 11 |
+
6. Word-final vowels predominate (open-syllable language)
|
| 12 |
+
|
| 13 |
+
For each language with IPA data, we compute:
|
| 14 |
+
- open_syllable_ratio: % of syllables that are open (end in vowel)
|
| 15 |
+
- cluster_ratio: % of words containing consonant clusters (CC+)
|
| 16 |
+
- mean_word_length: average number of segments per word
|
| 17 |
+
- final_vowel_ratio: % of words ending in a vowel
|
| 18 |
+
- cv_ratio: ratio of C to V segments (Linear A-like ≈ 1.0-1.5)
|
| 19 |
+
|
| 20 |
+
A composite "Linear A similarity score" ranks languages.
|
| 21 |
+
|
| 22 |
+
Input: cognate_pairs_phono_filtered.parquet (only phonologically reliable pairs)
|
| 23 |
+
Output: analysis/typology_linear_a.tsv
|
| 24 |
+
"""
|
| 25 |
+
import csv
|
| 26 |
+
import os
|
| 27 |
+
import sys
|
| 28 |
+
import unicodedata
|
| 29 |
+
from collections import defaultdict
|
| 30 |
+
from pathlib import Path
|
| 31 |
+
|
| 32 |
+
if sys.stdout.encoding != 'utf-8':
|
| 33 |
+
sys.stdout.reconfigure(encoding='utf-8')
|
| 34 |
+
|
| 35 |
+
# ── IPA Classification ──
|
| 36 |
+
|
| 37 |
+
VOWELS = set('aeiouyɑæɐəɛɪɨɔʊʉɯøœɤɒʌɜɞɵɘ')
|
| 38 |
+
# Include nasalized vowels (base char is vowel + combining tilde)
|
| 39 |
+
CONSONANTS = set(
|
| 40 |
+
'pbtdkgqɢʔcɟʈɖfvszʃʒxɣhɦθðçʝχʁħʕɸβʂʐɬɮ'
|
| 41 |
+
'mnŋɲɳɴɱ'
|
| 42 |
+
'lrɾɹɻʎɭʟɽ'
|
| 43 |
+
'wjʋɰ'
|
| 44 |
+
'ʦʧʤʣɕʑ'
|
| 45 |
+
)
|
| 46 |
+
SKIP_CHARS = set('ˈˌːˑ.ˤʰʷʲ̃ᵊ⁼ˀ‿ʼ()[]{}/ \t-')
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def classify_segments(ipa: str) -> list:
|
| 50 |
+
"""Convert IPA string to list of (segment, type) where type is 'V' or 'C'."""
|
| 51 |
+
if not ipa or ipa == '-':
|
| 52 |
+
return []
|
| 53 |
+
ipa = unicodedata.normalize('NFC', ipa)
|
| 54 |
+
segments = []
|
| 55 |
+
for ch in ipa:
|
| 56 |
+
base = ch.lower()
|
| 57 |
+
cat = unicodedata.category(ch)
|
| 58 |
+
# Skip combining marks, suprasegmentals, brackets, whitespace
|
| 59 |
+
if cat.startswith('M') or ch in SKIP_CHARS or cat == 'Zs':
|
| 60 |
+
continue
|
| 61 |
+
if base in VOWELS:
|
| 62 |
+
segments.append((ch, 'V'))
|
| 63 |
+
elif base in CONSONANTS:
|
| 64 |
+
segments.append((ch, 'C'))
|
| 65 |
+
# Skip unknown (tone marks, numbers, etc.)
|
| 66 |
+
return segments
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def compute_word_stats(ipa: str) -> dict:
|
| 70 |
+
"""Compute phonotactic statistics for a single IPA word."""
|
| 71 |
+
segments = classify_segments(ipa)
|
| 72 |
+
if not segments:
|
| 73 |
+
return None
|
| 74 |
+
|
| 75 |
+
types = ''.join(t for _, t in segments)
|
| 76 |
+
n_v = types.count('V')
|
| 77 |
+
n_c = types.count('C')
|
| 78 |
+
total = len(types)
|
| 79 |
+
|
| 80 |
+
if total == 0:
|
| 81 |
+
return None
|
| 82 |
+
|
| 83 |
+
# Open syllable heuristic: count CV and V sequences
|
| 84 |
+
# A syllable is "open" if it ends in V (no coda consonant)
|
| 85 |
+
# Simple heuristic: split into syllables at each V→C transition after V
|
| 86 |
+
syllables = []
|
| 87 |
+
current = ''
|
| 88 |
+
for t in types:
|
| 89 |
+
current += t
|
| 90 |
+
if t == 'V':
|
| 91 |
+
syllables.append(current)
|
| 92 |
+
current = ''
|
| 93 |
+
if current:
|
| 94 |
+
# Remaining consonants attach to last syllable as coda
|
| 95 |
+
if syllables:
|
| 96 |
+
syllables[-1] += current
|
| 97 |
+
else:
|
| 98 |
+
syllables.append(current)
|
| 99 |
+
|
| 100 |
+
open_count = sum(1 for s in syllables if s.endswith('V'))
|
| 101 |
+
total_syllables = len(syllables)
|
| 102 |
+
|
| 103 |
+
# Consonant clusters: CC or more in sequence
|
| 104 |
+
has_cluster = 'CC' in types
|
| 105 |
+
|
| 106 |
+
# Final segment
|
| 107 |
+
final_is_vowel = types[-1] == 'V' if types else False
|
| 108 |
+
|
| 109 |
+
# CV ratio
|
| 110 |
+
cv_ratio = n_c / n_v if n_v > 0 else float('inf')
|
| 111 |
+
|
| 112 |
+
return {
|
| 113 |
+
'n_segments': total,
|
| 114 |
+
'n_vowels': n_v,
|
| 115 |
+
'n_consonants': n_c,
|
| 116 |
+
'n_syllables': total_syllables,
|
| 117 |
+
'open_syllables': open_count,
|
| 118 |
+
'has_cluster': has_cluster,
|
| 119 |
+
'final_vowel': final_is_vowel,
|
| 120 |
+
'cv_ratio': cv_ratio,
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def main():
|
| 125 |
+
hf_dir = Path(__file__).parent.parent
|
| 126 |
+
parquet_path = hf_dir / 'data' / 'training' / 'cognate_pairs' / 'cognate_pairs_phono_filtered.parquet'
|
| 127 |
+
|
| 128 |
+
# Use pyarrow to stream efficiently
|
| 129 |
+
import pyarrow.parquet as pq
|
| 130 |
+
|
| 131 |
+
print('Loading filtered Parquet...')
|
| 132 |
+
table = pq.read_table(parquet_path, columns=['Lang_A', 'IPA_A', 'Lang_B', 'IPA_B'])
|
| 133 |
+
print(f' {table.num_rows:,} pairs')
|
| 134 |
+
|
| 135 |
+
# Collect unique (language, ipa) entries
|
| 136 |
+
# We need per-language IPA forms — extract from both A and B sides
|
| 137 |
+
print('Extracting per-language IPA forms...')
|
| 138 |
+
lang_forms = defaultdict(set) # lang → set of IPA forms
|
| 139 |
+
|
| 140 |
+
lang_a = table['Lang_A'].to_pylist()
|
| 141 |
+
ipa_a = table['IPA_A'].to_pylist()
|
| 142 |
+
lang_b = table['Lang_B'].to_pylist()
|
| 143 |
+
ipa_b = table['IPA_B'].to_pylist()
|
| 144 |
+
|
| 145 |
+
for i in range(len(lang_a)):
|
| 146 |
+
la, ia = lang_a[i], ipa_a[i]
|
| 147 |
+
lb, ib = lang_b[i], ipa_b[i]
|
| 148 |
+
if ia and ia != '-':
|
| 149 |
+
lang_forms[la].add(ia)
|
| 150 |
+
if ib and ib != '-':
|
| 151 |
+
lang_forms[lb].add(ib)
|
| 152 |
+
|
| 153 |
+
# Free memory
|
| 154 |
+
del lang_a, ipa_a, lang_b, ipa_b, table
|
| 155 |
+
print(f' {len(lang_forms):,} languages with IPA data')
|
| 156 |
+
|
| 157 |
+
# Compute per-language statistics
|
| 158 |
+
print('Computing phonotactic statistics...')
|
| 159 |
+
results = []
|
| 160 |
+
|
| 161 |
+
for lang, forms in sorted(lang_forms.items()):
|
| 162 |
+
if len(forms) < 5: # Skip languages with too few forms
|
| 163 |
+
continue
|
| 164 |
+
|
| 165 |
+
total_words = 0
|
| 166 |
+
total_segments = 0
|
| 167 |
+
total_syllables = 0
|
| 168 |
+
total_open = 0
|
| 169 |
+
total_with_cluster = 0
|
| 170 |
+
total_final_vowel = 0
|
| 171 |
+
total_cv_ratios = []
|
| 172 |
+
|
| 173 |
+
for ipa in forms:
|
| 174 |
+
stats = compute_word_stats(ipa)
|
| 175 |
+
if stats is None:
|
| 176 |
+
continue
|
| 177 |
+
total_words += 1
|
| 178 |
+
total_segments += stats['n_segments']
|
| 179 |
+
total_syllables += stats['n_syllables']
|
| 180 |
+
total_open += stats['open_syllables']
|
| 181 |
+
if stats['has_cluster']:
|
| 182 |
+
total_with_cluster += 1
|
| 183 |
+
if stats['final_vowel']:
|
| 184 |
+
total_final_vowel += 1
|
| 185 |
+
if stats['cv_ratio'] != float('inf'):
|
| 186 |
+
total_cv_ratios.append(stats['cv_ratio'])
|
| 187 |
+
|
| 188 |
+
if total_words < 5:
|
| 189 |
+
continue
|
| 190 |
+
|
| 191 |
+
open_syl_ratio = total_open / total_syllables if total_syllables > 0 else 0
|
| 192 |
+
cluster_ratio = total_with_cluster / total_words
|
| 193 |
+
final_vowel_ratio = total_final_vowel / total_words
|
| 194 |
+
mean_word_len = total_segments / total_words
|
| 195 |
+
mean_cv_ratio = sum(total_cv_ratios) / len(total_cv_ratios) if total_cv_ratios else 0
|
| 196 |
+
|
| 197 |
+
# ── Linear A Similarity Score ──
|
| 198 |
+
# Components (each 0-1, higher = more Linear A-like):
|
| 199 |
+
#
|
| 200 |
+
# 1. Open syllable score: open_syl_ratio (already 0-1)
|
| 201 |
+
# Linear A syllabary is CV-based → high open syllable ratio expected
|
| 202 |
+
#
|
| 203 |
+
# 2. No-cluster score: 1 - cluster_ratio
|
| 204 |
+
# Linear A can't represent clusters → low cluster frequency expected
|
| 205 |
+
#
|
| 206 |
+
# 3. Final-vowel score: final_vowel_ratio (already 0-1)
|
| 207 |
+
# Open-syllable languages tend to end words with vowels
|
| 208 |
+
#
|
| 209 |
+
# 4. CV-ratio score: closeness to 1.2 (ideal CV balance for CV syllabary)
|
| 210 |
+
# Pure CV language has ratio ~1.0; allowing for some CVC, ideal ~1.0-1.5
|
| 211 |
+
# Score = 1 - min(|cv_ratio - 1.2| / 1.5, 1.0)
|
| 212 |
+
#
|
| 213 |
+
# 5. Word length score: moderate length preferred (agglutinative = longer words)
|
| 214 |
+
# Isolating languages have very short words (2-3), agglutinative have 5-10
|
| 215 |
+
# Score peaks at mean_word_len ≈ 5-7
|
| 216 |
+
# Score = 1 - min(|mean_word_len - 6| / 6, 1.0)
|
| 217 |
+
|
| 218 |
+
s_open = open_syl_ratio
|
| 219 |
+
s_nocluster = 1.0 - cluster_ratio
|
| 220 |
+
s_finalv = final_vowel_ratio
|
| 221 |
+
s_cvratio = 1.0 - min(abs(mean_cv_ratio - 1.2) / 1.5, 1.0)
|
| 222 |
+
s_wordlen = 1.0 - min(abs(mean_word_len - 6.0) / 6.0, 1.0)
|
| 223 |
+
|
| 224 |
+
# Weighted composite (open syllables and no-clusters are most diagnostic)
|
| 225 |
+
linear_a_score = (
|
| 226 |
+
0.30 * s_open +
|
| 227 |
+
0.25 * s_nocluster +
|
| 228 |
+
0.20 * s_finalv +
|
| 229 |
+
0.15 * s_cvratio +
|
| 230 |
+
0.10 * s_wordlen
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
results.append({
|
| 234 |
+
'Language': lang,
|
| 235 |
+
'N_Forms': total_words,
|
| 236 |
+
'Open_Syllable_Ratio': round(open_syl_ratio, 4),
|
| 237 |
+
'Cluster_Ratio': round(cluster_ratio, 4),
|
| 238 |
+
'Final_Vowel_Ratio': round(final_vowel_ratio, 4),
|
| 239 |
+
'Mean_Word_Length': round(mean_word_len, 2),
|
| 240 |
+
'Mean_CV_Ratio': round(mean_cv_ratio, 3),
|
| 241 |
+
'Score_Open': round(s_open, 4),
|
| 242 |
+
'Score_NoCluster': round(s_nocluster, 4),
|
| 243 |
+
'Score_FinalVowel': round(s_finalv, 4),
|
| 244 |
+
'Score_CVRatio': round(s_cvratio, 4),
|
| 245 |
+
'Score_WordLen': round(s_wordlen, 4),
|
| 246 |
+
'Linear_A_Score': round(linear_a_score, 4),
|
| 247 |
+
})
|
| 248 |
+
|
| 249 |
+
# Sort by Linear A similarity
|
| 250 |
+
results.sort(key=lambda x: -x['Linear_A_Score'])
|
| 251 |
+
|
| 252 |
+
# Write output
|
| 253 |
+
out_dir = hf_dir / 'analysis'
|
| 254 |
+
out_dir.mkdir(exist_ok=True)
|
| 255 |
+
out_path = out_dir / 'typology_linear_a.tsv'
|
| 256 |
+
|
| 257 |
+
COLUMNS = [
|
| 258 |
+
'Language', 'N_Forms', 'Open_Syllable_Ratio', 'Cluster_Ratio',
|
| 259 |
+
'Final_Vowel_Ratio', 'Mean_Word_Length', 'Mean_CV_Ratio',
|
| 260 |
+
'Score_Open', 'Score_NoCluster', 'Score_FinalVowel',
|
| 261 |
+
'Score_CVRatio', 'Score_WordLen', 'Linear_A_Score',
|
| 262 |
+
]
|
| 263 |
+
|
| 264 |
+
with open(out_path, 'w', encoding='utf-8', newline='') as f:
|
| 265 |
+
writer = csv.DictWriter(f, fieldnames=COLUMNS, delimiter='\t')
|
| 266 |
+
writer.writeheader()
|
| 267 |
+
for row in results:
|
| 268 |
+
writer.writerow(row)
|
| 269 |
+
|
| 270 |
+
print(f'\nWrote {len(results)} languages to {out_path}')
|
| 271 |
+
|
| 272 |
+
# Print top 50
|
| 273 |
+
print(f'\n{"="*100}')
|
| 274 |
+
print(f'TOP 50 LANGUAGES BY LINEAR A TYPOLOGICAL SIMILARITY')
|
| 275 |
+
print(f'{"="*100}')
|
| 276 |
+
print(f'{"Rank":>4} {"Lang":>8} {"Score":>6} {"OpenSyl":>8} {"NoClustr":>8} {"FinalV":>8} {"CVRatio":>8} {"WordLen":>8} {"N_Forms":>8}')
|
| 277 |
+
print(f'{"-"*4:>4} {"-"*8:>8} {"-"*6:>6} {"-"*8:>8} {"-"*8:>8} {"-"*8:>8} {"-"*8:>8} {"-"*8:>8} {"-"*8:>8}')
|
| 278 |
+
for i, r in enumerate(results[:50], 1):
|
| 279 |
+
print(f'{i:>4} {r["Language"]:>8} {r["Linear_A_Score"]:.4f} '
|
| 280 |
+
f'{r["Open_Syllable_Ratio"]:.4f} {1-r["Cluster_Ratio"]:.4f} '
|
| 281 |
+
f'{r["Final_Vowel_Ratio"]:.4f} {r["Mean_CV_Ratio"]:.3f} '
|
| 282 |
+
f'{r["Mean_Word_Length"]:.2f} {r["N_Forms"]:>6}')
|
| 283 |
+
|
| 284 |
+
# Print bottom 10 for contrast
|
| 285 |
+
print(f'\n--- BOTTOM 10 (least Linear A-like) ---')
|
| 286 |
+
for i, r in enumerate(results[-10:], len(results)-9):
|
| 287 |
+
print(f'{i:>4} {r["Language"]:>8} {r["Linear_A_Score"]:.4f} '
|
| 288 |
+
f'{r["Open_Syllable_Ratio"]:.4f} {1-r["Cluster_Ratio"]:.4f} '
|
| 289 |
+
f'{r["Final_Vowel_Ratio"]:.4f} {r["Mean_CV_Ratio"]:.3f} '
|
| 290 |
+
f'{r["Mean_Word_Length"]:.2f} {r["N_Forms"]:>6}')
|
| 291 |
+
|
| 292 |
+
# Language family distribution in top 50
|
| 293 |
+
print(f'\n--- LANGUAGE FAMILIES IN TOP 50 ---')
|
| 294 |
+
# We'll use a simple heuristic based on ISO codes — for a proper family
|
| 295 |
+
# classification we'd need Glottolog, but let's at least note patterns
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
if __name__ == '__main__':
|
| 299 |
+
main()
|
scripts/build_linear_a_validation.py
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Build the Linear A Phonotactics Validation dataset.
|
| 4 |
+
|
| 5 |
+
Criteria for inclusion:
|
| 6 |
+
1. Both Lang_A and Lang_B have Linear_A_Score >= 0.80
|
| 7 |
+
(open syllable ratio, no clusters, final vowels, CV structure)
|
| 8 |
+
2. Confidence = "certain" (expert-determined, not contested)
|
| 9 |
+
3. Phono_Quality = "strong" (SCA Score >= 0.5, clear phonological similarity)
|
| 10 |
+
|
| 11 |
+
This produces a gold-standard dataset of proven cognate pairs between
|
| 12 |
+
languages whose phonotactics resemble Linear A. Used to validate
|
| 13 |
+
cognate detection models before applying them to Linear A.
|
| 14 |
+
|
| 15 |
+
Input:
|
| 16 |
+
- analysis/typology_linear_a.tsv (Linear A similarity scores per language)
|
| 17 |
+
- data/training/cognate_pairs/cognate_pairs_inherited.parquet (full dataset)
|
| 18 |
+
Output:
|
| 19 |
+
- data/training/cognate_pairs/linear_a_phonotactics_validation.parquet
|
| 20 |
+
"""
|
| 21 |
+
import csv
|
| 22 |
+
import sys
|
| 23 |
+
from pathlib import Path
|
| 24 |
+
|
| 25 |
+
if sys.stdout.encoding != 'utf-8':
|
| 26 |
+
sys.stdout.reconfigure(encoding='utf-8')
|
| 27 |
+
|
| 28 |
+
THRESHOLD = 0.80
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def main():
|
| 32 |
+
hf_dir = Path(__file__).parent.parent
|
| 33 |
+
|
| 34 |
+
# 1. Load typology scores
|
| 35 |
+
print('Loading Linear A typology scores...')
|
| 36 |
+
lang_scores = {}
|
| 37 |
+
with open(hf_dir / 'analysis' / 'typology_linear_a.tsv', encoding='utf-8') as f:
|
| 38 |
+
for row in csv.DictReader(f, delimiter='\t'):
|
| 39 |
+
lang_scores[row['Language']] = float(row['Linear_A_Score'])
|
| 40 |
+
|
| 41 |
+
qualified_langs = {l for l, s in lang_scores.items() if s >= THRESHOLD}
|
| 42 |
+
print(f' Languages with score >= {THRESHOLD}: {len(qualified_langs)}')
|
| 43 |
+
|
| 44 |
+
# 2. Load full Parquet
|
| 45 |
+
import pyarrow as pa
|
| 46 |
+
import pyarrow.parquet as pq
|
| 47 |
+
import pyarrow.compute as pc
|
| 48 |
+
|
| 49 |
+
print('Loading full inherited Parquet...')
|
| 50 |
+
table = pq.read_table(
|
| 51 |
+
hf_dir / 'data' / 'training' / 'cognate_pairs' / 'cognate_pairs_inherited.parquet'
|
| 52 |
+
)
|
| 53 |
+
print(f' Total rows: {table.num_rows:,}')
|
| 54 |
+
|
| 55 |
+
# 3. Filter: both languages qualified, certain confidence, strong phono
|
| 56 |
+
print('Filtering...')
|
| 57 |
+
lang_a = table['Lang_A'].to_pylist()
|
| 58 |
+
lang_b = table['Lang_B'].to_pylist()
|
| 59 |
+
conf = table['Confidence'].to_pylist()
|
| 60 |
+
phono = table['Phono_Quality'].to_pylist()
|
| 61 |
+
|
| 62 |
+
mask = []
|
| 63 |
+
for i in range(len(lang_a)):
|
| 64 |
+
keep = (
|
| 65 |
+
lang_a[i] in qualified_langs
|
| 66 |
+
and lang_b[i] in qualified_langs
|
| 67 |
+
and conf[i] == 'certain'
|
| 68 |
+
and phono[i] == 'strong'
|
| 69 |
+
)
|
| 70 |
+
mask.append(keep)
|
| 71 |
+
|
| 72 |
+
mask_arr = pa.array(mask)
|
| 73 |
+
filtered = table.filter(mask_arr)
|
| 74 |
+
print(f' Filtered rows: {filtered.num_rows:,}')
|
| 75 |
+
|
| 76 |
+
# 4. Add Linear_A_Score columns for both languages
|
| 77 |
+
scores_a = [lang_scores.get(la, 0.0) for la, m in zip(lang_a, mask) if m]
|
| 78 |
+
scores_b = [lang_scores.get(lb, 0.0) for lb, m in zip(lang_b, mask) if m]
|
| 79 |
+
|
| 80 |
+
filtered = filtered.append_column(
|
| 81 |
+
'Linear_A_Score_A', pa.array([round(s, 4) for s in scores_a], type=pa.float64())
|
| 82 |
+
)
|
| 83 |
+
filtered = filtered.append_column(
|
| 84 |
+
'Linear_A_Score_B', pa.array([round(s, 4) for s in scores_b], type=pa.float64())
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# 5. Write output
|
| 88 |
+
out_path = hf_dir / 'data' / 'training' / 'cognate_pairs' / 'linear_a_phonotactics_validation.parquet'
|
| 89 |
+
pq.write_table(filtered, str(out_path), compression='zstd', compression_level=3)
|
| 90 |
+
|
| 91 |
+
import os
|
| 92 |
+
size = os.path.getsize(out_path)
|
| 93 |
+
print(f'\n Written to: {out_path}')
|
| 94 |
+
print(f' Size: {size/1024/1024:.1f} MB')
|
| 95 |
+
|
| 96 |
+
# 6. Statistics
|
| 97 |
+
print(f'\n=== VALIDATION DATASET STATISTICS ===')
|
| 98 |
+
print(f'Total pairs: {filtered.num_rows:,}')
|
| 99 |
+
|
| 100 |
+
# Unique languages
|
| 101 |
+
langs = set()
|
| 102 |
+
fa = filtered['Lang_A'].to_pylist()
|
| 103 |
+
fb = filtered['Lang_B'].to_pylist()
|
| 104 |
+
for a in fa:
|
| 105 |
+
langs.add(a)
|
| 106 |
+
for b in fb:
|
| 107 |
+
langs.add(b)
|
| 108 |
+
print(f'Unique languages: {len(langs)}')
|
| 109 |
+
|
| 110 |
+
# Load family map
|
| 111 |
+
t2 = pq.read_table(str(hf_dir / 'data' / 'training' / 'metadata' / 'languages.parquet'))
|
| 112 |
+
iso_to_family = dict(zip(t2['ISO'].to_pylist(), t2['Family'].to_pylist()))
|
| 113 |
+
|
| 114 |
+
# Family distribution
|
| 115 |
+
fam_counts = {}
|
| 116 |
+
for l in langs:
|
| 117 |
+
fam = iso_to_family.get(l, 'unknown')
|
| 118 |
+
fam_counts[fam] = fam_counts.get(fam, 0) + 1
|
| 119 |
+
print(f'\nLanguage families:')
|
| 120 |
+
for fam, c in sorted(fam_counts.items(), key=lambda x: -x[1]):
|
| 121 |
+
print(f' {fam}: {c}')
|
| 122 |
+
|
| 123 |
+
# Source distribution
|
| 124 |
+
sources = filtered['Source'].to_pylist()
|
| 125 |
+
src_counts = {}
|
| 126 |
+
for s in sources:
|
| 127 |
+
src_counts[s] = src_counts.get(s, 0) + 1
|
| 128 |
+
print(f'\nSource distribution:')
|
| 129 |
+
for src, c in sorted(src_counts.items(), key=lambda x: -x[1]):
|
| 130 |
+
print(f' {src}: {c:,}')
|
| 131 |
+
|
| 132 |
+
# Score distribution
|
| 133 |
+
scores = filtered['Score'].to_pylist()
|
| 134 |
+
float_scores = [float(s) for s in scores if s and s != '-1']
|
| 135 |
+
if float_scores:
|
| 136 |
+
print(f'\nSCA Score distribution:')
|
| 137 |
+
print(f' Min: {min(float_scores):.4f}')
|
| 138 |
+
print(f' Max: {max(float_scores):.4f}')
|
| 139 |
+
print(f' Mean: {sum(float_scores)/len(float_scores):.4f}')
|
| 140 |
+
bins = [(0.5, 0.6), (0.6, 0.7), (0.7, 0.8), (0.8, 0.9), (0.9, 1.01)]
|
| 141 |
+
for lo, hi in bins:
|
| 142 |
+
n = sum(1 for s in float_scores if lo <= s < hi)
|
| 143 |
+
print(f' [{lo:.1f}, {hi:.1f}): {n:,}')
|
| 144 |
+
|
| 145 |
+
# Load names for top contributing languages
|
| 146 |
+
iso_to_name = {}
|
| 147 |
+
glot_path = hf_dir / 'data' / 'training' / 'raw' / 'glottolog_cldf' / 'languages.csv'
|
| 148 |
+
with open(glot_path, encoding='utf-8') as f:
|
| 149 |
+
for row in csv.DictReader(f):
|
| 150 |
+
iso = row.get('ISO639P3code', '').strip()
|
| 151 |
+
name = row.get('Name', '').strip()
|
| 152 |
+
if iso and name:
|
| 153 |
+
iso_to_name[iso] = name
|
| 154 |
+
|
| 155 |
+
# Top 20 languages by pair count
|
| 156 |
+
lang_pair_counts = {}
|
| 157 |
+
for a, b in zip(fa, fb):
|
| 158 |
+
lang_pair_counts[a] = lang_pair_counts.get(a, 0) + 1
|
| 159 |
+
lang_pair_counts[b] = lang_pair_counts.get(b, 0) + 1
|
| 160 |
+
print(f'\nTop 20 languages by pair count:')
|
| 161 |
+
for lang, c in sorted(lang_pair_counts.items(), key=lambda x: -x[1])[:20]:
|
| 162 |
+
name = iso_to_name.get(lang, '?')
|
| 163 |
+
fam = iso_to_family.get(lang, '?')
|
| 164 |
+
score = lang_scores.get(lang, 0)
|
| 165 |
+
print(f' {lang} ({name}) - {fam} - LA score {score:.4f} - {c:,} pairs')
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
if __name__ == '__main__':
|
| 169 |
+
main()
|