2

I was building a suggestion engine from my database. I'm using fuzzywuzzy module to match user input string with existing database as shown below.

if request.method == "GET":
    display_dict = {}
    user_input = request.GET["user_input"]  # get user input
    match_dict = {}

    food_items_obj = food_items.objects.all() # fetch all objects from table

    for items in food_items_obj :
        match = fuzz.ratio(user_input.lower(),items.name.lower()) # try to match the user input with existing names
        match_dict[match] = items.name        # put it one dictionary

    matched_dict = OrderedDict(sorted(match_dict.items(),reverse = True)) # sort the dictionary 
    if max(matched_dict.keys()) == 100: # if exact match then put in a dict and return back
        display_dict[100] = matched_dict[100]
    else :                              
  # if not found then try best matching words.. 
  ###########THIS PART I NEED SOME HELP! #############
        for k,v in matched_dict.items():
            if user_input in v :
                display_dict[k] = v  # if user input is substring of the names in database
            else:
                if k > sum([k for k,v in matched_dict.items()])/len(matched_dict): # best I could think of was to take average of ratio and display items greater than that.  
                display_dict[k] = v

    return render(request,"suggestions/home.html",{'recommendations_list':display_dict,'time_taken': time_taken})

So I need some inputs on the else part. I'm not able to pick the right exact words which I want from the database.

Example input :
user input : gobi
suggestions that came up:
Did you mean maaza ## unexpected word 
Did you mean gobi 65
Did you mean gobi pepper fry
Did you mean gobi parota
Did you mean kadai gobi
Did you mean aloo gobi

How to improve this suggestions ? What more libraries can I use ? What could be the other best (best in the sense of memory and time) possible way to do the same ? Thanks in advance!

gincard
  • 1,814
  • 3
  • 16
  • 24
d-coder
  • 83
  • 5

0 Answers0