class_docstr: Object for loading word embeddings and related distances for TextAttack.
  This class has a lot of internal components (e.g. get consine similarity)
  implemented. Consider using this class if you can provide the appropriate
  input data to create the object.
  
  Args:
      emedding_matrix (ndarray): 2-D array of shape N x D where N represents size of vocab and D is the dimension of embedding vectors.
      word2index (Union[dict|object]): dictionary (or a similar object) that maps word to its index with in the embedding matrix.
      index2word (Union[dict|object]): dictionary (or a similar object) that maps index to its word.
      nn_matrix (ndarray): Matrix for precomputed nearest neighbours. It should be a 2-D integer array of shape N x K
          where N represents size of vocab and K is the top-K nearest neighbours. If this is set to `None`, we have to compute nearest neighbours
          on the fly for `nearest_neighbours` method, which is costly.
class_docstr_tok: ["Object","for","loading","word","embeddings","and","related","distances","for","TextAttack",".","This","class","has","a","lot","of","internal","components","(","e.g",".","get","consine","similarity",")","implemented",".","Consider","using","this","class","if","you","can","provide","the","appropriate","input","data","to","create","the","object",".","Args",":","emedding_matrix","(","ndarray",")",":","2-D","array","of","shape","N","x","D","where","N","represents","size","of","vocab","and","D","is","the","dimension","of","embedding","vectors",".","word2index","(","Union","[","dict|object","]",")",":","dictionary","(","or","a","similar","object",")","that","maps","word","to","its","index","with","in","the","embedding","matrix",".","index2word","(","Union","[","dict|object","]",")",":","dictionary","(","or","a","similar","object",")","that","maps","index","to","its","word",".","nn_matrix","(","ndarray",")",":","Matrix","for","precomputed","nearest","neighbours",".","It","should","be","a","2-D","integer","array","of","shape","N","x","K","where","N","represents","size","of","vocab","and","K","is","the","top-K","nearest","neighbours",".","If","this","is","set","to","`","None","`",",","we","have","to","compute","nearest","neighbours","on","the","fly","for","`","nearest_neighbours","`","method",",","which","is","costly","."]
docstr: Get top-N nearest neighbours for a word
  Args:
      index (int): ID of the word for which we're finding the nearest neighbours
      topn (int): Used for specifying N nearest neighbours
  Returns:
      neighbours (list[int]): List of indices of the nearest neighbours
docstr_tok: ["Get","top-N","nearest","neighbours","for","a","word","Args",":","index","(","int",")",":","ID","of","the","word","for","which","we","'re","finding","the","nearest","neighbours","topn","(","int",")",":","Used","for","specifying","N","nearest","neighbours","Returns",":","neighbours","(","list","[","int","]",")",":","List","of","indices","of","the","nearest","neighbours"]
code: def nearest_neighbours(self, index, topn):
      """
      Get top-N nearest neighbours for a word
      Args:
          index (int): ID of the word for which we're finding the nearest neighbours
          topn (int): Used for specifying N nearest neighbours
      Returns:
          neighbours (list[int]): List of indices of the nearest neighbours
      """
      if isinstance(index, str):
          index = self._word2index[index]
      if self.nn_matrix is not None:
          nn = self.nn_matrix[index][1 : (topn + 1)]
      else:
          try:
              nn = self._nn_cache[index]
          except KeyError:
              embedding = torch.tensor(self.embedding_matrix).to(
                  utils.device
              )
              vector = torch.tensor(self.embedding_matrix[index]).to(
                  utils.device
              )
              dist = torch.norm(embedding - vector, dim=1, p=None)
              # Since closest neighbour will be the same word, we consider N+1 nearest neighbours
              nn = dist.topk(topn + 1, largest=False)[1:].tolist()
              self._nn_cache[index] = nn
  
      return nn
  
imports: from abc import ABC, abstractmethod
  from collections import defaultdict
  import os
  import pickle
  import numpy
  import torch
  from textattack.shared import utils
label_desc: Use image node_id 7 for calling the WordEmbedding obj's underlying member method code with example usage: obj.nearest_neighbours(index, topn) and returns: nn