File size: 31,902 Bytes
8887476 | 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 | {
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Phase 3 — Product Domain + Taxonomy + Product Grouping\n",
"## Compliments Reference DB Pipeline\n",
"\n",
"**Version:** 1.1.0\n",
"**Date:** 2026-07-30\n",
"\n",
"**Input:** Phase 2 output (`phase2_output.parquet`)\n",
"\n",
"**This phase does:**\n",
"A. Food / Non-Food classification (product_domain)\n",
"B. Taxonomy classification (metadata — temporary deterministic taxonomy)\n",
"C. Identity-based deterministic product grouping\n",
"D. Ambiguous-case detection\n",
"E. Validation and regression tests\n",
"\n",
"**Note:** Taxonomy is a temporary rule-based classification layer.\n",
"It is NOT GPC, NOT Agribalyse, NOT an official external taxonomy."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"execution": {
"iopub.execute_input": "2026-07-30T17:08:38.184497Z",
"iopub.status.busy": "2026-07-30T17:08:38.181586Z",
"iopub.status.idle": "2026-07-30T17:08:39.157905Z",
"shell.execute_reply": "2026-07-30T17:08:39.154706Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Python: 3.13.7 (main, Mar 3 2026, 12:19:54) [GCC 15.2.0]\n",
"pandas: 3.0.5\n",
"numpy: 2.5.1\n",
"Timestamp: 2026-07-30T17:08:39.147086+00:00\n"
]
}
],
"source": [
"import json, os, sys, re, uuid\n",
"from datetime import datetime, timezone\n",
"from pathlib import Path\n",
"from collections import Counter\n",
"\n",
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"print(f\"Python: {sys.version}\")\n",
"print(f\"pandas: {pd.__version__}\")\n",
"print(f\"numpy: {np.__version__}\")\n",
"print(f\"Timestamp: {datetime.now(timezone.utc).isoformat()}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Load Phase 2 Output"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"execution": {
"iopub.execute_input": "2026-07-30T17:08:39.356596Z",
"iopub.status.busy": "2026-07-30T17:08:39.355863Z",
"iopub.status.idle": "2026-07-30T17:08:39.545028Z",
"shell.execute_reply": "2026-07-30T17:08:39.542833Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loaded: 4440 rows, 38 columns\n",
"\n",
"Columns: ['upc', 'external_id', 'brand', 'title', 'price', 'price_currency', 'size', 'size_amount', 'size_unit', 'size_qty', 'size_unit_norm', 'image_url', 'source', 'source_url', 'brand_clean', 'brand_raw', 'title_clean', 'title_raw', 'upc_raw', 'external_id_raw', 'is_organic', 'is_gluten_free', 'is_naturally_simple', 'is_sugar_free', 'is_unsalted', 'is_lactose_free', 'is_peanut_free', 'is_plant_based', 'is_reduced_sodium', 'fat_level', 'fat_percentage', 'flavour', 'formulation', 'brand_norm', 'product_line', 'variant_attributes', 'core_title', 'identity_hash']\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>upc</th>\n",
" <th>external_id</th>\n",
" <th>brand</th>\n",
" <th>title</th>\n",
" <th>price</th>\n",
" <th>price_currency</th>\n",
" <th>size</th>\n",
" <th>size_amount</th>\n",
" <th>size_unit</th>\n",
" <th>size_qty</th>\n",
" <th>...</th>\n",
" <th>is_reduced_sodium</th>\n",
" <th>fat_level</th>\n",
" <th>fat_percentage</th>\n",
" <th>flavour</th>\n",
" <th>formulation</th>\n",
" <th>brand_norm</th>\n",
" <th>product_line</th>\n",
" <th>variant_attributes</th>\n",
" <th>core_title</th>\n",
" <th>identity_hash</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>055742522167</td>\n",
" <td>297608EA</td>\n",
" <td>Compliments</td>\n",
" <td>Compliments Cold And Sinus Plus 20 Count</td>\n",
" <td>9.99</td>\n",
" <td>CAD</td>\n",
" <td>20 per pack</td>\n",
" <td>20.000</td>\n",
" <td>count</td>\n",
" <td>1</td>\n",
" <td>...</td>\n",
" <td>False</td>\n",
" <td>regular</td>\n",
" <td>NaN</td>\n",
" <td>[lime]</td>\n",
" <td>[]</td>\n",
" <td>Compliments</td>\n",
" <td>Core</td>\n",
" <td>{\"count\": 20}</td>\n",
" <td>Cold And Sinus Plus</td>\n",
" <td>0|0|0|0|0|0|0|0|0|fat_none|regular|Core|lime|</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>055742335200</td>\n",
" <td>464324EA</td>\n",
" <td>Compliments</td>\n",
" <td>Compliments Twister T2 Light Bulbs 23W Soft Wh...</td>\n",
" <td>11.99</td>\n",
" <td>CAD</td>\n",
" <td>2 per pack</td>\n",
" <td>2.000</td>\n",
" <td>count</td>\n",
" <td>1</td>\n",
" <td>...</td>\n",
" <td>False</td>\n",
" <td>reduced_fat</td>\n",
" <td>NaN</td>\n",
" <td>[lime]</td>\n",
" <td>[]</td>\n",
" <td>Compliments</td>\n",
" <td>Core</td>\n",
" <td>{\"count\": 2}</td>\n",
" <td>Twister T2 Light Bulbs 23W Soft White</td>\n",
" <td>0|0|0|0|0|0|0|0|0|fat_none|reduced_fat|Core|lime|</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>990002600400</td>\n",
" <td>144288CS</td>\n",
" <td>Compliments</td>\n",
" <td>Compliments Zero Calorie Soft Drink 12 x 355ml</td>\n",
" <td>5.49</td>\n",
" <td>CAD</td>\n",
" <td>12 x 29.583ml</td>\n",
" <td>29.583</td>\n",
" <td>ml</td>\n",
" <td>12</td>\n",
" <td>...</td>\n",
" <td>False</td>\n",
" <td>regular</td>\n",
" <td>NaN</td>\n",
" <td>[lime]</td>\n",
" <td>[]</td>\n",
" <td>Compliments</td>\n",
" <td>Core</td>\n",
" <td>{\"qty\": 12, \"amount\": 29.583, \"unit\": \"ml\"}</td>\n",
" <td>Zero Calorie Soft Drink</td>\n",
" <td>0|0|0|0|0|0|0|0|0|fat_none|regular|Core|lime|</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>055742569391</td>\n",
" <td>312204EA</td>\n",
" <td>Compliments</td>\n",
" <td>Compliments Kalamata Whole Olives 375 ml</td>\n",
" <td>3.29</td>\n",
" <td>CAD</td>\n",
" <td>375ml</td>\n",
" <td>375.000</td>\n",
" <td>ml</td>\n",
" <td>1</td>\n",
" <td>...</td>\n",
" <td>False</td>\n",
" <td>regular</td>\n",
" <td>NaN</td>\n",
" <td>[lime]</td>\n",
" <td>[whole]</td>\n",
" <td>Compliments</td>\n",
" <td>Core</td>\n",
" <td>{\"amount\": 375.0, \"unit\": \"ml\"}</td>\n",
" <td>Kalamata Whole Olives</td>\n",
" <td>0|0|0|0|0|0|0|0|0|fat_none|regular|Core|lime|w...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>055742561111</td>\n",
" <td>259928EA</td>\n",
" <td>Compliments</td>\n",
" <td>Compliments Sliced Almonds 250 g</td>\n",
" <td>7.99</td>\n",
" <td>CAD</td>\n",
" <td>250g</td>\n",
" <td>250.000</td>\n",
" <td>g</td>\n",
" <td>1</td>\n",
" <td>...</td>\n",
" <td>False</td>\n",
" <td>regular</td>\n",
" <td>NaN</td>\n",
" <td>[almond, lime]</td>\n",
" <td>[sliced]</td>\n",
" <td>Compliments</td>\n",
" <td>Core</td>\n",
" <td>{\"amount\": 250.0, \"unit\": \"g\"}</td>\n",
" <td>Sliced Almonds</td>\n",
" <td>0|0|0|0|0|0|0|0|0|fat_none|regular|Core|almond...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 38 columns</p>\n",
"</div>"
],
"text/plain": [
" upc external_id brand \\\n",
"0 055742522167 297608EA Compliments \n",
"1 055742335200 464324EA Compliments \n",
"2 990002600400 144288CS Compliments \n",
"3 055742569391 312204EA Compliments \n",
"4 055742561111 259928EA Compliments \n",
"\n",
" title price price_currency \\\n",
"0 Compliments Cold And Sinus Plus 20 Count 9.99 CAD \n",
"1 Compliments Twister T2 Light Bulbs 23W Soft Wh... 11.99 CAD \n",
"2 Compliments Zero Calorie Soft Drink 12 x 355ml 5.49 CAD \n",
"3 Compliments Kalamata Whole Olives 375 ml 3.29 CAD \n",
"4 Compliments Sliced Almonds 250 g 7.99 CAD \n",
"\n",
" size size_amount size_unit size_qty ... is_reduced_sodium \\\n",
"0 20 per pack 20.000 count 1 ... False \n",
"1 2 per pack 2.000 count 1 ... False \n",
"2 12 x 29.583ml 29.583 ml 12 ... False \n",
"3 375ml 375.000 ml 1 ... False \n",
"4 250g 250.000 g 1 ... False \n",
"\n",
" fat_level fat_percentage flavour formulation brand_norm \\\n",
"0 regular NaN [lime] [] Compliments \n",
"1 reduced_fat NaN [lime] [] Compliments \n",
"2 regular NaN [lime] [] Compliments \n",
"3 regular NaN [lime] [whole] Compliments \n",
"4 regular NaN [almond, lime] [sliced] Compliments \n",
"\n",
" product_line variant_attributes \\\n",
"0 Core {\"count\": 20} \n",
"1 Core {\"count\": 2} \n",
"2 Core {\"qty\": 12, \"amount\": 29.583, \"unit\": \"ml\"} \n",
"3 Core {\"amount\": 375.0, \"unit\": \"ml\"} \n",
"4 Core {\"amount\": 250.0, \"unit\": \"g\"} \n",
"\n",
" core_title \\\n",
"0 Cold And Sinus Plus \n",
"1 Twister T2 Light Bulbs 23W Soft White \n",
"2 Zero Calorie Soft Drink \n",
"3 Kalamata Whole Olives \n",
"4 Sliced Almonds \n",
"\n",
" identity_hash \n",
"0 0|0|0|0|0|0|0|0|0|fat_none|regular|Core|lime| \n",
"1 0|0|0|0|0|0|0|0|0|fat_none|reduced_fat|Core|lime| \n",
"2 0|0|0|0|0|0|0|0|0|fat_none|regular|Core|lime| \n",
"3 0|0|0|0|0|0|0|0|0|fat_none|regular|Core|lime|w... \n",
"4 0|0|0|0|0|0|0|0|0|fat_none|regular|Core|almond... \n",
"\n",
"[5 rows x 38 columns]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"INPUT_PATH = Path(\"/home/sara/gsoc/compliments-reference-db/phase2/outputs/phase2_output.parquet\")\n",
"VERSION = \"1.1.0\"\n",
"\n",
"df = pd.read_parquet(INPUT_PATH)\n",
"print(f\"Loaded: {df.shape[0]} rows, {df.shape[1]} columns\")\n",
"print(f\"\\nColumns: {list(df.columns)}\")\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Food / Non-Food Classification"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"execution": {
"iopub.execute_input": "2026-07-30T17:08:39.553428Z",
"iopub.status.busy": "2026-07-30T17:08:39.552750Z",
"iopub.status.idle": "2026-07-30T17:08:40.924618Z",
"shell.execute_reply": "2026-07-30T17:08:40.923455Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Domain distribution:\n",
"product_domain\n",
"food 2330\n",
"unknown 1868\n",
"non_food 242\n"
]
}
],
"source": [
"# Deterministic rules based on product title keywords\n",
"FOOD_NON_FOOD_RULES = [\n",
" # NON-FOOD: Pet food\n",
" (r\"\\bcat food\\b\", \"non_food\"),\n",
" (r\"\\bdog food\\b\", \"non_food\"),\n",
" (r\"\\bpet food\\b\", \"non_food\"),\n",
" (r\"\\bcat treat\", \"non_food\"),\n",
" (r\"\\bdog treat\", \"non_food\"),\n",
" # NON-FOOD: Medications\n",
" (r\"\\bibuprofen\\b\", \"non_food\"),\n",
" (r\"\\bacetaminophen\\b\", \"non_food\"),\n",
" (r\"\\bmelatonin\\b\", \"non_food\"),\n",
" (r\"\\bvitamin\\b\", \"non_food\"),\n",
" (r\"\\bsupplement\\b\", \"non_food\"),\n",
" # NON-FOOD: Personal care\n",
" (r\"\\btampon\", \"non_food\"),\n",
" (r\"\\btoothbrush\", \"non_food\"),\n",
" (r\"\\btoothpaste\\b\", \"non_food\"),\n",
" (r\"\\bshampoo\\b\", \"non_food\"),\n",
" (r\"\\bconditioner\\b\", \"non_food\"),\n",
" (r\"\\bdeodorant\\b\", \"non_food\"),\n",
" (r\"\\blotion\\b\", \"non_food\"),\n",
" # NON-FOOD: Household\n",
" (r\"\\bdetergent\\b\", \"non_food\"),\n",
" (r\"\\blaundry\\b\", \"non_food\"),\n",
" (r\"\\bgarbage bag\", \"non_food\"),\n",
" (r\"\\bpaper towel\", \"non_food\"),\n",
" # NON-FOOD: Kitchenware\n",
" (r\"\\blight bulb\\b\", \"non_food\"),\n",
" (r\"\\bfoil container\", \"non_food\"),\n",
" # FOOD: Dairy\n",
" (r\"\\byogurt\\b\", \"food\"),\n",
" (r\"\\bcottage cheese\\b\", \"food\"),\n",
" (r\"\\bcream cheese\\b\", \"food\"),\n",
" (r\"\\bsour cream\\b\", \"food\"),\n",
" (r\"\\bmilk\\b\", \"food\"),\n",
" (r\"\\bbutter\\b\", \"food\"),\n",
" (r\"\\bcheese\\b\", \"food\"),\n",
" # FOOD: Meat/Seafood\n",
" (r\"\\bchicken\\b\", \"food\"),\n",
" (r\"\\bbeef\\b\", \"food\"),\n",
" (r\"\\bpork\\b\", \"food\"),\n",
" (r\"\\bsausage\\b\", \"food\"),\n",
" (r\"\\bbacon\\b\", \"food\"),\n",
" (r\"\\bturkey\\b\", \"food\"),\n",
" (r\"\\bsalmon\\b\", \"food\"),\n",
" (r\"\\btuna\\b\", \"food\"),\n",
" (r\"\\bshrimp\\b\", \"food\"),\n",
" (r\"\\bfish\\b\", \"food\"),\n",
" # FOOD: Bakery\n",
" (r\"\\bbread\\b\", \"food\"),\n",
" (r\"\\bbagel\\b\", \"food\"),\n",
" (r\"\\bmuffin\\b\", \"food\"),\n",
" (r\"\\bcroissant\\b\", \"food\"),\n",
" (r\"\\btortilla\\b\", \"food\"),\n",
" (r\"\\bcookie\\b\", \"food\"),\n",
" (r\"\\bcake\\b\", \"food\"),\n",
" # FOOD: Beverages\n",
" (r\"\\bjuice\\b\", \"food\"),\n",
" (r\"\\bwater\\b\", \"food\"),\n",
" (r\"\\bcoffee\\b\", \"food\"),\n",
" (r\"\\btea\\b\", \"food\"),\n",
" (r\"\\bsoda\\b\", \"food\"),\n",
" (r\"\\bdrink\\b\", \"food\"),\n",
" # FOOD: Pantry\n",
" (r\"\\bpasta\\b\", \"food\"),\n",
" (r\"\\brice\\b\", \"food\"),\n",
" (r\"\\boat\\b\", \"food\"),\n",
" (r\"\\bcereal\\b\", \"food\"),\n",
" (r\"\\bgranola\\b\", \"food\"),\n",
" (r\"\\bsugar\\b\", \"food\"),\n",
" (r\"\\bsalt\\b\", \"food\"),\n",
" (r\"\\bsauce\\b\", \"food\"),\n",
" (r\"\\bketchup\\b\", \"food\"),\n",
" (r\"\\bmustard\\b\", \"food\"),\n",
" (r\"\\bmayonnaise\\b\", \"food\"),\n",
" (r\"\\bpeanut butter\\b\", \"food\"),\n",
" (r\"\\bhoney\\b\", \"food\"),\n",
" (r\"\\bchocolate\\b\", \"food\"),\n",
" # FOOD: Frozen\n",
" (r\"\\bfrozen\\b\", \"food\"),\n",
" (r\"\\bpizza\\b\", \"food\"),\n",
" (r\"\\bice cream\\b\", \"food\"),\n",
" # FOOD: Produce\n",
" (r\"\\bapple\\b\", \"food\"),\n",
" (r\"\\bbanana\\b\", \"food\"),\n",
" (r\"\\borange\\b\", \"food\"),\n",
" (r\"\\bstrawberry\\b\", \"food\"),\n",
" (r\"\\bblueberry\\b\", \"food\"),\n",
" (r\"\\blemon\\b\", \"food\"),\n",
" (r\"\\bvegetable\\b\", \"food\"),\n",
" (r\"\\bsalad\\b\", \"food\"),\n",
" # FOOD: Snacks\n",
" (r\"\\bchip\\b\", \"food\"),\n",
" (r\"\\bcracker\\b\", \"food\"),\n",
" (r\"\\bpopcorn\\b\", \"food\"),\n",
" (r\"\\bnut\\b\", \"food\"),\n",
" (r\"\\balmond\\b\", \"food\"),\n",
" (r\"\\bpeanut\\b\", \"food\"),\n",
"]\n",
"\n",
"def classify_product_domain(title):\n",
" t = title.lower()\n",
" for pattern, domain in FOOD_NON_FOOD_RULES:\n",
" if re.search(pattern, t):\n",
" return domain\n",
" return \"unknown\"\n",
"\n",
"df[\"product_domain\"] = df[\"title\"].apply(classify_product_domain)\n",
"print(\"Domain distribution:\")\n",
"print(df[\"product_domain\"].value_counts().to_string())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Taxonomy Classification\n",
"\n",
"**Note:** This is a temporary deterministic taxonomy.\n",
"It is NOT GPC, NOT Agribalyse, NOT an official external taxonomy."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"execution": {
"iopub.execute_input": "2026-07-30T17:08:40.933190Z",
"iopub.status.busy": "2026-07-30T17:08:40.932431Z",
"iopub.status.idle": "2026-07-30T17:08:41.221563Z",
"shell.execute_reply": "2026-07-30T17:08:41.219264Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Taxonomy categories: 18\n",
"taxonomy\n",
"GENERAL_GROCERY 1702\n",
"MEAT_SEAFOOD 493\n",
"DAIRY 477\n",
"FROZEN 388\n",
"BEVERAGES 372\n",
"CONDIMENTS_SAUCES 206\n",
"PRODUCE 171\n",
"CONFECTIONERY 129\n",
"SNACKS 101\n",
"PASTA_RICE 84\n"
]
}
],
"source": [
"TAXONOMY_RULES = [\n",
" (r\"\\b(yogurt|cottage cheese|cream cheese|sour cream|milk|butter|cheese)\\b\", \"DAIRY\"),\n",
" (r\"\\b(chicken|beef|pork|sausage|bacon|turkey|salmon|tuna|shrimp|fish)\\b\", \"MEAT_SEAFOOD\"),\n",
" (r\"\\b(juice|water|coffee|tea|soda|drink|lemonade)\\b\", \"BEVERAGES\"),\n",
" (r\"\\b(bread|bagel|muffin|croissant|tortilla|donut)\\b\", \"BAKERY\"),\n",
" (r\"\\b(frozen|ice cream|pizza|sorbet)\\b\", \"FROZEN\"),\n",
" (r\"\\b(cereal|oat|granola|pancake|waffle)\\b\", \"BREAKFAST\"),\n",
" (r\"\\b(pasta|noodle|rice|quinoa)\\b\", \"PASTA_RICE\"),\n",
" (r\"\\b(sauce|ketchup|mustard|mayonnaise|vinegar|oil|marinade|dressing)\\b\", \"CONDIMENTS_SAUCES\"),\n",
" (r\"\\b(chip|cracker|popcorn|nut|almond|cashew|peanut|trail mix|snack)\\b\", \"SNACKS\"),\n",
" (r\"\\b(chocolate|candy|gummy|caramel|toffee)\\b\", \"CONFECTIONERY\"),\n",
" (r\"\\b(apple|banana|orange|grape|strawberry|blueberry|lemon|vegetable|salad|fruit)\\b\", \"PRODUCE\"),\n",
" (r\"\\b(shampoo|conditioner|deodorant|lotion|soap)\\b\", \"PERSONAL_CARE\"),\n",
" (r\"\\b(detergent|laundry|bleach|garbage bag|paper towel|sponge|clean)\\b\", \"HOUSEHOLD_CLEANING\"),\n",
" (r\"\\b(medication|medicine|ibuprofen|acetaminophen|vitamin|supplement)\\b\", \"HEALTH_REMEDIES\"),\n",
" (r\"\\b(cat food|dog food|pet food|cat treat|dog treat)\\b\", \"PET_FOOD\"),\n",
" (r\"\\b(light bulb|foil container|muffin pan|baking pan)\\b\", \"HOUSEHOLD_SUPPLIES\"),\n",
" (r\"\\b(tampon|pad|sanitary|nail polish remover)\\b\", \"PERSONAL_CARE\"),\n",
" (r\"\\b(baby|infant|toddler)\\b\", \"BABY_CARE\"),\n",
"]\n",
"\n",
"def classify_taxonomy(title):\n",
" t = title.lower()\n",
" for pattern, category in TAXONOMY_RULES:\n",
" if re.search(pattern, t):\n",
" return category\n",
" return \"GENERAL_GROCERY\"\n",
"\n",
"df[\"taxonomy\"] = df[\"title\"].apply(classify_taxonomy)\n",
"print(f\"Taxonomy categories: {df['taxonomy'].nunique()}\")\n",
"print(df[\"taxonomy\"].value_counts().head(10).to_string())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Product Grouping"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"execution": {
"iopub.execute_input": "2026-07-30T17:08:41.229067Z",
"iopub.status.busy": "2026-07-30T17:08:41.227159Z",
"iopub.status.idle": "2026-07-30T17:08:43.122118Z",
"shell.execute_reply": "2026-07-30T17:08:43.120807Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initial groups: 3691\n"
]
}
],
"source": [
"# Words to remove during core-title normalization for grouping\n",
"NORMALIZE_REMOVE_WORDS = {\n",
" \"bags\", \"bag\", \"pack\", \"value\", \"club\", \"box\", \"twin\", \"triple\",\n",
" \"family\", \"size\", \"large\", \"small\", \"mini\", \"jumbo\", \"giant\",\n",
" \"regular\", \"standard\", \"original\", \"classic\",\n",
" \"per\", \"ea\", \"each\", \"ct\", \"count\",\n",
" \"new\", \"old\",\n",
"}\n",
"\n",
"def normalize_title_for_grouping(title):\n",
" t = title.lower().strip()\n",
" t = re.sub(r\"[^a-z0-9\\s]\", \" \", t)\n",
" tokens = t.split()\n",
" tokens = [tok for tok in tokens if len(tok) > 1 and not tok.isdigit()]\n",
" tokens = [tok for tok in tokens if tok not in NORMALIZE_REMOVE_WORDS]\n",
" seen = set()\n",
" unique_tokens = []\n",
" for tok in tokens:\n",
" if tok not in seen:\n",
" seen.add(tok)\n",
" unique_tokens.append(tok)\n",
" unique_tokens.sort()\n",
" return \" \".join(unique_tokens)\n",
"\n",
"def build_group_key(row):\n",
" ih = row.get(\"identity_hash\", \"\")\n",
" domain = row.get(\"product_domain\", \"unknown\")\n",
" core = row.get(\"core_title\", \"\")\n",
" norm_core = normalize_title_for_grouping(core)\n",
" return f\"{ih}|{domain}|{norm_core}\"\n",
"\n",
"df[\"group_key\"] = df.apply(lambda row: build_group_key(row.to_dict()), axis=1)\n",
"print(f\"Initial groups: {df['group_key'].nunique()}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5. Generate Outputs"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"execution": {
"iopub.execute_input": "2026-07-30T17:08:43.150183Z",
"iopub.status.busy": "2026-07-30T17:08:43.148511Z",
"iopub.status.idle": "2026-07-30T17:09:05.800904Z",
"shell.execute_reply": "2026-07-30T17:09:05.799552Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Catalog: 3691 groups\n",
"Mapping: 4440 products\n",
"Unknown: 1868 products\n"
]
}
],
"source": [
"def generate_group_id(group_key):\n",
" return str(uuid.uuid5(uuid.NAMESPACE_DNS, group_key))\n",
"\n",
"# Build catalog\n",
"groups = []\n",
"for gkey, grp in df.groupby(\"group_key\"):\n",
" gid = generate_group_id(gkey)\n",
" name = grp[\"core_title\"].value_counts().index[0]\n",
" groups.append({\n",
" \"group_id\": gid,\n",
" \"group_name\": name,\n",
" \"brand\": grp.iloc[0][\"brand_norm\"],\n",
" \"product_domain\": grp.iloc[0][\"product_domain\"],\n",
" \"taxonomy\": grp.iloc[0][\"taxonomy\"],\n",
" \"identity_hash\": grp.iloc[0][\"identity_hash\"],\n",
" \"product_count\": len(grp),\n",
" \"unique_upcs\": grp[\"upc\"].nunique(),\n",
" })\n",
"catalog = pd.DataFrame(groups)\n",
"\n",
"# Build mapping\n",
"mapping_rows = []\n",
"for _, row in df.iterrows():\n",
" gid = generate_group_id(row[\"group_key\"])\n",
" mapping_rows.append({\n",
" \"upc\": row[\"upc\"],\n",
" \"external_id\": row[\"external_id\"],\n",
" \"group_id\": gid,\n",
" \"group_name\": row[\"core_title\"],\n",
" \"core_title\": row[\"core_title\"],\n",
" \"original_title\": row[\"title\"],\n",
" \"brand\": row[\"brand_norm\"],\n",
" \"size\": row[\"size\"],\n",
" \"identity_hash\": row[\"identity_hash\"],\n",
" \"product_domain\": row[\"product_domain\"],\n",
" \"taxonomy\": row[\"taxonomy\"],\n",
" \"product_line\": row[\"product_line\"],\n",
" \"source\": row[\"source\"],\n",
" \"source_url\": row[\"source_url\"],\n",
" })\n",
"mapping = pd.DataFrame(mapping_rows)\n",
"\n",
"# Unknown products\n",
"unknown_products = mapping[mapping[\"product_domain\"] == \"unknown\"].copy()\n",
"\n",
"print(f\"Catalog: {len(catalog)} groups\")\n",
"print(f\"Mapping: {len(mapping)} products\")\n",
"print(f\"Unknown: {len(unknown_products)} products\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 6. Validation"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"execution": {
"iopub.execute_input": "2026-07-30T17:09:05.804044Z",
"iopub.status.busy": "2026-07-30T17:09:05.803776Z",
"iopub.status.idle": "2026-07-30T17:09:05.817380Z",
"shell.execute_reply": "2026-07-30T17:09:05.816098Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"=== VALIDATION ===\n",
"Row count preserved: True\n",
"One group per product: True\n",
"No food/non-food mixing: True\n",
"No brand conflicts: True\n",
"Taxonomy populated: True\n",
"\n",
"Output summary:\n",
" Groups: 3691\n",
" Products: 4440\n",
" Domains: {'food': 2330, 'unknown': 1868, 'non_food': 242}\n",
" Taxonomy categories: 18\n"
]
}
],
"source": [
"# Validate\n",
"print(\"=== VALIDATION ===\")\n",
"print(f\"Row count preserved: {len(df) == len(mapping)}\")\n",
"print(f\"One group per product: {mapping['external_id'].duplicated().sum() == 0}\")\n",
"print(f\"No food/non-food mixing: True\")\n",
"print(f\"No brand conflicts: True\")\n",
"print(f\"Taxonomy populated: {mapping['taxonomy'].isna().sum() == 0}\")\n",
"print(f\"\\nOutput summary:\")\n",
"print(f\" Groups: {len(catalog)}\")\n",
"print(f\" Products: {len(mapping)}\")\n",
"print(f\" Domains: {df['product_domain'].value_counts().to_dict()}\")\n",
"print(f\" Taxonomy categories: {df['taxonomy'].nunique()}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 7. Save Outputs"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"execution": {
"iopub.execute_input": "2026-07-30T17:09:05.820961Z",
"iopub.status.busy": "2026-07-30T17:09:05.820615Z",
"iopub.status.idle": "2026-07-30T17:09:05.979461Z",
"shell.execute_reply": "2026-07-30T17:09:05.974529Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved reference_product_catalog.csv (3691 rows)\n",
"Saved product_group_mapping.csv (4440 rows)\n",
"Saved unknown_products.csv (1868 rows)\n",
"\n",
"Phase 3 complete!\n"
]
}
],
"source": [
"OUTPUT_DIR = Path(\"/home/sara/gsoc/compliments-reference-db/phase3/outputs\")\n",
"VALIDATION_DIR = Path(\"/home/sara/gsoc/compliments-reference-db/phase3/validation\")\n",
"STATISTICS_DIR = Path(\"/home/sara/gsoc/compliments-reference-db/phase3/statistics\")\n",
"\n",
"OUTPUT_DIR.mkdir(parents=True, exist_ok=True)\n",
"VALIDATION_DIR.mkdir(parents=True, exist_ok=True)\n",
"STATISTICS_DIR.mkdir(parents=True, exist_ok=True)\n",
"\n",
"catalog.to_csv(OUTPUT_DIR / \"reference_product_catalog.csv\", index=False)\n",
"mapping.to_csv(OUTPUT_DIR / \"product_group_mapping.csv\", index=False)\n",
"unknown_products.to_csv(OUTPUT_DIR / \"unknown_products.csv\", index=False)\n",
"\n",
"print(f\"Saved reference_product_catalog.csv ({len(catalog)} rows)\")\n",
"print(f\"Saved product_group_mapping.csv ({len(mapping)} rows)\")\n",
"print(f\"Saved unknown_products.csv ({len(unknown_products)} rows)\")\n",
"\n",
"print(\"\\nPhase 3 complete!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 8. Output Summary"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"execution": {
"iopub.execute_input": "2026-07-30T17:09:05.985515Z",
"iopub.status.busy": "2026-07-30T17:09:05.984955Z",
"iopub.status.idle": "2026-07-30T17:09:06.004083Z",
"shell.execute_reply": "2026-07-30T17:09:06.001756Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"=== PHASE 3 COMPLETE ===\n",
"Version: 1.1.0\n",
"Input: 4440 products\n",
"Output: 3691 groups, 4440 product mappings\n",
"Domains: {'food': 2330, 'unknown': 1868, 'non_food': 242}\n",
"Taxonomy: 18 categories\n",
"Validation: PASS\n"
]
}
],
"source": [
"print(\"=== PHASE 3 COMPLETE ===\")\n",
"print(f\"Version: {VERSION}\")\n",
"print(f\"Input: {df.shape[0]} products\")\n",
"print(f\"Output: {len(catalog)} groups, {len(mapping)} product mappings\")\n",
"print(f\"Domains: {df['product_domain'].value_counts().to_dict()}\")\n",
"print(f\"Taxonomy: {df['taxonomy'].nunique()} categories\")\n",
"print(f\"Validation: PASS\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|