"""Deterministic, source-grouped splits with explicit provenance and exclusion input.""" import argparse,hashlib,json,unicodedata from pathlib import Path def normalized(s): return ' '.join(unicodedata.normalize('NFKC',s).casefold().split()) def source_key(row): return hashlib.sha256(normalized(row['source']).encode()).hexdigest() def document_key(row): document=row.get('document_id') if document is None:return None identity=[row.get('provenance',''),row.get('revision',''),str(document)] return hashlib.sha256(json.dumps(identity,ensure_ascii=False).encode()).hexdigest() def prepare(rows, excluded=()): seen=set(); partitions={'train':[],'validation':[],'test':[]} excluded=list(excluded); blocked={source_key(r) for r in excluded} blocked_docs={document_key(r) for r in excluded if document_key(r) is not None} accepted=[];parent={} def find(key): parent.setdefault(key,key) root=key while parent[root]!=root:root=parent[root] while parent[key]!=key: nxt=parent[key];parent[key]=root;key=nxt return root def union(a,b): a,b=find(a),find(b) if a!=b:parent[max(a,b)]=min(a,b) for row in rows: if row.get('usage')=='evaluation_only' or row.get('dataset')=='FLORES+': raise ValueError('Evaluation-only benchmarks cannot enter training preparation') required=('source','target','source_lang','target_lang','license','provenance','revision') if any(not isinstance(row.get(k),str) or not row[k].strip() for k in required):continue if any('<|plamo:' in row[k] or '\x00' in row[k] for k in ('source','target')):continue key=source_key(row);source_node='source:'+key find(source_node) doc=document_key(row) if doc is not None:union(source_node,'doc:'+doc) pair=(key,normalized(row['target']),row['source_lang'],row['target_lang']) ratio=len(row['target'])/max(len(row['source']),1) if pair in seen or not 0.05