code: def test_categorical_encoder_saving(tmpdir):
      from speechbrain.dataio.encoder import CategoricalEncoder
  
      encoder = CategoricalEncoder(starting_index=3)
      encoding_file = tmpdir / "char_encoding.txt"
      # First time this runs, the encoding is created:
      if not encoder.load_if_possible(encoding_file):
          encoder.update_from_iterable("abcd")
          encoder.save(encoding_file)
      else:
          assert False  # We should not get here!
      # Now, imagine a recovery:
      encoder = CategoricalEncoder()
      encoder.expect_len(4)
      # The second time, the encoding is just loaded from file:
      if not encoder.load_if_possible(encoding_file):
          assert False  # We should not get here!
      integers = encoder.encode_sequence("dcba")
      assert all(isinstance(i, int) for i in integers)
      assert encoder.starting_index == 3  # This is also loaded
  
      # Also possible to encode tuples and load
      encoder = CategoricalEncoder()
      encoding_file = tmpdir / "tuple_encoding.txt"
      encoder.add_label((1, 2, 3))
      encoder.insert_label((1, 2), index=-1)
      encoder.save(encoding_file)
      # Reload
      encoder = CategoricalEncoder()
      encoder.expect_len(2)
      assert encoder.load_if_possible(encoding_file)
      assert encoder.encode_label((1, 2)) == -1
  
      # Load unk:
      encoder = CategoricalEncoder(unk_label="UNKNOWN")
      encoding_file = tmpdir / "unk_encoding.txt"
      encoder.update_from_iterable("abc")
      encoder.save(encoding_file)
      encoder = CategoricalEncoder()
      encoder.expect_len(4)
      assert encoder.load_if_possible(encoding_file)
      assert encoder.encode_label("a") == 1
      assert encoder.decode_ndim(encoder.encode_label("d")) == "UNKNOWN"
      # Even if set differently:
      encoder = CategoricalEncoder()
      encoder.add_unk()
      encoder.expect_len(4)
      assert encoder.load_if_possible(encoding_file)
      assert encoder.encode_label("a") == 1
      assert encoder.decode_ndim(encoder.encode_label("d")) == "UNKNOWN"
  
imports: import pytest
label_desc: Use image node_id 2 for calling a global function with example usage: test_categorical_encoder_saving(tmpdir) without return types