File size: 3,848 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
from __future__ import annotations

import json
from collections.abc import Collection
from typing import Any

from reference_db.phase_3.grouping.candidates import generate_candidates
from reference_db.phase_3.grouping.decision import make_group_decision
from reference_db.phase_3.grouping.models import (
    IDENTITY_COLUMNS,
    ProductRecord,
    ResolutionResult,
)
from reference_db.phase_3.grouping.resolution import resolve_groups
from reference_db.phase_3.grouping.validation import (
    ValidationResult,
    validate_resolution,
)


def _get_value(row: Any, key: str, default: Any = None) -> Any:
    if isinstance(row, dict):
        return row.get(key, default)

    return getattr(row, key, default)


def build_identity_attributes(row: Any) -> dict[str, Any]:
    return {
        column: _get_value(row, column)
        for column in IDENTITY_COLUMNS
    }


def extract_functional_variant(row: Any) -> str | None:
    value = _get_value(row, "variant_attributes")

    if value is None:
        return None

    if isinstance(value, str):
        try:
            value = json.loads(value)
        except json.JSONDecodeError:
            return None

    if not isinstance(value, dict):
        return None

    functional_variant = value.get("functional_variant")

    if functional_variant is None:
        return None

    return str(functional_variant).strip().lower() or None


def build_product_record(row: Any) -> ProductRecord:
    external_id = _get_value(row, "external_id")
    product_name = _get_value(row, "product_name")
    core_title = _get_value(row, "core_title")

    if external_id is None:
        raise ValueError("Product is missing external_id")

    if product_name is None:
        product_name = ""

    if core_title is None:
        core_title = product_name

    return ProductRecord(
        external_id=str(external_id),
        product_name=str(product_name).strip(),
        core_title=str(core_title).strip(),
        identity_attributes=build_identity_attributes(row),
        functional_variant=extract_functional_variant(row),
    )


def prepare_products(rows: list[Any]) -> list[ProductRecord]:
    return [build_product_record(row) for row in rows]


def run_grouping(
    rows: list[Any],
    threshold: float = 0.85,
    isolated_ids: Collection[str] = (),
) -> tuple[
    list[ProductRecord],
    list[Any],
    list[Any],
    ResolutionResult,
    ValidationResult,
]:
    products = prepare_products(rows)

    candidates = generate_candidates(
        products,
        threshold=threshold,
    )

    product_by_id = {
        product.external_id: product
        for product in products
    }

    decisions = []

    for candidate in candidates:
        product_a = product_by_id[candidate.external_id_a]
        product_b = product_by_id[candidate.external_id_b]

        decision = make_group_decision(
            candidate,
            product_a,
            product_b,
        )

        decisions.append(decision)

    external_ids = [
        product.external_id
        for product in products
    ]

    resolution = resolve_groups(
        external_ids,
        decisions,
        isolated_ids=isolated_ids,
    )

    validation = validate_resolution(
        external_ids,
        resolution,
        decisions,
    )

    return (
        products,
        candidates,
        decisions,
        resolution,
        validation,
    )


def group_product(
    core_title: str,
    identity_attributes: dict[str, Any],
) -> str:
    product = ProductRecord(
        external_id="temporary",
        product_name=core_title,
        core_title=core_title,
        identity_attributes=identity_attributes,
        functional_variant=None,
    )

    resolution = resolve_groups(
        [product.external_id],
        [],
    )

    return resolution.groups[0].group_id