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

112 lines
5.4 KiB
Python

import os, sys, re
sys.stdout.reconfigure(encoding='utf-8')
target_files = [
# 02_토목부문
'resources/knowledge/original/원가계산/건설공사_표준품셈/02_토목부문/제1장_도로포장공사.md',
'resources/knowledge/original/원가계산/건설공사_표준품셈/02_토목부문/제2장_하천공사.md',
'resources/knowledge/original/원가계산/건설공사_표준품셈/02_토목부문/제3장_터널공사.md',
'resources/knowledge/original/원가계산/건설공사_표준품셈/02_토목부문/제4장_궤도공사.md',
'resources/knowledge/original/원가계산/건설공사_표준품셈/02_토목부문/제5장_강구조공사.md',
'resources/knowledge/original/원가계산/건설공사_표준품셈/02_토목부문/제6장_관부설및접합공사.md',
'resources/knowledge/original/원가계산/건설공사_표준품셈/02_토목부문/제7장_항만공사.md',
'resources/knowledge/original/원가계산/건설공사_표준품셈/02_토목부문/제8장_지반조사.md',
'resources/knowledge/original/원가계산/건설공사_표준품셈/02_토목부문/제9장_측량.md',
# 05_유지관리부문
'resources/knowledge/original/원가계산/건설공사_표준품셈/05_유지관리부문/제1장_공통.md',
'resources/knowledge/original/원가계산/건설공사_표준품셈/05_유지관리부문/제2장_토목.md',
'resources/knowledge/original/원가계산/건설공사_표준품셈/05_유지관리부문/제3장_건축.md',
'resources/knowledge/original/원가계산/건설공사_표준품셈/05_유지관리부문/제4장_기계설비.md',
# 04_기계설비부문 제13장
'resources/knowledge/original/원가계산/건설공사_표준품셈/04_기계설비부문/제13장_플랜트설비공사.md',
]
print("=== Deep Scan for Multi-item Rows Duplicating Preceding Items ===")
suspects = []
for fpath in target_files:
fname = os.path.basename(fpath)
with open(fpath, 'r', encoding='utf-8') as f:
lines = f.readlines()
tables = []
cur_t = []
cur_start = 0
for idx, l in enumerate(lines):
line_str = l.strip()
if line_str.startswith('|') and line_str.endswith('|'):
if not cur_t:
cur_start = idx + 1
cur_t.append((idx + 1, line_str))
else:
if cur_t:
tables.append((cur_start, cur_t))
cur_t = []
if cur_t:
tables.append((cur_start, cur_t))
for t_start, t_rows in tables:
data_rows = []
for lno, r_str in t_rows:
cells = [c.strip() for c in r_str.split('|')[1:-1]]
if all(re.match(r'^:?-+:?$', c) for c in cells if c):
continue
if cells and cells[0] in ["비고", "비 고", "주", "[주]"]:
continue
# Header check
if any(c in ["구 분", "구분", "품 명", "품명", "규 격", "규격", "단 위", "단위", "수 량", "수량", "직 종", "직종"] for c in cells):
continue
data_rows.append((lno, r_str, cells))
if len(data_rows) < 3:
continue
# Collect individual item names from preceding rows (cell 0 or cell 1)
preceding_items = {} # item_name -> list of line numbers
for r_idx, (lno, r_str, cells) in enumerate(data_rows):
c0 = cells[0] if len(cells) > 0 else ""
c1 = cells[1] if len(cells) > 1 else ""
# Check if this row has multi-item in c0 or c1
# E.g., "시 멘 트 전 주 〃" or "도 복 장 강 관 〃 〃 〃 〃"
# Normalize whitespace: "시 멘 트" -> "시멘트"
# If multiple items:
c0_clean = re.sub(r'\s+', '', c0)
c1_clean = re.sub(r'\s+', '', c1)
# Check against preceding items
# Preceding items normalized
matched_items = []
for p_item, p_lnos in preceding_items.items():
if len(p_item) >= 2 and p_item in c0_clean and p_item != c0_clean:
matched_items.append((p_item, p_lnos))
elif len(p_item) >= 2 and p_item in c1_clean and p_item != c1_clean:
matched_items.append((p_item, p_lnos))
if len(matched_items) >= 2:
# 2 or more distinct preceding items appear in this single row's item cell!
suspects.append({
'file': fname,
'line': lno,
'table_start': t_start,
'row': r_str[:90],
'matched_preceding_items': matched_items
})
# Add single items from this row
# If c0 looks like a single item (not too many spaces/words)
words0 = c0.split()
if 1 <= len(words0) <= 2 and len(c0_clean) >= 2:
preceding_items.setdefault(c0_clean, []).append(lno)
words1 = c1.split()
if 1 <= len(words1) <= 2 and len(c1_clean) >= 2:
preceding_items.setdefault(c1_clean, []).append(lno)
print(f"Total suspects found: {len(suspects)}")
for s in suspects:
print(f"\n[{s['file']}:L{s['line']}] (Table starting at L{s['table_start']})")
print(f" Suspect Row: {s['row']}")
print(f" Matches preceding items: {s['matched_preceding_items']}")