import sys, os, re 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() def count_tables_in_text(text): lines = text.split('\n') tables = 0 curr = [] for l in lines: ls = l.strip() if ls.startswith('|') and ls.endswith('|'): curr.append(ls) else: if curr: if any(re.match(r'^\|(?:\s*:?-+:?\s*\|)+$', r) for r in curr): tables += 1 curr = [] if curr and any(re.match(r'^\|(?:\s*:?-+:?\s*\|)+$', r) for r in curr): tables += 1 return tables # Split merged_text by divisions: # 공통부문, 토목부문, 건축부문, 기계설비부문, 유지관리부문 div_names = ["공통부문", "토목부문", "건축부문", "기계설비부문", "유지관리부문"] # find division boundaries in merged div_spans = [] for dn in div_names: # search for '# ' or main header of division m = re.search(rf'(?:^|\n)#+\s*{dn}', merged_text) if not m: m = re.search(rf'(?:^|\n){dn}', merged_text) pos = m.start() if m else -1 div_spans.append((dn, pos)) print(f"Division spans in merged: {div_spans}") # Count per division folder in split 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)) split_by_div = {} for p in all_split_files: rel = os.path.relpath(p, base_dir) div = rel.split(os.sep)[0] with open(p, 'r', encoding='utf-8') as f: txt = f.read() c = count_tables_in_text(txt) split_by_div[div] = split_by_div.get(div, 0) + c for d, cnt in sorted(split_by_div.items()): print(f"Split {d}: {cnt} tables")