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

88 lines
3.3 KiB
Python

import os, sys, re
sys.stdout.reconfigure(encoding='utf-8')
ch9_path = "resources/knowledge/original/원가계산/건설공사_표준품셈/02_토목부문/제9장_측량.md"
with open(ch9_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
new_lines = []
fixed_tables = 0
# Common tasks lists in survey
known_task_patterns = [
["계획준비", "답사선점", "조표(매설)", "관 측", "계 산", "정리점검"],
["계획준비", "답사선점", "조표(매설)", "관측", "계산", "정리점검"],
["계획준비", "답사선점", "관 측", "계 산", "정리점검"],
["계획준비", "답사선점", "관측", "계산", "정리점검"],
["계획준비", "답사", "선점", "매설", "관측", "계산", "정리점검"],
["계획준비", "선점", "매설", "관측", "계산", "정리점검"],
]
for idx, line in enumerate(lines):
line_str = line.strip()
if not (line_str.startswith('|') and line_str.endswith('|')):
new_lines.append(line)
continue
cells = [c.strip() for c in line_str.split('|')[1:-1]]
# Check if first cell contains tasks
matched_tasks = None
c0 = cells[0]
# Try exact task sequence match
for pat in known_task_patterns:
pat_str = " ".join(pat)
if c0 == pat_str or c0.replace(" ", "") == "".join(pat).replace(" ", ""):
matched_tasks = pat
break
if not matched_tasks:
# Try generic tasks split if tokens are known tasks
toks = c0.split()
if len(toks) >= 4 and all(any(k in t for k in ["계획", "준비", "답사", "선점", "조표", "매설", "관측", "계산", "정리", "점검", "수준", "측량", "항공", "도화", "편집"]) for t in toks):
matched_tasks = toks
if matched_tasks:
N = len(matched_tasks)
# Check if following data columns can be split into N tokens
# Some columns might have single value (like 비고) or N tokens
can_split = True
col_splits = {}
for c_idx in range(1, len(cells)):
val = cells[c_idx]
if not val or val == "-":
col_splits[c_idx] = [val] * N
continue
v_toks = val.split()
if len(v_toks) == N:
col_splits[c_idx] = v_toks
elif c_idx == len(cells) - 1: # Last column is often 비고
col_splits[c_idx] = [val] + [""] * (N - 1)
else:
# Cannot cleanly split this column
can_split = False
break
if can_split:
# Generate N rows
for r_idx in range(N):
row_cells = [matched_tasks[r_idx]]
for c_idx in range(1, len(cells)):
row_cells.append(col_splits[c_idx][r_idx])
new_line_str = "| " + " | ".join(row_cells) + " |\n"
new_lines.append(new_line_str)
fixed_tables += 1
print(f"L{idx+1}: Fixed survey table with {N} tasks: {cells[0][:30]}...")
continue
new_lines.append(line)
print(f"\nTotal survey task tables fixed in 제9장_측량.md: {fixed_tables}")
if fixed_tables > 0:
with open(ch9_path, 'w', encoding='utf-8') as f:
f.writelines(new_lines)
print("Saved 제9장_측량.md successfully.")