File size: 4,724 Bytes
1cd137c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from __future__ import annotations

import pandas as pd

AGRIBALYSE_VERSION = "3.2"

MAPPING_VERSION = "phase6-taxonomy-v1"


# These are broad taxonomy mappings.
# They are NOT exact product-level Agribalyse matches.
#
# ciqual_family is intentionally omitted because the current
# Reference DB does not contain an exact Ciqual match.
#
# The official Agribalyse database contains individual food
# entries associated with Ciqual codes, but this pipeline currently
# performs only category-level mapping.

TAXONOMY_MAPPING = {
    "DAIRY": {
        "agribalyse_category": "Milk and dairy products",
        "confidence": "MEDIUM",
    },
    "MEAT_SEAFOOD": {
        "agribalyse_category": "Meat, eggs and fish",
        "confidence": "MEDIUM",
    },
    "PRODUCE": {
        "agribalyse_category": "Fruits, vegetables, legumes and oilseeds",
        "confidence": "MEDIUM",
    },
    "BAKERY": {
        "agribalyse_category": "Cereal products / bakery",
        "confidence": "MEDIUM",
    },
    "PASTA_RICE": {
        "agribalyse_category": "Cereal products",
        "confidence": "MEDIUM",
    },
    "BREAKFAST": {
        "agribalyse_category": "Cereal products",
        "confidence": "MEDIUM",
    },
    "CONFECTIONERY": {
        "agribalyse_category": "Sweet products",
        "confidence": "LOW",
    },
    "SNACKS": {
        "agribalyse_category": "Snack products",
        "confidence": "LOW",
    },
    "CONDIMENTS_SAUCES": {
        "agribalyse_category": "Condiments and sauces",
        "confidence": "LOW",
    },
    "OILS_VINEGARS": {
        "agribalyse_category": "Fats and oils",
        "confidence": "MEDIUM",
    },
    "FROZEN": {
        "agribalyse_category": "Frozen foods",
        "confidence": "LOW",
    },
    "GENERAL_GROCERY": {
        "agribalyse_category": "General grocery foods",
        "confidence": "LOW",
    },
    "BAKING_INGREDIENTS": {
        "agribalyse_category": "Baking ingredients",
        "confidence": "LOW",
    },
    "BABY_CARE": {
        "agribalyse_category": None,
        "confidence": "NONE",
    },
}


def map_taxonomy_to_agribalyse(
    taxonomy,
    domain,
) -> dict[str, object]:
    """Map Reference DB taxonomy to a broad Agribalyse category."""

    if domain != "food":
        return {
            "agribalyse_category": None,
            "mapping_method": "not_applicable",
            "mapping_confidence": "NONE",
            "mapping_status": "NOT_APPLICABLE",
            "mapping_note": "Non-food product.",
        }

    if pd.isna(taxonomy):
        return {
            "agribalyse_category": None,
            "mapping_method": "not_available",
            "mapping_confidence": "NONE",
            "mapping_status": "NOT_AVAILABLE",
            "mapping_note": "Taxonomy is unavailable.",
        }

    mapping = TAXONOMY_MAPPING.get(taxonomy)

    if mapping is None:
        return {
            "agribalyse_category": None,
            "mapping_method": "not_available",
            "mapping_confidence": "NONE",
            "mapping_status": "NOT_AVAILABLE",
            "mapping_note": "No deterministic taxonomy mapping defined.",
        }

    if mapping["agribalyse_category"] is None:
        return {
            "agribalyse_category": None,
            "mapping_method": "not_applicable",
            "mapping_confidence": "NONE",
            "mapping_status": "NOT_APPLICABLE",
            "mapping_note": "Category is excluded from Agribalyse mapping.",
        }

    return {
        "agribalyse_category": mapping["agribalyse_category"],
        "mapping_method": "taxonomy_mapping",
        "mapping_confidence": mapping["confidence"],
        "mapping_status": "MAPPED",
        "mapping_note": (
            "Broad taxonomy-level mapping; not an exact product-level "
            "Agribalyse or Ciqual match."
        ),
    }


def add_agribalyse_mapping(df: pd.DataFrame) -> pd.DataFrame:
    result = df.copy()

    mapped = result.apply(
        lambda row: map_taxonomy_to_agribalyse(
            row.get("taxonomy"),
            row.get("domain"),
        ),
        axis=1,
    )

    result["agribalyse_category"] = mapped.map(
        lambda x: x["agribalyse_category"]
    )

    result["mapping_method"] = mapped.map(
        lambda x: x["mapping_method"]
    )

    result["mapping_confidence"] = mapped.map(
        lambda x: x["mapping_confidence"]
    )

    result["mapping_status"] = mapped.map(
        lambda x: x["mapping_status"]
    )

    result["mapping_note"] = mapped.map(
        lambda x: x["mapping_note"]
    )

    result["mapping_source"] = (
        "ADEME Agribalyse 3.2 category reference"
    )

    result["mapping_version"] = MAPPING_VERSION

    return result