class_docstr: Builds and/or loads a dictionary.
  
  The dictionary provides access to the frequency of each token, functions to
  translate sentences from tokens to their vectors (list of ints, each int is the
  index of a token in the dictionary) and back from vectors to tokenized text.
class_docstr_tok: ["Builds","and\/or","loads","a","dictionary",".","The","dictionary","provides","access","to","the","frequency","of","each","token",",","functions","to","translate","sentences","from","tokens","to","their","vectors","(","list","of","ints",",","each","int","is","the","index","of","a","token","in","the","dictionary",")","and","back","from","vectors","to","tokenized","text","."]
docstr: Sort the dictionary.
  
  Inline operation. Rearranges the dictionary so that the elements with
  the lowest index have the highest counts. This reindexes the dictionary
  according to the sorted frequencies, breaking ties alphabetically by
  token.
  
  :param bool trim:
      If True, truncate the dictionary based on minfreq and maxtokens.
code: def sort(self, trim=True):
      """
      Sort the dictionary.
  
      Inline operation. Rearranges the dictionary so that the elements with
      the lowest index have the highest counts. This reindexes the dictionary
      according to the sorted frequencies, breaking ties alphabetically by
      token.
  
      :param bool trim:
          If True, truncate the dictionary based on minfreq and maxtokens.
      """
      if trim and self.tokenizer == "gpt2":
          raise RuntimeError(
              "You should not trim the dictionary when using gpt-2."
          )
      if trim and self.tokenizer == "bytelevelbpe":
          raise RuntimeError(
              "You should not trim the dictionary when using bytelevelbpe."
          )
      # sort first by count, then alphabetically
      if trim:
          self.remove_tail(self.minfreq)
      sorted_pairs = sorted(
          self.freq.items(), key=lambda x: (-x[1], x[0])
      )
      new_tok2ind = {}
      new_ind2tok = {}
      for i, (tok, _) in enumerate(sorted_pairs):
          new_tok2ind[tok] = i
          new_ind2tok[i] = tok
      self.tok2ind = new_tok2ind
      self.ind2tok = new_ind2tok
      if trim:
          self.resize_to_max(self.maxtokens)
      assert len(self.freq) == len(self.ind2tok) == len(self.tok2ind)
      return sorted_pairs
  
imports: from typing import Optional
  from parlai.core.params import ParlaiParser
  from parlai.core.opt import Opt
  from parlai.core.build_data import modelzoo_path
  from parlai.utils.bpe import bpe_factory, BPEHelper
  from .agents import Agent
  from .build_data import make_dir
  from collections import defaultdict
  import copy
  import numpy
  import os
  import json
  import re
  import parlai.utils.logging
  from parlai.utils.io import PathManager
  from typing import List
  import enum
label_desc: Use image node_id 29 for calling the DictionaryAgent obj's underlying member method code with example usage: obj.sort(trim) and returns: sorted_pairs