code: def _encode_multivalent_categorical_feature(
      feature_array: pa.Array, max_encoding_length: int
  ) -> List[int]:
      """Encodes multivalent categorical features into fixed length representation.
  
      Categorical multivalent features are encoded using a bag-of-words strategy.
      Encodings are obtained by counting the occurences of each unique value in the
      feature domain for each example. If the number of unique values in the
      feature's domain exceeds max_encoding_length, the top
      (max_encoding_length - 1) ocurring categories will be used to encode
      examples. The presence of other less frequently occurring values will
      contribute to the frequency count of the last category.
  
      Args:
        feature_array: Arrow Array.
        max_encoding_length: The maximum length of an encoded feature value.
  
      Returns:
        A list containing the encoded feature values for each example.
      """
      flattened_feature_values = (
          _get_flattened_feature_values_without_nulls(feature_array)
      )
      category_frequencies = dict(
          zip(*np.unique(flattened_feature_values, return_counts=True))
      )
      if not category_frequencies:
          encoding_length = max_encoding_length
      else:
          encoding_length = min(
              max_encoding_length, len(category_frequencies)
          )
      categorical_encoding = _get_categorical_feature_encoding(
          category_frequencies, max_encoding_length
      )
      return _apply_categorical_encoding_to_feature_array(
          feature_array, categorical_encoding, encoding_length
      )
  
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 4 for calling a global function with example usage: _encode_multivalent_categorical_feature(feature_array, max_encoding_length) and returns: _apply_categorical_encoding_to_feature_array