(
        self, vocabulary: Dict[str, int] | Iterable[str]
    )
return vocabulary, is_fixed
Validate an input vocabulary. If it's a mapping, ensure that term ids
        are unique and compact (i.e. without any gaps between 0 and the number
        of terms in ``vocabulary``. If it's a sequence, sort terms then assign
        integer ids in ascending order.
Validate an input vocabulary. If it's a mapping, ensure that term ids
        are unique and compact (i.e. without any gaps between 0 and the number
        of terms in ``vocabulary``. If it's a sequence, sort terms then assign
        integer ids in ascending order.
Validate an input vocabulary . If it s a mapping ensure that term ids are unique and compact ( i . e . without any gaps between 0 and the number of terms in vocabulary . If it s a sequence sort terms then assign integer ids in ascending order .
def _validate_vocabulary(
        self, vocabulary: Dict[str, int] | Iterable[str]
    ) -> Tuple[Dict[str, int], bool]:
        """
        Validate an input vocabulary. If it's a mapping, ensure that term ids
        are unique and compact (i.e. without any gaps between 0 and the number
        of terms in ``vocabulary``. If it's a sequence, sort terms then assign
        integer ids in ascending order.
        """
        if vocabulary is not None:
            if not isinstance(vocabulary, collections.abc.Mapping):
                vocab = {}
                for i, term in enumerate(sorted(vocabulary)):
                    if vocab.setdefault(term, i) != i:
                        raise ValueError(
                            f"Terms in `vocabulary` must be unique, but '{term}' "
                            "was found more than once."
                        )
                vocabulary = vocab
            else:
                ids = set(vocabulary.values())
                if len(ids) != len(vocabulary):
                    counts = collections.Counter(vocabulary.values())
                    n_dupe_term_ids = sum(
                        1
                        for term_id, term_id_count in counts.items()
                        if term_id_count > 1
                    )
                    raise ValueError(
                        "Term ids in `vocabulary` must be unique, but "
                        f"{n_dupe_term_ids} ids were assigned to more than one term."
                    )
                for i in range(len(vocabulary)):
                    if i not in ids:
                        raise ValueError(
                            "Term ids in `vocabulary` must be compact, i.e. "
                            f"not have any gaps, but term id {i} is missing from "
                            f"a vocabulary of {len(vocabulary)} terms"
                        )
            if not vocabulary:
                raise ValueError("`vocabulary` must not be empty.")
            is_fixed = True
        else:
            is_fixed = False
        return vocabulary, is_fixed
def _validate_vocabulary ( self , vocabulary : Dict [ str , int ] | Iterable [ str ] ) -> Tuple [ Dict [ str , int ] , bool ] : if vocabulary is not None : if not isinstance ( vocabulary , collections . abc . Mapping ) : vocab = { } for i , term in enumerate ( sorted ( vocabulary ) ) : if vocab . setdefault ( term , i ) != i : raise ValueError ( f"Terms in `vocabulary` must be unique, but '{term}' " "was found more than once." ) vocabulary = vocab else : ids = set ( vocabulary . values ( ) ) if len ( ids ) != len ( vocabulary ) : counts = collections . Counter ( vocabulary . values ( ) ) n_dupe_term_ids = sum ( 1 for term_id , term_id_count in counts . items ( ) if term_id_count > 1 ) raise ValueError ( "Term ids in `vocabulary` must be unique, but " f"{n_dupe_term_ids} ids were assigned to more than one term." ) for i in range ( len ( vocabulary ) ) : if i not in ids : raise ValueError ( "Term ids in `vocabulary` must be compact, i.e. " f"not have any gaps, but term id {i} is missing from " f"a vocabulary of {len(vocabulary)} terms" ) if not vocabulary : raise ValueError ( "`vocabulary` must not be empty." ) is_fixed = True else : is_fixed = False return vocabulary , is_fixed