def read_segments(a_lines):
    """Read file and return a list of segment dictionaries.

    Args:
      a_lines (list): decoded lines of the input file

    Returns:
      dict: mapping from tokens to segments

    """
    segs2toks = {}
    s_c = t_c = 0
    tokens = []
    atoks = []
    new_seg = None
    active_tokens = set()
    active_segments = []
    for iline in a_lines:
        iline = iline.strip()
        if not iline:
            continue
        active_tokens.clear()
        del atoks[:]
        del active_segments[:]
        tokens = iline.split()
        for tok in tokens:
            if tok[0] == '(' and len(tok) > 1:
                active_tokens = set(atoks)
                del atoks[:]
                for a_s in active_segments:
                    segs2toks[a_s].update(active_tokens)
                new_seg = s_c, tok[1:]
                active_segments.append(new_seg)
                segs2toks[new_seg] = set()
                s_c += 1
                continue
            elif tok == ')':
                assert active_segments, 'Unbalanced closing parenthesis at line: ' + repr(
                    iline)
                active_tokens = set(atoks)
                del atoks[:]
                for a_s in active_segments:
                    segs2toks[a_s].update(active_tokens)
                active_segments.pop()
                continue
            else:
                atoks.append((t_c, tok))
                t_c += 1
        assert not active_segments, 'Unbalanced opening parenthesis at line: ' + repr(
            iline)
    toks2segs = dict()
    segments = segs2toks.keys()
    segments.sort(key=lambda el: el[0])
    for seg in segments:
        toks = frozenset(segs2toks[seg])
        if toks in toks2segs:
            continue
        assert toks not in toks2segs, "Multiple segments correspond to the same tokenset: '" + repr(
            toks) + "': " + repr(seg) + ', ' + repr(toks2segs[toks])
        toks2segs[toks] = seg
    return toks2segs
