from pathlib import Path import json,re,hashlib ROOT=Path.cwd() BASE=ROOT/'plans/extract-remaining-elements' batch=json.loads((BASE/'batch_3.json').read_text()) case_notes=json.loads((BASE/'agent3_case_notes.json').read_text()) def clean(s): s=re.sub(r'\[([^\]]+)\]\(https?://[^)]+\)',r'\1',s) s=re.sub(r'https?://[^\s<>|)]+','',s) s=re.sub(r'(?m)^---\s*$','',s) return re.sub(r'\n{3,}','\n\n',s).strip() def pieces(t): matches=list(re.finditer(r'^#{2,4} .+$',t,re.M)) return [(m.group(),t[m.end():matches[i+1].start() if i+1 원문: [요건사실론_{x["case"]}_v1_edit.md](../raw/{src.name})', '> 원문에 기재된 대한민국 민사청구의 요건·적용 조건·예외·주장증명책임을 추출한 검토용 자료다. 법령 기준일 및 검증 표지는 원문에서 승계하며, 이번 편집에서 법령·판례를 독립적으로 재검증하지 않았다. 구체적 사건의 사실·적용시점은 별도로 대조한다.', '> 절 번호와 요건·항변·권위 ID는 원문 연결을 위해 유지한다. 생략된 절을 가리키는 참조는 원문의 해당 절을 뜻한다. 원문 총괄표보다 개별 요건의 조건과 말미 유보사항을 함께 적용한다.','']) # Retain routing and exclusions that decide which elements can be used. out.append('## 청구경로와 적용 범위') out.extend(['### 적용 경로 선택',case_notes[x['case']]['scope']+' (원문 §1·§4·§5·§12 대조 요약)']) for n in ['1.1','1.2','1.4']:out.append(getsection(t,n)) out.append('## 주문 특정·청구 조건·기간') for n in ['2.2','2.3','3.1','3.2','3.3']:out.append(getsection(t,n)) sec4=t[t.index('## §4.'):t.index('## §6.')] # Redundant element inventories are replaced by the actual element records. for h,b in pieces(sec4): if h.startswith('### 4.1') or h.startswith('### 4.3'):continue if h.startswith('## §4.') or h.startswith('## §5.'): out.extend([h,b]);continue if re.match(r'#### R-',h):b=fields(b,'element') # Remove only fenced duplicate requirement inventories, not prose criteria. b=re.sub(r'```[^\n]*\n(?:R-[^\n]*\n)+```','',b) b=re.sub(r'(?m)^\*\*요건 목록\*\*\s*$','',b) out.extend([h,b]) s6=t[t.index('## §6.'):t.index('## §7.')] out.append(s6) # §7 includes potentially lossy summaries; keep target links only and rely # on full conditional burden text above. s7=t[t.index('## §7.'):t.index('## §8.')] rows=[l for l in s7.splitlines() if l.startswith('| R-')] if rows: out.extend(['## 요건별 대응 항변 연결','| 요건 ID | 대응 항변 ID |','|---|---|']) for r in rows: cols=[c.strip() for c in r.strip('|').split('|')] out.append('| '+cols[0]+' | '+cols[-1]+' |') out.append('## 요건 분류와 미해결 사항') out.append(getsection(t,'12.6').replace('### 12.6 학설','### 원문 §12.6: 요건 분류에 관련된 견해')) out.append('### 원문 §12.7에서 승계한 적용상 한계') out.extend('- '+s for s in case_notes[x['case']]['limits']) out.append(uncertain_authorities(t,'\n'.join(out))) text='\n\n'.join(s for s in out if s.strip()) # Preserve front matter, but collapse superfluous blank lines in body. front,body=text.split('# 요건사실 항목:',1) return re.sub(r'\n\n','\n',front).rstrip()+'\n\n# 요건사실 항목: '+clean(body)+'\n' if __name__=='__main__': for x in batch: p=ROOT/x['output'] if p.exists():raise RuntimeError('Existing output: '+str(p)) p.write_text(extract(x)) print('Drafts created:',len(batch))