approximate-query #11
@ -1,6 +1,6 @@
|
|||||||
from flask_restx import Namespace, Resource, fields
|
from flask_restx import Namespace, Resource, fields
|
||||||
from flask_restx import reqparse
|
from flask_restx import reqparse
|
||||||
from flask import make_response
|
from flask import make_response, abort
|
||||||
from api.clientGetter import getClientSafely
|
from api.clientGetter import getClientSafely
|
||||||
import logging
|
import logging
|
||||||
import random
|
import random
|
||||||
@ -18,15 +18,11 @@ class getExactFile(Resource):
|
|||||||
def get(self, file_name):
|
def get(self, file_name):
|
||||||
client = getClientSafely()
|
client = getClientSafely()
|
||||||
if client is None:
|
if client is None:
|
||||||
return {
|
abort(500, "S3 failed to start")
|
||||||
"message" : "Error connecting to S3"
|
|
||||||
}, 500
|
|
||||||
if file_name in client.getCurrentMemeList():
|
if file_name in client.getCurrentMemeList():
|
||||||
return make_response(client.getMeme(file_name))
|
return make_response(client.getMeme(file_name))
|
||||||
else:
|
else:
|
||||||
return {
|
abort(400, "Requested file '" + file_name + "' not found")
|
||||||
"message": "Requested file '" + file_name + "' not found"
|
|
||||||
}, 404
|
|
||||||
|
|
||||||
@api.route('/random')
|
@api.route('/random')
|
||||||
@api.doc(description="Returns a random meme")
|
@api.doc(description="Returns a random meme")
|
||||||
@ -37,9 +33,7 @@ class getRandomFile(Resource):
|
|||||||
def get(self):
|
def get(self):
|
||||||
client = getClientSafely()
|
client = getClientSafely()
|
||||||
if client is None:
|
if client is None:
|
||||||
return {
|
abort(500, "S3 failed to start")
|
||||||
"message" : "Error connecting to S3"
|
|
||||||
}, 500
|
|
||||||
choice = random.choice(tuple(client.getCurrentMemeList()))
|
choice = random.choice(tuple(client.getCurrentMemeList()))
|
||||||
return make_response(client.getMeme(choice))
|
return make_response(client.getMeme(choice))
|
||||||
|
|
||||||
@ -54,9 +48,7 @@ class getRandomFile(Resource):
|
|||||||
def get(self):
|
def get(self):
|
||||||
client = getClientSafely()
|
client = getClientSafely()
|
||||||
if client is None:
|
if client is None:
|
||||||
return {
|
abort(500, "S3 failed to start")
|
||||||
"message" : "Error connecting to S3"
|
|
||||||
}, 500
|
|
||||||
|
|
||||||
choice = random.choice(tuple(client.getCurrentMemeList()))
|
choice = random.choice(tuple(client.getCurrentMemeList()))
|
||||||
while choice in self.cache:
|
while choice in self.cache:
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
from flask_restx import Namespace, Resource, fields
|
from flask_restx import Namespace, Resource, fields
|
||||||
from flask_restx import reqparse
|
from flask_restx import reqparse
|
||||||
from api.clientGetter import getClientSafely
|
from api.clientGetter import getClientSafely
|
||||||
|
from api.get import getExactFile as getApi
|
||||||
|
from flask import abort
|
||||||
|
import nlp
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
# Exported namespace
|
# Exported namespace
|
||||||
@ -18,11 +21,39 @@ class exactSearch(Resource):
|
|||||||
def get(self, query):
|
def get(self, query):
|
||||||
client = getClientSafely()
|
client = getClientSafely()
|
||||||
if client is None:
|
if client is None:
|
||||||
return {
|
abort(500, "S3 failed to start")
|
||||||
"message": "Error connecting to S3"
|
|
||||||
}, 500
|
|
||||||
if query in client.getCurrentMemeList():
|
if query in client.getCurrentMemeList():
|
||||||
return "nice"
|
return {
|
||||||
|
"found" : True,
|
||||||
|
"url" : "/resource/exact/" + query,
|
||||||
|
"name" : query
|
||||||
|
}
|
||||||
else:
|
else:
|
||||||
return "boo"
|
return { "found" : False }
|
||||||
|
|
||||||
|
@api.route('/close/<string:query>')
|
||||||
|
@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)
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
from flask_restx import Namespace, Resource, fields
|
from flask_restx import Namespace, Resource, fields
|
||||||
from flask_restx import reqparse
|
from flask_restx import reqparse
|
||||||
|
from flask import abort
|
||||||
from api.clientGetter import getClientSafely
|
from api.clientGetter import getClientSafely
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
@ -12,10 +13,7 @@ class getCount(Resource):
|
|||||||
def get(self):
|
def get(self):
|
||||||
client = getClientSafely()
|
client = getClientSafely()
|
||||||
if client is None:
|
if client is None:
|
||||||
return {
|
abort(500, "S3 failed to start")
|
||||||
"message": "Error connecting to S3"
|
|
||||||
}, 500
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"count" : len(client.getCurrentMemeList())
|
"count" : len(client.getCurrentMemeList())
|
||||||
}, 200
|
}, 200
|
||||||
|
@ -6,7 +6,7 @@ from api import api
|
|||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
|
||||||
isDebug = True
|
isDebug = True
|
||||||
|
|
||||||
|
32
src/nlp.py
Normal file
32
src/nlp.py
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user