Exemple #1
0
def search_docs(request, rerank = True):
    """Search documents and return a json with the original result set and a re-ranked version"""
    if not 'q' in request.REQUEST:
        return HttpResponseBadRequest()
    
    query = request.REQUEST.get('q')
    #the language defaults to english
    lang = request.REQUEST.get('hl') or 'en'
    
    #search: assume the results are already json encoded    
    results = do_search(query, lang)
    if rerank:
        reranked =  re_rank(request.profile, results)
    else:
        reranked = []   
    return HttpResponse(jsonlib.dumps({'results': results,
                                       'reranked': reranked}, ensure_ascii=False), mimetype="application/json")
Exemple #2
0
def get_recommendations(request):    
    #get the terms
    context = request.REQUEST['context']
    lang = request.REQUEST.get('lang', 'en')
    #default to a supported language:
    lang = lang if lang in [e[0] for e in settings.LANGUAGES] else 'en'
    service = request.REQUEST.get('service', '')    
    service = service if service in WEB_SERVICES.keys() or (not service and lang == 'en') else "yahoo"#'tagthe'
    use_service = bool(service)    
    
    try:
        terms = build_query(context, language=lang, use_web_service=use_service, web_service=service, fail_silently=False)
    except:
        return HttpResponseServerError("Error extracting terms")
     
    #do the search
    results = do_search(terms, lang=lang)
    #re-rank
    if hasattr(request, 'profile'):
        results=re_rank(request.profile, results)
    else:
        return HttpResponseBadRequest("No profile found")
    #return    
    return {'results': results, 'terms': unicode(terms)}