"""Asset diversity at selection time (issue A6): the same asset must not be picked again for another piece of the same role while an equally suitable runner-up exists. Applied to the ranked hits of one need before the first admissible hit is committed.""" # roles where two copies in one home look like a defect (beds in two bedrooms); chairs of one table must match, so they are not listed DIVERSIFY_ROLES = frozenset({'bed', 'wardrobe', 'desk', 'sofa', 'armchair', 'side_table', 'bookcase', 'coffee_table', 'media_console'}) TOP_N = 3 # only the best few hits are reordered: a poorly ranked asset never leapfrogs for the sake of variety def diversify_ranked(ranked, used_assets, role, top_n=TOP_N): """`ranked` hits (dicts with asset_id), best first. Hits among the first `top_n` whose asset is already used for another piece of this role move behind the unused ones; everything else keeps its position.""" if role not in DIVERSIFY_ROLES or not used_assets or len(ranked) < 2: return ranked head, tail = list(ranked[:top_n]), list(ranked[top_n:]) return [h for h in head if h['asset_id'] not in used_assets] + [h for h in head if h['asset_id'] in used_assets] + tail