diff --git a/app/define_word.py b/app/define_word.py index 8c8d091c..c0542418 100755 --- a/app/define_word.py +++ b/app/define_word.py @@ -17,7 +17,17 @@ def get_definition(content): "https://api.urbandictionary.com/v0/define?term={}".format( "%20".join(word) ) - ).json()["list"][0]["definition"] + ).json()["list"] + # UD returns all the results in a json blob. Calculate which result has the highest upvotes to downvotes ratio + # as this is the result that shows up at the top on urbandictionary.com + ratios = {} + for result in definition: + upvotes = result["thumbs_up"] + downvotes = result["thumbs_down"] + if int(downvotes) > 0: + ratio = int(upvotes) / int(downvotes) + ratios[definition.index(result)] = ratio + definition = definition[(max(ratios, key=ratios.get))]["definition"] site = "Urban Dictionary" except IndexError: