import re


def _extract_sentence_tags(tagged_sentence):
    """Given a tagged sentence, extracts a dictionary mapping tags to the words
    or phrases that they tag.

    Parameters
    ----------
    tagged_sentence : str
        The sentence with Medscan annotations and tags

    Returns
    -------
    tags : dict
        A dictionary mapping tags to the words or phrases that they tag.
    """
    p = re.compile('ID{([0-9,]+)=([^}]+)}')
    tags = {}

    # Iteratively look for all matches of this pattern
    endpos = 0
    while True:
        match = p.search(tagged_sentence, pos=endpos)
        if not match:
            break
        endpos = match.end()

        tags[match.group(1)] = match.group(2)
    return tags