def get_terms(request): """Given an ajax request, return, in a json, the index terms""" context = request.REQUEST.get('context') lang = request.REQUEST.get('lang', 'en') service = request.REQUEST.get('service') service = service if service in WEB_SERVICES.keys() or not service else 'yahoo' use_service = bool(service) if context: terms = build_query(context, language=lang, use_web_service=use_service, web_service=service) else: terms = "" if request.is_ajax(): return HttpResponse(json.dumps({'terms': terms}, ensure_ascii=False), mimetype="application/json") else: return HttpResponse(terms, mimetype="text/plain")
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)}
def update_profile(profile, context, docs, lang='en', terms=True, **kwargs): """Update a profile with a spreading activation algorithm: determine the concepts in which the user might be interested, save the session and update the activation values, proceeding then to update the profile itself Args: profile: the client user context: the last context terms or text docs: the ids of the documents of our database the user found interesting lang: the language of the user terms: whether the context is already a string of index terms or a full text """ #build the context list: #context = context + list(DocumentSurrogate.) lang = lang if lang in [e[0] for e in settings.LANGUAGES] else 'en' #STEP 0: build the concepts set and set their activation values: CON = {} if not terms and context: context = build_query(context, language=lang) if not hasattr(context, '__iter__'): context = [context,] #Populate the concepts list with a dictionary of the form {concept: similarity} for d in context: CON.update(DmozCategory.get_for_query(d, lang, as_dict=True)) for d in DocumentSurrogate.objects.filter(pk__in=docs).values_list('category', flat=True).iterator(): #TODO: should I compute the document's summary similarity to its alleged category? CON.update({d:1.0}) #logging.debug("Concepts gathered %s" % CON) #Spreading: add to the interest list the attenuated weight of it's ancestors: for c in CON.keys(): curr_concept = c parent = DmozCategory.objects.filter(pk=curr_concept).values_list('parent', flat=True)[0] while parent: #multiple children of a parent might be in CON, ensure that the maximum score is the one that survives #by selecting the maximum each time ch_weight= DmozCategory.objects.filter(pk=curr_concept).values_list('weight', flat=True)[0] CON.update({parent: max(CON.get(parent, 0.0), CON[curr_concept] * ch_weight)}) curr_concept = parent parent = DmozCategory.objects.filter(pk=curr_concept).values_list('parent', flat=True)[0] #logging.debug("After propagation: %s" % CON) #STEP 2: Evolve the profile #Use linear combination to update existing_preferences = [] #logging.debug("Profile before update: %s" % profile.preferences.all()) for preference in profile.preferences.iterator(): #if the preference is not in this session, decay ctg = preference.category.pk if not ctg in CON: preference.score = DECAY*preference.score else: #it is, augment: preference.score = DECAY*preference.score + (1-DECAY)*CON[ctg] preference.save() #add the preference to the set of existing ones: existing_preferences += [ctg,] #determine which preferences to add to the profile: to_add = set(CON.keys()) - set(existing_preferences) for newcat in to_add: #pref = ClientPreference(category=DmozCategory.objects.get(pk=c), score=CON[newcat], user=profile) #DO NOT store zero weighted preferences: if CON[newcat]: new_pref = ClientPreference(category_id=newcat, score=CON[newcat], user=profile) new_pref.save() #logging.debug("Profile after update: %s" %profile.preferences.all()) return True