import re


def output_to_dict(output):
    """
    Convert the ROUGE output into python dictionary for further
    processing.

    """
    #0 ROUGE-1 Average_R: 0.02632 (95%-conf.int. 0.02632 - 0.02632)
    pattern = re.compile(
        r"(\d+) (ROUGE-\S+) (Average_\w): (\d.\d+) "
        r"\(95%-conf.int. (\d.\d+) - (\d.\d+)\)")
    results = {}
    for line in output.split("\n"):
        match = pattern.match(line)
        if match:
            sys_id, rouge_type, measure, result, conf_begin, conf_end = \
                match.groups()
            measure = {
                'Average_R': 'recall',
                'Average_P': 'precision',
                'Average_F': 'f_score'
                }[measure]
            rouge_type = rouge_type.lower().replace("-", '_')
            key = "{}_{}".format(rouge_type, measure)
            results[key] = float(result)
            results["{}_cb".format(key)] = float(conf_begin)
            results["{}_ce".format(key)] = float(conf_end)
    return results