Skip to content

Commit

Permalink
Revert "Feat: 특정 레시피 클릭 안했을 시 댓글, 좋아요, 북마크 합으로 레시피 추천 (#137)"
Browse files Browse the repository at this point in the history
This reverts commit 625f01f.
  • Loading branch information
newbission committed Jun 19, 2024
1 parent 879ef3b commit cee2476
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 49 deletions.
1 change: 1 addition & 0 deletions app/collabo/utils/interaction_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def get_recent_interactions(user):
recent_interactions = (
Interaction.objects.filter(user=user).order_by("-id").values_list("recipe_id", flat=True)
)
print((Interaction.objects.filter(user=user).order_by("-id").values_list("recipe_id", flat=True)))

recent_recipe_ids = []
for i in recent_interactions:
Expand Down
78 changes: 30 additions & 48 deletions app/collabo/utils/similary_utils.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,39 @@
from collabo.models import RecipeSimilarity
from collabo.utils.interaction_utils import get_recent_interactions
from recipes.models import Recipe
from django.db.models import Case, When, Count, F, Q
from django.db.models import Case, When

def get_category_recipes(category_name):
"""
해당 카테고리의 레시피를 상호작용 수 기준으로 정렬하여 반환합니다.
"""
return (
Recipe.objects.filter(category=category_name)
.annotate(
like_count=Count('like', filter=Q(like__recipe_id=F('id'))),
comment_count=Count('comment', filter=Q(comment__recipe_id=F('id'))),
bookmark_count=Count('bookmark', filter=Q(bookmark__recipe_id=F('id'))),
total_interaction=F('like_count') + F('comment_count') + F('bookmark_count')
)
.order_by('-total_interaction')
)


def get_similar_recipes(user_id, category):
def get_similar_recipes(user_id):
# 사용자의 최근 상호작용 레시피 ID 목록 가져오기
recent_recipe_ids = get_recent_interactions(user_id)
similar_recipes_with_scores = {}
if recent_recipe_ids:
WEIGHTS = [1.5, 1.4, 1.3, 1.2, 1.1]
for idx, recipe_id in enumerate(recent_recipe_ids):
# 최근 상호작용일수록 높은 가중치 부여
similar_recipe_ids = (
RecipeSimilarity.objects.filter(recipe=recipe_id)
.values("similar_recipe", "similarity_score")
)

WEIGHTS = [1.5, 1.4, 1.3, 1.2, 1.1]
for idx, recipe_id in enumerate(recent_recipe_ids):
# 최근 상호작용일수록 높은 가중치 부여
similar_recipe_ids = (
RecipeSimilarity.objects.filter(recipe=recipe_id)
.values("similar_recipe", "similarity_score")
)

for similar_recipe in similar_recipe_ids:
sim_recipe_id = similar_recipe["similar_recipe"]
similarity_score = similar_recipe["similarity_score"]
score = similarity_score * WEIGHTS[idx]
if score > 0.0:
print(f"similarity_score: {similarity_score}, weight: {WEIGHTS[idx]}, score: {score}")
if sim_recipe_id in similar_recipes_with_scores:
similar_recipes_with_scores[sim_recipe_id] += score
else:
similar_recipes_with_scores[sim_recipe_id] = score
for similar_recipe in similar_recipe_ids:
sim_recipe_id = similar_recipe["similar_recipe"]
similarity_score = similar_recipe["similarity_score"]
score = similarity_score * WEIGHTS[idx]
if score > 0.0:
print(f"similarity_score: {similarity_score}, weight: {WEIGHTS[idx]}, score: {score}")

if sim_recipe_id in similar_recipes_with_scores:
similar_recipes_with_scores[sim_recipe_id] += score
else:
similar_recipes_with_scores[sim_recipe_id] = score

sorted_similar_recipe_ids = sorted(similar_recipes_with_scores.keys(),
key=lambda x: similar_recipes_with_scores[x],
reverse=True)
preserved_order = Case(
*[When(id=pk, then=pos) for pos, pk in enumerate(sorted_similar_recipe_ids)]
)

similar_recipes = Recipe.objects.filter(id__in=sorted_similar_recipe_ids).order_by(preserved_order)
return similar_recipes
else:
return get_category_recipes(category)
sorted_similar_recipe_ids = sorted(similar_recipes_with_scores.keys(),
key=lambda x: similar_recipes_with_scores[x],
reverse=True)
preserved_order = Case(
*[When(id=pk, then=pos) for pos, pk in enumerate(sorted_similar_recipe_ids)]
)

similar_recipes = Recipe.objects.filter(id__in=sorted_similar_recipe_ids).order_by(preserved_order)
return similar_recipes
2 changes: 1 addition & 1 deletion app/recipes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ def get(self, request, category=None):
# 사용자가 북마크한 레시피만 필터링
filtered_recipes = Recipe.objects.filter(bookmark__user_id=user_id)
elif category_name:
similar_recipes = get_similar_recipes(user_id, category_name)
similar_recipes = get_similar_recipes(user_id)
filtered_recipes = similar_recipes.filter(category=category_name)
else:
return Response(
Expand Down

0 comments on commit cee2476

Please sign in to comment.