code: def load_embeddings(
      vocab=None,
      pretrained_name_or_dir="glove.6B.50d",
      unknown="<unk>",
      unk_method=None,
  ):
      """Load pretrained word embeddings for building an embedding matrix for a given Vocab.
  
      This function supports loading GloVe, Word2Vec and FastText word embeddings from remote sources.
      You can also load your own embedding file(txt with Word2Vec or GloVe format) from a given file path.
  
      Glove: an unsupervised learning algorithm for obtaining vector representations for words.
      Training is performed on aggregated global word-word co-occurrence statistics from a corpus, and
      the resulting representations showcase interesting linear substructures of the word vector
      space. (Source from https://nlp.stanford.edu/projects/glove/)
      
      Available sources:
      ['glove.42B.300d', 'glove.6B.100d', 'glove.6B.200d', 'glove.6B.300d', 'glove.6B.50d', \
       'glove.840B.300d', 'glove.twitter.27B.100d', 'glove.twitter.27B.200d', \
       'glove.twitter.27B.25d', 'glove.twitter.27B.50d']
  
      Word2Vec: an unsupervised learning algorithm for obtaining vector representations for words.
      Training is performed with continuous bag-of-words or skip-gram architecture for computing vector
      representations of words.
  
      Available sources:
      ['GoogleNews-vectors-negative300', 'freebase-vectors-skipgram1000', \
       'freebase-vectors-skipgram1000-en']
  
      FastText: an open-source, free, lightweight library that allows users to learn text
      representations and text classifiers. It works on standard, generic hardware. Models can later
      be reduced in size to even fit on mobile devices. (Source from https://fasttext.cc/)
  
      Available sources:
      ['cc.af.300', ..., 'cc.en.300', ..., 'crawl-300d-2M', 'crawl-300d-2M-subword', \
       'wiki-news-300d-1M', 'wiki-news-300d-1M-subword', \
       'wiki.aa', ..., 'wiki.multi.ar', ..., 'wiki.zu']
  
      Detailed sources can be founded by `gluonnlp.embedding.list_sources('FastText')`
  
      For 'wiki.multi' embedding:
      Word Translation Without Parallel Data
      Alexis Conneau, Guillaume Lample, Marc'Aurelio Ranzato, Ludovic Denoyer, and Herve Jegou.
      https://arxiv.org/abs/1710.04087
  
      Parameters
      ----------
      vocab : gluonnlp.data.Vocab object, default None
          A vocabulary on which an embedding matrix is built.
          If `vocab` is `None`, then all tokens in the pretrained file will be used.
      pretrained_name_or_dir : str, default 'glove.6B.50d'
          A file path for a pretrained embedding file or the name of the pretrained token embedding file.
          This method would first check if it is a file path.
          If not, the method will load from cache or download.
      unknown : str, default '<unk>'
          To specify the unknown token in the pretrained file.
      unk_method : Callable, default None
          A function which receives `List[str]` and returns `numpy.ndarray`.
          The input of the function is a list of words which are in the `vocab`,
          but do not occur in the pretrained file.
          And the function is aimed to return an embedding matrix for these words.
          If `unk_method` is None, we generate vectors for these words,
          by sampling from normal distribution with the same std and mean of the embedding matrix.
          It is only useful when `vocab` is not `None`.
  
      Returns
      -------
      If `vocab` is `None`
          numpy.ndarray:
              An embedding matrix in the pretrained file.
          gluonnlp.data.Vocab:
              The vocabulary in the pretrained file.
      Otherwise,
          numpy.ndarray:
              An embedding matrix for the given vocabulary.
      """
      assert isinstance(
          vocab, (Vocab, type(None))
      ), "Only gluonnlp.data.Vocab is supported."
      file_path = _check_and_get_path(pretrained_name_or_dir)
      if file_path is None:
          raise ValueError(
              "Cannot recognize `{}`".format(pretrained_name_or_dir)
          )
  
      if file_path.endswith(".npz"):
          matrix, result = _load_embedding_npz(
              file_path, vocab, unknown
          )
      else:
          matrix, result = _load_embedding_txt(
              file_path, vocab, unknown
          )
      dim = matrix.shape[-1]
      logging.info("Pre-trained embedding dim: {}".format(dim))
      if vocab is None:
          return matrix, result
      else:
          hit_flags = result
          total_hits = sum(hit_flags)
          logging.info(
              "Found {} out of {} words in the pretrained embedding.".format(
                  total_hits, len(vocab)
              )
          )
          if total_hits != len(vocab):
              if unk_method is None:
                  found_vectors = matrix[hit_flags]
                  mean = np.mean(found_vectors, axis=0, keepdims=True)
                  std = np.std(found_vectors, axis=0, keepdims=True)
                  unfound_vec_num = len(vocab) - total_hits
                  r_vecs = (
                      np.random.randn(unfound_vec_num, dim).astype(
                          "float32"
                      )
                      * std
                      + mean
                  )
                  matrix[hit_flags == False] = r_vecs
              else:
                  unk_idxs = (hit_flags == False).nonzero()[0]
                  matrix[hit_flags == False] = unk_method(
                      vocab.to_tokens(unk_idxs)
                  )
  
          return matrix
  
file_path: gluon-nlp/src/gluonnlp/embedding/embed_loader.py
imports: import io
  import logging
  import os
  import warnings
  import fasttext
  import numpy
  from mxnet.gluon.utils import download, check_sha1, _get_repo_file_url
  from .None import _constants
  from ..base import get_home_dir
  from ..data import Vocab
label_desc: Use image node_id 8 for calling a global function with example usage: load_embeddings(vocab, pretrained_name_or_dir, unknown, unk_method) and returns: matrix,  result, matrix