From 0e87a33297cb9e0fecd45f1765d9776759198ea7 Mon Sep 17 00:00:00 2001 From: ckertam Date: Sat, 9 Dec 2023 21:27:58 +0300 Subject: [PATCH] Add API for context extract & change API for recommendation engine #238 --- backend/user/urls.py | 1 + backend/user/views.py | 31 ++++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/backend/user/urls.py b/backend/user/urls.py index a596bbae..ecca4170 100644 --- a/backend/user/urls.py +++ b/backend/user/urls.py @@ -57,4 +57,5 @@ path('recommendationsByUsers', GetRecommendationsByUserView.as_view(), name='get_recommendations'), path('updateRecommendationsByUsers', UpdateRecommendationsByUserView.as_view(), name='update_recommendations'), path('allStorieswithOwn',AllStorywithOwnView.as_view()), + path('keywordExtraction',KeywordExtractionView.as_view()), ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/backend/user/views.py b/backend/user/views.py index fc4ecabb..881442c4 100644 --- a/backend/user/views.py +++ b/backend/user/views.py @@ -275,13 +275,18 @@ def post(self, request, pk): # Check if the liker has already liked the story if liker in story.likes.all(): - # If the liker has already liked the story, remove the like story.likes.remove(liker) story.save() - - # Log the activity of unliking the story for the author Activity.objects.create(user=author, activity_type='story_unliked', target_story=story, target_user=liker) + # Remove the unliked story from related stories in recommendations + recommendations = StoryRecommendation.objects.filter(user=liker, related_stories=story) + for recommendation in recommendations: + recommendation.related_stories.remove(story) + # If no more related stories, delete the recommendation + if recommendation.related_stories.count() == 0: + recommendation.delete() + return Response({'success': True, 'msg': 'Disliked.'}, status=status.HTTP_201_CREATED) else: # If the liker has not liked the story, add a new like @@ -1156,3 +1161,23 @@ def get(self, request): 'prev_page': page.previous_page_number() if page.has_previous() else None, 'total_pages': total_pages, }, status=status.HTTP_200_OK) + + +class KeywordExtractionView(views.APIView): + def post(self, request): + story_id = request.data.get('story_id') + text = request.data.get('text') + + if story_id: + try: + story = Story.objects.get(id=story_id) + content = story.content + except Story.DoesNotExist: + return Response({'error': 'Story not found'}, status=status.HTTP_404_NOT_FOUND) + elif text: + content = text + else: + return Response({'error': 'No valid input provided'}, status=status.HTTP_400_BAD_REQUEST) + + keywords = extract_keywords_enhanced(content) + return Response({'keywords': keywords}, status=status.HTTP_200_OK)