from typing import Set
from typing import Tuple


def confusion_matrix(pred: Set, gold: Set) -> Tuple[Set, Set, Set]:
    """Return a confusion matrix.

    This can be used for both entity-level and mention-level

    :param pred: a set of predicted entities/candidates
    :type pred: set
    :param gold: a set of golden entities/candidates
    :type gold: set
    :return: a tuple of TP, FP, and FN
    :rtype: (set, set, set)
    """
    if not isinstance(pred, set):
        pred = set(pred)
    if not isinstance(gold, set):
        gold = set(gold)
    TP = pred.intersection(gold)
    FP = pred.difference(gold)
    FN = gold.difference(pred)
    return (TP, FP, FN)