我正在从我的数据库构建一个建议引擎。我使用fuzzywuzzy模块将用户输入字符串与现有数据库进行匹配,如下所示。
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test
所以我需要在else部分输入一些内容。我无法从数据库中选择我想要的正确单词。
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})
如何改进这个建议?我还可以使用哪些库?什么可能是另一个最好的(在记忆和时间意义上最好)可能的方式来做同样的事情?提前谢谢!