code: def test_get_token_index(self):
      # The behavior of get_token_index depends on whether or not the namespace has an OOV token.
      vocab = Vocabulary(
          counter={
              "labels": {"foo": 3, "bar": 2},
              "tokens": {"foo": 3, "bar": 2},
          },
          non_padded_namespaces=["labels"],
      )
  
      # Quick sanity check, this is what the token to index mappings should look like.
      expected_token_to_index_dicts = {
          "tokens": {
              vocab._padding_token: 0,
              vocab._oov_token: 1,
              "foo": 2,
              "bar": 3,
          },
          "labels": {"foo": 0, "bar": 1},
      }
      assert (
          vocab._token_to_index["tokens"]
          == expected_token_to_index_dicts["tokens"]
      )
      assert (
          vocab._token_to_index["labels"]
          == expected_token_to_index_dicts["labels"]
      )
  
      # get_token_index should return the OOV token index for OOV tokens when it can.
      assert vocab.get_token_index("baz", "tokens") == 1
  
      # get_token_index should raise helpful error message when token is OOV and there
      # is no default OOV token in the namespace.
      with pytest.raises(
          KeyError,
          match=r"'baz' not found .* and namespace does not contain the default OOV token .*",
      ):
          vocab.get_token_index("baz", "labels")
  
      # same should happen for the default OOV token itself, if not in namespace.
      with pytest.raises(
          KeyError, match=rf"'{vocab._oov_token}' not found .*"
      ):
          vocab.get_token_index(vocab._oov_token, "labels")
  
      # Now just make sure the token_to_index mappings haven't been modified
      # (since we're defaultdicts we need to be a little careful here).
      assert (
          vocab._token_to_index["tokens"]
          == expected_token_to_index_dicts["tokens"]
      )
      assert (
          vocab._token_to_index["labels"]
          == expected_token_to_index_dicts["labels"]
      )
  
imports: import codecs
  import gzip
  import pickle
  import shutil
  import zipfile
  from copy import deepcopy
  import pytest
  from allennlp.common.checks import ConfigurationError
  from allennlp.common.params import Params
  from allennlp.common.testing import AllenNlpTestCase
  from allennlp.data import Instance, Token
  from allennlp.data.batch import Batch
  from allennlp.data.fields import TextField
  from allennlp.data.token_indexers import SingleIdTokenIndexer, TokenCharactersIndexer
  from allennlp.data.tokenizers import CharacterTokenizer
  from allennlp.data.vocabulary import _NamespaceDependentDefaultDict, _read_pretrained_tokens, DEFAULT_OOV_TOKEN, Vocabulary
  from allennlp.modules.token_embedders.embedding import format_embeddings_file_uri
label_desc: Use image node_id 11 for calling the TestVocabulary obj's underlying member method code with example usage: obj.test_get_token_index() without return types