code: def _get_categorical_feature_encoding(
      category_frequencies: Dict[Any, int], max_encoding_length: int
  ) -> Dict[Any, int]:
      """Gets the encoding for a categorical feature based on category frequency.
  
      Assigns a unique index for the max_encoding_length-1 most frequently occurring
      categories. This index corresponds to the index of the encoding this category
      maps to.
  
      Args:
        category_frequencies: A dict where the key is the category and the value is
          the number of times the category occurred.
        max_encoding_length: The maximum length of an encoded feature value.
  
      Returns:
        A dict where the key is the category and the value is an int which
          corresponds to the index of the encoding which the category maps to.
      """
      categorical_feature_encoding = {}
      for index, value in enumerate(
          sorted(
              category_frequencies,
              key=category_frequencies.get,
              reverse=True,
          )[: max_encoding_length - 1]
      ):
          categorical_feature_encoding[value] = index
      return categorical_feature_encoding
  
imports: import collections
  from typing import Any, Dict, Iterable, List, Optional, Set, Tuple
  from absl import logging
  import apache_beam
  import numpy
  import pandas
  import pyarrow
  from tensorflow_data_validation import types
  from tensorflow_data_validation.statistics.generators import partitioned_stats_generator
  from tensorflow_data_validation.utils import feature_partition_util
  from tensorflow_data_validation.utils import mutual_information_util
  from tensorflow_data_validation.utils import schema_util
  from tensorflow_data_validation.utils import stats_util
  from tfx_bsl.arrow import array_util
  from tensorflow_metadata.proto.v0 import schema_pb2
  from tensorflow_metadata.proto.v0 import statistics_pb2
label_desc: Use image node_id 2 for calling a global function with example usage: _get_categorical_feature_encoding(category_frequencies, max_encoding_length) and returns: categorical_feature_encoding