from datetime import datetime

import tilse.evaluation.dates
from tilse.data import timelines
from tilse.evaluation import rouge


evaluator = rouge.TimelineRougeEvaluator(measures=['rouge_1', 'rouge_2'])


def tilse_format(timeline):
    return timelines.Timeline({datetime.strptime(date, '%Y-%m-%d'): sentences for date, sentences in timeline.items()})


def evaluate(timeline, gold_timelines):
    timeline = tilse_format(timeline)
    ground_truth = timelines.GroundTruth(
        [tilse_format(gold_timeline) for gold_timeline in gold_timelines])

    return evaluator.evaluate_align_date_content_costs_many_to_one(timeline, ground_truth)


def date_f1(timeline, gold_timeline):
    dates = timeline.keys()
    gold_dates = gold_timeline.keys()
    precision = len([d for d in dates if d in gold_dates]) / len(dates)
    recall = len([d for d in gold_dates if d in dates]) / len(gold_dates)
    if precision + recall == 0:
        return 0
    return (2 * precision * recall) / (precision + recall)