refactor: eliminate all hardcoded tags, replacing with generic algorithmic equipment & line detection rules
This commit is contained in:
+21
-12
@@ -228,34 +228,43 @@ class CompilerAgent(BaseAgent):
|
|||||||
seen_equip: dict = {} # canonical key → EquipmentItem (deduplication)
|
seen_equip: dict = {} # canonical key → EquipmentItem (deduplication)
|
||||||
eq_tags = [t for t in texts if t["classification"] == "EQUIPMENT_TAG"]
|
eq_tags = [t for t in texts if t["classification"] == "EQUIPMENT_TAG"]
|
||||||
for eq in eq_tags:
|
for eq in eq_tags:
|
||||||
tag = eq["tag"]
|
tag = eq["tag"].strip()
|
||||||
# Clean OCR digit noise (e.g. 26-CX-90111 -> 26-CX-9011)
|
canon_key = re.sub(r'^\d{2,3}-', '', tag.upper())
|
||||||
tag_clean = re.sub(r'(26-CX-9011)1$', r'\1', tag)
|
|
||||||
canon_key = re.sub(r'^\d{2,3}-', '', tag_clean.upper())
|
|
||||||
|
|
||||||
# Deduplicate exact canonical keys (e.g. merge bare KA-901 into 26-KA-901)
|
# Generic algorithmic deduplication:
|
||||||
# Parallel trains (HA-911-C01 vs HA-911-C02) and Motor drivers (KA-901-M01) are preserved as distinct items
|
# 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 canon_key in seen_equip:
|
||||||
if len(tag_clean) > len(seen_equip[canon_key].tag):
|
matched_key = canon_key
|
||||||
seen_equip[canon_key].tag = tag_clean
|
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
|
continue
|
||||||
|
|
||||||
coords = None
|
coords = None
|
||||||
for sym in symbols:
|
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"]]
|
coords = [sym["ymin"], sym["xmin"], sym["ymax"], sym["xmax"]]
|
||||||
break
|
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_code = code_match.group(1).upper() if code_match else ""
|
||||||
eq_type = self._ISA_EQUIP_DESC.get(eq_code, "Generic Equipment")
|
eq_type = self._ISA_EQUIP_DESC.get(eq_code, "Generic Equipment")
|
||||||
|
|
||||||
# Detect Motor Drivers (e.g. 26-KA-901-M01)
|
# 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"
|
eq_type = "Motor / Driver"
|
||||||
|
|
||||||
for sym in symbols:
|
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()
|
st = sym["symbol_type"].replace('_', ' ').title()
|
||||||
if "Equipment" not in st and "Unknown" not in st:
|
if "Equipment" not in st and "Unknown" not in st:
|
||||||
eq_type = st
|
eq_type = st
|
||||||
|
|||||||
@@ -225,8 +225,8 @@ def trace_lines_and_connections(
|
|||||||
# 6. Global drawing equipment/line fallback (guarantees zero orphan instruments)
|
# 6. Global drawing equipment/line fallback (guarantees zero orphan instruments)
|
||||||
if not target_entity:
|
if not target_entity:
|
||||||
if equipment:
|
if equipment:
|
||||||
area_prefix = itag.split('-')[0] if '-' in itag and itag.split('-')[0].isdigit() else "26"
|
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 e.get("tag", "").startswith(area_prefix)]
|
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")
|
target_entity = same_area_eq[0] if same_area_eq else equipment[0].get("tag")
|
||||||
elif line_items:
|
elif line_items:
|
||||||
target_entity = line_items[0].get("tag")
|
target_entity = line_items[0].get("tag")
|
||||||
|
|||||||
@@ -641,8 +641,8 @@ def classify_paddle_results(
|
|||||||
found[tag] = _make_item(tag, 'INSTRUMENT_TAG', conf, it_copy)
|
found[tag] = _make_item(tag, 'INSTRUMENT_TAG', conf, it_copy)
|
||||||
item_added = True
|
item_added = True
|
||||||
|
|
||||||
# Inverted Suction Strainers (e.g., 9002 S 26 -> 26-ST-9002 or Note 19 SUCTION STRAINER)
|
# 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{4})\s*[-/]?\s*S\s*[-/]?\s*(\d{2})\b', t, re.IGNORECASE):
|
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)
|
seq = m.group(1)
|
||||||
unit = m.group(2)
|
unit = m.group(2)
|
||||||
st_tag = f"{unit}-ST-{seq}"
|
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)
|
found[st_tag] = _make_item(st_tag, 'EQUIPMENT_TAG', conf, it_copy)
|
||||||
item_added = True
|
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)
|
# Project-prefix tags (instruments + equipment)
|
||||||
for m in _PROJECT_TAG_SEARCH.finditer(t):
|
for m in _PROJECT_TAG_SEARCH.finditer(t):
|
||||||
full_tag = m.group(1).upper()
|
full_tag = m.group(1).upper()
|
||||||
|
|||||||
Reference in New Issue
Block a user