Ejemplo n.º 1
0
  def mine_size(self, product: Dict[str, Any]) -> Optional[str]:
    """Mines the size from product fields.

    Args:
      product: A dictionary containing product data.

    Returns:
      A size if it could be mined, otherwise None.
    """
    if 'sizes' in product and product['sizes'] and product['sizes'][0]:
      return product['sizes'][0]

    google_product_category = product.get('googleProductCategory', '')

    if self._language in _SUPPORTED_LANGUAGES:
      # Mines clothing size.
      if optimization_util.is_particular_google_product_category(
          google_product_category, constants
          .GOOGLE_PRODUCT_CATEGORY_APPAREL_ACCESSORIES_CLOTHING_KEYWORDS,
          constants.GOOGLE_PRODUCT_CATEGORY_APPAREL_ACCESSORIES_CLOTHING_IDS):
        return self._mine_clothing_size(product)

      # Mines shoes size.
      if optimization_util.is_particular_google_product_category(
          google_product_category,
          constants.GOOGLE_PRODUCT_CATEGORY_APPAREL_ACCESSORIES_SHOES_KEYWORDS,
          constants.GOOGLE_PRODUCT_CATEGORY_APPAREL_ACCESSORIES_SHOES_IDS):
        return self._mine_shoe_size(product)
    else:
      logging.warning(
          'The optimizer did not mine size because the language %s is not supported.',
          self._language)
      return None
Ejemplo n.º 2
0
    def is_size_in_attribute(self, product: Dict[str, Any],
                             attribute: str) -> bool:
        """Checks if the size in a given attribute.

    Args:
      product: A dictionary containing product data.
      attribute: An attribute of the product to be inspected.

    Returns:
      Whether the size is in the attribute.
    """
        google_product_category = product.get('googleProductCategory', '')

        if self._language == constants.LANGUAGE_CODE_JA:
            # Mines clothing size.
            if optimization_util.is_particular_google_product_category(
                    google_product_category, constants.
                    GOOGLE_PRODUCT_CATEGORY_APPAREL_ACCESSORIES_CLOTHING_KEYWORDS,
                    constants.
                    GOOGLE_PRODUCT_CATEGORY_APPAREL_ACCESSORIES_CLOTHING_IDS):
                if self._mine_clothing_size_from_attribute(
                        product.get(attribute, '')):
                    return True
                else:
                    return False

            # Mines shoes size.
            elif optimization_util.is_particular_google_product_category(
                    google_product_category, constants.
                    GOOGLE_PRODUCT_CATEGORY_APPAREL_ACCESSORIES_SHOES_KEYWORDS,
                    constants.
                    GOOGLE_PRODUCT_CATEGORY_APPAREL_ACCESSORIES_SHOES_IDS):
                if self._mine_shoe_size_from_attribute(
                        product.get(attribute, '')):
                    return True
                else:
                    return False

            else:
                logging.info(
                    'The optimizer did not check if the size is in %s because googleProductCategory is not one that needs size.'
                )
                return False
        else:
            logging.warning(
                'The optimizer did not check if the size is in %s because the language %s is not supported.',
                attribute, self._language)
            return False
Ejemplo n.º 3
0
    def test_is_particular_google_product_category_returns_false_when_given_category_is_not_equal_to_any_of_category_ids(
            self):
        category = '1234'
        category_ids = ['5678']

        result = optimization_util.is_particular_google_product_category(
            category, [], category_ids)

        self.assertFalse(result)
Ejemplo n.º 4
0
    def test_is_particular_google_product_category_returns_true_when_given_category_is_equal_to_one_of_category_ids(
            self):
        category = '1234'
        category_ids = ['1234', '5678']

        result = optimization_util.is_particular_google_product_category(
            category, [], category_ids)

        self.assertTrue(result)
Ejemplo n.º 5
0
    def test_is_particular_google_product_category_returns_false_when_given_category_does_not_have_category_keyword(
            self):
        category = 'Apparel & Accessories > Shoes'
        category_keywords = ['Health & Beauty']

        result = optimization_util.is_particular_google_product_category(
            category, category_keywords, [])

        self.assertFalse(result)
Ejemplo n.º 6
0
    def test_is_particular_google_product_category_returns_true_when_given_category_has_category_keyword(
            self):
        category = 'Apparel & Accessories > Shoes'
        category_keywords = ['Apparel & Accessories']

        result = optimization_util.is_particular_google_product_category(
            category, category_keywords, [])

        self.assertTrue(result)
Ejemplo n.º 7
0
  def mine_color(
      self,
      product: Dict[str,
                    Any]) -> Tuple[Optional[List[str]], Optional[List[str]]]:
    """Mines the color from product fields.

    Args:
      product: A dictionary containing product data.

    Returns:
      Mined standard colors and unique colors, or None if no colors could be
      found.
    """
    color_field = product.get('color')
    if color_field:
      return [color_field], [color_field]

    if not optimization_util.is_particular_google_product_category(
        product.get('googleProductCategory', ''),
        constants.GOOGLE_PRODUCT_CATEGORY_APPAREL_ACCESSORIES_KEYWORDS,
        constants.GOOGLE_PRODUCT_CATEGORY_APPAREL_ACCESSORIES_IDS):
      logging.info(
          'Did not attempt to mine color since googleProductCategory is not "Apparel & Accessories"'
      )
      return None, None

    if not self._color_config:
      logging.warning(
          'Did not attempt to mine color since color config could not be '
          'loaded')
      return None, None

    mined_standard_colors, mined_unique_colors = self._mine_color_from_attribute(
        product.get('title', ''))

    if not mined_standard_colors and not mined_unique_colors:
      mined_standard_colors, mined_unique_colors = self._mine_color_from_attribute(
          product.get('description', ''))

    return mined_standard_colors, mined_unique_colors