class_docstr: Contextual string embeddings of words, as proposed in Akbik et al., 2018.
code: def _add_embeddings_internal(
      self, sentences: List[Sentence]
  ) -> List[Sentence]:
      # if cache is used, try setting embeddings from cache first
      if "cache" in self.__dict__ and self.cache is not None:
          # try populating embeddings from cache
          all_embeddings_retrieved_from_cache: bool = True
          for sentence in sentences:
              key = sentence.to_tokenized_string()
              embeddings = self.cache.get(key)
  
              if not embeddings:
                  all_embeddings_retrieved_from_cache = False
                  break
              else:
                  for token, embedding in zip(sentence, embeddings):
                      token.set_embedding(
                          self.name, torch.FloatTensor(embedding)
                      )
  
          if all_embeddings_retrieved_from_cache:
              return sentences
  
      # if this is not possible, use LM to generate embedding. First, get text sentences
      text_sentences = [
          sentence.to_tokenized_string() for sentence in sentences
      ]
  
      start_marker = "\n"
      end_marker = " "
  
      # get hidden states from language model
      all_hidden_states_in_lm = self.lm.get_representation(
          text_sentences, start_marker, end_marker, self.chars_per_chunk
      )
  
      # take first or last hidden states from language model as word representation
      for i, sentence in enumerate(sentences):
          sentence_text = sentence.to_tokenized_string()
  
          offset_forward: int = len(start_marker)
          offset_backward: int = len(sentence_text) + len(start_marker)
  
          for token in sentence.tokens:
              offset_forward += len(token.text)
  
              offset = (
                  offset_forward
                  if self.is_forward_lm
                  else offset_backward
              )
  
              embedding = all_hidden_states_in_lm[offset, i, :]
  
              # if self.tokenized_lm or token.whitespace_after:
              offset_forward += 1
              offset_backward -= 1
  
              offset_backward -= len(token.text)
  
              token.set_embedding(self.name, embedding)
  
      if "cache" in self.__dict__ and self.cache is not None:
          for sentence in sentences:
              self.cache[sentence.to_tokenized_string()] = [
                  token._embeddings[self.name].tolist()
                  for token in sentence
              ]
  
      return sentences
  
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 5 for calling the CharLMEmbeddings obj's underlying member method code with example usage: obj._add_embeddings_internal(sentences) and returns: sentences, sentences