def osmSyntheticToTraining(xml_file):
    address_list = xmlToAddrList(xml_file)
    train_addr_list = []

    trainFileName = 'training/training_data/synthetic_'+re.sub(r'\W+', '_', re.sub(r'.*/', '', xml_file))+'.xml'
    testFileName = 'training/test_data/synthetic_'+re.sub(r'\W+', '_', re.sub(r'.*/', '', xml_file))+'.xml'

    synthetic_order = [
            ('addr:housenumber', 'AddressNumber', 'Street'),
            ('addr:street:prefix', 'StreetNamePreDirectional', 'Street'),
            ('addr:street:name', 'StreetName', 'Street'),
            ('addr:street:type', 'StreetNamePostType', 'Street'),
            ('addr:city', 'PlaceName', 'City'),
            ('addr:state', 'StateName', 'Area'),
            ('addr:postcode', 'ZipCode', 'Area')]
    
    for address in address_list:
        train_addr = etree.Element('AddressString')
        components = {'Street' : [], 'City' : [], 'Area' : []}
        for source_tag, target_tag, tag_type in synthetic_order:
            if source_tag in list(address.keys()):
                words = address[source_tag].split()
                for word in words:
                    token_xml = etree.Element(target_tag)
                    token_xml.text = word
                    token_xml.tail = ' '
                    components[tag_type].append(token_xml)
        
        for tag_type in ('Street','City', 'Area') :
            l = components[tag_type]
            if l :
                l[-1].text += ','

        address_xml = (components['Street'] 
                       + components['City'] 
                       + components['Area'])
        
        address_xml[-1].text = address_xml[-1].text[:-1]
        address_xml[-1].tail = None

        for xml_element in address_xml:
            train_addr.append(xml_element)

        train_addr_list.append(train_addr)

    random.shuffle(train_addr_list)
    percent_20 = int(len(train_addr_list) * 0.2)

    test_data = etree.Element('AddressCollection')
    test_data.extend(train_addr_list[:percent_20])

    train_data = etree.Element('AddressCollection')
    train_data.extend(train_addr_list[percent_20:])

    with open(trainFileName, 'w') as f:
        f.write(etree.tostring(train_data, pretty_print=True))

    with open(testFileName, 'w') as f:
        f.write(etree.tostring(test_data, pretty_print=True))


# us50 data -> training or test file (xml)
dataset/ETHPy150Open datamade/usaddress/parse_scripts/parse.py/osmSyntheticToTraining/original