filter_goods.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. """
  2. 筛选逻辑:
  3. 1. 品类筛选:基于主推款,筛选可搭配品类,允许的品类组合:上装+裤装,上装+半裙,外套+连衣裙,外套+裤装,外套+连衣裙,上装+裤装+外套,上装+裙装+外套
  4. 2. 颜色筛选:色彩,可同色调搭配,可对比色调搭配,整体色彩不超过三种,(排除红绿搭配)
  5. 3. 季节筛选:季节:同季节可互搭 春秋款可互搭 春夏款可互搭 秋冬款可互搭 夏季和冬季不可搭配
  6. """
  7. from utils.logger_config import setup_logger
  8. from utils.tools import read_json_file, has_four_color
  9. logger = setup_logger(__name__)
  10. class FilterGoods:
  11. def __init__(self, filter_config_file):
  12. self.filter_config = read_json_file(filter_config_file)
  13. # 定义商品类别层级关系
  14. self.category_hierarchy = self.filter_config["category_hierarchy"]
  15. # 定义商品颜色层级关系
  16. self.color_hierarchy = self.filter_config["color_hierarchy"]
  17. # 品类组合
  18. self.allowed_category_combos = {
  19. frozenset({'上装', '裤装'}),
  20. frozenset({'上装', '半裙'}),
  21. frozenset({'外套', '连衣裙'}),
  22. frozenset({'外套', '裤装'}),
  23. frozenset({"上装", "裤装", "外套"}),
  24. frozenset({"上装", "半裙", "外套"})
  25. }
  26. # 季节搭配
  27. self.allowed_season_combos = {
  28. frozenset({'春季', '春季'}),
  29. frozenset({'秋季', '秋季'}),
  30. frozenset({'夏季', '夏季'}),
  31. frozenset({'冬季', '冬季'}),
  32. frozenset({'初夏', '初夏'}),
  33. frozenset({'初冬', '初冬'}),
  34. frozenset({'夏季', '初夏'}),
  35. frozenset({'冬季', '初冬'}),
  36. frozenset({'春季', '秋季'}),
  37. frozenset({'春季', '夏季'}),
  38. frozenset({'春季', '初夏'}),
  39. frozenset({'秋季', '初冬'}),
  40. frozenset({'秋季', '冬季'})
  41. }
  42. # 色彩搭配
  43. self.not_allowed_color_combos = {
  44. frozenset({'红色', '绿色'})
  45. }
  46. def can_combine(self, goods_list):
  47. if len(goods_list) < 2:
  48. return False
  49. return (
  50. self._check_category_combo(goods_list) and
  51. self._check_season_combo(goods_list) and
  52. self._check_color_combo(goods_list)
  53. )
  54. def _get_top_category(self, category):
  55. """
  56. 获取商品的最顶层类别
  57. :param category: 商品的具体类别
  58. :return: 顶层类别
  59. """
  60. for top_cat, sub_cats in self.category_hierarchy.items():
  61. if category == top_cat or category in sub_cats:
  62. return top_cat
  63. return category
  64. def _check_category_combo(self, goods_list):
  65. """检查品类组合是否允许"""
  66. # 获取所有商品的顶层类别
  67. top_categories = set()
  68. for good in goods_list:
  69. category = good["category"]
  70. top_category = self._get_top_category(category)
  71. top_categories.add(top_category)
  72. # 检查顶层类别组合是否在允许的组合中
  73. if_can_combine = frozenset(top_categories) in self.allowed_category_combos
  74. logger.info(f"can_category_combine: {if_can_combine}")
  75. return if_can_combine
  76. def _check_season_combo(self, goods_list):
  77. season = frozenset(goods['season'] for goods in goods_list)
  78. if_can_combine = season in self.allowed_season_combos
  79. logger.info(f"can_season_combine: {if_can_combine}")
  80. return if_can_combine
  81. def _get_top_color(self, color):
  82. """
  83. 获取商品的最顶层颜色
  84. :param category: 商品的具体颜色
  85. :return: 顶层颜色
  86. """
  87. for top_col, sub_col in self.color_hierarchy.items():
  88. if color == top_col or color in sub_col:
  89. return top_col
  90. return color
  91. def _check_color_combo(self, goods_list):
  92. """检查品类组合是否允许"""
  93. # 获取所有商品的顶层颜色
  94. top_colors = set()
  95. for good in goods_list:
  96. color = good["color"]
  97. top_color = self._get_top_color(color)
  98. top_colors.add(top_color)
  99. color_text = ''.join(top_colors)
  100. if_four_color = has_four_color(color_text)
  101. # 检查顶层类别组合是否在允许的组合中
  102. if_can_combine = frozenset(top_colors) not in self.not_allowed_color_combos and not(if_four_color)
  103. logger.info(f"can_color_combine: {if_can_combine}")
  104. return if_can_combine
  105. filter_goods = FilterGoods('./config/filter_config.json')
  106. if __name__ == "__main__":
  107. filter_goods = FilterGoods('./config/filter_config.json')
  108. goods1 = {"category": "T恤", "color": "红色", "season": "秋季"} # 属于"上装"
  109. goods2 = {"category": "牛仔裤", "color": "军绿", "season": "春季"} # 属于"裤装"
  110. goods3 = {"category": "外套", "color": "灰紫色", "season": "秋季"} # 属于"外套"
  111. print(filter_goods.can_combine([goods1, goods2])) # T恤+牛仔裤 -> 上装+下装,应返回True
  112. print(filter_goods.can_combine([goods1, goods2, goods3])) # 上装+下装+外套,应返回True