# Path: autoencoder/utils/io_utils.py
# def dump_json(data, file):
#     try:
#         with open(file, 'w') as datafile:
#             json.dump(data, datafile)
#     except Exception as e:
#         raise e
# 
# Path: autoencoder/utils/op_utils.py
# def unitmatrix(matrix, norm='l2', axis=1):
#     if norm == 'l1':
#         maxtrixlen = np.sum(np.abs(matrix), axis=axis)
#     if norm == 'l2':
#         maxtrixlen = np.linalg.norm(matrix, axis=axis)
# 
#     if np.any(maxtrixlen <= 0):
#         return matrix
#     else:
#         maxtrixlen = maxtrixlen.reshape(1, len(maxtrixlen)) if axis == 0 else maxtrixlen.reshape(len(maxtrixlen), 1)
#         return matrix / maxtrixlen


import numpy as np
from gensim import corpora
from gensim.models.ldamodel import LdaModel
from ..utils.io_utils import dump_json
from ..utils.op_utils import unitmatrix
'''
Created on Jan, 2017

@author: hugo

'''
from __future__ import absolute_import



def train_lda(corpus, vocab_dict, n_topics, n_iter, save_model):
    lda = LdaModel(corpus, num_topics=n_topics, id2word=vocab_dict, \
        passes=n_iter, minimum_probability=1e-3)
    lda.save(save_model)

    return lda

def generate_doc_codes(model, corpus, output):
    model.minimum_probability = 1e-3
    n_topics = model.num_topics
    doc_codes = {}
    for key, doc_bow in corpus.iteritems():
        code = np.zeros(n_topics)
        for idx, val in model[doc_bow]:
            code[idx] = val
        doc_codes[key] = code.tolist()

# Path: autoencoder/utils/io_utils.py
# def dump_json(data, file):
#     try:
#         with open(file, 'w') as datafile:
#             json.dump(data, datafile)
#     except Exception as e:
#         raise e
# 
# Path: autoencoder/utils/op_utils.py
# def unitmatrix(matrix, norm='l2', axis=1):
#     if norm == 'l1':
#         maxtrixlen = np.sum(np.abs(matrix), axis=axis)
#     if norm == 'l2':
#         maxtrixlen = np.linalg.norm(matrix, axis=axis)
# 
#     if np.any(maxtrixlen <= 0):
#         return matrix
#     else:
#         maxtrixlen = maxtrixlen.reshape(1, len(maxtrixlen)) if axis == 0 else maxtrixlen.reshape(len(maxtrixlen), 1)
#         return matrix / maxtrixlen


# Path: autoencoder/baseline/lda.py
import numpy as np
from gensim import corpora
from gensim.models.ldamodel import LdaModel
from ..utils.io_utils import dump_json
from ..utils.op_utils import unitmatrix

'''
Created on Jan, 2017

@author: hugo

'''
from __future__ import absolute_import



def train_lda(corpus, vocab_dict, n_topics, n_iter, save_model):
    lda = LdaModel(corpus, num_topics=n_topics, id2word=vocab_dict, \
        passes=n_iter, minimum_probability=1e-3)
    lda.save(save_model)

    return lda

def generate_doc_codes(model, corpus, output):
    model.minimum_probability = 1e-3
    n_topics = model.num_topics
    doc_codes = {}
    for key, doc_bow in corpus.iteritems():
        code = np.zeros(n_topics)
        for idx, val in model[doc_bow]:
            code[idx] = val
        doc_codes[key] = code.tolist()

    dump_json(doc_codes, output)