import json,pathlib,re B=json.load(open('plans/extract-remaining-elements/batch_1.json')) N=json.load(open('plans/extract-remaining-elements/agent1_notes.json')) DROP={'2.1','3.4','3.5','9.4','10.4'} for i,r in enumerate(B): p=pathlib.Path(r['output']);s=p.read_text(); parts=re.split(r'(?=^#{2,3} )',s,flags=re.M);out=[] for c in parts: h=c.split('\n')[0] if h.startswith('## §12.'):continue m=re.match(r'^### (\d+\.\d+)\b',h) if m and m[1] in DROP and not(i==1 and m[1]=='10.4'):continue # Per-element authority identifier retains source connection; prose citation duplicated elsewhere. blocks=re.split(r'(?=^#{4,6} R-)',c,flags=re.M) for j,z in enumerate(blocks): if re.search(r'\*\*(?:권위 ID|근거 ID)\*\*',z): z=re.sub(r'^- \*\*규범적 근거\*\*\s*[::].*(?:\n(?![-#\n]).*)*','',z,flags=re.M) blocks[j]=z c=''.join(blocks) # Remove empty scaffolding and cardinality assertions / cross-section generation instructions. c=re.sub(r'^.*(?:기계 게이트|스크립트가 투영|손으로 고치지|게이트 S[0-9]|린트 L-).*$','',c,flags=re.M) c=re.sub(r'^.*(?:각 블록이 존재한다|블록이 존재해야 한다).*$','',c,flags=re.M) out.append(c) s=''.join(out).strip() s+='\n\n## §12. 요건 선택·분류에 관련된 원문 유보\n\n'+N[r['case']]+'\n' s=re.sub(r'\n{3,}','\n\n',s) p.write_text(s) print(r['case'],round(100*(1-p.stat().st_size/r['bytes']),1))