Add search
This commit is contained in:
parent
7c0393bc5d
commit
3347fd95b4
@ -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/<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)
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@ from api import api
|
||||
import os
|
||||
import logging
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
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
|
Reference in New Issue
Block a user