Files
Aislo/resources/tester/scratch/count_tables_by_chapter.py
T

48 lines
1.5 KiB
Python

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()
# Helper to count tables in text
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
# Count tables in each split file
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))
total_split_tables = 0
split_counts = {}
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()
c = count_tables_in_text(t)
split_counts[rel] = c
total_split_tables += c
print(f"Total split tables: {total_split_tables}")
merged_total = count_tables_in_text(merged_text)
print(f"Total merged tables: {merged_total}")