import json,pathlib,re,hashlib,collections B=json.load(open('plans/extract-remaining-elements/batch_1.json')) results=[] def definitions(s):return collections.Counter(re.findall(r'^#{3,6}\s+(R-[A-Za-z0-9-]+)',s,re.M)) def routes(s):return set(re.findall(r'^### (5\.P\d+)\s',s,re.M)) def blocks(s): out={} for c in re.split(r'(?=^#{3,6} R-)',s,flags=re.M): m=re.match(r'^#{3,6}\s+(R-[A-Za-z0-9-]+)',c) if m:out[m[1]]=c return out for r in B: p=pathlib.Path(r['source']);q=pathlib.Path(r['output']);a=p.read_text();b=q.read_text();ab=blocks(a);bb=blocks(b) # Reinstate any authority explanation that itself qualifies applicability rather than merely listing citations. for rid,c in ab.items(): m=re.search(r'^- \*\*규범적 근거\*\*[^\n]*(?:\n(?![-#\n]).*)*',c,re.M) if m and re.search(r'UNVERIFIED|UNKNOWN|QUALIFIED|유추|한정|직접 판례|미확인',m[0]) and m[0] not in bb[rid]: h=bb[rid].split('\n')[0];b=b.replace(h+'\n',h+'\n'+m[0]+'\n',1) q.write_text(b);bb=blocks(b) auth_lost=[] for rid,c in ab.items(): for x in re.findall(r'^- \*\*(?:권위 ID|근거 ID)\*\*.*',c,re.M): # c extends until next R heading, so some heading-boundary material can contain next path intro; comparison is conservative. if x not in bb[rid]:auth_lost.append([rid,x]) table_bad=[];in_table=False;width=None for k,line in enumerate(b.splitlines(),1): if line.startswith('|'): count=len(re.split(r'(?