This repository has been archived on 2024-11-23. You can view files and clone it, but cannot push or open issues or pull requests.
Meme-service/src/nlp.py
2023-02-24 13:41:56 -05:00

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