def compact(self, term_doc_matrix, verbose=False):
        '''
        Parameters
        ----------
        term_doc_matrix : TermDocMatrix
            Term document matrix object to compact
        Returns
        -------
        New term doc matrix
        '''

        rank_df = self.scorer.get_rank_df(term_doc_matrix)
        optimal_rank = self._find_optimal_rank(rank_df)

        terms_to_remove = rank_df.index[np.isnan(rank_df[rank_df <= optimal_rank])
        .apply(lambda x: all(x), axis=1)]

        if self.include_n_most_frequent_terms > 0:
            most_frequent_terms = self.scorer.get_frequencies(term_doc_matrix).sort_values(
                ascending=False
            ).index[:self.include_n_most_frequent_terms]
            terms_to_remove = set(terms_to_remove) - set(most_frequent_terms)

        compacted_term_doc_matrix = self._remove_terms(term_doc_matrix, terms_to_remove)

        if verbose:
            #print('max terms', self.max_terms,
            #      'optimal_rank', optimal_rank,
            #      'num_terms', compacted_term_doc_matrix.get_num_terms())
            pass
        return compacted_term_doc_matrix

        Parameters
        ----------
        term_doc_matrix : TermDocMatrix
            Term document matrix object to compact
        Returns
        -------
        New term doc matrix
        
scattertext/termcompaction/AssociationCompactor.py