# Path: shorttext/utils/compactmodel_io.py
# class CompactIOMachine:
#     """ Base class that implements compact model I/O.
# 
#     This is to replace the original :func:`compactio` decorator.
# 
#     """
#     def __init__(self, infodict, prefix, suffices):
#         """
# 
#         :param infodict: information about the model. Must contain the key 'classifier'.
#         :param prefix: prefix of names of the model file
#         :param suffices: suffices of the names of the model file
#         :type infodict: dict
#         :type prefix: str
#         :type suffices: list
#         """
#         self.infodict = infodict
#         self.prefix = prefix
#         self.suffices = suffices
# 
#     def savemodel(self, nameprefix):
#         """ Abstract method for `savemodel`.
# 
#         :param nameprefix: prefix of the model path
#         :type nameprefix: str
#         """
#         raise e.OperationNotDefinedException()
# 
#     def loadmodel(self, nameprefix):
#         """ Abstract method for `loadmodel`.
# 
#         :param nameprefix: prefix of the model path
#         :type nameprefix: str
#         """
#         raise e.OperationNotDefinedException()
# 
#     def save_compact_model(self, filename, *args, **kwargs):
#         """ Save the model in a compressed binary format.
# 
#         :param filename: name of the model file
#         :param args: arguments
#         :param kwargs: arguments
#         :type filename: str
#         :type args: dict
#         :type kwargs: dict
#         """
#         save_compact_model(filename, self.savemodel, self.prefix, self.suffices, self.infodict, *args, **kwargs)
# 
#     def load_compact_model(self, filename, *args, **kwargs):
#         """ Load the model in a compressed binary format.
# 
#         :param filename: name of the model file
#         :param args: arguments
#         :param kwargs: arguments
#         :type filename: str
#         :type args: dict
#         :type kwargs: dict
#         """
#         return load_compact_model(filename, self.loadmodel, self.prefix, self.infodict, *args, **kwargs)
# 
#     def get_info(self):
#         """ Getting information for the dressed machine.
# 
#         :return: dictionary of the information for the dressed machine.
#         :rtype: dict
#         """
#         return {'classifier': self.infodict['classifier'],
#                 'prefix': self.prefix,
#                 'suffices': self.suffices}
# 
# Path: shorttext/utils/classification_exceptions.py
# class NotImplementedException(Exception):
#     def __init__(self):
#         self.message = 'Method not implemented.'


import numpy as np
import pickle
from gensim.corpora import Dictionary
from gensim.models import TfidfModel
from scipy.sparse import dok_matrix
from .compactmodel_io import CompactIOMachine
from .classification_exceptions import NotImplementedException



dtm_suffices = ['_docids.pkl', '_dictionary.dict', '_dtm.pkl']

class DocumentTermMatrix(CompactIOMachine):
    """ Document-term matrix for corpus.

    This is a class that handles the document-term matrix (DTM). With a given corpus, users can
    retrieve term frequency, document frequency, and total term frequency. Weighing using tf-idf
    can be applied.
    """
    def __init__(self, corpus, docids=None, tfidf=False):
        """ Initialize the document-term matrix (DTM) class with a given corpus.

        If document IDs (docids) are given, it will be stored and output as approrpriate.
        If not, the documents are indexed by numbers.

        Users can choose to weigh by tf-idf. The default is not to weigh.

        The corpus has to be a list of lists, with each of the inside list contains all the tokens
        in each document.

        :param corpus: corpus.
        :param docids: list of designated document IDs. (Default: None)
        :param tfidf: whether to weigh using tf-idf. (Default: False)
        :type corpus: list
        :type docids: list
        :type tfidf: bool
        """

# Path: shorttext/utils/compactmodel_io.py
# class CompactIOMachine:
#     """ Base class that implements compact model I/O.
# 
#     This is to replace the original :func:`compactio` decorator.
# 
#     """
#     def __init__(self, infodict, prefix, suffices):
#         """
# 
#         :param infodict: information about the model. Must contain the key 'classifier'.
#         :param prefix: prefix of names of the model file
#         :param suffices: suffices of the names of the model file
#         :type infodict: dict
#         :type prefix: str
#         :type suffices: list
#         """
#         self.infodict = infodict
#         self.prefix = prefix
#         self.suffices = suffices
# 
#     def savemodel(self, nameprefix):
#         """ Abstract method for `savemodel`.
# 
#         :param nameprefix: prefix of the model path
#         :type nameprefix: str
#         """
#         raise e.OperationNotDefinedException()
# 
#     def loadmodel(self, nameprefix):
#         """ Abstract method for `loadmodel`.
# 
#         :param nameprefix: prefix of the model path
#         :type nameprefix: str
#         """
#         raise e.OperationNotDefinedException()
# 
#     def save_compact_model(self, filename, *args, **kwargs):
#         """ Save the model in a compressed binary format.
# 
#         :param filename: name of the model file
#         :param args: arguments
#         :param kwargs: arguments
#         :type filename: str
#         :type args: dict
#         :type kwargs: dict
#         """
#         save_compact_model(filename, self.savemodel, self.prefix, self.suffices, self.infodict, *args, **kwargs)
# 
#     def load_compact_model(self, filename, *args, **kwargs):
#         """ Load the model in a compressed binary format.
# 
#         :param filename: name of the model file
#         :param args: arguments
#         :param kwargs: arguments
#         :type filename: str
#         :type args: dict
#         :type kwargs: dict
#         """
#         return load_compact_model(filename, self.loadmodel, self.prefix, self.infodict, *args, **kwargs)
# 
#     def get_info(self):
#         """ Getting information for the dressed machine.
# 
#         :return: dictionary of the information for the dressed machine.
#         :rtype: dict
#         """
#         return {'classifier': self.infodict['classifier'],
#                 'prefix': self.prefix,
#                 'suffices': self.suffices}
# 
# Path: shorttext/utils/classification_exceptions.py
# class NotImplementedException(Exception):
#     def __init__(self):
#         self.message = 'Method not implemented.'


# Path: shorttext/utils/dtm.py
import numpy as np
import pickle
from gensim.corpora import Dictionary
from gensim.models import TfidfModel
from scipy.sparse import dok_matrix
from .compactmodel_io import CompactIOMachine
from .classification_exceptions import NotImplementedException




dtm_suffices = ['_docids.pkl', '_dictionary.dict', '_dtm.pkl']

class DocumentTermMatrix(CompactIOMachine):
    """ Document-term matrix for corpus.

    This is a class that handles the document-term matrix (DTM). With a given corpus, users can
    retrieve term frequency, document frequency, and total term frequency. Weighing using tf-idf
    can be applied.
    """
    def __init__(self, corpus, docids=None, tfidf=False):
        """ Initialize the document-term matrix (DTM) class with a given corpus.

        If document IDs (docids) are given, it will be stored and output as approrpriate.
        If not, the documents are indexed by numbers.

        Users can choose to weigh by tf-idf. The default is not to weigh.

        The corpus has to be a list of lists, with each of the inside list contains all the tokens
        in each document.

        :param corpus: corpus.
        :param docids: list of designated document IDs. (Default: None)
        :param tfidf: whether to weigh using tf-idf. (Default: False)
        :type corpus: list
        :type docids: list
        :type tfidf: bool
        """

        CompactIOMachine.__init__(self, {'classifier': 'dtm'}, 'dtm', dtm_suffices)