def flatten(text):
    """
    Flatten the text:
    * make sure each record is on one line.
    * remove parenthesis 
    """
    lines = text.split('\n')
    tokens = []
    for l in lines:
        if len(l) == 0:
            continue
        l = l.replace('\t', ' ')
        tokens += filter(lambda x: len(x) > 0, l.split(' ')) + ['']
    capturing = False
    captured = []
    flattened = []
    while len(tokens) > 0:
        tok = tokens.pop(0)
        if not capturing and len(tok) == 0:
            if len(captured) > 0:
                flattened.append(' '.join(captured))
                captured = []
            continue
        if tok.startswith('('):
            tok = tok.lstrip('(')
            capturing = True
        if capturing and tok.endswith(')'):
            tok = tok.rstrip(')')
            capturing = False
        captured.append(tok)
    return '\n'.join(flattened)
