import collections


def _build_segment_index(metadata):
    """Return a dictionary mapping each key to a list of keys corresponding to the same segment."""
    segment_id_to_keys = collections.defaultdict(list)
    index = {}
    for key, item in metadata.items():
        full_segment_id = (item['song_name'], item['segment_id'])
        item['full_segment_id'] = full_segment_id
        segment_id_to_keys[full_segment_id].append(key)
        index[key] = segment_id_to_keys[full_segment_id]
    return index