def build_doc_term_matrix(terms_lists,
                          weighting='tf',
                          normalize=False, sublinear_tf=False, smooth_idf=True,
                          min_df=1, max_df=1.0, min_ic=0.0, max_n_terms=None):
    """
    Build a document-term matrix of shape (# docs, # unique terms) from a sequence
    of documents, each represented as a sequence of (str) terms, with a variety of
    weighting and normalization schemes for the matrix values.

    Args:
        terms_lists (iterable(iterable(str))): a sequence of documents, each as a
            sequence of (str) terms; note that the terms in each doc are to be
            counted, so these probably shouldn't be sets containing *unique* terms;
            example inputs::

                >>> ([tok.lemma_ for tok in spacy_doc]
                ...  for spacy_doc in spacy_docs)
                >>> ((ne.text for ne in doc.named_entities())
                ...  for doc in textcorpus)
                >>> (tuple(ng.text for ng in itertools.chain.from_iterable(doc.ngrams(i) for i in range(1, 3)))
                ...  for doc in docs)

        weighting (str {'tf', 'tfidf', 'binary'}, optional): if 'tf', matrix values
            (i, j) correspond to the number of occurrences of term j in doc i; if
            'tfidf', term frequencies (tf) are multiplied by their corresponding
            inverse document frequencies (idf); if 'binary', all non-zero values
            are set equal to 1
        normalize (bool, optional): if True, normalize term frequencies by the
            L2 norms of the vectors
        binarize (bool, optional): if True, set all term frequencies greater than
            0 equal to 1
        sublinear_tf (bool, optional): if True, apply sub-linear term-frequency
            scaling, i.e. tf => 1 + log(tf)
        smooth_idf (bool, optional): if True, add 1 to all document frequencies,
            equivalent to adding a single document to the corpus containing every
            unique term
        min_df (float or int, optional): if float, value is the fractional proportion
            of the total number of documents, which must be in [0.0, 1.0]; if int,
            value is the absolute number; filter terms whose document frequency
            is less than ``min_df``
        max_df (float or int, optional): if float, value is the fractional proportion
            of the total number of documents, which must be in [0.0, 1.0]; if int,
            value is the absolute number; filter terms whose document frequency
            is greater than ``max_df``
        min_ic (float, optional): filter terms whose information content is less
            than `min_ic`; value must be in [0.0, 1.0]
        max_n_terms (int, optional): only include terms whose document frequency
            is within the top ``max_n_terms``

    Returns:
        :class:`scipy.sparse.csr_matrix <scipy.sparse.csr_matrix>`: sparse matrix
            of shape (# docs, # unique terms), where value (i, j) is the weight
            of term j in doc i
        dict: id to term mapping, where keys are unique integers as term ids and
            values are corresponding strings
    """
    stringstore = StringStore()
    data = []; rows = []; cols = []
    for row_idx, terms_list in enumerate(terms_lists):

        # an empty string always occupies index 0 in the stringstore, which causes
        # an empty first col in the doc-term matrix that we don't want;
        # so, we subtract 1 from the stringstore's assigned id
        bow = tuple((stringstore[term] - 1, count)
                    for term, count in collections.Counter(terms_list).items()
                    if term)

        data.extend(count for _, count in bow)
        cols.extend(term_id for term_id, _ in bow)
        rows.extend(itertools.repeat(row_idx, times=len(bow)))

    doc_term_matrix = sp.coo_matrix((data, (rows, cols)), dtype=int).tocsr()
    # ignore the 0-index empty string in stringstore, as above
    id_to_term = {term_id - 1: term for term_id, term in enumerate(stringstore)
                  if term_id != 0}

    # filter terms by document frequency or information content?
    if max_df != 1.0 or min_df != 1 or max_n_terms is not None:
        doc_term_matrix, id_to_term = filter_terms_by_df(
            doc_term_matrix, id_to_term,
            max_df=max_df, min_df=min_df, max_n_terms=max_n_terms)
    if min_ic != 0.0:
        doc_term_matrix, id_to_term = filter_terms_by_ic(
            doc_term_matrix, id_to_term,
            min_ic=min_ic, max_n_terms=max_n_terms)

    if weighting == 'binary':
        doc_term_matrix = binarize_mat(doc_term_matrix, threshold=0.0, copy=False)
    else:
        if sublinear_tf is True:
            doc_term_matrix = doc_term_matrix.astype(np.float64)
            np.log(doc_term_matrix.data, doc_term_matrix.data)
            doc_term_matrix.data += 1
        if weighting == 'tfidf':
            doc_term_matrix = apply_idf_weighting(doc_term_matrix,
                                                  smooth_idf=smooth_idf)

    if normalize is True:
        doc_term_matrix = normalize_mat(doc_term_matrix,
                                        norm='l2', axis=1, copy=False)

    return (doc_term_matrix, id_to_term)
dataset/ETHPy150Open chartbeat-labs/textacy/textacy/representations/vsm.py/build_doc_term_matrix/original