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
|
|
|
|
2017-05-22 10:28:58 +00:00
|
|
|
import plac
|
2017-04-13 11:51:54 +00:00
|
|
|
from pathlib import Path
|
2018-11-30 19:16:14 +00:00
|
|
|
from wasabi import Printer
|
💫 Replace ujson, msgpack and dill/pickle/cloudpickle with srsly (#3003)
Remove hacks and wrappers, keep code in sync across our libraries and move spaCy a few steps closer to only depending on packages with binary wheels 🎉
See here: https://github.com/explosion/srsly
Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy have steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place.
At the same time, we noticed that having a lot of small dependencies was making maintainence harder, and making installation slower. To solve this, we've made srsly standalone, by including the component packages directly within it. This way we can provide all the serialization utilities we need in a single binary wheel.
srsly currently includes forks of the following packages:
ujson
msgpack
msgpack-numpy
cloudpickle
* WIP: replace json/ujson with srsly
* Replace ujson in examples
Use regular json instead of srsly to make code easier to read and follow
* Update requirements
* Fix imports
* Fix typos
* Replace msgpack with srsly
* Fix warning
2018-12-03 00:28:22 +00:00
|
|
|
import srsly
|
2019-08-29 10:04:01 +00:00
|
|
|
import re
|
2017-04-07 11:04:17 +00:00
|
|
|
|
2019-03-15 17:14:46 +00:00
|
|
|
from .converters import conllu2json, iob2json, conll_ner2json
|
2018-08-14 12:04:32 +00:00
|
|
|
from .converters import ner_jsonl2json
|
2018-11-30 19:16:14 +00:00
|
|
|
|
2017-04-07 11:04:17 +00:00
|
|
|
|
2019-08-29 10:04:01 +00:00
|
|
|
# Converters are matched by file extension except for ner/iob, which are
|
|
|
|
# matched by file extension and content. To add a converter, add a new
|
2017-10-27 12:38:39 +00:00
|
|
|
# entry to this dict with the file extension mapped to the converter function
|
|
|
|
# imported from /converters.
|
2017-04-07 11:04:17 +00:00
|
|
|
CONVERTERS = {
|
2019-03-15 17:14:46 +00:00
|
|
|
"conllubio": conllu2json,
|
2018-11-30 19:16:14 +00:00
|
|
|
"conllu": conllu2json,
|
|
|
|
"conll": conllu2json,
|
|
|
|
"ner": conll_ner2json,
|
|
|
|
"iob": iob2json,
|
|
|
|
"jsonl": ner_jsonl2json,
|
2017-04-07 11:04:17 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 19:16:14 +00:00
|
|
|
# File types
|
2019-03-08 22:15:23 +00:00
|
|
|
FILE_TYPES = ("json", "jsonl", "msg")
|
|
|
|
FILE_TYPES_STDOUT = ("json", "jsonl")
|
2018-11-30 19:16:14 +00:00
|
|
|
|
2017-04-07 11:04:17 +00:00
|
|
|
|
2017-05-22 10:28:58 +00:00
|
|
|
@plac.annotations(
|
2018-11-30 19:16:14 +00:00
|
|
|
input_file=("Input file", "positional", None, str),
|
2019-03-08 22:15:23 +00:00
|
|
|
output_dir=("Output directory. '-' for stdout.", "positional", None, str),
|
|
|
|
file_type=("Type of data to produce: {}".format(FILE_TYPES), "option", "t", str),
|
2019-08-29 10:04:01 +00:00
|
|
|
n_sents=("Number of sentences per doc (0 to disable)", "option", "n", int),
|
|
|
|
seg_sents=("Segment sentences (for -c ner)", "flag", "s"),
|
|
|
|
model=("Model for sentence segmentation (for -s)", "option", "b", str),
|
2019-03-08 22:15:23 +00:00
|
|
|
converter=("Converter: {}".format(tuple(CONVERTERS.keys())), "option", "c", str),
|
2018-08-14 12:04:32 +00:00
|
|
|
lang=("Language (if tokenizer required)", "option", "l", str),
|
2018-11-30 19:16:14 +00:00
|
|
|
morphology=("Enable appending morphology to tags", "flag", "m", bool),
|
|
|
|
)
|
|
|
|
def convert(
|
|
|
|
input_file,
|
|
|
|
output_dir="-",
|
2019-04-12 09:31:23 +00:00
|
|
|
file_type="json",
|
2018-11-30 19:16:14 +00:00
|
|
|
n_sents=1,
|
2019-08-29 10:04:01 +00:00
|
|
|
seg_sents=False,
|
|
|
|
model=None,
|
2018-11-30 19:16:14 +00:00
|
|
|
morphology=False,
|
|
|
|
converter="auto",
|
|
|
|
lang=None,
|
|
|
|
):
|
2017-05-27 18:01:46 +00:00
|
|
|
"""
|
|
|
|
Convert files into JSON format for use with train command and other
|
2018-11-30 19:16:14 +00:00
|
|
|
experiment management functions. If no output_dir is specified, the data
|
2019-04-12 09:31:23 +00:00
|
|
|
is written to stdout, so you can pipe them forward to a JSON file:
|
|
|
|
$ spacy convert some_file.conllu > some_file.json
|
2017-05-22 10:28:58 +00:00
|
|
|
"""
|
2018-11-30 19:16:14 +00:00
|
|
|
msg = Printer()
|
2017-04-07 11:04:17 +00:00
|
|
|
input_path = Path(input_file)
|
2018-11-30 19:16:14 +00:00
|
|
|
if file_type not in FILE_TYPES:
|
|
|
|
msg.fail(
|
2018-12-08 10:49:43 +00:00
|
|
|
"Unknown file type: '{}'".format(file_type),
|
|
|
|
"Supported file types: '{}'".format(", ".join(FILE_TYPES)),
|
2018-11-30 19:16:14 +00:00
|
|
|
exits=1,
|
|
|
|
)
|
2019-03-08 22:15:23 +00:00
|
|
|
if file_type not in FILE_TYPES_STDOUT and output_dir == "-":
|
|
|
|
# TODO: support msgpack via stdout in srsly?
|
|
|
|
msg.fail(
|
|
|
|
"Can't write .{} data to stdout.".format(file_type),
|
|
|
|
"Please specify an output directory.",
|
|
|
|
exits=1,
|
|
|
|
)
|
2017-05-07 21:25:29 +00:00
|
|
|
if not input_path.exists():
|
2018-12-08 10:49:43 +00:00
|
|
|
msg.fail("Input file not found", input_path, exits=1)
|
2018-11-30 19:16:14 +00:00
|
|
|
if output_dir != "-" and not Path(output_dir).exists():
|
2018-12-08 10:49:43 +00:00
|
|
|
msg.fail("Output directory not found", output_dir, exits=1)
|
2019-08-29 10:04:01 +00:00
|
|
|
input_data = input_path.open("r", encoding="utf-8").read()
|
2018-11-30 19:16:14 +00:00
|
|
|
if converter == "auto":
|
2017-10-10 01:06:28 +00:00
|
|
|
converter = input_path.suffix[1:]
|
2019-08-29 10:04:01 +00:00
|
|
|
if converter == "ner" or converter == "iob":
|
|
|
|
converter_autodetect = autodetect_ner_format(input_data)
|
|
|
|
if converter_autodetect == "ner":
|
|
|
|
msg.info("Auto-detected token-per-line NER format")
|
|
|
|
converter = converter_autodetect
|
|
|
|
elif converter_autodetect == "iob":
|
|
|
|
msg.info("Auto-detected sentence-per-line NER format")
|
|
|
|
converter = converter_autodetect
|
|
|
|
else:
|
2019-08-31 11:39:06 +00:00
|
|
|
msg.warn(
|
|
|
|
"Can't automatically detect NER format. Conversion may not succeed. See https://spacy.io/api/cli#convert"
|
|
|
|
)
|
2017-10-27 12:38:39 +00:00
|
|
|
if converter not in CONVERTERS:
|
2018-12-08 10:49:43 +00:00
|
|
|
msg.fail("Can't find converter for {}".format(converter), exits=1)
|
2018-11-30 19:16:14 +00:00
|
|
|
# Use converter function to convert data
|
2017-10-10 01:06:28 +00:00
|
|
|
func = CONVERTERS[converter]
|
2019-08-31 11:39:06 +00:00
|
|
|
data = func(
|
|
|
|
input_data,
|
|
|
|
n_sents=n_sents,
|
|
|
|
seg_sents=seg_sents,
|
|
|
|
use_morphology=morphology,
|
|
|
|
lang=lang,
|
|
|
|
model=model,
|
|
|
|
)
|
2018-11-30 19:16:14 +00:00
|
|
|
if output_dir != "-":
|
|
|
|
# Export data to a file
|
|
|
|
suffix = ".{}".format(file_type)
|
|
|
|
output_file = Path(output_dir) / Path(input_path.parts[-1]).with_suffix(suffix)
|
|
|
|
if file_type == "json":
|
💫 Replace ujson, msgpack and dill/pickle/cloudpickle with srsly (#3003)
Remove hacks and wrappers, keep code in sync across our libraries and move spaCy a few steps closer to only depending on packages with binary wheels 🎉
See here: https://github.com/explosion/srsly
Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy have steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place.
At the same time, we noticed that having a lot of small dependencies was making maintainence harder, and making installation slower. To solve this, we've made srsly standalone, by including the component packages directly within it. This way we can provide all the serialization utilities we need in a single binary wheel.
srsly currently includes forks of the following packages:
ujson
msgpack
msgpack-numpy
cloudpickle
* WIP: replace json/ujson with srsly
* Replace ujson in examples
Use regular json instead of srsly to make code easier to read and follow
* Update requirements
* Fix imports
* Fix typos
* Replace msgpack with srsly
* Fix warning
2018-12-03 00:28:22 +00:00
|
|
|
srsly.write_json(output_file, data)
|
2018-11-30 19:16:14 +00:00
|
|
|
elif file_type == "jsonl":
|
💫 Replace ujson, msgpack and dill/pickle/cloudpickle with srsly (#3003)
Remove hacks and wrappers, keep code in sync across our libraries and move spaCy a few steps closer to only depending on packages with binary wheels 🎉
See here: https://github.com/explosion/srsly
Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy have steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place.
At the same time, we noticed that having a lot of small dependencies was making maintainence harder, and making installation slower. To solve this, we've made srsly standalone, by including the component packages directly within it. This way we can provide all the serialization utilities we need in a single binary wheel.
srsly currently includes forks of the following packages:
ujson
msgpack
msgpack-numpy
cloudpickle
* WIP: replace json/ujson with srsly
* Replace ujson in examples
Use regular json instead of srsly to make code easier to read and follow
* Update requirements
* Fix imports
* Fix typos
* Replace msgpack with srsly
* Fix warning
2018-12-03 00:28:22 +00:00
|
|
|
srsly.write_jsonl(output_file, data)
|
2019-03-08 22:15:23 +00:00
|
|
|
elif file_type == "msg":
|
|
|
|
srsly.write_msgpack(output_file, data)
|
2019-08-31 11:39:06 +00:00
|
|
|
msg.good(
|
|
|
|
"Generated output file ({} documents): {}".format(len(data), output_file)
|
|
|
|
)
|
2018-11-30 19:16:14 +00:00
|
|
|
else:
|
|
|
|
# Print to stdout
|
|
|
|
if file_type == "json":
|
💫 Replace ujson, msgpack and dill/pickle/cloudpickle with srsly (#3003)
Remove hacks and wrappers, keep code in sync across our libraries and move spaCy a few steps closer to only depending on packages with binary wheels 🎉
See here: https://github.com/explosion/srsly
Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy have steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place.
At the same time, we noticed that having a lot of small dependencies was making maintainence harder, and making installation slower. To solve this, we've made srsly standalone, by including the component packages directly within it. This way we can provide all the serialization utilities we need in a single binary wheel.
srsly currently includes forks of the following packages:
ujson
msgpack
msgpack-numpy
cloudpickle
* WIP: replace json/ujson with srsly
* Replace ujson in examples
Use regular json instead of srsly to make code easier to read and follow
* Update requirements
* Fix imports
* Fix typos
* Replace msgpack with srsly
* Fix warning
2018-12-03 00:28:22 +00:00
|
|
|
srsly.write_json("-", data)
|
2018-11-30 19:16:14 +00:00
|
|
|
elif file_type == "jsonl":
|
💫 Replace ujson, msgpack and dill/pickle/cloudpickle with srsly (#3003)
Remove hacks and wrappers, keep code in sync across our libraries and move spaCy a few steps closer to only depending on packages with binary wheels 🎉
See here: https://github.com/explosion/srsly
Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy have steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place.
At the same time, we noticed that having a lot of small dependencies was making maintainence harder, and making installation slower. To solve this, we've made srsly standalone, by including the component packages directly within it. This way we can provide all the serialization utilities we need in a single binary wheel.
srsly currently includes forks of the following packages:
ujson
msgpack
msgpack-numpy
cloudpickle
* WIP: replace json/ujson with srsly
* Replace ujson in examples
Use regular json instead of srsly to make code easier to read and follow
* Update requirements
* Fix imports
* Fix typos
* Replace msgpack with srsly
* Fix warning
2018-12-03 00:28:22 +00:00
|
|
|
srsly.write_jsonl("-", data)
|
2019-08-29 10:04:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def autodetect_ner_format(input_data):
|
|
|
|
# guess format from the first 20 lines
|
|
|
|
lines = input_data.split("\n")[:20]
|
|
|
|
format_guesses = {"ner": 0, "iob": 0}
|
|
|
|
iob_re = re.compile(r"\S+\|(O|[IB]-\S+)")
|
|
|
|
ner_re = re.compile(r"\S+\s+(O|[IB]-\S+)$")
|
|
|
|
for line in lines:
|
|
|
|
line = line.strip()
|
|
|
|
if iob_re.search(line):
|
|
|
|
format_guesses["iob"] += 1
|
|
|
|
if ner_re.search(line):
|
|
|
|
format_guesses["ner"] += 1
|
|
|
|
if format_guesses["iob"] == 0 and format_guesses["ner"] > 0:
|
|
|
|
return "ner"
|
|
|
|
if format_guesses["ner"] == 0 and format_guesses["iob"] > 0:
|
|
|
|
return "iob"
|
|
|
|
return None
|