def test_to_standard():
    passage = convert.from_site(load_xml("test_files/site3.xml"))
    ref = load_xml("test_files/standard3.xml")  # old format of xml
    new_ref = convert.to_standard(convert.from_standard(ref))   # converting to the new xml format
    root = convert.to_standard(passage)
    assert (textutil.indent_xml(ETree.tostring(new_ref)).splitlines() ==
            textutil.indent_xml(ETree.tostring(root)).splitlines())
Beispiel #2
0
def parse():
    text = request.values["input"]
    print("Parsing text: '%s'" % text)
    in_passage = next(from_text(text))
    out_passage = next(get_parser().parse(in_passage))[0]
    root = to_standard(out_passage)
    xml = tostring(root).decode()
    return Response(indent_xml(xml), headers={"Content-Type": "xml/application"})
Beispiel #3
0
def parse():
    text = request.values["input"]
    print("Parsing text: '%s'" % text)
    in_passage = next(from_text(text))
    out_passage = next(get_parser().parse(in_passage))[0]
    root = to_standard(out_passage)
    xml = tostring(root).decode()
    return Response(indent_xml(xml),
                    headers={"Content-Type": "xml/application"})
def main():
    argparser = argparse.ArgumentParser(description=desc)
    argparser.add_argument("filename",
                           nargs="?",
                           help="XML file name to convert")
    argparser.add_argument("-o",
                           "--outfile",
                           help="output file for standard XML")
    argparser.add_argument("-b",
                           "--binary",
                           help="output file for binary pickel")
    argparser.add_argument("-d", "--db", help="DB file to get input from")
    argparser.add_argument("-p",
                           "--pid",
                           type=int,
                           help="PassageID to query DB")
    argparser.add_argument("-u", "--user", help="Username to DB query")
    args = argparser.parse_args()

    # Checking for illegal combinations
    if args.db and args.filename:
        argparser.error("Only one source, XML or DB file, can be used")
    if (not args.db) and (not args.filename):
        argparser.error("Must specify one source, XML or DB file")
    if args.db and not (args.pid and args.user):
        argparser.error("Must specify a username and a passage ID when " +
                        "using DB file option")
    if (args.pid or args.user) and not args.db:
        argparser.error(
            "Cannot use user and passage ID options without DB file")

    if args.filename:
        passage = site2passage(args.filename)
    else:
        conn = sqlite3.connect(args.db)
        c = conn.cursor()
        passage = db2passage(c, args.pid, args.user)

    if args.binary:
        with open(args.binary, "wb") as binf:
            pickle.dump(passage, binf)
    else:
        root = ucca.convert.to_standard(passage)
        output = indent_xml(tostring(root).decode())
        if args.outfile:
            with open(args.outfile, "w", encoding="utf-8") as outf:
                outf.write(output)
        else:
            print(output)

    sys.exit(0)
Beispiel #5
0
def passage2file(passage, filename, indent=True, binary=False):
    """Writes a UCCA passage as a standard XML file or a binary pickle
    :param passage: passage object to write
    :param filename: file name to write to
    :param indent: whether to indent each line
    :param binary: whether to write pickle format (or XML)
    """
    if binary:
        with open(filename, 'wb') as h:
            pickle.dump(passage, h)
    else:  # xml
        root = to_standard(passage)
        xml = tostring(root).decode()
        output = indent_xml(xml) if indent else xml
        with open(filename, 'w') as h:
            h.write(output)
def main():
    argparser = argparse.ArgumentParser(description=desc)
    argparser.add_argument("filename", nargs="?", help="XML file name to convert")
    argparser.add_argument("-o", "--outfile", help="output file for standard XML")
    argparser.add_argument("-b", "--binary", help="output file for binary pickel")
    argparser.add_argument("-d", "--db", help="DB file to get input from")
    argparser.add_argument("-p", "--pid", type=int, help="PassageID to query DB")
    argparser.add_argument("-u", "--user", help="Username to DB query")
    args = argparser.parse_args()

    # Checking for illegal combinations
    if args.db and args.filename:
        argparser.error("Only one source, XML or DB file, can be used")
    if (not args.db) and (not args.filename):
        argparser.error("Must specify one source, XML or DB file")
    if args.db and not (args.pid and args.user):
        argparser.error("Must specify a username and a passage ID when " +
                     "using DB file option")
    if (args.pid or args.user) and not args.db:
        argparser.error("Cannot use user and passage ID options without DB file")

    if args.filename:
        passage = site2passage(args.filename)
    else:
        conn = sqlite3.connect(args.db)
        c = conn.cursor()
        passage = db2passage(c, args.pid, args.user)

    if args.binary:
        with open(args.binary, "wb") as binf:
            pickle.dump(passage, binf)
    else:
        root = ucca.convert.to_standard(passage)
        output = indent_xml(tostring(root).decode())
        if args.outfile:
            with open(args.outfile, "w", encoding="utf-8") as outf:
                outf.write(output)
        else:
            print(output)

    sys.exit(0)
Beispiel #7
0
def main(args):
    if args.filenames:
        passages = ((filename, site2passage(filename))
                    for filename in args.filenames)
    else:
        conn = sqlite3.connect(args.db)
        c = conn.cursor()
        passages = ((pid, db2passage(c, pid, args.user)) for pid in args.pids)

    for filename, passage in passages:
        if args.binary:
            with open(outfile(filename, args.binary, ".pickle"), "wb") as binf:
                pickle.dump(passage, binf)
        else:
            root = ucca.convert.to_standard(passage)
            output = indent_xml(tostring(root).decode())
            if args.outfile:
                with open(outfile(filename, args.outfile, ".xml"),
                          "w",
                          encoding="utf-8") as outf:
                    outf.write(output)
            else:
                print(output)
Beispiel #8
0
def write(graph, input, file):
    passage = graph2passage(graph, input)
    root = to_standard(passage)
    xml_string = ET.tostring(root).decode()
    output = textutil.indent_xml(xml_string)
    file.write(output)