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-05-22 13:28:58 +03:00
|
|
|
import plac
|
2017-04-13 14:51:54 +03:00
|
|
|
from pathlib import Path
|
2017-04-07 14:04:17 +03:00
|
|
|
|
2018-07-18 19:55:42 +03:00
|
|
|
from .converters import conllu2json, conllubio2json, iob2json, conll_ner2json
|
2018-04-03 16:50:31 +03:00
|
|
|
from ._messages import Messages
|
2017-05-08 00:25:29 +03:00
|
|
|
from ..util import prints
|
2017-04-07 14:04:17 +03:00
|
|
|
|
2017-10-27 15:38:39 +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.
|
2017-04-07 14:04:17 +03:00
|
|
|
CONVERTERS = {
|
2018-07-18 19:55:42 +03:00
|
|
|
'conllubio': conllubio2json,
|
2017-10-10 04:06:28 +03:00
|
|
|
'conllu': conllu2json,
|
|
|
|
'conll': conllu2json,
|
|
|
|
'ner': conll_ner2json,
|
|
|
|
'iob': iob2json,
|
2017-04-07 14:04:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-22 13:28:58 +03:00
|
|
|
@plac.annotations(
|
|
|
|
input_file=("input file", "positional", None, str),
|
|
|
|
output_dir=("output directory for converted file", "positional", None, str),
|
2017-08-18 23:26:12 +03:00
|
|
|
n_sents=("Number of sentences per doc", "option", "n", int),
|
2017-10-10 04:06:28 +03:00
|
|
|
converter=("Name of converter (auto, iob, conllu or ner)", "option", "c", str),
|
2017-10-27 15:38:39 +03:00
|
|
|
morphology=("Enable appending morphology to tags", "flag", "m", bool))
|
2018-01-04 23:33:47 +03:00
|
|
|
def convert(input_file, output_dir, n_sents=1, morphology=False, converter='auto'):
|
2017-05-27 21:01:46 +03:00
|
|
|
"""
|
|
|
|
Convert files into JSON format for use with train command and other
|
2017-05-22 13:28:58 +03:00
|
|
|
experiment management functions.
|
|
|
|
"""
|
2017-04-07 14:04:17 +03:00
|
|
|
input_path = Path(input_file)
|
|
|
|
output_path = Path(output_dir)
|
2017-05-08 00:25:29 +03:00
|
|
|
if not input_path.exists():
|
2018-04-03 16:50:31 +03:00
|
|
|
prints(input_path, title=Messages.M028, exits=1)
|
2017-04-07 14:04:17 +03:00
|
|
|
if not output_path.exists():
|
2018-04-03 16:50:31 +03:00
|
|
|
prints(output_path, title=Messages.M029, exits=1)
|
2017-10-10 04:06:28 +03:00
|
|
|
if converter == 'auto':
|
|
|
|
converter = input_path.suffix[1:]
|
2017-10-27 15:38:39 +03:00
|
|
|
if converter not in CONVERTERS:
|
2018-04-03 16:50:31 +03:00
|
|
|
prints(Messages.M031.format(converter=converter),
|
|
|
|
title=Messages.M030, exits=1)
|
2017-10-10 04:06:28 +03:00
|
|
|
func = CONVERTERS[converter]
|
|
|
|
func(input_path, output_path,
|
|
|
|
n_sents=n_sents, use_morphology=morphology)
|