import pandas as pd import numpy as np CATEGORY_MAP = { 0: "Contract", 1: "General", 2: "Taxes and Permits", 3: "Personnel and Facilities", 4: "Site, Office and Administrative Affairs", 5: "Transportation and Storage", 6: "Occupational Health and Safety", 7: "Auxiliary Equipment and Machinery", 8: "Documentation and Reporting", 9: "Insurance and Guarantees", 10: "Water and Energy Supply and Lighting", 11: "Labor", 12: "Inspection and Testing", 13: "Quality Control and Assurance", 14: "Design and Engineering", 15: "Construction Works", } def map_category_label(category_value): """Map known numeric category ids while preserving free-text categories.""" if pd.isna(category_value): return "" try: numeric_value = float(category_value) if numeric_value.is_integer(): return CATEGORY_MAP.get(int(numeric_value), str(category_value)) except (TypeError, ValueError): pass return str(category_value) def combined_confidence(party_confidence, stakeholder_confidence): """Return joint confidence for the two dependent classification stages.""" party = pd.to_numeric(party_confidence, errors="coerce").fillna(0.0).clip(0.0, 1.0) stakeholder = pd.to_numeric(stakeholder_confidence, errors="coerce").fillna(0.0).clip(0.0, 1.0) return party * stakeholder def class_position(classes, label): """Return a label's probability-column position without assuming numeric ids.""" matches = np.flatnonzero(np.asarray(classes) == label) return int(matches[0]) if matches.size else None