From 673e89d40e3b796325fe8291cccf7c18c3265389 Mon Sep 17 00:00:00 2001 From: Luke Robles <98352913+lrobles-iterable@users.noreply.github.com> Date: Thu, 28 Jul 2022 18:25:50 -0600 Subject: [PATCH] urbandictionary command will now return the definition with the highest ratio of upvotes to downvotes --- app/define_word.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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: