

def trainFileFromLines(addr_file, is_train=True):
    lines = open(addr_file, 'r')
    addr_index = 0
    token_index = 0
    if (is_train == True):
        outputFileName = (('training/training_data/' + re.sub('\\W+', '_', re.sub('.*/', '', addr_file))) + '.xml')
    else:
        outputFileName = (('training/test_data/' + re.sub('\\W+', '_', re.sub('.*/', '', addr_file))) + '.xml')
    tag_list = [None, 'AddressNumber', 'USPSBox', 'StreetName', 'StreetNamePostType', 'PlaceName', 'StateName', 'ZipCode', 'suffix']
    addr_list = etree.Element('AddressCollection')
    addr = etree.Element('AddressString')
    for line in lines:
        if (line == '\n'):
            addr[(- 1)].tail = None
            addr_list.append(addr)
            addr = etree.Element('AddressString')
        else:
            split = line.split(' |')
            addr_line = split[0]
            addr_tokens = addr_line.split()
            token_num = int(split[1].rstrip())
            token_tag = tag_list[token_num]
            for token in addr_tokens:
                token_xml = etree.Element(token_tag)
                token_xml.text = token
                token_xml.tail = ' '
                addr.append(token_xml)
    output = etree.tostring(addr_list, pretty_print=True)
    with open(outputFileName, 'w') as f:
        f.write(output)

dataset/ETHPy150Open datamade/usaddress/parse_scripts/parse.py trainFileFromLines/original