code: def _get_pmi_df(self, freqs: pd.Series) -> pd.DataFrame:
      # ngram_lens = np.array([len(x.split()) for x in freqs.index])
      wc = dict(
          pd.DataFrame(
              {
                  "Len": [len(x.split()) for x in freqs.index],
                  "Freq": freqs,
              }
          )
          .groupby("Len")
          .sum()["Freq"]
      )
  
      backoff = freqs.mean()
  
      def prob(ngram):
          if len(ngram) == 0:
              return 1
  
          joined_ngram = " ".join(ngram)
          if joined_ngram in freqs:
              return freqs.loc[joined_ngram] / (
                  wc[len(ngram)] - len(ngram) + 1
              )
  
          if ngram[0] in freqs.index:
              return freqs.loc[ngram[0]] / wc[1] * prob(ngram[1:])
  
          return backoff / wc[1] * prob(ngram[1:])
  
      apply = lambda df: df.apply
      if self.show_progress:
          apply = lambda df: df.progress_apply
  
      return (
          pd.DataFrame({"Count": freqs})
          .reset_index()
          .rename(columns={"term": "Term"})
          .assign(
              Len=lambda df: df.Term.apply(lambda x: len(x.split())),
          )[lambda df: df.Len > 1]
          .assign(
              WholeProb=lambda df: apply(df.Term)(
                  lambda x: prob(x.split())
              ),
              UniProb=lambda df: apply(df.Term)(
                  lambda x: np.exp(
                      sum(np.log(prob([tok])) for tok in x.split())
                  )
              ),
              PMI=lambda df: np.log(df.WholeProb) - np.log(df.UniProb),
              NPMI=lambda df: -df.PMI / np.log(df.WholeProb),
          )
      )
  
file_path: scattertext/scattertext/termcompaction/npmi_compactor.py
imports: import numpy
  import pandas
  from tqdm.auto import tqdm
  from scattertext.termranking.TermRanker import TermRanker
  from scattertext.termranking.AbsoluteFrequencyRanker import AbsoluteFrequencyRanker
label_desc: Use image node_id 6 for calling the NPMICompactor obj's underlying member method code with example usage: obj._get_pmi_df(freqs) and returns: pd, unknown, int, unknown, unknown