"""
Functions to parse and preprocess the reference timelines ("gold timelines")
against which to evaluate.
"""

import itertools
import re


def gold_timeline_from_file(path):
    """
    Returns the gold timeline from the file with the given path.
    The output is a dict with the following structure:
    {
        date: [sentences]
    }

    :param path: The path to a gold timeline file.
    :return: A dictionary containing lists of sentences by date.
    """

    with open(path, encoding='utf-8') as f:
        lines = [line for line in f.readlines()]

    # split list of lines into summaries (separated by lines only containing dashes)
    summaries = [list(y) for x, y in itertools.groupby(lines, lambda z: re.match('^-+$', z)) if not x]

    timeline = {}
    for summary in summaries:
        date = summary[0].strip()
        sentences = [s.strip() for s in summary[1:]]
        timeline[date] = sentences

    return timeline
