Created
March 13, 2020 19:46
-
-
Save WouterNieuwerth/0571173ca8c41e8f00f73dd0b87514b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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