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

92 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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_cnt = 0
tech_words = [
"특급기술자", "고급기술자", "중급기술자", "초급기술자", "초급기능사(측량)", "초급기능사", "인부", "측량사", "측량사보"
]
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]]
if len(cells) < 3 or cells[0] in ["계", "합계"]:
new_lines.append(line)
continue
# Check if first cell contains multiple technician grades
c0 = cells[0]
c0_clean = c0.replace(" ", "")
matched_grades = []
temp_s = c0_clean
for tw in sorted(tech_words, key=len, reverse=True):
while tw in temp_s:
matched_grades.append(tw)
temp_s = temp_s.replace(tw, "", 1)
if len(matched_grades) >= 2 and temp_s == "":
# Reconstruct in order
ordered_grades = []
pos = 0
while pos < len(c0_clean):
for tw in sorted(tech_words, key=len, reverse=True):
if c0_clean[pos:].startswith(tw):
ordered_grades.append(tw)
pos += len(tw)
break
N = len(ordered_grades)
# Check column 1 (수량/산식)
# Often contains N formulas ending with = number
# e.g. "3×10/16×1.2×1.12= 2.52 24.5×10/16..."
c1 = cells[1]
# Match formulas like "...= <num>"
f_matches = list(re.finditer(r'(.*?=\s*\d+(?:\.\d+)?)\s*(?=[^=]*=\s*\d+|$)', c1))
c1_parts = [m.group(1).strip() for m in f_matches if m.group(1).strip()]
# Check column 2 (단가)
c2 = cells[2] if len(cells) > 2 else ""
c2_toks = c2.split()
# Check column 3 (금액)
c3 = cells[3] if len(cells) > 3 else ""
w_matches = list(re.finditer(r'(W\s*=\s*[^W]+?)(?=\s*W\s*=|(?:\s*1\s+)?$)', c3))
c3_parts = [m.group(1).strip() for m in w_matches if m.group(1).strip()]
if len(c1_parts) == N:
# We can split!
for r_idx in range(N):
g_name = ordered_grades[r_idx]
qty_val = c1_parts[r_idx]
rate_val = c2_toks[r_idx] if len(c2_toks) == N else (c2_toks[0] if c2_toks else "")
amt_val = c3_parts[r_idx] if len(c3_parts) == N else ""
row_cells = [g_name, qty_val, rate_val, amt_val]
# If there were more columns, append them
for extra_c in range(4, len(cells)):
row_cells.append("" if r_idx > 0 else cells[extra_c])
new_line_str = "| " + " | ".join(row_cells) + " |\n"
new_lines.append(new_line_str)
fixed_cnt += 1
print(f"L{idx+1}: Fixed survey calc table with {N} grades: {c0[:40]}")
continue
new_lines.append(line)
print(f"\nTotal survey calc tables fixed: {fixed_cnt}")
if fixed_cnt > 0:
with open(ch9_path, 'w', encoding='utf-8') as f:
f.writelines(new_lines)
print("Saved 제9장_측량.md successfully.")