import sys, os, re, json sys.stdout.reconfigure(encoding='utf-8') with open('scratch/confirmed_attached_headers.json', 'r', encoding='utf-8') as f: fixes = json.load(f) # Group fixes by file fixes_by_file = {} for fix in fixes: f = fix['file'] if f not in fixes_by_file: fixes_by_file[f] = [] fixes_by_file[f].append(fix) print(f"Files to fix: {len(fixes_by_file)}") # Test on 제1장_적용기준.md test_file = "01_공통부문\\제1장_적용기준.md" base_dir = "resources/knowledge/original/원가계산/건설공사_표준품셈" fpath = os.path.join(base_dir, test_file) with open(fpath, 'r', encoding='utf-8') as f: text = f.read() lines = text.split('\n') print(f"Original lines in {test_file}: {len(lines)}") # We will apply insertion from end to beginning or per line file_fixes = fixes_by_file[test_file] print(f"Fixes for {test_file}: {len(file_fixes)}") # For each line, insert newline before sec_num # Sort fixes by line and pos descending file_fixes_sorted = sorted(file_fixes, key=lambda x: (x['line'], x['matched_pos']), reverse=True) modified_lines = list(lines) for fix in file_fixes_sorted: l_idx = fix['line'] - 1 sec_num = fix['sec_num'] # Check if sec_num still present in line cur_line = modified_lines[l_idx] # We want to replace something like "...한다.1-2-2" with "...한다.\n1-2-2" # Find the exact pattern pattern = re.compile(rf'([^\s#\-\*\>\|\(‘\'"\[])({re.escape(sec_num)}\s+)') # replace first occurrence from right or exact m = pattern.search(cur_line) if m: new_line = cur_line[:m.start(2)] + '\n' + cur_line[m.start(2):] # split if newline inserted modified_lines[l_idx] = new_line else: print(f"Pattern not found in line {fix['line']}: {sec_num}") # Flatten modified_lines new_text = '\n'.join(modified_lines) new_lines = new_text.split('\n') print(f"New lines count: {len(new_lines)} (added {len(new_lines) - len(lines)} lines)") # Show sample diffs print("\nSample restored headers in 제1장:") for idx, l in enumerate(new_lines): for fix in file_fixes[:5]: if l.startswith(fix['sec_num']): print(f"Line {idx+1}: {repr(l[:60])}")