def postprocess_candidate_chains(kb, topic_ent, answers, candidate_chains,
                                 num_hop):
  """Postprocess shortest paths and keep the one that leads to answers.

  Args:
    kb: dictionary to store the KB {subject: {relation: objects}}
    topic_ent: topic entity to start with
    answers: a set of answers
    candidate_chains: all possible shortest paths
    num_hop: max number of hops

  Returns:
    the best shortest path (None if not exist)
  """
  for chain in candidate_chains:
    if num_hop == 3:
      if len(chain) < 3 or chain[1] != chain[0] + '-inv':
        continue

    intermediate_entities = get_intermediate_entities(kb, topic_ent, chain)
    if intermediate_entities[-1] == answers:
      return chain, intermediate_entities

  return None, None
Postprocess shortest paths and keep the one that leads to answers.

  Args:
    kb: dictionary to store the KB {subject: {relation: objects}}
    topic_ent: topic entity to start with
    answers: a set of answers
    candidate_chains: all possible shortest paths
    num_hop: max number of hops

  Returns:
    the best shortest path (None if not exist)
  
language/labs/drkit/metaqa/preprocessing/metaqa_preprocess.py