我正在从我的数据库中构建一个建议引擎。我正在使用fuzzywuzzy 模块将用户输入字符串与现有数据库进行匹配,如下所示。
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})
所以我需要一些关于其他部分的输入。我无法从数据库中选择我想要的正确单词。
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
如何改进这个建议?我还可以使用哪些库?另一种最好的(在记忆和时间方面最好的)可能的方法是什么?提前致谢!