class_docstr: Salience map clustering.
class_docstr_tok: ["Salience","map","clustering","."]
docstr: Build a vocabulary from the given token saliencies.
  
  This creates a mapping from word type to index in the vocabulary taken from
  all tokens in `token_saliencies`. The order of word types in the vocabulary
  depends on the order of the tokens in the input.
  
  Args:
    token_saliencies: List of mappings from salience field to TokenSaliency
      objects. This is the result of a post-hoc explanation method, such as
      gradient l2.
  
  Returns:
    1. Mapping from word type to its index in the vocabulary.
    2. Ordered list of word types.
docstr_tok: ["Build","a","vocabulary","from","the","given","token","saliencies",".","This","creates","a","mapping","from","word","type","to","index","in","the","vocabulary","taken","from","all","tokens","in","`","token_saliencies","`",".","The","order","of","word","types","in","the","vocabulary","depends","on","the","order","of","the","tokens","in","the","input",".","Args",":","token_saliencies",":","List","of","mappings","from","salience","field","to","TokenSaliency","objects",".","This","is","the","result","of","a","post-hoc","explanation","method",",","such","as","gradient","l2",".","Returns",":","1",".","Mapping","from","word","type","to","its","index","in","the","vocabulary",".","2",".","Ordered","list","of","word","types","."]
code: def _build_vocab(
      self, token_saliencies: list[JsonDict]
  ) -> tuple[dict[str, int], list[str]]:
      """Build a vocabulary from the given token saliencies.
  
      This creates a mapping from word type to index in the vocabulary taken from
      all tokens in `token_saliencies`. The order of word types in the vocabulary
      depends on the order of the tokens in the input.
  
      Args:
        token_saliencies: List of mappings from salience field to TokenSaliency
          objects. This is the result of a post-hoc explanation method, such as
          gradient l2.
  
      Returns:
        1. Mapping from word type to its index in the vocabulary.
        2. Ordered list of word types.
      """
      vocab_lookup = {}
  
      for instance in token_saliencies:
          for token_salience in instance.values():
              for token in token_salience.tokens:
                  vocab_lookup[token] = vocab_lookup.get(
                      token, len(vocab_lookup)
                  )
  
      vocab = [""] * len(vocab_lookup)
      for token, idx in vocab_lookup.items():
          vocab[idx] = token
      return vocab_lookup, vocab
  
file_path: lit/lit_nlp/components/salience_clustering.py
imports: from collections.abc import Sequence
  from typing import Optional
  from lit_nlp.api import components
  from lit_nlp.api import dataset
  from lit_nlp.api import model
  from lit_nlp.api import types
  import numpy
  from sklearn import cluster
label_desc: Use image node_id 2 for calling the SalienceClustering obj's underlying member method code with example usage: obj._build_vocab(token_saliencies) and returns: vocab_lookup,  vocab