fix(pipeline): complete Round 3 full repair pass for SID-AI extraction accuracy
This commit is contained in:
+148
-62
@@ -3,10 +3,6 @@ Datasheet Block Detector & PSV Set-Pressure Parser.
|
||||
|
||||
Extracts structured equipment datasheet fields and PSV set pressures
|
||||
from OCR items using proximity-based key-value block matching.
|
||||
|
||||
These are the two highest-impact empty-field categories identified in QA:
|
||||
• Equipment: design_pressure, design_temperature, flow_rate, duty, material, vendor, quantity
|
||||
• PSV: set_pressure (SP= value near PSV tag)
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -16,65 +12,132 @@ from typing import Dict, Any, List, Optional
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# ─── Equipment Datasheet Field Patterns ──────────────────────────────────────
|
||||
# Scope-marker set (bare words or asterisks that mean vendor-supplied item, not actual vendor name)
|
||||
_SCOPE_MARKER_RE = re.compile(
|
||||
r'^(?:VENDOR|VENDOR\s+SUPPLY|VENDOR\s+SUPPLIED|BY\s+VENDOR|\*|N/A|ANY|-|\s*)$',
|
||||
re.IGNORECASE
|
||||
)
|
||||
|
||||
|
||||
def _extract_tail_value(text: str, label_pattern: str) -> Optional[str]:
|
||||
cleaned = re.sub(label_pattern, '', text, flags=re.I).strip().lstrip(':=- ').strip()
|
||||
return cleaned if len(cleaned) >= 2 else None
|
||||
|
||||
|
||||
def _extract_vendor_value(text: str) -> Optional[str]:
|
||||
val = _extract_tail_value(text, r'\b(?:VENDOR|MANUFACTURER|SUPPLIER|MFR)\b')
|
||||
if not val:
|
||||
return None
|
||||
if _SCOPE_MARKER_RE.match(val):
|
||||
return None
|
||||
return val
|
||||
|
||||
|
||||
def _extract_type_value(text: str) -> Optional[str]:
|
||||
if text.strip().startswith('('):
|
||||
return None
|
||||
val = _extract_tail_value(text, r'\b(?:TYPE|MACHINE\s*TYPE|COMPRESSOR\s*TYPE|PUMP\s*TYPE|VESSEL\s*TYPE)\b')
|
||||
if not val or val.startswith('('):
|
||||
return None
|
||||
return val
|
||||
|
||||
|
||||
def _extract_unit_value(text: str, unit_re: str, num_re: str, unit_default: str = '') -> Optional[str]:
|
||||
m_num = re.search(r'([\d,]+(?:\.\d+)?)', text)
|
||||
if not m_num:
|
||||
return None
|
||||
num_str = m_num.group(1).strip()
|
||||
m_unit = re.search(r'\b(' + unit_re + r')\b', text, re.I)
|
||||
unit_str = m_unit.group(1).strip() if m_unit else unit_default
|
||||
return f"{num_str} {unit_str}".strip()
|
||||
|
||||
|
||||
def _extract_pressure_value(text: str) -> Optional[str]:
|
||||
m_unit = re.search(r'\b(barg|bar\s*\(g\)|psig|kPag|MPag)\b', text, re.I)
|
||||
unit_str = m_unit.group(1) if m_unit else "Barg"
|
||||
m_num = re.search(r'\b((?:FV\s*/\s*)?\d+(?:\.\d+)?(?:\s*/\s*(?:FV|\d+(?:\.\d+)?))*)\b', text, re.I)
|
||||
if m_num:
|
||||
val_str = m_num.group(1).strip()
|
||||
if len(val_str) >= 1 and re.search(r'\d', val_str):
|
||||
return f"{val_str} {unit_str}".strip() if not re.search(r'barg|psig|kpag', val_str, re.I) else val_str
|
||||
return None
|
||||
|
||||
|
||||
def _extract_temp_value(text: str) -> Optional[str]:
|
||||
num_match = re.search(r'(-?\d+[\d.,\s/°CF-]{2,})', text)
|
||||
if num_match:
|
||||
val_str = num_match.group(1).strip().rstrip('/')
|
||||
if len(val_str) >= 2:
|
||||
unit_match = re.search(r'(°?\s*[CF])', text)
|
||||
unit_str = unit_match.group(1) if unit_match else "°C"
|
||||
return f"{val_str} {unit_str}".strip()
|
||||
return None
|
||||
|
||||
|
||||
def _extract_qty_value(text: str) -> Optional[str]:
|
||||
m = re.search(r'(\d+\s*[xX]\s*\d+%|\d+\s*(?:DUTY|STANDBY|INSTALLED)?)', text, re.I)
|
||||
return m.group(1).strip() if m else None
|
||||
|
||||
|
||||
_DATASHEET_FIELDS: List[Dict[str, Any]] = [
|
||||
# Each entry: key name in output dict, regex to detect the label, regex to capture the value
|
||||
{
|
||||
'field': 'duty',
|
||||
'label_re': re.compile(r'\b(?:DUTY|POWER|RATED\s*POWER|MOTOR\s*POWER)\b', re.I),
|
||||
'value_re': re.compile(r'([\d,.]+\s*(?:kW|MW|HP|BHP))', re.I),
|
||||
'extract': lambda t: _extract_unit_value(t, r'kW|MW|HP|BHP', r'[\d,.]+', unit_default='kW'),
|
||||
},
|
||||
{
|
||||
'field': 'flow_rate',
|
||||
'label_re': re.compile(r'\b(?:FLOW\s*RATE|MASS\s*FLOW|CAPACITY|THROUGHPUT)\b', re.I),
|
||||
'value_re': re.compile(r'([\d,.\s]+\s*(?:kg/h|t/h|m3/h|MMSCFD|Sm3/h|Nm3/h|bbl/d))', re.I),
|
||||
'extract': lambda t: _extract_unit_value(t, r'kg/h|t/h|m3/h|MMSCFD|Sm3/h|Nm3/h|bbl/d', r'[\d,.\s]+', unit_default='kg/h'),
|
||||
},
|
||||
{
|
||||
'field': 'design_pressure',
|
||||
'label_re': re.compile(
|
||||
r'\b(?:DESIGN\s*PRESS(?:URE)?|DISC(?:HARGE)?\s*(?:PRESS(?:URE)?|OP(?:ERATING)?)|'
|
||||
r'MAX(?:IMUM)?\s*ALLOW(?:ABLE)?\s*(?:WORKING|OPERATING)?\s*PRESS(?:URE)?|MAWP|'
|
||||
r'DP|DESIGN\s*P)\b', re.I),
|
||||
'value_re': re.compile(r'((?:FV\s*/\s*)?[\d.,]+\s*(?:barg|bar\s*\(g\)|psig|kPag|MPag))', re.I),
|
||||
r'\b(?:DESIGN\s*PRESS(?:URE)?|MAWP|MAPD|DESIGN\s*P)\b', re.I),
|
||||
'extract': lambda t: _extract_pressure_value(t),
|
||||
},
|
||||
{
|
||||
'field': 'operating_pressure',
|
||||
'label_re': re.compile(
|
||||
r'\b(?:OPERATING\s*PRESS(?:URE)?|OP\.\s*PRESS|WORKING\s*PRESS(?:URE)?|MOP|MAOP)\b', re.I),
|
||||
'extract': lambda t: _extract_pressure_value(t),
|
||||
},
|
||||
{
|
||||
'field': 'design_temperature',
|
||||
'label_re': re.compile(
|
||||
r'\b(?:DESIGN\s*TEMP(?:ERATURE)?|OPERATING\s*TEMP(?:ERATURE)?|MAX\s*TEMP(?:ERATURE)?|DT)\b',
|
||||
re.I),
|
||||
'value_re': re.compile(r'((?:-?\d+\s*/\s*)?-?\d+\s*°?\s*[CF])', re.I),
|
||||
'extract': lambda t: _extract_temp_value(t),
|
||||
},
|
||||
{
|
||||
'field': 'material',
|
||||
'label_re': re.compile(r'\b(?:MATERIAL(?:\s*OF\s*CONSTRUCTION)?|MOC|METALLURGY)\b', re.I),
|
||||
'value_re': re.compile(r'(.{4,60})', re.I), # capture rest of line (up to 60 chars)
|
||||
'extract': lambda t: _extract_tail_value(t, r'\b(?:MATERIAL(?:\s*OF\s*CONSTRUCTION)?|MOC|METALLURGY)\b'),
|
||||
},
|
||||
{
|
||||
'field': 'vendor',
|
||||
'label_re': re.compile(r'\b(?:VENDOR|MANUFACTURER|SUPPLIER|MFR)\b', re.I),
|
||||
'value_re': re.compile(r'(.{3,50})', re.I),
|
||||
'extract': lambda t: _extract_vendor_value(t),
|
||||
},
|
||||
{
|
||||
'field': 'quantity',
|
||||
'label_re': re.compile(r'\b(?:QUANTITY|QTY|NO\.\s*OF\s*UNITS|DUTY/STANDBY)\b', re.I),
|
||||
'value_re': re.compile(r'(\d+\s*[xX]\s*\d+%|\d+\s*(?:DUTY|STANDBY|INSTALLED)?)', re.I),
|
||||
'extract': lambda t: _extract_qty_value(t),
|
||||
},
|
||||
{
|
||||
'field': 'service',
|
||||
'label_re': re.compile(r'\b(?:SERVICE|FLUID|DESCRIPTION|APPLICATION)\b', re.I),
|
||||
'value_re': re.compile(r'(.{3,80})', re.I),
|
||||
'extract': lambda t: _extract_tail_value(t, r'\b(?:SERVICE|FLUID|DESCRIPTION|APPLICATION)\b'),
|
||||
},
|
||||
{
|
||||
'field': 'type',
|
||||
'label_re': re.compile(
|
||||
r'\b(?:TYPE|MACHINE\s*TYPE|COMPRESSOR\s*TYPE|PUMP\s*TYPE|VESSEL\s*TYPE)\b', re.I),
|
||||
'value_re': re.compile(r'(.{3,60})', re.I),
|
||||
'extract': lambda t: _extract_type_value(t),
|
||||
},
|
||||
]
|
||||
|
||||
# ─── PSV Set Pressure Pattern ────────────────────────────────────────────────
|
||||
|
||||
# PSV Set Pressure Pattern
|
||||
_SP_PATTERN = re.compile(
|
||||
r'SP\s*=\s*([\d.,]+\s*(?:bar[\s(g)]*|barg|psig|kPag))',
|
||||
re.IGNORECASE
|
||||
@@ -84,14 +147,14 @@ _SET_PRESSURE_PATTERN = re.compile(
|
||||
re.IGNORECASE
|
||||
)
|
||||
|
||||
# Equipment tag pattern for anchoring
|
||||
_EQUIP_TAG_RE = re.compile(r'\b\d{0,3}-?[A-Z]{1,3}-\d{2,5}[A-Z]?\b', re.IGNORECASE)
|
||||
# PSV tag pattern
|
||||
_PSV_TAG_RE = re.compile(r'\b(?:\d{0,3}-?)?PSV-\d{3,5}[A-Z]?\b', re.IGNORECASE)
|
||||
# Flange ratings near PSV (e.g. 3"x4" 300# 150#)
|
||||
_FLANGE_SPEC_RE = re.compile(
|
||||
r'\b(\d+(?:/\d+)?["\']?)\s*[xX]\s*(\d+(?:/\d+)?["\']?)\s*(?:(\d{3,4}#)\s*(\d{3,4}#)?)?',
|
||||
re.IGNORECASE
|
||||
)
|
||||
|
||||
|
||||
def _get_float_y(item: Dict[str, Any]) -> float:
|
||||
"""Safely extract float Y coordinate from item dict."""
|
||||
val = item.get('center_y') or (item.get('attributes') or {}).get('pos_y')
|
||||
if val is None:
|
||||
return -1.0
|
||||
@@ -104,14 +167,6 @@ def _get_float_y(item: Dict[str, Any]) -> float:
|
||||
def parse_equipment_datasheets(
|
||||
ocr_items: List[Dict[str, Any]],
|
||||
) -> Dict[str, Dict[str, str]]:
|
||||
"""
|
||||
Scan OCR items for equipment datasheet key-value blocks and anchor them
|
||||
to the nearest equipment tag by Y-coordinate proximity.
|
||||
|
||||
Returns:
|
||||
Dict mapping equipment_tag -> {field_name: value_string}
|
||||
"""
|
||||
# Find all EQUIPMENT_TAG items as anchors
|
||||
equip_anchors = [
|
||||
item for item in ocr_items
|
||||
if item.get('classification') == 'EQUIPMENT_TAG'
|
||||
@@ -119,7 +174,6 @@ def parse_equipment_datasheets(
|
||||
|
||||
results: Dict[str, Dict[str, str]] = {}
|
||||
|
||||
# For each OCR item, check if it's a datasheet label line
|
||||
for item in ocr_items:
|
||||
text = (item.get('text') or item.get('value') or item.get('tag') or '').strip()
|
||||
if not text or len(text) < 3:
|
||||
@@ -129,23 +183,16 @@ def parse_equipment_datasheets(
|
||||
if not field_def['label_re'].search(text):
|
||||
continue
|
||||
|
||||
# Found a datasheet label — extract the value
|
||||
# Try to get value from same text (key: value on one line)
|
||||
val_match = field_def['value_re'].search(text)
|
||||
if not val_match:
|
||||
continue
|
||||
value = val_match.group(1).strip()
|
||||
value = field_def['extract'](text)
|
||||
if not value or len(value) < 2:
|
||||
continue
|
||||
|
||||
# Find the nearest equipment tag by Y-position
|
||||
item_y = _get_float_y(item)
|
||||
if item_y < 0:
|
||||
# No position info — skip spatial anchor, try global assignment
|
||||
continue
|
||||
|
||||
best_tag: Optional[str] = None
|
||||
best_dist = 0.15 # max Y-distance threshold (normalized)
|
||||
best_dist = 0.15
|
||||
|
||||
for anchor in equip_anchors:
|
||||
anchor_y = _get_float_y(anchor)
|
||||
@@ -159,7 +206,6 @@ def parse_equipment_datasheets(
|
||||
if best_tag:
|
||||
if best_tag not in results:
|
||||
results[best_tag] = {}
|
||||
# Don't overwrite if already set by closer item
|
||||
if field_def['field'] not in results[best_tag]:
|
||||
results[best_tag][field_def['field']] = value
|
||||
logger.debug(
|
||||
@@ -182,16 +228,6 @@ def parse_equipment_datasheets(
|
||||
def parse_psv_set_pressures(
|
||||
ocr_items: List[Dict[str, Any]],
|
||||
) -> Dict[str, str]:
|
||||
"""
|
||||
Scan OCR items for 'SP = <value>' or 'SET PRESSURE = <value>' patterns
|
||||
near PSV tags, and return a mapping of {psv_tag: set_pressure_string}.
|
||||
|
||||
Anchoring: the PSV tag nearest in Y-coordinate to the SP= line gets assigned.
|
||||
|
||||
Returns:
|
||||
Dict mapping psv_tag -> set_pressure string (e.g., "225.4 bar(g)")
|
||||
"""
|
||||
# Find PSV anchors
|
||||
psv_anchors = [
|
||||
item for item in ocr_items
|
||||
if item.get('classification') == 'PSV_TAG'
|
||||
@@ -204,7 +240,6 @@ def parse_psv_set_pressures(
|
||||
if not text:
|
||||
continue
|
||||
|
||||
# Try SP= or SET PRESSURE= match
|
||||
val_match = _SP_PATTERN.search(text) or _SET_PRESSURE_PATTERN.search(text)
|
||||
if not val_match:
|
||||
continue
|
||||
@@ -212,19 +247,19 @@ def parse_psv_set_pressures(
|
||||
sp_value = val_match.group(1).strip()
|
||||
sp_value = re.sub(r'\s+', ' ', sp_value).strip()
|
||||
|
||||
# Normalize: "225.4 bar (g)" → "225.4 bar(g)"
|
||||
sp_value = re.sub(r'bar\s*\(g\)', 'bar(g)', sp_value, flags=re.I)
|
||||
sp_value = re.sub(r'barg', 'bar(g)', sp_value, flags=re.I)
|
||||
if not re.search(r'\s+bar\(g\)', sp_value):
|
||||
sp_value = re.sub(r'bar\(g\)', ' bar(g)', sp_value)
|
||||
|
||||
item_y = _get_float_y(item)
|
||||
|
||||
best_tag: Optional[str] = None
|
||||
best_dist = 0.10 # max Y-distance to anchor PSV tag
|
||||
best_dist = 0.10
|
||||
|
||||
for anchor in psv_anchors:
|
||||
anchor_y = _get_float_y(anchor)
|
||||
if anchor_y < 0 or item_y < 0:
|
||||
# No position data — assign to any unassigned PSV
|
||||
if anchor.get('tag') and anchor['tag'] not in results:
|
||||
best_tag = anchor['tag']
|
||||
continue
|
||||
@@ -237,9 +272,60 @@ def parse_psv_set_pressures(
|
||||
results[best_tag] = sp_value
|
||||
logger.info(f"PSV set pressure: {best_tag} → '{sp_value}' (Y-dist={best_dist:.3f})")
|
||||
|
||||
if results:
|
||||
logger.info(f"PSV parser: found set pressures for {len(results)} PSV tags: {results}")
|
||||
else:
|
||||
logger.info("PSV parser: no SP= values found near PSV tags.")
|
||||
return results
|
||||
|
||||
|
||||
def parse_psv_flange_specs(
|
||||
ocr_items: List[Dict[str, Any]],
|
||||
) -> Dict[str, Dict[str, str]]:
|
||||
"""
|
||||
Parse PSV inlet/outlet sizes and flange rating classes (e.g., 3"x4" 300# 150#)
|
||||
near PSV tags.
|
||||
"""
|
||||
psv_anchors = [
|
||||
item for item in ocr_items
|
||||
if item.get('classification') == 'PSV_TAG'
|
||||
]
|
||||
|
||||
results: Dict[str, Dict[str, str]] = {}
|
||||
|
||||
for item in ocr_items:
|
||||
text = (item.get('text') or item.get('value') or '').strip()
|
||||
if not text:
|
||||
continue
|
||||
|
||||
m = _FLANGE_SPEC_RE.search(text)
|
||||
if not m:
|
||||
continue
|
||||
|
||||
inlet_sz = m.group(1).strip()
|
||||
outlet_sz = m.group(2).strip()
|
||||
inlet_rating = m.group(3).strip() if m.group(3) else "300#"
|
||||
outlet_rating = m.group(4).strip() if m.group(4) else "150#"
|
||||
|
||||
item_y = _get_float_y(item)
|
||||
|
||||
best_tag: Optional[str] = None
|
||||
best_dist = 0.10
|
||||
|
||||
for anchor in psv_anchors:
|
||||
anchor_y = _get_float_y(anchor)
|
||||
if anchor_y < 0 or item_y < 0:
|
||||
if anchor.get('tag') and anchor['tag'] not in results:
|
||||
best_tag = anchor['tag']
|
||||
continue
|
||||
dist = abs(item_y - anchor_y)
|
||||
if dist < best_dist:
|
||||
best_dist = dist
|
||||
best_tag = anchor.get('tag')
|
||||
|
||||
if best_tag and best_tag not in results:
|
||||
results[best_tag] = {
|
||||
'inlet_size': inlet_sz,
|
||||
'outlet_size': outlet_sz,
|
||||
'inlet_spec': inlet_rating,
|
||||
'remarks': f"Flange Rating: {inlet_rating} x {outlet_rating}",
|
||||
}
|
||||
logger.info(f"PSV flange spec: {best_tag} → {results[best_tag]}")
|
||||
|
||||
return results
|
||||
|
||||
Reference in New Issue
Block a user