class_docstr: Loads a pre-weighted inverted index of token/document terms.
  
  Scores new queries by taking sparse dot products.
docstr: Create a sparse tfidf-weighted word vector from query.
  
  tfidf = log(tf + 1) * log((N - Nt + 0.5) / (Nt + 0.5))
code: def text2spvec(self, query):
      """
      Create a sparse tfidf-weighted word vector from query.
  
      tfidf = log(tf + 1) * log((N - Nt + 0.5) / (Nt + 0.5))
      """
      # Get hashed ngrams
      words = self.parse(utils.normalize(query))
      wids = [utils.hash(w, self.hash_size) for w in words]
  
      if len(wids) == 0:
          if self.strict:
              raise RuntimeError("No valid word in: %s" % query)
          else:
              logger.warning("No valid word in: %s" % query)
              return sp.csr_matrix((1, self.hash_size))
  
      # Count TF
      wids_unique, wids_counts = np.unique(wids, return_counts=True)
      tfs = np.log1p(wids_counts)
  
      # Count IDF
      Ns = self.doc_freqs[wids_unique]
      idfs = np.log((self.num_docs - Ns + 0.5) / (Ns + 0.5))
      idfs[idfs < 0] = 0
  
      # TF-IDF
      data = np.multiply(tfs, idfs)
  
      # One row, sparse csr matrix
      indptr = np.array([0, len(wids_unique)])
      spvec = sp.csr_matrix(
          (data, wids_unique, indptr), shape=(1, self.hash_size)
      )
  
      return spvec
  
file_path: ParlAI/parlai/agents/tfidf_retriever/tfidf_doc_ranker.py
imports: import numpy
  import scipy.sparse
  from .None import utils
  from .None import tokenizers
  from parlai.utils.logging import logger
label_desc: Use image node_id 5 for calling the TfidfDocRanker obj's underlying member method code with example usage: obj.text2spvec(query) and returns: spvec, sp