from typing import Counter


def get_probs(occurrences):
    """
    Computes conditional probabilities based on frequency of co-occurrences

    Parameters
    ----------
    occurrences: occurences[x][y] number of times with (X=x and Y=y)

    Returns
    -------
    probs : probs[x][y] = Pr(Y=y | X=x)
    reverse_probs : reverse_probs[y][x] = Pr(X=x | Y=y)
    """
    probs = {}
    reverse_probs = {}
    y_occ = Counter()
    for x, ys in occurrences.items():
        total = sum(ys.values())
        probs[x] = {}
        for y, occ in ys.items():
            probs[x][y] = occ / total
            y_occ[y] += occ
    for x, ys in occurrences.items():
        for y, occ in ys.items():
            reverse_probs.setdefault(y, {})[x] = occ / y_occ[y]

    return probs, reverse_probs