File size: 1,636 Bytes
c5beb5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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