当前位置: 首页>>代码示例>>Python>>正文


Python Dictionary.correct方法代码示例

本文整理汇总了Python中dictionary.Dictionary.correct方法的典型用法代码示例。如果您正苦于以下问题:Python Dictionary.correct方法的具体用法?Python Dictionary.correct怎么用?Python Dictionary.correct使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在dictionary.Dictionary的用法示例。


在下文中一共展示了Dictionary.correct方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: search

# 需要导入模块: from dictionary import Dictionary [as 别名]
# 或者: from dictionary.Dictionary import correct [as 别名]
def search(request, page=1):
    """
    Search page
    """
    print "dsdf %s" % request
    if request.POST:
        if request.is_ajax():
            print "ajax!"
        spell_check = Dictionary()
        spell_check.init_dict()

        required = request.POST.getlist("required[]")
        corrected_required = []
        for term in required:
            correct_term = spell_check.correct(term)
            corrected_required.append(correct_term)
        required = corrected_required

        optional = request.POST.getlist("optional[]")
        corrected_optional = []
        for term in optional:
            correct_term = spell_check.correct(term)
            corrected_optional.append(correct_term)
        optional = corrected_optional

        print "required: %s" % required
        drinks = []
        master_set = set(Drink.objects.all())
        if required:
            for term in required:
                results = Drink.objects.filter(Q(name__contains=term) | Q(ingredients__name__contains=term))
                master_set = master_set & set(results)
            drinks = list(master_set)
        else:
            for term in optional:
                drinks.extend(
                    list(Drink.objects.filter(Q(name__contains=term) | Q(ingredients__name__contains=term)).distinct())
                )
            drinks = list(set(drinks))

        if len(drinks) == 0:
            drinks_list = []
        else:
            drinks_tuple = rank(required + optional, drinks)
            drinks_list = []
            print "Starting views"
            for drink in drinks_tuple:
                drinks_list.append(drink[0])
                print "%s" % drink[0].name.encode("utf-8")
        json_drinks = []
        for drink in drinks_list:
            curr_drink = {}
            curr_drink["name"] = drink.name
            curr_drink["directions"] = drink.directions
            curr_drink["url"] = drink.url
            curr_drink["ingredients"] = []

            for ingredient in drink.ingredients.all():
                curr_ingredient = {}
                curr_ingredient["name"] = ingredient.name
                curr_ingredient["unit"] = ingredient.unit
                curr_ingredient["amount"] = ingredient.amount
                curr_drink["ingredients"].append(curr_ingredient)

            json_drinks.append(curr_drink)
        return HttpResponse(
            json.dumps(json_drinks), mimetype="application/json"
        )  # render_to_response('search.html', {'form': form,
        #'results':drinks[(page-1)*500:page*500],
        #'page':page,
        #'next':page+1,
        #'prev':page-1},
        #                           context_instance=RequestContext(request))

    else:
        form = Search_Form()
        return render_to_response("search.html", {"form": form}, context_instance=RequestContext(request))
开发者ID:michael-b-willingham,项目名称:The-Inebriator,代码行数:79,代码来源:views.py


注:本文中的dictionary.Dictionary.correct方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。