-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Feat: 특정 레시피 클릭 안했을 시 댓글, 좋아요, 북마크 합으로 레시피 추천 (#137)"
This reverts commit 625f01f.
- Loading branch information
1 parent
879ef3b
commit cee2476
Showing
3 changed files
with
32 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters