# 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
    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',
      conceptnet_with_custom_identities, 'ConceptNet')
    # To regenerate these next two, run yelp_matrix_creation.py
    yelpwordstensor = get_picklecached_thing('yelp-tfidf.pickle', None, 'Yelp TFIDF')
    yelpcatstensor = get_picklecached_thing('yelp_cats_and_price.pickle', None, 'Yelp Cats')
    print yelpwordstensor
    print yelpcatstensor
    return run_3blend_simple(cnettensor, yelpwordstensor, yelpcatstensor, None,
    0.03)

if __name__ == '__main__':
    svd = get_picklecached_thing('yelp-cnet-blend.pickle', 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

    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',
      conceptnet_with_custom_identities, 'ConceptNet')
    # To regenerate these next two, run yelp_matrix_creation.py
    yelpwordstensor = get_picklecached_thing('yelp-tfidf.pickle', None, 'Yelp TFIDF')
    yelpcatstensor = get_picklecached_thing('yelp_cats_and_price.pickle', None, 'Yelp Cats')
    print yelpwordstensor
    print yelpcatstensor
    return run_3blend_simple(cnettensor, yelpwordstensor, yelpcatstensor, None,
    0.03)

if __name__ == '__main__':
    svd = get_picklecached_thing('yelp-cnet-blend.pickle', get_yelp_blend)

    export_svdview(svd, "/csc/svdview/data/yelp-tweek.tsv")