# Path: csc/divisi/cnet.py

# 
# Path: csc/divisi/labeled_tensor.py
# class OldDenseLabeledTensor(LabeledView):
# class OldSparseLabeledTensor(LabeledView):
#     def __repr__(self):
#     def __init__(self, *a, **kw):
#     def __repr__(self):
#     def dot(self, other):
#     def array_op(self, ufunc, *others):
#         def take_data(tensor):
#     def slice(self, mode, label):
#     def dense_slice(self, mode, label):
#     def load(cls, filebase):
# 
# Path: csc/divisi/util.py

# 
# Path: csc/divisi/export_svdview.py
# def denormalize(concept_text):
# def null_denormalize(x): return x
# def _sorted_rowvectors(m, denormalize, num_dims):
#     def fix_concept(c):
# def write_tsv(matrix, outfn, denormalize=None, cutoff=40):
# def write_packed(matrix, out_basename, denormalize=None, cutoff=40):
# def write_annotated(matrix, out_basename, denormalize=None, cutoff=40,
# filter=None, annotations=None, links=None):
# def add_link_2way(links, source, target):
# def feature_str(feature):
# def svdview_single_tensor(tensor, out_basename):


from csc.divisi.cnet import conceptnet_2d_from_db #, conceptnet_2d_from_db_tuple
from csc.divisi.labeled_tensor import SparseLabeledTensor
from csc.divisi.util import get_picklecached_thing
from csc.divisi.export_svdview import export_svdview
import cPickle as pickle
import gzip, os
    # Only first sigmas
    t1 = tensor1.svd(k=20)
    sigma1 = t1.svals[0:10]
    a = sigma1[0]
    t2 = tensor2.svd(k=20)
    sigma2 = t2.svals[0:10]
    b = sigma2[0]
    return float(a/(a+b))

def pickledumpsvd(svd, basename):
    svd_u, svd_s, svd_v, svd_a = ['pickle/'+ basename + '_%s_.4f.pickle.gz' % (typ) for typ in ['u', 's', 'v', 'a']]
    pickledump(svd_u, svd.u)
    pickledump(svd_s, svd.svals)
    pickledump(svd_v, svd.v)
    pickledump(svd_a, blend)


def conceptnet_with_custom_identities(identconst=20):
    cnet = conceptnet_2d_from_db('en', identities=0.0)
    for concept in cnet.label_list(0):
        cnet[concept, "DescribedAs/"+concept] = identconst
        cnet[concept, "IsA/"+concept] = identconst
    for (key, value) in cnet.iteritems():
        concept, feature = key
        if value < 0:
            if feature.startswith('CapableOf') or feature.endswith('CapableOf') or feature.startswith('Desires') or feature.endswith('Desires'): continue
            cnet[key] = 0
    return cnet

def get_yelp_blend():

# Path: csc/divisi/cnet.py

# 
# Path: csc/divisi/labeled_tensor.py
# class OldDenseLabeledTensor(LabeledView):
# class OldSparseLabeledTensor(LabeledView):
#     def __repr__(self):
#     def __init__(self, *a, **kw):
#     def __repr__(self):
#     def dot(self, other):
#     def array_op(self, ufunc, *others):
#         def take_data(tensor):
#     def slice(self, mode, label):
#     def dense_slice(self, mode, label):
#     def load(cls, filebase):
# 
# Path: csc/divisi/util.py

# 
# Path: csc/divisi/export_svdview.py
# def denormalize(concept_text):
# def null_denormalize(x): return x
# def _sorted_rowvectors(m, denormalize, num_dims):
#     def fix_concept(c):
# def write_tsv(matrix, outfn, denormalize=None, cutoff=40):
# def write_packed(matrix, out_basename, denormalize=None, cutoff=40):
# def write_annotated(matrix, out_basename, denormalize=None, cutoff=40,
# filter=None, annotations=None, links=None):
# def add_link_2way(links, source, target):
# def feature_str(feature):
# def svdview_single_tensor(tensor, out_basename):


# Path: csc/divisi/blending.py
from csc.divisi.cnet import conceptnet_2d_from_db #, conceptnet_2d_from_db_tuple
from csc.divisi.labeled_tensor import SparseLabeledTensor
from csc.divisi.util import get_picklecached_thing
from csc.divisi.export_svdview import export_svdview
import cPickle as pickle
import gzip, os

    # Only first sigmas
    t1 = tensor1.svd(k=20)
    sigma1 = t1.svals[0:10]
    a = sigma1[0]
    t2 = tensor2.svd(k=20)
    sigma2 = t2.svals[0:10]
    b = sigma2[0]
    return float(a/(a+b))

def pickledumpsvd(svd, basename):
    svd_u, svd_s, svd_v, svd_a = ['pickle/'+ basename + '_%s_.4f.pickle.gz' % (typ) for typ in ['u', 's', 'v', 'a']]
    pickledump(svd_u, svd.u)
    pickledump(svd_s, svd.svals)
    pickledump(svd_v, svd.v)
    pickledump(svd_a, blend)


def conceptnet_with_custom_identities(identconst=20):
    cnet = conceptnet_2d_from_db('en', identities=0.0)
    for concept in cnet.label_list(0):
        cnet[concept, "DescribedAs/"+concept] = identconst
        cnet[concept, "IsA/"+concept] = identconst
    for (key, value) in cnet.iteritems():
        concept, feature = key
        if value < 0:
            if feature.startswith('CapableOf') or feature.endswith('CapableOf') or feature.startswith('Desires') or feature.endswith('Desires'): continue
            cnet[key] = 0
    return cnet

def get_yelp_blend():

    cnettensor = get_picklecached_thing('cnet.pickle',