Ejemplo n.º 1
0
 def test_simple(self):
     string = "ml"
     expected = dict(symbol="ml",
                     type="quantity",
                     si=dict(symbol="l", factor=0.001))
     actual = extract_unit(string)
     self.assertDictEqual(actual, expected)
Ejemplo n.º 2
0
 def test_with_suffix(self):
     string = "hgKrok"
     expected = dict(symbol="hg",
                     type=unit_types.QUANTITY,
                     si=dict(symbol="kg", factor=0.01))
     actual = extract_unit(string)
     self.assertDictEqual(actual, expected)
Ejemplo n.º 3
0
 def test_with_varying_case(self):
     string = "MlBendit"
     expected = dict(symbol="ml",
                     type=unit_types.QUANTITY,
                     si=dict(symbol="l", factor=0.001))
     actual = extract_unit(string)
     self.assertDictEqual(actual, expected)
Ejemplo n.º 4
0
 def test_value_with_currency(self):
     string = "kr/kg"
     expected = dict(symbol="kg",
                     type=unit_types.QUANTITY_VALUE,
                     si=dict(symbol="kg", factor=1))
     actual = extract_unit(string)
     self.assertDictEqual(actual, expected)
Ejemplo n.º 5
0
 def test_value(self):
     string = "/l"
     expected = dict(symbol="l",
                     type=unit_types.QUANTITY_VALUE,
                     si=dict(symbol="l", factor=1))
     actual = extract_unit(string)
     self.assertDictEqual(actual, expected)
Ejemplo n.º 6
0
def extract_quantity(strings: List[str], safe_units=None) -> QuantityField:
    extracted_strings = []
    for string in strings:
        context = extract_numbers_with_context(string)
        extracted_numbers = (
            pydash.chain(context).map(extract_units_from_number_context).value()
        )
        extracted_numbers = handle_multipliers(extracted_numbers)
        extracted_strings.append(extracted_numbers)
    size = {}
    pieces = {}
    for string in extracted_strings:
        for (i, number) in enumerate(x for x in string if x):
            unit = number.get("unit")
            unit = alt_unit_map[unit] if unit in alt_unit_map.keys() else unit
            unit = extract_unit(unit)

            if unit["type"] not in (unit_types.QUANTITY, unit_types.PIECE):
                continue
            if unit["symbol"] in quantity_units:
                if safe_units and unit["si"]["symbol"] not in safe_units:
                    continue
                size_value = number.get("value")
                size_amount = dict(min=size_value, max=size_value)
                size = dict(unit=unit, amount=size_amount)
                result = dict(size=size, pieces=dict())
            elif unit["symbol"] in piece_units:
                pieces_value = number.get("value")
                pieces_amount = dict(min=pieces_value, max=pieces_value)
                pieces = dict(unit=unit, amount=pieces_amount)

    result = dict(size=size, pieces=pieces)
    return result
Ejemplo n.º 7
0
 def test_pieces(self):
     string = "stk"
     expected = dict(
         symbol="stk",
         type=unit_types.PIECE,
     )
     actual = extract_unit(string)
     self.assertDictEqual(actual, expected)
Ejemplo n.º 8
0
 def test_with_ambiguous_unit(self):
     string = "m"
     expected = dict(
         symbol="m",
         type=unit_types.QUANTITY,
     )
     actual = extract_unit(string)
     self.assertDictContainsSubset(expected, actual)
Ejemplo n.º 9
0
 def test_with_multiplier(self):
     string = "x"
     expected = dict(
         symbol="x",
         type=unit_types.MULTIPLIER,
     )
     actual = extract_unit(string)
     self.assertDictEqual(actual, expected)
Ejemplo n.º 10
0
 def test_value_with_pieces(self):
     string = "krboks"
     expected = dict(
         symbol="boks",
         type=unit_types.PIECE_VALUE,
     )
     actual = extract_unit(string)
     self.assertDictEqual(actual, expected)
Ejemplo n.º 11
0
def analyze_quantity(offer: MpnOffer) -> MpnOffer:
    # Use price unit as size when the product price is denominated as a unit.
    price_unit_string: str = offer["pricing"].get("priceUnit")
    if price_unit_string:
        price_unit = extract_unit(price_unit_string)
        if price_unit and price_unit["type"] in (
            unit_types.QUANTITY,
            unit_types.QUANTITY_VALUE,
        ):
            if not pydash.get(offer, ["quantity", "size", "amount"]):
                offer["quantity"]["size"] = {
                    "amount": {"min": 1, "max": 1},
                    "unit": price_unit,
                }
    size_amount = pydash.get(offer, ["quantity", "size", "amount"])
    if size_amount and not pydash.get(offer, ["value", "size", "amount"]):
        # Has quantity but not value. Let's calculate value.
        try:
            price = offer["pricing"]["price"]
            offer["value"]["size"] = {
                "amount": {
                    "min": price / size_amount["min"],
                    "max": price / size_amount["max"],
                },
                "unit": pydash.get(offer, ["quantity", "size", "unit"]),
            }
        except Exception:
            pass
    value_amount = pydash.get(offer, ["value", "size", "amount"])
    if value_amount and not size_amount:
        # Has value but not quantity. Let's calculate quantity.
        try:
            price = offer["pricing"]["price"]
            offer["quantity"]["size"] = {
                "amount": {
                    "min": size_amount["min"] / price,
                    "max": size_amount["max"] / price,
                },
                "unit": pydash.get(offer, ["value", "size", "unit"]),
            }
        except Exception:
            pass
    return offer
Ejemplo n.º 12
0
 def test_with_no_valid_unit(self):
     string = "-I-"
     actual = extract_unit(string)
     self.assertIsNone(actual)