@plac.annotations(<EOL>input_file=("<STR_LIT>", "<STR_LIT>", None, str),<EOL>output_dir=("<STR_LIT>", "<STR_LIT>", None, str),<EOL>file_type=("<STR_LIT>".format(FILE_TYPES), "<STR_LIT>", "<STR_LIT:t>", str),<EOL>n_sents=("<STR_LIT>", "<STR_LIT>", "<STR_LIT:n>", int),<EOL>converter=("<STR_LIT>".format(tuple(CONVERTERS.keys())), "<STR_LIT>", "<STR_LIT:c>", str),<EOL>lang=("<STR_LIT>", "<STR_LIT>", "<STR_LIT:l>", str),<EOL>morphology=("<STR_LIT>", "<STR_LIT>", "<STR_LIT:m>", bool),<EOL>)<EOL>def convert(<EOL>input_file,<EOL>output_dir="<STR_LIT:->",<EOL>file_type="<STR_LIT>",<EOL>n_sents=<NUM_LIT:1>,<EOL>morphology=False,<EOL>converter="<STR_LIT>",<EOL>lang=None,<EOL>):
msg = Printer()<EOL>input_path = Path(input_file)<EOL>if file_type not in FILE_TYPES:<EOL><INDENT>msg.fail(<EOL>"<STR_LIT>".format(file_type),<EOL>"<STR_LIT>".format("<STR_LIT:U+002CU+0020>".join(FILE_TYPES)),<EOL>exits=<NUM_LIT:1>,<EOL>)<EOL><DEDENT>if file_type not in FILE_TYPES_STDOUT and output_dir == "<STR_LIT:->":<EOL><INDENT>msg.fail(<EOL>"<STR_LIT>".format(file_type),<EOL>"<STR_LIT>",<EOL>exits=<NUM_LIT:1>,<EOL>)<EOL><DEDENT>if not input_path.exists():<EOL><INDENT>msg.fail("<STR_LIT>", input_path, exits=<NUM_LIT:1>)<EOL><DEDENT>if output_dir != "<STR_LIT:->" and not Path(output_dir).exists():<EOL><INDENT>msg.fail("<STR_LIT>", output_dir, exits=<NUM_LIT:1>)<EOL><DEDENT>if converter == "<STR_LIT>":<EOL><INDENT>converter = input_path.suffix[<NUM_LIT:1>:]<EOL><DEDENT>if converter not in CONVERTERS:<EOL><INDENT>msg.fail("<STR_LIT>".format(converter), exits=<NUM_LIT:1>)<EOL><DEDENT>func = CONVERTERS[converter]<EOL>input_data = input_path.open("<STR_LIT:r>", encoding="<STR_LIT:utf-8>").read()<EOL>data = func(input_data, n_sents=n_sents, use_morphology=morphology, lang=lang)<EOL>if output_dir != "<STR_LIT:->":<EOL><INDENT>suffix = "<STR_LIT>".format(file_type)<EOL>output_file = Path(output_dir) / Path(input_path.parts[-<NUM_LIT:1>]).with_suffix(suffix)<EOL>if file_type == "<STR_LIT>":<EOL><INDENT>srsly.write_json(output_file, data)<EOL><DEDENT>elif file_type == "<STR_LIT>":<EOL><INDENT>srsly.write_jsonl(output_file, data)<EOL><DEDENT>elif file_type == "<STR_LIT>":<EOL><INDENT>srsly.write_msgpack(output_file, data)<EOL><DEDENT>msg.good("<STR_LIT>".format(len(data)), output_file)<EOL><DEDENT>else:<EOL><INDENT>if file_type == "<STR_LIT>":<EOL><INDENT>srsly.write_json("<STR_LIT:->", data)<EOL><DEDENT>elif file_type == "<STR_LIT>":<EOL><INDENT>srsly.write_jsonl("<STR_LIT:->", data)<EOL><DEDENT><DEDENT>
Convert files into JSON format for use with train command and other
experiment management functions. If no output_dir is specified, the data
is written to stdout, so you can pipe them forward to a JSON file:
$ spacy convert some_file.conllu > some_file.json