33 lines
913 B
Python
33 lines
913 B
Python
|
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
|