def _maybe_find_duplicates()

in tools/tensorflow_docs/api_generator/doc_generator_visitor.py [0:0]


  def _maybe_find_duplicates(self):
    """Compute data structures containing information about duplicates.

    Find duplicates in `index` and decide on one to be the "main" name.

    Computes a reverse_index mapping each object id to its main name.

    Also computes a map `duplicate_of` from aliases to their main name (the
    main name itself has no entry in this map), and a map `duplicates` from
    main names to a lexicographically sorted list of all aliases for that name
    (incl. the main name).

    All these are computed and set as fields if they haven't already.
    """
    if self._reverse_index is not None:
      return

    # Maps the id of a symbol to its fully qualified name. For symbols that have
    # several aliases, this map contains the first one found.
    # We use id(py_object) to get a hashable value for py_object. Note all
    # objects in _index are in memory at the same time so this is safe.
    reverse_index = {}

    # Decide on main names, rewire duplicates and make a duplicate_of map
    # mapping all non-main duplicates to the main name. The main symbol
    # does not have an entry in this map.
    duplicate_of = {}

    # Duplicates maps the main symbols to the set of all duplicates of that
    # symbol (incl. itself).
    duplicates = {}

    for path, node in self._api_tree.index.items():
      if not path:
        continue
      full_name = node.full_name
      py_object = node.py_object
      object_id = id(py_object)
      if full_name in duplicates:
        continue

      aliases = self._api_tree.aliases[object_id]
      if not aliases:
        aliases = [node]

      names = [alias.full_name for alias in aliases]

      names = sorted(names)
      # Choose the main name with a lexical sort on the tuples returned by
      # by _score_name.
      main_name = min(names, key=self._score_name)

      if names:
        duplicates[main_name] = list(names)

      names.remove(main_name)
      for name in names:
        duplicate_of[name] = main_name

      # Set the reverse index to the canonical name.
      if not maybe_singleton(py_object):
        reverse_index[object_id] = main_name

    self._duplicate_of = duplicate_of
    self._duplicates = duplicates
    self._reverse_index = reverse_index