generate_xml.py 2.25 KB
from lxml import etree

def create_xml(filename, unified_frame, matchings):
    root = create_root()
    frame = create_frame(root)
    ids_map = add_arguments(frame, unified_frame)
    add_connections(frame, matchings, ids_map)
    with open(filename, 'w') as output_file:
        output_file.write(etree.tostring(root, pretty_print=True,
                                         xml_declaration=False, encoding='UTF-8'))

def create_root():
    root = etree.Element('matching')
    return root

def create_frame(root):
    frame = etree.SubElement(root, 'unifier_frame')
    return frame

def add_arguments(frame, unified_frame):
    print unified_frame
    ids_map = {}
    i = 0
    for role in unified_frame.get_role_labels():
        for argument in unified_frame.get_arguments(role):
            ids_map[argument._id] = i
            add_argument(frame, role, i)
            i += 1
    return ids_map

def add_argument(frame, role, index):
    argument = etree.SubElement(frame, 'argument')
    argument.attrib['id'] = str(index)
    add_semantic_role(argument, role)

def add_semantic_role(argument, roles_str):
    semantic_role = etree.SubElement(argument, 'semantic_role')
    roles_list = roles_str.split('_')
    if len(roles_list) == 1:
        semantic_role.attrib['type'] = 'role'
    else:
        semantic_role.attrib['type'] = 'alternative'
    roles = etree.SubElement(argument, 'roles')
    for role_name in roles_list:
        role = etree.SubElement(roles, 'role')
        role.attrib['name'] = role_name
        

def add_connections(frame, matchings, ids_map):
    connections = etree.SubElement(frame, 'connections')
    for frame_id, matching in matchings.items():
        add_frame_connection(connections, frame_id, matching, ids_map)

def add_frame_connection(connections, frame_id, matching, ids_map):
    frame = etree.SubElement(connections, 'slowal_frame')
    frame.attrib['id'] = str(frame_id)
    argument_connections = etree.SubElement(frame, 'argument_connections')
    for slowal_id, unifier_temp_id in matching.items():
        argument_connection = etree.SubElement(argument_connections, 'argument_connection')
        argument_connection.attrib['unifier_argument_id'] = str(ids_map[unifier_temp_id])
        argument_connection.attrib['slowal_id'] = str(slowal_id)