spaCy/spacy/cli/convert.py

33 lines
1001 B
Python
Raw Normal View History

2017-04-07 14:04:17 +03:00
# coding: utf8
2017-04-13 14:51:54 +03:00
from __future__ import unicode_literals
2017-04-07 14:04:17 +03:00
2017-04-13 14:51:54 +03:00
from pathlib import Path
2017-04-07 14:04:17 +03:00
2017-05-19 21:24:39 +03:00
from .converters import conllu2json, iob2json
2017-05-08 00:25:29 +03:00
from ..util import prints
2017-04-07 14:04:17 +03: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 14:13:48 +03:00
'.conllu': conllu2json,
2017-05-19 21:24:39 +03:00
'.conll': conllu2json,
'.iob': iob2json
2017-04-07 14:04:17 +03:00
}
def convert(input_file, output_dir, *args):
input_path = Path(input_file)
output_path = Path(output_dir)
2017-05-08 00:25:29 +03:00
if not input_path.exists():
prints(input_path, title="Input file not found", exits=True)
2017-04-07 14:04:17 +03:00
if not output_path.exists():
2017-05-08 00:25:29 +03:00
prints(output_path, title="Output directory not found", exits=True)
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=True)
CONVERTERS[file_ext](input_path, output_path, *args)