Download src/macjev/v4data/split_isolation.py from VTN-TokenFactory/Telasap-0.8B-v1.0: direct link, hf CLI and curl.
- Browser
- Download file 3.15 kB
-
https://huggingface.co/VTN-TokenFactory/Telasap-0.8B-v1.0/resolve/main/src/macjev/v4data/split_isolation.py
- Command line
-
hf download hf://VTN-TokenFactory/Telasap-0.8B-v1.0/src/macjev/v4data/split_isolation.py
-
curl -L -o split_isolation.py https://huggingface.co/VTN-TokenFactory/Telasap-0.8B-v1.0/resolve/main/src/macjev/v4data/split_isolation.py
3.15 kB
| """Conservative group-closure isolation, using no benchmark labels. | |
| Exact and punctuation/case-normalized states/inputs, explicit source identities, | |
| and rare long state leaves join whole groups. Empty states never join unrelated | |
| questions. A transitive match reserves the entire connected group for the highest | |
| priority split; it never moves a held-out row into training. | |
| """ | |
| import hashlib | |
| import json | |
| from collections import defaultdict | |
| from macjev.assemble import iter_strings,near_key,near_norm,near_state_key | |
| from macjev.schema import content_hash | |
| PRIORITY={'test':0,'dev':1,'cal':2,'train':3} | |
| def h(text):return hashlib.sha256(text.encode()).hexdigest() | |
| class Isolation: | |
| def __init__(self): | |
| self.parent={};self.priority={};self.keys={};self.leaves=defaultdict(set) | |
| def root(self,g): | |
| p=self.parent[g] | |
| if p!=g:self.parent[g]=self.root(p) | |
| return self.parent[g] | |
| def join(self,a,b): | |
| a,b=self.root(a),self.root(b) | |
| if a==b:return | |
| # Stable root ensures deterministic results independent of source order. | |
| if b<a:a,b=b,a | |
| self.parent[b]=a | |
| self.priority[a]=min(self.priority[a],self.priority[b]) | |
| def add(self,r): | |
| g=r['group_id'];s=r['split'] | |
| if g not in self.parent:self.parent[g]=g;self.priority[g]=PRIORITY[s] | |
| self.priority[self.root(g)]=min(self.priority[self.root(g)],PRIORITY[s]) | |
| st=r.get('state');q=r.get('question') or {} | |
| keys=['row:'+near_key(st,q)] if q.get('t') and 'ins' in q else [] | |
| empty=st in (None,'','{}',{}) | |
| if not empty: | |
| keys+=['state:'+content_hash(st),'near_state:'+near_state_key(st)] | |
| if empty and isinstance(q.get('ins'),str) and len(near_norm(q['ins']))>=80: | |
| # Also isolate text relocated from a state into a self-contained question. | |
| keys+=['near_state:'+near_state_key(q['ins'])] | |
| prov=r.get('provenance') or {};meta=r.get('meta') or {} | |
| dataset=prov.get('dataset') or r.get('source') | |
| for k in ('origin_id','original_id','source_id','source_group_id','document_id','dialogue_id', | |
| 'hpqa_id','contest_number','query_id'): | |
| value=prov.get(k,meta.get(k)) | |
| if value is not None:keys.append('origin:'+h(json.dumps([dataset,k,value],sort_keys=True))) | |
| if prov.get('file') is not None and prov.get('line') is not None: | |
| keys.append('source_line:'+h(json.dumps([dataset,prov['file'],prov['line']]))) | |
| for key in keys: | |
| if key in self.keys:self.join(g,self.keys[key]) | |
| else:self.keys[key]=g | |
| for text in iter_strings(st): | |
| n=near_norm(text) | |
| if len(n)>=160: | |
| key=h(n) | |
| if len(self.leaves[key])<=8:self.leaves[key].add(g) | |
| def finish(self): | |
| for groups in self.leaves.values(): | |
| if len(groups)<=8: | |
| groups=sorted(groups) | |
| for g in groups[1:]:self.join(groups[0],g) | |
| self.leaves.clear();self.keys.clear() | |
| def allowed(self,r): | |
| return self.priority[self.root(r['group_id'])]==PRIORITY[r['split']] | |