# Path: divisi2/network.py
# def conceptnet_matrix(lang):
#     # load from the included pickle file
#     import divisi2
#     try:
#         matrix = divisi2.load('data:matrices/conceptnet_%s.gz' % lang)
#         return matrix
#     except IOError:
#         graph = divisi2.load('data:graphs/conceptnet_%s.graph.gz' % lang)
#         matrix = sparse_matrix(graph, 'concepts', 'features', 3)
#         divisi2.save(matrix, 'data:matrices/conceptnet_%s.gz' % lang)
#         return matrix
# 
# def conceptnet_assoc(lang):
#     import divisi2
#     try:
#         matrix = divisi2.load('data:matrices/conceptnet_assoc_%s.gz' % lang)
#         return matrix
#     except IOError:
#         graph = divisi2.load('data:graphs/conceptnet_%s.graph.gz' % lang)
#         matrix = sparse_matrix(graph, 'concepts', 'concepts', 3)
#         divisi2.save(matrix, 'data:matrices/conceptnet_assoc_%s.gz' % lang)
#         return matrix
# 
# Path: divisi2/reconstructed.py
# def reconstruct(u, s, v, shifts=None):
#     """
#     Reconstruct an approximation to the original matrix A from the SVD result
#     (U, S, V).
#     """
#     return ReconstructedMatrix(u*s, v.T, shifts)
# 
# def reconstruct_similarity(u, s, post_normalize=True, offset=0.0, cutoff=0.0):
#     """
#     Reconstruct the symmetrical, weighted similarity matrix U * Sigma^2 * U^T.
# 
#     If `post_normalize` is true, then (U * Sigma) will be normalized so that
#     the entries of the result are in the range [-1, 1].
# 
#     If `offset` is set to a positive value, then that value will be added
#     to the magnitudes of all rows before normalizing. This can help reduce
#     spurious results. `offset` is meaningless without `post_normalize`. 
# 
#     If `cutoff` is set to a positive value, then all rows with a magnitude
#     less than that (before `offset`) will be dropped entirely, eliminating
#     useless results that will never be similar to anything.
#     """
#     mat = u*s
#     if cutoff > 0.0:
#         row_norms = np.sqrt(np.sum(np.asarray(mat) ** 2, axis=1))
#         rows_to_keep = list(np.nonzero(row_norms >= cutoff)[0])
#         mat = mat[rows_to_keep]
# 
#     if post_normalize:
#         mat = mat.normalize_rows(offset=offset)
#     return reconstruct_symmetric(mat)
# 
# def reconstruct_activation(V, S, post_normalize=True, offset=0.0, cutoff=0.0):
#     """
#     A square matrix can be decomposed as A = V * Lambda * V^T, where Lambda
#     contains the eigenvalues of the matrix and V contains the eigenvectors.
#     This is similar to the SVD, A = U * Sigma * V^T.
# 
#     In fact, if you take the SVD, you will get U = V and Sigma = Lambda^2.
#     
#     By exponentiating Lambda, we can turn our decomposition into an operator
#     that simulates an infinite number of steps of spreading activation, with
#     diminishing effects.
#     
#     This function takes in the SVD results V and Sigma (or, equivalently, U and
#     Sigma), and reconstructs a spreading activation operator from them.
#     """
#     Lambda = np.sqrt(S)
#     mat = (V * np.exp(Lambda/2))
#     if cutoff > 0.0:
#         row_norms = np.sqrt(np.sum(np.asarray(mat) ** 2, axis=1))
#         rows_to_keep = list(np.nonzero(row_norms >= cutoff)[0])
#         mat = mat[rows_to_keep]
#     if post_normalize:
#         mat = mat.normalize_rows(offset=offset)
#     return reconstruct_symmetric(mat)


from divisi2.network import conceptnet_matrix, conceptnet_assoc
from divisi2.reconstructed import reconstruct, reconstruct_similarity, reconstruct_activation
import divisi2
import numpy as np

def analogyspace_predictions():
    cnet = conceptnet_matrix('en')
    U, S, V = cnet.normalize_all().svd(k=100)
    return reconstruct(U, S, V)

def analogyspace_similarity():

# Path: divisi2/network.py
# def conceptnet_matrix(lang):
#     # load from the included pickle file
#     import divisi2
#     try:
#         matrix = divisi2.load('data:matrices/conceptnet_%s.gz' % lang)
#         return matrix
#     except IOError:
#         graph = divisi2.load('data:graphs/conceptnet_%s.graph.gz' % lang)
#         matrix = sparse_matrix(graph, 'concepts', 'features', 3)
#         divisi2.save(matrix, 'data:matrices/conceptnet_%s.gz' % lang)
#         return matrix
# 
# def conceptnet_assoc(lang):
#     import divisi2
#     try:
#         matrix = divisi2.load('data:matrices/conceptnet_assoc_%s.gz' % lang)
#         return matrix
#     except IOError:
#         graph = divisi2.load('data:graphs/conceptnet_%s.graph.gz' % lang)
#         matrix = sparse_matrix(graph, 'concepts', 'concepts', 3)
#         divisi2.save(matrix, 'data:matrices/conceptnet_assoc_%s.gz' % lang)
#         return matrix
# 
# Path: divisi2/reconstructed.py
# def reconstruct(u, s, v, shifts=None):
#     """
#     Reconstruct an approximation to the original matrix A from the SVD result
#     (U, S, V).
#     """
#     return ReconstructedMatrix(u*s, v.T, shifts)
# 
# def reconstruct_similarity(u, s, post_normalize=True, offset=0.0, cutoff=0.0):
#     """
#     Reconstruct the symmetrical, weighted similarity matrix U * Sigma^2 * U^T.
# 
#     If `post_normalize` is true, then (U * Sigma) will be normalized so that
#     the entries of the result are in the range [-1, 1].
# 
#     If `offset` is set to a positive value, then that value will be added
#     to the magnitudes of all rows before normalizing. This can help reduce
#     spurious results. `offset` is meaningless without `post_normalize`. 
# 
#     If `cutoff` is set to a positive value, then all rows with a magnitude
#     less than that (before `offset`) will be dropped entirely, eliminating
#     useless results that will never be similar to anything.
#     """
#     mat = u*s
#     if cutoff > 0.0:
#         row_norms = np.sqrt(np.sum(np.asarray(mat) ** 2, axis=1))
#         rows_to_keep = list(np.nonzero(row_norms >= cutoff)[0])
#         mat = mat[rows_to_keep]
# 
#     if post_normalize:
#         mat = mat.normalize_rows(offset=offset)
#     return reconstruct_symmetric(mat)
# 
# def reconstruct_activation(V, S, post_normalize=True, offset=0.0, cutoff=0.0):
#     """
#     A square matrix can be decomposed as A = V * Lambda * V^T, where Lambda
#     contains the eigenvalues of the matrix and V contains the eigenvectors.
#     This is similar to the SVD, A = U * Sigma * V^T.
# 
#     In fact, if you take the SVD, you will get U = V and Sigma = Lambda^2.
#     
#     By exponentiating Lambda, we can turn our decomposition into an operator
#     that simulates an infinite number of steps of spreading activation, with
#     diminishing effects.
#     
#     This function takes in the SVD results V and Sigma (or, equivalently, U and
#     Sigma), and reconstructs a spreading activation operator from them.
#     """
#     Lambda = np.sqrt(S)
#     mat = (V * np.exp(Lambda/2))
#     if cutoff > 0.0:
#         row_norms = np.sqrt(np.sum(np.asarray(mat) ** 2, axis=1))
#         rows_to_keep = list(np.nonzero(row_norms >= cutoff)[0])
#         mat = mat[rows_to_keep]
#     if post_normalize:
#         mat = mat.normalize_rows(offset=offset)
#     return reconstruct_symmetric(mat)


# Path: divisi2/examples.py
from divisi2.network import conceptnet_matrix, conceptnet_assoc
from divisi2.reconstructed import reconstruct, reconstruct_similarity, reconstruct_activation
import divisi2
import numpy as np


def analogyspace_predictions():
    cnet = conceptnet_matrix('en')
    U, S, V = cnet.normalize_all().svd(k=100)
    return reconstruct(U, S, V)

def analogyspace_similarity():

    cnet = conceptnet_matrix('en')