2018-08-14 12:04:32 +00:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
💫 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
|
2018-11-30 19:16:14 +00:00
|
|
|
|
|
|
|
from ...util import get_lang_class
|
2018-08-14 12:04:32 +00:00
|
|
|
from .._messages import Messages
|
|
|
|
|
|
|
|
|
2018-11-30 19:16:14 +00:00
|
|
|
def ner_jsonl2json(input_data, lang=None, n_sents=10, use_morphology=False):
|
2018-08-14 12:04:32 +00:00
|
|
|
if lang is None:
|
2018-11-30 19:16:14 +00:00
|
|
|
raise ValueError(Messages.M054)
|
2018-08-14 12:04:32 +00:00
|
|
|
json_docs = []
|
💫 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
|
|
|
input_tuples = [srsly.json_loads(line) for line in input_data]
|
2018-08-14 12:04:32 +00:00
|
|
|
nlp = get_lang_class(lang)()
|
|
|
|
for i, (raw_text, ents) in enumerate(input_tuples):
|
|
|
|
doc = nlp.make_doc(raw_text)
|
|
|
|
doc[0].is_sent_start = True
|
2018-11-30 19:16:14 +00:00
|
|
|
doc.ents = [doc.char_span(s, e, label=L) for s, e, L in ents["entities"]]
|
|
|
|
json_docs.append(doc.to_json())
|
|
|
|
return json_docs
|