Skip to content

Commit

Permalink
Add API for context extract & change API for recommendation engine #238
Browse files Browse the repository at this point in the history
  • Loading branch information
ckertam committed Dec 9, 2023
1 parent 975e727 commit 0e87a33
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions backend/user/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
31 changes: 28 additions & 3 deletions backend/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 0e87a33

Please # to comment.