# Path: shorttext/utils/misc.py
# def textfile_generator(textfile, linebreak=True, encoding=None):
#     """ Return a generator that reads lines in a text file.
# 
#     :param textfile: file object of a text file
#     :param linebreak: whether to return a line break at the end of each line (Default: True)
#     :param encoding: encoding of the text file (Default: None)
#     :return: a generator that reads lines in a text file
#     :type textfile: file
#     :type linebreak: bool
#     :type encoding: str
#     :rtype: generator
#     """
#     for t in textfile:
#         if len(t) > 0:
#             if encoding is None:
#                 yield t.strip() + ('\n' if linebreak else '')
#             else:
#                 yield t.decode(encoding).strip() + ('\n' if linebreak else '')


from functools import partial
from scipy.sparse import csc_matrix
from gensim.corpora import Dictionary
from sklearn.preprocessing import OneHotEncoder
from shorttext.utils.misc import textfile_generator
import numpy as np
        :param endsig: signal character at the end of the sentence (Default: False)
        :return: rank-3 tensor of the sentences
        :type sentences: list
        :type maxlen: int
        :type sparse: bool
        :type startsig: bool
        :type endsig: bool
        :rtype: scipy.sparse.csc_matrix or numpy.array
        """
        encode_sent_func = partial(self.encode_sentence, startsig=startsig, endsig=endsig, maxlen=maxlen)
        list_encoded_sentences_map = map(encode_sent_func, sentences)
        if sparse:
            return list(list_encoded_sentences_map)
        else:
            return np.array([sparsevec.toarray() for sparsevec in list_encoded_sentences_map])

    def __len__(self):
        return len(self.dictionary)


def initSentenceToCharVecEncoder(textfile, encoding=None):
    """ Instantiate a class of SentenceToCharVecEncoder from a text file.

    :param textfile: text file
    :param encoding: encoding of the text file (Default: None)
    :return: an instance of SentenceToCharVecEncoder
    :type textfile: file
    :type encoding: str
    :rtype: SentenceToCharVecEncoder
    """

# Path: shorttext/utils/misc.py
# def textfile_generator(textfile, linebreak=True, encoding=None):
#     """ Return a generator that reads lines in a text file.
# 
#     :param textfile: file object of a text file
#     :param linebreak: whether to return a line break at the end of each line (Default: True)
#     :param encoding: encoding of the text file (Default: None)
#     :return: a generator that reads lines in a text file
#     :type textfile: file
#     :type linebreak: bool
#     :type encoding: str
#     :rtype: generator
#     """
#     for t in textfile:
#         if len(t) > 0:
#             if encoding is None:
#                 yield t.strip() + ('\n' if linebreak else '')
#             else:
#                 yield t.decode(encoding).strip() + ('\n' if linebreak else '')


# Path: shorttext/generators/charbase/char2vec.py
from functools import partial
from scipy.sparse import csc_matrix
from gensim.corpora import Dictionary
from sklearn.preprocessing import OneHotEncoder
from shorttext.utils.misc import textfile_generator
import numpy as np

        :param endsig: signal character at the end of the sentence (Default: False)
        :return: rank-3 tensor of the sentences
        :type sentences: list
        :type maxlen: int
        :type sparse: bool
        :type startsig: bool
        :type endsig: bool
        :rtype: scipy.sparse.csc_matrix or numpy.array
        """
        encode_sent_func = partial(self.encode_sentence, startsig=startsig, endsig=endsig, maxlen=maxlen)
        list_encoded_sentences_map = map(encode_sent_func, sentences)
        if sparse:
            return list(list_encoded_sentences_map)
        else:
            return np.array([sparsevec.toarray() for sparsevec in list_encoded_sentences_map])

    def __len__(self):
        return len(self.dictionary)


def initSentenceToCharVecEncoder(textfile, encoding=None):
    """ Instantiate a class of SentenceToCharVecEncoder from a text file.

    :param textfile: text file
    :param encoding: encoding of the text file (Default: None)
    :return: an instance of SentenceToCharVecEncoder
    :type textfile: file
    :type encoding: str
    :rtype: SentenceToCharVecEncoder
    """

    dictionary = Dictionary(map(lambda line: [c for c in line], textfile_generator(textfile, encoding=encoding)))