Skip to content

Instantly share code, notes, and snippets.

@WouterNieuwerth
Created March 13, 2020 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WouterNieuwerth/0571173ca8c41e8f00f73dd0b87514b4 to your computer and use it in GitHub Desktop.
Save WouterNieuwerth/0571173ca8c41e8f00f73dd0b87514b4 to your computer and use it in GitHub Desktop.
from fuzzywuzzy import fuzz
from fuzzywuzzy import process
def fuzzy_flask(request):
"""Responds to a HTTP request.
Args:
request (flask.Request): HTTP request object.
Returns:
The response text or any set of values that can be turned into a
Response object using
`make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
"""
request_json = request.get_json()
if request_json and 'searchterms' in request_json and 'brands' in request_json:
return fuzzy_match_list(request_json['searchterms'], request_json['brands'])
else:
return f'No JSON with searchterms or brands found!'
def fuzzy_match_list (searchterms, brands):
output = {}
for term in searchterms:
highest = process.extractOne(term, brands, scorer=fuzz.token_set_ratio)
output[term] = highest
return str(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment