

def osmNaturalToTraining(xml_file):
    address_list = xmlToAddrList(xml_file)
    train_addr_list = etree.Element('AddressCollection')
    trainFileName = (('../training_data/' + re.sub('\\W+', '_', xml_file)) + '.xml')
    punc_list = ',.'
    osm_tags_to_addr_tags = {
        'addr:housenumber': 'AddressNumber',
        'addr:street:prefix': 'StreetNamePreDirectional',
        'addr:street:name': 'StreetName',
        'addr:street:type': 'StreetNamePostType',
        'addr:city': 'PlaceName',
        'addr:state': 'StateName',
        'addr:postcode': 'ZipCode',
    }
    for address in address_list:
        addr_tokens = address['addr:full'].split()
        train_addr = etree.Element('AddressString')
        is_addr_taggable = True
        for token in addr_tokens:
            is_token_taggable = False
            for (key, value) in list(address.items()):
                if ((key in list(osm_tags_to_addr_tags.keys())) and (key != 'addr:full') and (token in value.split())):
                    is_taggable = True
                    token_xml = etree.Element(osm_tags_to_addr_tags[key])
                    token_xml.text = token
                    if (token[(- 1)] in punc_list):
                        token_xml.text = token[0:(- 1)]
                        token_xml.tail = token[(- 1)]
                    train_addr.append(token_xml)
            if (is_token_taggable == False):
                is_addr_taggable = False
        if (is_addr_taggable == True):
            train_addr_list.append(train_addr)
    output = etree.tostring(train_addr_list, pretty_print=True)
    with open(trainFileName, 'w') as f:
        f.write(output)

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