def transition_model(corpus, page, damping_factor):
    """
    Return a probability distribution over which page to visit next,
    given a current page.

    With probability `damping_factor`, choose a link at random
    linked to by `page`. With probability `1 - damping_factor`, choose
    a link at random chosen from all pages in the corpus.
    """
    # If page has no links at all, assume it has links to all pages
    links_in_page = corpus[page]
    links_in_page = links_in_page if links_in_page else set(corpus)

    # Probability associated with links in current page
    prob_links = damping_factor / len(links_in_page)
    # Probability associated with all pages
    prob_page = (1 - damping_factor) / len(corpus)

    # Build the transition model
    transition_distribution = {}
    for page_i in corpus:
        prob_links_i = prob_links if page_i in links_in_page else 0
        transition_distribution[page_i] = (prob_links_i + prob_page)

    return transition_distribution