import logging


def get_f1(pred_tags_lst, golden_tags_lst):
    """
    get sentence level f score
    :param pred_tags_lst: list of one hot alike tags i.e. [[1, 0, 1]]
    :param golden_tags_lst: list of one hot alike tags i.e. [[1, 0, 1]]
    :return: precision, recall, f1
    """
    tp, fp, fn = 0, 0, 0
    # print(len(pred_tags_lst), len(pred_tags_lst[0]), golden_tags_lst.shape)
    for pred_tags, golden_tags in zip(pred_tags_lst, golden_tags_lst):
        if len(pred_tags) != len(golden_tags):
            logging.error('Unmatched tags: \npred:{}{}\ngold:{}{}'.format(
                len(pred_tags), pred_tags, len(golden_tags), golden_tags)
            )
            raise RuntimeError
        for pred_t, gold_t in zip(pred_tags, golden_tags):
            if pred_t == 1:
                if pred_t == gold_t:
                    tp += 1
                elif gold_t == 0:
                    fp += 1
                else:
                    raise RuntimeError
            elif pred_t == 0:
                if pred_t == gold_t:
                    pass
                elif gold_t == 1:
                    fn += 1
                else:
                    raise RuntimeError
    if tp == 0:
        precision = 0
        recall = 0
        f1 = 0
    else:
        precision = 1.0 * tp / (tp + fp)
        recall = 1.0 * tp / (tp + fn)
        f1 = 2 * precision * recall / (precision + recall)
    return precision, recall, f1