class_docstr: Utility class for opening embeddings text files. Handles various compression formats,
  as well as context management.
  
  # Parameters
  
  file_uri : `str`
      It can be:
  
      * a file system path or a URL of an eventually compressed text file or a zip/tar archive
        containing a single file.
      * URI of the type `(archive_path_or_url)#file_path_inside_archive` if the text file
        is contained in a multi-file archive.
  
  encoding : `str`
  cache_dir : `str`
class_docstr_tok: ["Utility","class","for","opening","embeddings","text","files",".","Handles","various","compression","formats",",","as","well","as","context","management",".","#","Parameters","file_uri",":","`","str","`","It","can","be",":","*","a","file","system","path","or","a","URL","of","an","eventually","compressed","text","file","or","a","zip\/tar","archive","containing","a","single","file",".","*","URI","of","the","type","`","(","archive_path_or_url",")","#","file_path_inside_archive","`","if","the","text","file","is","contained","in","a","multi-file","archive",".","encoding",":","`","str","`","cache_dir",":","`","str","`"]
code: def __init__(
      self,
      file_uri: str,
      encoding: str = DEFAULT_ENCODING,
      cache_dir: str = None,
  ) -> None:
      self.uri = file_uri
      self._encoding = encoding
      self._cache_dir = cache_dir
      self._archive_handle: Any = (
          None  # only if the file is inside an archive
      )
  
      main_file_uri, path_inside_archive = parse_embeddings_file_uri(
          file_uri
      )
      main_file_local_path = cached_path(
          main_file_uri, cache_dir=cache_dir
      )
  
      if zipfile.is_zipfile(main_file_local_path):  # ZIP archive
          self._open_inside_zip(main_file_uri, path_inside_archive)
  
      elif tarfile.is_tarfile(main_file_local_path):  # TAR archive
          self._open_inside_tar(main_file_uri, path_inside_archive)
  
      else:  # all the other supported formats, including uncompressed files
          if path_inside_archive:
              raise ValueError(
                  "Unsupported archive format: %s" + main_file_uri
              )
  
          # All the python packages for compressed files share the same interface of io.open
          extension = get_file_extension(main_file_uri)
  
          # Some systems don't have support for all of these libraries, so we import them only
          # when necessary.
          package = None
          if extension in [".txt", ".vec"]:
              package = io
          elif extension == ".gz":
              import gzip
  
              package = gzip
          elif extension == ".bz2":
              import bz2
  
              package = bz2
          elif extension == ".xz":
              import lzma
  
              package = lzma
  
          if package is None:
              logger.warning(
                  'The embeddings file has an unknown file extension "%s". '
                  "We will assume the file is an (uncompressed) text file",
                  extension,
              )
              package = io
  
          self._handle = package.open(  # type: ignore
              main_file_local_path, "rt", encoding=encoding
          )
  
      # To use this with tqdm we'd like to know the number of tokens. It's possible that the
      # first line of the embeddings file contains this: if it does, we want to start iteration
      # from the 2nd line, otherwise we want to start from the 1st.
      # Unfortunately, once we read the first line, we cannot move back the file iterator
      # because the underlying file may be "not seekable"; we use itertools.chain instead.
      first_line = next(self._handle)  # this moves the iterator forward
      self.num_tokens = (
          EmbeddingsTextFile._get_num_tokens_from_first_line(first_line)
      )
      if self.num_tokens:
          # the first line is a header line: start iterating from the 2nd line
          self._iterator = self._handle
      else:
          # the first line is not a header line: start iterating from the 1st line
          self._iterator = itertools.chain([first_line], self._handle)
  
imports: import io
  import itertools
  import logging
  import re
  import tarfile
  import warnings
  import zipfile
  from typing import Any, cast, Iterator, NamedTuple, Optional, Sequence, Tuple, BinaryIO
  import numpy
  import torch
  from torch.nn.functional import embedding
  from allennlp.common import Tqdm
  from allennlp.common.checks import ConfigurationError
  from allennlp.common.file_utils import cached_path, get_file_extension, is_url_or_existing_file
  from allennlp.data.vocabulary import Vocabulary
  from allennlp.modules.time_distributed import TimeDistributed
  from allennlp.modules.token_embedders.token_embedder import TokenEmbedder
  from allennlp.nn import util
label_desc: Use image node_id 1 to create a new EmbeddingsTextFile object from inherited base classes: Iterator with example: obj = EmbeddingsTextFile(file_uri, encoding, cache_dir)