def _map_years_to_objects(facts, qid_numfacts, min_year, max_year):
  """Map each year between min, max to the corresponding object in facts.

  Args:
    facts: a list of facts with the same subject and relation.
    qid_numfacts: a dict mapping wikidata QIDs to number of facts.
    min_year: an int, starting year to map.
    max_year: an int, ending year to map.

  Returns:
    year2obj: a dict mapping each year between (min_year, max_year) to the
      corresponding most 'popular' object for that year.
  """
  year2obj = {}
  numfacts = lambda x: qid_numfacts.get(x, 0)
  for f in facts:
    min_ = f[3][0] if f[3] is not None else min_year
    max_ = f[4][0] if f[4] is not None else max_year
    min_ = max(min_, min_year)
    max_ = min(max_, max_year)
    for yr in range(min_, max_ + 1):
      if yr in year2obj:
        # Keep the more popular object.
        if numfacts(year2obj[yr]) < numfacts(f[2]):
          year2obj[yr] = f[2]
      else:
        year2obj[yr] = f[2]
  return year2obj