def _maybe_replace_entity(funql, utterance, mention_map, geobase_entity):
  """Replace entity identifiers so that they can be generated using copy."""
  # GeoQuery has <= 2 mentions per query.
  mention_marker = "m1" if "m0" in utterance else "m0"

  # Split utterance to avoid replacing some substring of a token.
  tokens = utterance.split(" ")

  # Order aliases by longest alias, since some can be nested in others.
  aliases = sorted(geobase_entity.aliases, key=lambda x: -len(x))
  for alias in aliases:
    alias_tokens = alias.split(" ")
    new_tokens = _maybe_list_replace(tokens, alias_tokens, mention_marker)
    if new_tokens:
      normalized_identifier = geobase_entity.identifier.replace("'", "")
      new_funql = funql.replace(normalized_identifier, mention_marker)
      new_utterance = " ".join(new_tokens)
      mention_map[mention_marker] = geobase_entity.identifier
      return new_funql, new_utterance, mention_map

  # Could not find alias.
  return funql, utterance, mention_map
Replace entity identifiers so that they can be generated using copy.
language/compgen/nqg/tasks/geoquery/entity_utils.py