def eval_assertion(request, lang, concept1, reltype, concept2): c1 = Concept.get(concept1, lang) c2 = Concept.get(concept2, lang) svd = get_svd_results(lang) from csc.conceptnet4.analogyspace import eval_assertion lval, rval = eval_assertion(svd, relationtype=reltype, ltext=c1.text, rtext=c2.text) return {'lfeat_val': lval, 'rfeat_val': rval}
def category_similarity(request, lang, cat1, cat2): from math import sqrt svd = get_svd_results(lang) cat1_vec = category_from_urlcategory(svd, lang, cat1) cat2_vec = category_from_urlcategory(svd, lang, cat2) return { 'similarity': cat1_vec*cat2_vec / (sqrt(cat1_vec*cat1_vec) * sqrt(cat2_vec*cat2_vec)) }
def similar_concepts(request, lang, category): # Default to retrieving 10 items. count = int(request.GET.get('count', 10)) svd = get_svd_results(lang) cat = category_from_urlcategory(svd, lang, category) items = svd.u_distances_to(cat).top_items(count) return { 'similar': [{ 'text': canonical_form(item[0], lang), 'score': item[1], } for item in items] }
def similar_features(request, lang, category): # Default to retrieving 10 items. count = int(request.GET.get('count', 10)) fmt = request.GET.get('format', 'frame_blank') svd = get_svd_results(lang) cat = category_from_urlcategory(svd, lang, category) items = svd.v_distances_to(cat).top_items(count) def feature_to_dict(feature_tup, score): feature = Feature.from_tuple(feature_tup) return dict( raw = feature_tup, logical = str(feature), text = feature.nl_statement('__'), score = score ) return { 'similar': [feature_to_dict(feature, score) for (feature, score) in items] }
def get_predictions(lang, concepts): count = 100 tensor = get_tensor(lang) svd = get_svd_results(lang) cat = make_category_failsoft(svd, concepts, [], concepts) return get_prediction_vector(lang, cat)
def get_tops_of_axes(lang, num): svd = get_svd_results(lang) tops_of_axes = [svd.u[:, n].top_items(1)[0][0] for n in xrange(num)] return [Concept.objects.get(text=text, language=lang) for text in tops_of_axes]