class_docstr: Salience map clustering.
class_docstr_tok: ["Salience","map","clustering","."]
docstr: Converts the given variable length-vector into a fixed-length vector.
  
  This function creates a zero vector of the length of the vocabulary and
  fills the positions of the given tokens by their salience.
  
  Args:
    token_weights: Mapping from tokens to their salience weights.
    vocab_lookup: Mapping from word type to its index in the vocabulary.
    top_k: Number of tokens to be considered to represent each sentence.
  
  Returns:
    Vector of the size of the vocabulary that contains salience weights at
    the vocabulary indexes of tokens in `token_weights` and zeros elsewhere.
docstr_tok: ["Converts","the","given","variable","length-vector","into","a","fixed-length","vector",".","This","function","creates","a","zero","vector","of","the","length","of","the","vocabulary","and","fills","the","positions","of","the","given","tokens","by","their","salience",".","Args",":","token_weights",":","Mapping","from","tokens","to","their","salience","weights",".","vocab_lookup",":","Mapping","from","word","type","to","its","index","in","the","vocabulary",".","top_k",":","Number","of","tokens","to","be","considered","to","represent","each","sentence",".","Returns",":","Vector","of","the","size","of","the","vocabulary","that","contains","salience","weights","at","the","vocabulary","indexes","of","tokens","in","`","token_weights","`","and","zeros","elsewhere","."]
code: def _convert_to_bow_vector(
      self,
      token_weights: JsonDict,
      vocab_lookup: dict[str, int],
      top_k: int,
  ) -> np.ndarray:
      """Converts the given variable length-vector into a fixed-length vector.
  
      This function creates a zero vector of the length of the vocabulary and
      fills the positions of the given tokens by their salience.
  
      Args:
        token_weights: Mapping from tokens to their salience weights.
        vocab_lookup: Mapping from word type to its index in the vocabulary.
        top_k: Number of tokens to be considered to represent each sentence.
  
      Returns:
        Vector of the size of the vocabulary that contains salience weights at
        the vocabulary indexes of tokens in `token_weights` and zeros elsewhere.
      """
      vocab_vector = np.zeros((len(vocab_lookup),))
  
      # Find k-th largest weight value
      sorted_token_weights = list(
          sorted(
              token_weights.items(),
              key=lambda x: abs(x[1]),
              reverse=True,
          )
      )
      index_end = min(top_k, len(sorted_token_weights))
      weight_threshold = abs(sorted_token_weights[index_end - 1][1])
  
      # Check if there exist ties
      for token_weight in sorted_token_weights[index_end:]:
          if abs(token_weight[1]) == weight_threshold:
              index_end += 1
          else:
              break
  
      # Consider only top-k (including ties)
      for token_weight in sorted_token_weights[:index_end]:
          if token_weight[0] in vocab_lookup:
              vocab_vector[
                  vocab_lookup[token_weight[0]]
              ] = token_weight[1]
      return vocab_vector
  
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 3 for calling the SalienceClustering obj's underlying member method code with example usage: obj._convert_to_bow_vector(token_weights, vocab_lookup, top_k) and returns: vocab_vector