From c919e0e8676505ba7eaeea33508213d60289c91f Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Thu, 3 Sep 2026 12:42:53 +0530 Subject: [PATCH] refactor: eliminate all hardcoded tags, replacing with generic algorithmic equipment & line detection rules --- src/agents/compiler.py | 33 +++++++++++++++++++++------------ src/utils/line_tracer.py | 4 ++-- src/utils/tag_classifier.py | 9 ++------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/agents/compiler.py b/src/agents/compiler.py index 1c17874..d7095a5 100644 --- a/src/agents/compiler.py +++ b/src/agents/compiler.py @@ -228,34 +228,43 @@ class CompilerAgent(BaseAgent): seen_equip: dict = {} # canonical key → EquipmentItem (deduplication) eq_tags = [t for t in texts if t["classification"] == "EQUIPMENT_TAG"] for eq in eq_tags: - tag = eq["tag"] - # Clean OCR digit noise (e.g. 26-CX-90111 -> 26-CX-9011) - tag_clean = re.sub(r'(26-CX-9011)1$', r'\1', tag) - canon_key = re.sub(r'^\d{2,3}-', '', tag_clean.upper()) + tag = eq["tag"].strip() + canon_key = re.sub(r'^\d{2,3}-', '', tag.upper()) - # Deduplicate exact canonical keys (e.g. merge bare KA-901 into 26-KA-901) - # Parallel trains (HA-911-C01 vs HA-911-C02) and Motor drivers (KA-901-M01) are preserved as distinct items + # Generic algorithmic deduplication: + # 1. Exact canonical match (e.g. bare KA-901 merged into 26-KA-901) + # 2. Single-digit OCR run-on collision (e.g. CX-9011 vs CX-90111 where line size was OCR-concatenated) + matched_key = None if canon_key in seen_equip: - if len(tag_clean) > len(seen_equip[canon_key].tag): - seen_equip[canon_key].tag = tag_clean + matched_key = canon_key + else: + for k in seen_equip: + if (canon_key.startswith(k) and len(canon_key) == len(k) + 1 and canon_key[-1].isdigit()) or \ + (k.startswith(canon_key) and len(k) == len(canon_key) + 1 and k[-1].isdigit()): + matched_key = k + break + + if matched_key: + if len(tag) > len(seen_equip[matched_key].tag) and len(tag) <= len(seen_equip[matched_key].tag) + 3: + seen_equip[matched_key].tag = tag continue coords = None for sym in symbols: - if sym.get("inferred_tag") in (tag, tag_clean): + if sym.get("inferred_tag") == tag: coords = [sym["ymin"], sym["xmin"], sym["ymax"], sym["xmax"]] break - code_match = re.search(r'([A-Z]{1,3})(?=-?\d)', tag_clean, re.IGNORECASE) + code_match = re.search(r'([A-Z]{1,3})(?=-?\d)', tag, re.IGNORECASE) eq_code = code_match.group(1).upper() if code_match else "" eq_type = self._ISA_EQUIP_DESC.get(eq_code, "Generic Equipment") # Detect Motor Drivers (e.g. 26-KA-901-M01) - if re.search(r'-M\d{1,2}$', tag_clean, re.IGNORECASE) or tag_clean.endswith('-MOTOR'): + if re.search(r'-M\d{1,2}$', tag, re.IGNORECASE) or tag.endswith('-MOTOR'): eq_type = "Motor / Driver" for sym in symbols: - if sym.get("inferred_tag") in (tag, tag_clean) and sym.get("symbol_type"): + if sym.get("inferred_tag") == tag and sym.get("symbol_type"): st = sym["symbol_type"].replace('_', ' ').title() if "Equipment" not in st and "Unknown" not in st: eq_type = st diff --git a/src/utils/line_tracer.py b/src/utils/line_tracer.py index 56c19bd..7675668 100644 --- a/src/utils/line_tracer.py +++ b/src/utils/line_tracer.py @@ -225,8 +225,8 @@ def trace_lines_and_connections( # 6. Global drawing equipment/line fallback (guarantees zero orphan instruments) if not target_entity: if equipment: - area_prefix = itag.split('-')[0] if '-' in itag and itag.split('-')[0].isdigit() else "26" - same_area_eq = [e.get("tag") for e in equipment if e.get("tag", "").startswith(area_prefix)] + area_prefix = itag.split('-')[0] if '-' in itag and itag.split('-')[0].isdigit() else "" + same_area_eq = [e.get("tag") for e in equipment if area_prefix and e.get("tag", "").startswith(area_prefix)] target_entity = same_area_eq[0] if same_area_eq else equipment[0].get("tag") elif line_items: target_entity = line_items[0].get("tag") diff --git a/src/utils/tag_classifier.py b/src/utils/tag_classifier.py index 01d269d..02d6203 100644 --- a/src/utils/tag_classifier.py +++ b/src/utils/tag_classifier.py @@ -641,8 +641,8 @@ def classify_paddle_results( found[tag] = _make_item(tag, 'INSTRUMENT_TAG', conf, it_copy) item_added = True - # Inverted Suction Strainers (e.g., 9002 S 26 -> 26-ST-9002 or Note 19 SUCTION STRAINER) - for m in re.finditer(r'\b(\d{4})\s*[-/]?\s*S\s*[-/]?\s*(\d{2})\b', t, re.IGNORECASE): + # Inverted Suction Strainers (e.g., 9002 S 26 -> 26-ST-9002, 1001 ST 43 -> 43-ST-1001) + for m in re.finditer(r'\b(\d{3,5})\s*[-/]?\s*S(?:T)?\s*[-/]?\s*(\d{2,3})\b', t, re.IGNORECASE): seq = m.group(1) unit = m.group(2) st_tag = f"{unit}-ST-{seq}" @@ -653,11 +653,6 @@ def classify_paddle_results( found[st_tag] = _make_item(st_tag, 'EQUIPMENT_TAG', conf, it_copy) item_added = True - if ("SUCTION STRAINER" in t.upper() or "SUCTIONSTRAINER" in t.upper()) and "26-ST-9002" not in found: - it_copy = dict(item) - found["26-ST-9002"] = _make_item("26-ST-9002", 'EQUIPMENT_TAG', conf, it_copy) - item_added = True - # Project-prefix tags (instruments + equipment) for m in _PROJECT_TAG_SEARCH.finditer(t): full_tag = m.group(1).upper()