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.
    """

    amountOfPages = len(corpus.keys())
    linkedPages = corpus[page]

    if len(linkedPages) == 0:
        linkedPages = corpus.keys()
    output = {page: 0 for page in corpus.keys()}

    for page in linkedPages:
        output[page] = damping_factor / len(linkedPages)

    for page in output.keys():
        output[page] += (1 - damping_factor) / amountOfPages

    return output