(
        self,
        matrix: sp.csr_matrix,
        vocabulary: Dict[str, int],
        axis: Literal["rows", 0] | Literal["columns", 1],
    )
Sort terms in ``vocabulary`` alphabetically, modifying the vocabulary
        in-place, and returning a correspondingly reordered ``matrix`` along
        its rows or columns, depending on ``axis``.

        Args:
            matrix
            vocabulary
            axis
Sort terms in ``vocabulary`` alphabetically, modifying the vocabulary
        in-place, and returning a correspondingly reordered ``matrix`` along
        its rows or columns, depending on ``axis``.
Sort terms in vocabulary alphabetically modifying the vocabulary in - place and returning a correspondingly reordered matrix along its rows or columns depending on axis .
def _sort_vocab_and_matrix(
        self,
        matrix: sp.csr_matrix,
        vocabulary: Dict[str, int],
        axis: Literal["rows", 0] | Literal["columns", 1],
    ) -> sp.csr_matrix:
        """
        Sort terms in ``vocabulary`` alphabetically, modifying the vocabulary
        in-place, and returning a correspondingly reordered ``matrix`` along
        its rows or columns, depending on ``axis``.

        Args:
            matrix
            vocabulary
            axis
        """
        sorted_vocab = sorted(vocabulary.items())
        new_idx_array = np.empty(len(sorted_vocab), dtype=np.int32)
        for new_idx, (term, old_idx) in enumerate(sorted_vocab):
            new_idx_array[new_idx] = old_idx
            vocabulary[term] = new_idx
        # use fancy indexing to reorder rows or columns
        if axis == "rows" or axis == 0:
            return matrix[new_idx_array, :]
        elif axis == "columns" or axis == 1:
            return matrix[:, new_idx_array]
        else:
            raise ValueError(
                errors.value_invalid_msg("axis", axis, {"rows", "columns", 0, 1})
            )
def _sort_vocab_and_matrix ( self , matrix : sp . csr_matrix , vocabulary : Dict [ str , int ] , axis : Literal [ "rows" , 0 ] | Literal [ "columns" , 1 ] , ) -> sp . csr_matrix : sorted_vocab = sorted ( vocabulary . items ( ) ) new_idx_array = np . empty ( len ( sorted_vocab ) , dtype = np . int32 ) for new_idx , ( term , old_idx ) in enumerate ( sorted_vocab ) : new_idx_array [ new_idx ] = old_idx vocabulary [ term ] = new_idx # use fancy indexing to reorder rows or columns if axis == "rows" or axis == 0 : return matrix [ new_idx_array , : ] elif axis == "columns" or axis == 1 : return matrix [ : , new_idx_array ] else : raise ValueError ( errors . value_invalid_msg ( "axis" , axis , { "rows" , "columns" , 0 , 1 } ) )