Esempio n. 1
0
def makeXMLFile(currentTree, outpath):
    """
    dict, string -> None
    For a given tree structure, generates the XML structure. First generates
    the tags that are included in every XML file; then identifies the root
    of the given tree and generates the ET.SubElements that it requires.
    Finally, writes the XML content to the given output file.
    """
    xmlfile = open(outpath, "w", encoding="utf8")
    grammar = ET.Element("grammar")
    entry = ET.SubElement(grammar, "entry", attrib={"name": currentTree["NAME"]})
    family = ET.SubElement(entry, "family")
    family.text = outpath.split("/")[-2]
    trace = ET.SubElement(entry, "trace")
    traceClass = ET.SubElement(trace, "class")
    traceClass.text = outpath.split("/")[-2]
    tree = ET.SubElement(entry, "tree", attrib={"id": currentTree["NAME"]})
    root = currentTree["HIERARCHY"]["name"]

    if root is not None:
        # collecting all variables used in this tree in order to give new variable names
        # for occurences of "coref" in the XML tree
        used_vars = utils.collect_vars(currentTree["HIERARCHY"])

        root_attribs, root_name = utils.nodeNameToAttrDict(root)
        root_attribs.update({"name":root_name})
        root_node = ET.SubElement(tree, "node", root_attribs)
        root_narg = ET.SubElement(root_node, "narg")

        root_coref = "@" + utils.numberToString(len(used_vars))
        used_vars.add(root_coref)
        root_fs = ET.SubElement(root_narg, "fs", {"coref": root_coref})

        root_fs, used_vars = insert_feature_values(root_fs, root_name, currentTree["HIERARCHY"], used_vars)

        # take root XML node and add all daughters, and all their daughters, etc. to the XML structure
        tree = make_subtree(root_node, currentTree["HIERARCHY"], used_vars)

        # additional XML info
        frame = ET.SubElement(entry, "frame")
        semantics = ET.SubElement(entry, "semantics")
        interface = ET.SubElement(entry, "interface")
        fs = ET.SubElement(interface, "fs")
        print('<?xml version="1.0" encoding="utf-8" standalone="no" ?>\n<!DOCTYPE grammar SYSTEM "xmg-tag.dtd,xml">', file=xmlfile)
        output = ET.tostring(grammar)
        print(utils.prettifyXMLDocument(output).split(">", 1)[1].strip(), file=xmlfile)
        xmlfile.close()
    return
Esempio n. 2
0
def make_subtree(mother_node, dict_tree_containing_mother_node, used_vars):
    """
    ET.SubElement, dict, set -> ET.SubElement
    For the given mother node, insert all children into the XML structure.
    If the children also have children, call this function again.
    Return the updated ET.SubElement.
    """
    for daughter_dict in findDaughters(dict_tree_containing_mother_node):
        node_attribs, daughter_name = utils.nodeNameToAttrDict(daughter_dict["name"])

        node_attribs.update({"name":daughter_name})

        if daughter_name.islower():
            node_attribs.update({"type": "flex"})

        dtr = ET.SubElement(mother_node, "node", node_attribs)
        dtr_narg = ET.SubElement(dtr, "narg")
        fs_coref = "@" + utils.numberToString(len(used_vars))
        dtr_fs = ET.SubElement(dtr_narg, "fs", {"coref": fs_coref})
        dtr_fs, used_vars = insert_feature_values(dtr_fs, daughter_name, daughter_dict, used_vars)
        if findDaughters(daughter_dict) != []:
            dtr = make_subtree(dtr, daughter_dict, used_vars)
    return mother_node