# Path: shorttext/utils/classification_exceptions.py
# class ModelNotTrainedException(Exception):
#     def __init__(self):
#         self.message = 'Model not trained.'
# 
# Path: shorttext/utils/wordembed.py
# def shorttext_to_avgvec(shorttext, wvmodel):
#     """ Convert the short text into an averaged embedded vector representation.
# 
#     Given a short sentence, it converts all the tokens into embedded vectors according to
#     the given word-embedding model, sums
#     them up, and normalize the resulting vector. It returns the resulting vector
#     that represents this short sentence.
# 
#     :param shorttext: a short sentence
#     :param wvmodel: word-embedding model
#     :return: an embedded vector that represents the short sentence
#     :type shorttext: str
#     :type wvmodel: gensim.models.keyedvectors.KeyedVectors
#     :rtype: numpy.ndarray
#     """
#     vec = np.sum([wvmodel[token] for token in tokenize(shorttext) if token in wvmodel], axis=0)
# 
#     # normalize
#     norm = np.linalg.norm(vec)
#     if norm != 0:
#         vec /= norm
# 
#     return vec
# 
# 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}


import pickle
import numpy as np
from collections import defaultdict
from scipy.spatial.distance import cosine
from shorttext.utils.classification_exceptions import ModelNotTrainedException
from shorttext.utils import shorttext_to_avgvec
from shorttext.utils.compactmodel_io import CompactIOMachine
        If this has not been run, or a model was not loaded by :func:`~loadmodel`,
        a `ModelNotTrainedException` will be raised while performing prediction or saving
        the model.

        :param classdict: training data
        :return: None
        :type classdict: dict
        """
        self.addvec = defaultdict(lambda : np.zeros(self.vecsize))
        for classtype in classdict:
            self.addvec[classtype] = np.sum([self.shorttext_to_embedvec(shorttext)
                                             for shorttext in classdict[classtype]],
                                            axis=0)
            self.addvec[classtype] /= np.linalg.norm(self.addvec[classtype])
        self.addvec = dict(self.addvec)
        self.trained = True

    def savemodel(self, nameprefix):
        """ Save the trained model into files.

        Given the prefix of the file paths, save the model into files, with name given by the prefix,
        and add "_embedvecdict.pickle" at the end. If there is no trained model, a `ModelNotTrainedException`
        will be thrown.

        :param nameprefix: prefix of the file path
        :return: None
        :type nameprefix: str
        :raise: ModelNotTrainedException
        """
        if not self.trained:

# Path: shorttext/utils/classification_exceptions.py
# class ModelNotTrainedException(Exception):
#     def __init__(self):
#         self.message = 'Model not trained.'
# 
# Path: shorttext/utils/wordembed.py
# def shorttext_to_avgvec(shorttext, wvmodel):
#     """ Convert the short text into an averaged embedded vector representation.
# 
#     Given a short sentence, it converts all the tokens into embedded vectors according to
#     the given word-embedding model, sums
#     them up, and normalize the resulting vector. It returns the resulting vector
#     that represents this short sentence.
# 
#     :param shorttext: a short sentence
#     :param wvmodel: word-embedding model
#     :return: an embedded vector that represents the short sentence
#     :type shorttext: str
#     :type wvmodel: gensim.models.keyedvectors.KeyedVectors
#     :rtype: numpy.ndarray
#     """
#     vec = np.sum([wvmodel[token] for token in tokenize(shorttext) if token in wvmodel], axis=0)
# 
#     # normalize
#     norm = np.linalg.norm(vec)
#     if norm != 0:
#         vec /= norm
# 
#     return vec
# 
# 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/classifiers/embed/sumvec/SumEmbedVecClassification.py
import pickle
import numpy as np
from collections import defaultdict
from scipy.spatial.distance import cosine
from shorttext.utils.classification_exceptions import ModelNotTrainedException
from shorttext.utils import shorttext_to_avgvec
from shorttext.utils.compactmodel_io import CompactIOMachine

        If this has not been run, or a model was not loaded by :func:`~loadmodel`,
        a `ModelNotTrainedException` will be raised while performing prediction or saving
        the model.

        :param classdict: training data
        :return: None
        :type classdict: dict
        """
        self.addvec = defaultdict(lambda : np.zeros(self.vecsize))
        for classtype in classdict:
            self.addvec[classtype] = np.sum([self.shorttext_to_embedvec(shorttext)
                                             for shorttext in classdict[classtype]],
                                            axis=0)
            self.addvec[classtype] /= np.linalg.norm(self.addvec[classtype])
        self.addvec = dict(self.addvec)
        self.trained = True

    def savemodel(self, nameprefix):
        """ Save the trained model into files.

        Given the prefix of the file paths, save the model into files, with name given by the prefix,
        and add "_embedvecdict.pickle" at the end. If there is no trained model, a `ModelNotTrainedException`
        will be thrown.

        :param nameprefix: prefix of the file path
        :return: None
        :type nameprefix: str
        :raise: ModelNotTrainedException
        """
        if not self.trained:

            raise ModelNotTrainedException()