spaCy/spacy/cli/convert.py

43 lines
1.4 KiB
Python
Raw Normal View History

2017-04-07 11:04:17 +00:00
# coding: utf8
2017-04-13 11:51:54 +00:00
from __future__ import unicode_literals
2017-04-07 11:04:17 +00:00
import plac
2017-04-13 11:51:54 +00:00
from pathlib import Path
2017-04-07 11:04:17 +00:00
2017-05-19 18:24:39 +00:00
from .converters import conllu2json, iob2json
2017-05-07 21:25:29 +00:00
from ..util import prints
2017-04-07 11:04:17 +00:00
# Converters are matched by file extension. To add a converter, add a new entry
# to this dict with the file extension mapped to the converter function imported
# from /converters.
CONVERTERS = {
2017-05-17 11:13:48 +00:00
'.conllu': conllu2json,
2017-05-19 18:24:39 +00:00
'.conll': conllu2json,
'.iob': iob2json
2017-04-07 11:04:17 +00:00
}
@plac.annotations(
input_file=("input file", "positional", None, str),
output_dir=("output directory for converted file", "positional", None, str),
n_sents=("Number of sentences per doc", "option", "n", float),
morphology=("Enable appending morphology to tags", "flag", "m", bool)
)
2017-05-26 00:47:12 +00:00
def convert(_, input_file, output_dir, n_sents, morphology):
"""Convert files into JSON format for use with train command and other
experiment management functions.
"""
2017-04-07 11:04:17 +00:00
input_path = Path(input_file)
output_path = Path(output_dir)
2017-05-07 21:25:29 +00:00
if not input_path.exists():
prints(input_path, title="Input file not found", exits=1)
2017-04-07 11:04:17 +00:00
if not output_path.exists():
prints(output_path, title="Output directory not found", exits=1)
2017-05-07 21:25:29 +00:00
file_ext = input_path.suffix
if not file_ext in CONVERTERS:
prints("Can't find converter for %s" % input_path.parts[-1],
title="Unknown format", exits=1)
2017-05-26 00:47:12 +00:00
CONVERTERS[file_ext](input_path, output_path, n_sents, morphology)