class_docstr: Contextual string embeddings of words, as proposed in Akbik et al., 2018.
docstr: Initializes contextual string embeddings using a character-level language model.
  
  Args:
      model: model string, one of 'news-forward', 'news-backward', 'news-forward-fast', 'news-backward-fast',
          'mix-forward', 'mix-backward', 'german-forward', 'german-backward', 'polish-backward', 'polish-forward'
          depending on which character language model is desired.
      detach: if set to False, the gradient will propagate into the language model. this dramatically slows down
          training and often leads to worse results, so not recommended.
      use_cache: if set to False, will not write embeddings to file for later retrieval. this saves disk space but will
          not allow re-use of once computed embeddings that do not fit into memory
      cache_directory: if cache_directory is not set, the cache will be written to ~/.flair/embeddings. otherwise the cache
          is written to the provided directory.
docstr_tok: ["Initializes","contextual","string","embeddings","using","a","character-level","language","model",".","Args",":","model",":","model","string",",","one","of","'news-forward","'",",","'news-backward","'",",","'news-forward-fast","'",",","'news-backward-fast","'",",","'mix-forward","'",",","'mix-backward","'",",","'german-forward","'",",","'german-backward","'",",","'polish-backward","'",",","'polish-forward'","depending","on","which","character","language","model","is","desired",".","detach",":","if","set","to","False",",","the","gradient","will","propagate","into","the","language","model",".","this","dramatically","slows","down","training","and","often","leads","to","worse","results",",","so","not","recommended",".","use_cache",":","if","set","to","False",",","will","not","write","embeddings","to","file","for","later","retrieval",".","this","saves","disk","space","but","will","not","allow","re-use","of","once","computed","embeddings","that","do","not","fit","into","memory","cache_directory",":","if","cache_directory","is","not","set",",","the","cache","will","be","written","to","~\/.flair\/embeddings",".","otherwise","the","cache","is","written","to","the","provided","directory","."]
code: def __init__(
      self,
      model: str,
      detach: bool = True,
      use_cache: bool = False,
      cache_directory: Optional[Path] = None,
  ) -> None:
      """Initializes contextual string embeddings using a character-level language model.
  
      Args:
          model: model string, one of 'news-forward', 'news-backward', 'news-forward-fast', 'news-backward-fast',
              'mix-forward', 'mix-backward', 'german-forward', 'german-backward', 'polish-backward', 'polish-forward'
              depending on which character language model is desired.
          detach: if set to False, the gradient will propagate into the language model. this dramatically slows down
              training and often leads to worse results, so not recommended.
          use_cache: if set to False, will not write embeddings to file for later retrieval. this saves disk space but will
              not allow re-use of once computed embeddings that do not fit into memory
          cache_directory: if cache_directory is not set, the cache will be written to ~/.flair/embeddings. otherwise the cache
              is written to the provided directory.
      """
      super().__init__()
  
      cache_dir = Path("embeddings")
  
      # multilingual forward (English, German, French, Italian, Dutch, Polish)
      if model.lower() == "multi-forward":
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings-v0.4/lm-multi-forward-v0.1.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
      # multilingual backward  (English, German, French, Italian, Dutch, Polish)
      elif model.lower() == "multi-backward":
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings-v0.4/lm-multi-backward-v0.1.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
  
      # news-english-forward
      elif model.lower() == "news-forward":
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings/lm-news-english-forward-v0.2rc.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
  
      # news-english-backward
      elif model.lower() == "news-backward":
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings/lm-news-english-backward-v0.2rc.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
  
      # news-english-forward
      elif model.lower() == "news-forward-fast":
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings/lm-news-english-forward-1024-v0.2rc.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
  
      # news-english-backward
      elif model.lower() == "news-backward-fast":
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings/lm-news-english-backward-1024-v0.2rc.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
  
      # mix-english-forward
      elif model.lower() == "mix-forward":
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings/lm-mix-english-forward-v0.2rc.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
  
      # mix-english-backward
      elif model.lower() == "mix-backward":
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings/lm-mix-english-backward-v0.2rc.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
  
      # mix-german-forward
      elif (
          model.lower() == "german-forward"
          or model.lower() == "de-forward"
      ):
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings/lm-mix-german-forward-v0.2rc.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
  
      # mix-german-backward
      elif (
          model.lower() == "german-backward"
          or model.lower() == "de-backward"
      ):
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings/lm-mix-german-backward-v0.2rc.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
  
      # common crawl Polish forward
      elif (
          model.lower() == "polish-forward"
          or model.lower() == "pl-forward"
      ):
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings/lm-polish-forward-v0.2.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
  
      # common crawl Polish backward
      elif (
          model.lower() == "polish-backward"
          or model.lower() == "pl-backward"
      ):
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings/lm-polish-backward-v0.2.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
  
      # Slovenian forward
      elif (
          model.lower() == "slovenian-forward"
          or model.lower() == "sl-forward"
      ):
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings-v0.3/lm-sl-large-forward-v0.1.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
      # Slovenian backward
      elif (
          model.lower() == "slovenian-backward"
          or model.lower() == "sl-backward"
      ):
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings-v0.3/lm-sl-large-backward-v0.1.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
  
      # Bulgarian forward
      elif (
          model.lower() == "bulgarian-forward"
          or model.lower() == "bg-forward"
      ):
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings-v0.3/lm-bg-small-forward-v0.1.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
      # Bulgarian backward
      elif (
          model.lower() == "bulgarian-backward"
          or model.lower() == "bg-backward"
      ):
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings-v0.3/lm-bg-small-backward-v0.1.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
  
      # Dutch forward
      elif (
          model.lower() == "dutch-forward"
          or model.lower() == "nl-forward"
      ):
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings-v0.4/lm-nl-large-forward-v0.1.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
      # Dutch backward
      elif (
          model.lower() == "dutch-backward"
          or model.lower() == "nl-backward"
      ):
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings-v0.4/lm-nl-large-backward-v0.1.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
  
      # Swedish forward
      elif (
          model.lower() == "swedish-forward"
          or model.lower() == "sv-forward"
      ):
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings-v0.4/lm-sv-large-forward-v0.1.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
      # Swedish backward
      elif (
          model.lower() == "swedish-backward"
          or model.lower() == "sv-backward"
      ):
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings-v0.4/lm-sv-large-backward-v0.1.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
  
      # French forward
      elif (
          model.lower() == "french-forward"
          or model.lower() == "fr-forward"
      ):
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings/lm-fr-charlm-forward.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
      # French backward
      elif (
          model.lower() == "french-backward"
          or model.lower() == "fr-backward"
      ):
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings/lm-fr-charlm-backward.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
  
      # Czech forward
      elif (
          model.lower() == "czech-forward"
          or model.lower() == "cs-forward"
      ):
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings-v0.4/lm-cs-large-forward-v0.1.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
      # Czech backward
      elif (
          model.lower() == "czech-backward"
          or model.lower() == "cs-backward"
      ):
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings-v0.4/lm-cs-large-backward-v0.1.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
  
      # Portuguese forward
      elif (
          model.lower() == "portuguese-forward"
          or model.lower() == "pt-forward"
      ):
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings-v0.4/lm-pt-forward.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
      # Portuguese backward
      elif (
          model.lower() == "portuguese-backward"
          or model.lower() == "pt-backward"
      ):
          base_path = "https://s3.eu-central-1.amazonaws.com/alan-nlp/resources/embeddings-v0.4/lm-pt-backward.pt"
          model = cached_path(base_path, cache_dir=cache_dir)
  
      elif not Path(model).exists():
          raise ValueError(
              f'The given model "{model}" is not available or is not a valid path.'
          )
  
      self.name = str(model)
      self.static_embeddings = detach
  
      from flair.models import LanguageModel
  
      self.lm = LanguageModel.load_language_model(model)
      self.detach = detach
  
      self.is_forward_lm: bool = self.lm.is_forward_lm
  
      # initialize cache if use_cache set
      self.cache = None
      if use_cache:
          cache_path = (
              Path(f"{self.name}-tmp-cache.sqllite")
              if not cache_directory
              else cache_directory / f"{self.name}-tmp-cache.sqllite"
          )
          from sqlitedict import SqliteDict
  
          self.cache = SqliteDict(str(cache_path), autocommit=True)
  
      # embed a dummy sentence to determine embedding_length
      dummy_sentence: Sentence = Sentence(["hello"])
      embedded_dummy = self.embed(dummy_sentence)
      self.__embedding_length: int = len(
          embedded_dummy[0].get_token(1).get_embedding()
      )
  
      # set to eval mode
      self.eval()
  
imports: import logging
  import re
  from pathlib import Path
  from typing import List, Optional, Union
  import torch
  from deprecated.sphinx import deprecated
  import flair
  from flair.data import Sentence, Token
  from flair.embeddings.document import DocumentEmbeddings
  from flair.embeddings.token import StackedEmbeddings, TokenEmbeddings
  from flair.file_utils import cached_path
  from flair.nn import LockedDropout, WordDropout
label_desc: Use image node_id 1 to create a new CharLMEmbeddings object from inherited base classes: TokenEmbeddings with example: obj = CharLMEmbeddings(model, detach, use_cache, cache_directory)