import sys, os, re, json sys.stdout.reconfigure(encoding='utf-8') base_dir = "resources/knowledge/original/원가계산/건설공사_표준품셈" merged_file = os.path.join(base_dir, "2026년_건설공사_표준품셈.md") with open(merged_file, 'r', encoding='utf-8') as f: merged_text = f.read() all_split_files = [] for root, dirs, files in os.walk(base_dir): for f in files: if f.endswith('.md') and not f.startswith('_') and '개정사항' not in f and '2026년_건설공사_표준품셈.md' not in f: all_split_files.append(os.path.join(root, f)) # Function to extract all tables with their unique text fingerprints def extract_table_blocks(text, label): lines = text.split('\n') tables = [] curr_lines = [] start_line = 0 for idx, line in enumerate(lines): line_num = idx + 1 l_strip = line.strip() if l_strip.startswith('|') and l_strip.endswith('|'): if not curr_lines: start_line = line_num curr_lines.append(l_strip) else: if curr_lines: # check divider if any(re.match(r'^\|(?:\s*:?-+:?\s*\|)+$', r) for r in curr_lines): # take first 2 rows as key header_str = " // ".join(curr_lines[:2]) tables.append({ "file": label, "start_line": start_line, "line_count": len(curr_lines), "header_key": header_str, "first_row": curr_lines[0], "sample_cell": curr_lines[min(2, len(curr_lines)-1)] }) curr_lines = [] if curr_lines and any(re.match(r'^\|(?:\s*:?-+:?\s*\|)+$', r) for r in curr_lines): tables.append({ "file": label, "start_line": start_line, "line_count": len(curr_lines), "header_key": " // ".join(curr_lines[:2]), "first_row": curr_lines[0], "sample_cell": curr_lines[min(2, len(curr_lines)-1)] }) return tables merged_tbls = extract_table_blocks(merged_text, "합본") split_tbls = [] for p in sorted(all_split_files): rel = os.path.relpath(p, base_dir) with open(p, 'r', encoding='utf-8') as f: t = f.read() split_tbls.extend(extract_table_blocks(t, rel)) print(f"Merged tables: {len(merged_tbls)}") print(f"Split tables: {len(split_tbls)}") # Match each split table in merged_text # If first_row and sample_cell not found together in merged_text truly_missing_in_merged = [] for st in split_tbls: # check if first_row exists in merged_text r0 = st['first_row'] # remove spacing differences norm_r0 = re.sub(r'\s+', '', r0) # Check if this row exists in any merged table found = False for mt in merged_tbls: norm_mt = re.sub(r'\s+', '', mt['first_row']) if norm_r0 == norm_mt: # check sample_cell too norm_sc = re.sub(r'\s+', '', st['sample_cell']) norm_msc = re.sub(r'\s+', '', mt['sample_cell']) if norm_sc == norm_msc or norm_sc[:15] in norm_msc or norm_msc[:15] in norm_sc: found = True break if not found: truly_missing_in_merged.append(st) print(f"\nTruly missing tables in 합본: {len(truly_missing_in_merged)}") for t in truly_missing_in_merged: print(f" - {t['file']} (L{t['start_line']}): {t['first_row'][:60]} | sample={t['sample_cell'][:40]}") with open('scratch/truly_missing_in_merged.json', 'w', encoding='utf-8') as f: json.dump(truly_missing_in_merged, f, ensure_ascii=False, indent=2)