2020-08-18 14:06:37 +00:00
|
|
|
import warnings
|
2020-08-05 14:00:59 +00:00
|
|
|
from typing import Union, List, Iterable, Iterator, TYPE_CHECKING, Callable
|
2020-07-29 09:36:42 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2020-06-26 17:34:12 +00:00
|
|
|
from .. import util
|
|
|
|
from .example import Example
|
2020-08-18 14:06:37 +00:00
|
|
|
from ..errors import Warnings
|
2020-06-26 17:34:12 +00:00
|
|
|
from ..tokens import DocBin, Doc
|
2020-07-29 09:36:42 +00:00
|
|
|
from ..vocab import Vocab
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
# This lets us add type hints for mypy etc. without causing circular imports
|
|
|
|
from ..language import Language # noqa: F401
|
2020-06-26 17:34:12 +00:00
|
|
|
|
2020-08-18 14:06:37 +00:00
|
|
|
FILE_TYPE = ".spacy"
|
|
|
|
|
2020-06-26 17:34:12 +00:00
|
|
|
|
2020-08-04 13:09:37 +00:00
|
|
|
@util.registry.readers("spacy.Corpus.v1")
|
|
|
|
def create_docbin_reader(
|
|
|
|
path: Path, gold_preproc: bool, max_length: int = 0, limit: int = 0
|
|
|
|
) -> Callable[["Language"], Iterable[Example]]:
|
|
|
|
return Corpus(path, gold_preproc=gold_preproc, max_length=max_length, limit=limit)
|
|
|
|
|
|
|
|
|
2020-06-26 17:34:12 +00:00
|
|
|
class Corpus:
|
2020-08-04 13:09:37 +00:00
|
|
|
"""Iterate Example objects from a file or directory of DocBin (.spacy)
|
2020-08-06 13:29:44 +00:00
|
|
|
formatted data files.
|
2020-08-04 13:09:37 +00:00
|
|
|
|
|
|
|
path (Path): The directory or filename to read from.
|
|
|
|
gold_preproc (bool): Whether to set up the Example object with gold-standard
|
2020-08-05 14:00:59 +00:00
|
|
|
sentences and tokens for the predictions. Gold preprocessing helps
|
2020-08-04 13:09:37 +00:00
|
|
|
the annotations align to the tokenization, and may result in sequences
|
|
|
|
of more consistent length. However, it may reduce run-time accuracy due
|
|
|
|
to train/test skew. Defaults to False.
|
|
|
|
max_length (int): Maximum document length. Longer documents will be
|
|
|
|
split into sentences, if sentence boundaries are available. Defaults to
|
|
|
|
0, which indicates no limit.
|
|
|
|
limit (int): Limit corpus to a subset of examples, e.g. for debugging.
|
|
|
|
Defaults to 0, which indicates no limit.
|
2020-06-26 17:34:12 +00:00
|
|
|
|
2020-07-04 12:23:44 +00:00
|
|
|
DOCS: https://spacy.io/api/corpus
|
2020-06-26 17:34:12 +00:00
|
|
|
"""
|
|
|
|
|
2020-07-29 09:36:42 +00:00
|
|
|
def __init__(
|
2020-08-05 14:00:59 +00:00
|
|
|
self,
|
2020-08-07 12:30:59 +00:00
|
|
|
path: Union[str, Path],
|
2020-08-05 14:00:59 +00:00
|
|
|
*,
|
|
|
|
limit: int = 0,
|
|
|
|
gold_preproc: bool = False,
|
|
|
|
max_length: bool = False,
|
2020-07-29 09:36:42 +00:00
|
|
|
) -> None:
|
2020-08-04 13:09:37 +00:00
|
|
|
self.path = util.ensure_path(path)
|
|
|
|
self.gold_preproc = gold_preproc
|
|
|
|
self.max_length = max_length
|
2020-06-26 17:34:12 +00:00
|
|
|
self.limit = limit
|
|
|
|
|
|
|
|
@staticmethod
|
2020-07-29 09:36:42 +00:00
|
|
|
def walk_corpus(path: Union[str, Path]) -> List[Path]:
|
2020-06-26 17:34:12 +00:00
|
|
|
path = util.ensure_path(path)
|
2020-08-18 14:06:37 +00:00
|
|
|
if not path.is_dir() and path.parts[-1].endswith(FILE_TYPE):
|
2020-06-26 17:34:12 +00:00
|
|
|
return [path]
|
2020-08-18 14:06:37 +00:00
|
|
|
orig_path = path
|
2020-06-26 17:34:12 +00:00
|
|
|
paths = [path]
|
|
|
|
locs = []
|
|
|
|
seen = set()
|
|
|
|
for path in paths:
|
|
|
|
if str(path) in seen:
|
|
|
|
continue
|
|
|
|
seen.add(str(path))
|
2020-08-14 12:58:18 +00:00
|
|
|
if path.parts and path.parts[-1].startswith("."):
|
2020-06-26 17:34:12 +00:00
|
|
|
continue
|
|
|
|
elif path.is_dir():
|
|
|
|
paths.extend(path.iterdir())
|
2020-08-18 14:06:37 +00:00
|
|
|
elif path.parts[-1].endswith(FILE_TYPE):
|
2020-06-26 17:34:12 +00:00
|
|
|
locs.append(path)
|
2020-08-18 14:06:37 +00:00
|
|
|
if len(locs) == 0:
|
|
|
|
warnings.warn(Warnings.W090.format(path=orig_path))
|
2020-06-26 17:34:12 +00:00
|
|
|
return locs
|
|
|
|
|
2020-08-04 13:09:37 +00:00
|
|
|
def __call__(self, nlp: "Language") -> Iterator[Example]:
|
|
|
|
"""Yield examples from the data.
|
|
|
|
|
|
|
|
nlp (Language): The current nlp object.
|
|
|
|
YIELDS (Example): The examples.
|
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/corpus#call
|
|
|
|
"""
|
|
|
|
ref_docs = self.read_docbin(nlp.vocab, self.walk_corpus(self.path))
|
|
|
|
if self.gold_preproc:
|
|
|
|
examples = self.make_examples_gold_preproc(nlp, ref_docs)
|
|
|
|
else:
|
|
|
|
examples = self.make_examples(nlp, ref_docs, self.max_length)
|
|
|
|
yield from examples
|
|
|
|
|
2020-07-29 09:36:42 +00:00
|
|
|
def _make_example(
|
|
|
|
self, nlp: "Language", reference: Doc, gold_preproc: bool
|
|
|
|
) -> Example:
|
2020-07-03 10:58:16 +00:00
|
|
|
if gold_preproc or reference.has_unknown_spaces:
|
|
|
|
return Example(
|
|
|
|
Doc(
|
|
|
|
nlp.vocab,
|
|
|
|
words=[word.text for word in reference],
|
2020-07-04 12:23:44 +00:00
|
|
|
spaces=[bool(word.whitespace_) for word in reference],
|
2020-07-03 10:58:16 +00:00
|
|
|
),
|
2020-07-04 12:23:44 +00:00
|
|
|
reference,
|
2020-07-03 10:58:16 +00:00
|
|
|
)
|
|
|
|
else:
|
2020-07-04 12:23:44 +00:00
|
|
|
return Example(nlp.make_doc(reference.text), reference)
|
|
|
|
|
2020-07-29 09:36:42 +00:00
|
|
|
def make_examples(
|
|
|
|
self, nlp: "Language", reference_docs: Iterable[Doc], max_length: int = 0
|
|
|
|
) -> Iterator[Example]:
|
2020-06-26 17:34:12 +00:00
|
|
|
for reference in reference_docs:
|
2020-07-01 13:16:43 +00:00
|
|
|
if len(reference) == 0:
|
|
|
|
continue
|
|
|
|
elif max_length == 0 or len(reference) < max_length:
|
2020-07-03 10:58:16 +00:00
|
|
|
yield self._make_example(nlp, reference, False)
|
2020-07-01 13:16:43 +00:00
|
|
|
elif reference.is_sentenced:
|
|
|
|
for ref_sent in reference.sents:
|
|
|
|
if len(ref_sent) == 0:
|
|
|
|
continue
|
|
|
|
elif max_length == 0 or len(ref_sent) < max_length:
|
2020-07-03 10:58:16 +00:00
|
|
|
yield self._make_example(nlp, ref_sent.as_doc(), False)
|
|
|
|
|
2020-07-29 09:36:42 +00:00
|
|
|
def make_examples_gold_preproc(
|
|
|
|
self, nlp: "Language", reference_docs: Iterable[Doc]
|
|
|
|
) -> Iterator[Example]:
|
2020-06-26 17:34:12 +00:00
|
|
|
for reference in reference_docs:
|
|
|
|
if reference.is_sentenced:
|
|
|
|
ref_sents = [sent.as_doc() for sent in reference.sents]
|
|
|
|
else:
|
|
|
|
ref_sents = [reference]
|
|
|
|
for ref_sent in ref_sents:
|
2020-07-03 10:58:16 +00:00
|
|
|
eg = self._make_example(nlp, ref_sent, True)
|
2020-07-01 13:02:37 +00:00
|
|
|
if len(eg.x):
|
|
|
|
yield eg
|
2020-06-26 17:34:12 +00:00
|
|
|
|
2020-07-29 09:36:42 +00:00
|
|
|
def read_docbin(
|
|
|
|
self, vocab: Vocab, locs: Iterable[Union[str, Path]]
|
|
|
|
) -> Iterator[Doc]:
|
2020-06-26 17:34:12 +00:00
|
|
|
""" Yield training examples as example dicts """
|
|
|
|
i = 0
|
|
|
|
for loc in locs:
|
|
|
|
loc = util.ensure_path(loc)
|
2020-08-18 14:06:37 +00:00
|
|
|
if loc.parts[-1].endswith(FILE_TYPE):
|
2020-08-07 12:30:59 +00:00
|
|
|
doc_bin = DocBin().from_disk(loc)
|
2020-06-26 17:34:12 +00:00
|
|
|
docs = doc_bin.get_docs(vocab)
|
|
|
|
for doc in docs:
|
|
|
|
if len(doc):
|
|
|
|
yield doc
|
|
|
|
i += 1
|
|
|
|
if self.limit >= 1 and i >= self.limit:
|
|
|
|
break
|