From 7c0393bc5dafcde6f9e7044e775016780a5636c2 Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Fri, 24 Feb 2023 12:56:05 -0500 Subject: [PATCH 1/2] Add proper response body for search endpoint --- src/api/get.py | 18 +++++------------- src/api/search.py | 13 ++++++++----- src/api/util.py | 6 ++---- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/api/get.py b/src/api/get.py index 2ccc41a..2a5f994 100644 --- a/src/api/get.py +++ b/src/api/get.py @@ -1,6 +1,6 @@ from flask_restx import Namespace, Resource, fields from flask_restx import reqparse -from flask import make_response +from flask import make_response, abort from api.clientGetter import getClientSafely import logging import random @@ -18,15 +18,11 @@ class getExactFile(Resource): def get(self, file_name): client = getClientSafely() if client is None: - return { - "message" : "Error connecting to S3" - }, 500 + abort(500, "S3 failed to start") if file_name in client.getCurrentMemeList(): return make_response(client.getMeme(file_name)) else: - return { - "message": "Requested file '" + file_name + "' not found" - }, 404 + abort(400, "Requested file '" + file_name + "' not found") @api.route('/random') @api.doc(description="Returns a random meme") @@ -37,9 +33,7 @@ class getRandomFile(Resource): def get(self): client = getClientSafely() if client is None: - return { - "message" : "Error connecting to S3" - }, 500 + abort(500, "S3 failed to start") choice = random.choice(tuple(client.getCurrentMemeList())) return make_response(client.getMeme(choice)) @@ -54,9 +48,7 @@ class getRandomFile(Resource): def get(self): client = getClientSafely() if client is None: - return { - "message" : "Error connecting to S3" - }, 500 + abort(500, "S3 failed to start") choice = random.choice(tuple(client.getCurrentMemeList())) while choice in self.cache: diff --git a/src/api/search.py b/src/api/search.py index 931d520..1ffb003 100644 --- a/src/api/search.py +++ b/src/api/search.py @@ -1,6 +1,8 @@ from flask_restx import Namespace, Resource, fields from flask_restx import reqparse from api.clientGetter import getClientSafely +from api.get import getExactFile as getApi +from flask import abort import logging # Exported namespace @@ -18,11 +20,12 @@ class exactSearch(Resource): def get(self, query): client = getClientSafely() if client is None: - return { - "message": "Error connecting to S3" - }, 500 + abort(500, "S3 failed to start") if query in client.getCurrentMemeList(): - return "nice" + return { + "found" : True, + "url" : "/resource/exact/" + query + } else: - return "boo" + return { "found" : False } diff --git a/src/api/util.py b/src/api/util.py index 6293ed5..dd87da9 100644 --- a/src/api/util.py +++ b/src/api/util.py @@ -1,5 +1,6 @@ from flask_restx import Namespace, Resource, fields from flask_restx import reqparse +from flask import abort from api.clientGetter import getClientSafely import logging @@ -12,10 +13,7 @@ class getCount(Resource): def get(self): client = getClientSafely() if client is None: - return { - "message": "Error connecting to S3" - }, 500 - + abort(500, "S3 failed to start") return { "count" : len(client.getCurrentMemeList()) }, 200 From 3347fd95b47c2b6a75ac0ebfaa8da776e33396f4 Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Fri, 24 Feb 2023 13:41:56 -0500 Subject: [PATCH 2/2] Add search --- src/api/search.py | 30 +++++++++++++++++++++++++++++- src/app.py | 2 +- src/nlp.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/nlp.py diff --git a/src/api/search.py b/src/api/search.py index 1ffb003..42bbe84 100644 --- a/src/api/search.py +++ b/src/api/search.py @@ -3,6 +3,7 @@ from flask_restx import reqparse from api.clientGetter import getClientSafely from api.get import getExactFile as getApi from flask import abort +import nlp import logging # Exported namespace @@ -24,8 +25,35 @@ class exactSearch(Resource): if query in client.getCurrentMemeList(): return { "found" : True, - "url" : "/resource/exact/" + query + "url" : "/resource/exact/" + query, + "name" : query } else: return { "found" : False } +@api.route('/close/') +@api.doc(params={ + 'query' : 'Search query to attempt to compare against' + },description="FInd a meme thats close using levenshtein distance") +class textualClose(Resource): + def get(self, query): + client = getClientSafely() + if client is None: + abort(500, "S3 failed to start") + + foundMemes = nlp.getCloseMemes(client.getCurrentMemeList(), query) + listToReturn = [] + + for meme in foundMemes: + entry = { + "found" : True, + "url" : "/resource/exact/" + meme, + "name" : meme + } + listToReturn.append(entry) + + return { + "results" : listToReturn, + "numberOfResults" : len(foundMemes) + } + diff --git a/src/app.py b/src/app.py index 241d34a..dda269f 100755 --- a/src/app.py +++ b/src/app.py @@ -6,7 +6,7 @@ from api import api import os import logging -logging.basicConfig(level=logging.INFO) +logging.basicConfig(level=logging.DEBUG) isDebug = True diff --git a/src/nlp.py b/src/nlp.py new file mode 100644 index 0000000..6d1bf07 --- /dev/null +++ b/src/nlp.py @@ -0,0 +1,32 @@ +from fuzzywuzzy import fuzz, process +from api.clientGetter import getClientSafely +import logging + + +def getCloseMemes(allPossibleMemes: set, query: str): + if not isinstance(allPossibleMemes, set): + raise Exception("Expected set for allPossibleMemes") + if not isinstance(query, str): + raise Exception("Expected str for query") + + topMeme = '' + topMemes = [] + topScore = 0 + + for meme in allPossibleMemes: + currentScore = fuzz.partial_ratio(query, meme) + if currentScore > topScore: + topMeme = meme + topScore = currentScore + if currentScore == 100: + topMemes.append(meme) + topMeme = meme + + logging.info('Top memes for given query (' + query + ")") + logging.info('topMemes: ' + str(topMemes)) + logging.info('topMeme : ' + topMeme) + + if len(topMemes) == 0: + topMemes.append(topMeme) + + return topMemes