Source code for pie_extended.pipeline.build
""" What every `get_iterator_and_processor` does: a data iterator that does not give the excluded tokens of a tokenizer, and
a chain of processors
>>> from functools import partial
>>> from pie_extended.pipeline.postprocessor.proto import ProcessorPrototype, SelectTasksProcessor
>>> processor = chained(partial(SelectTasksProcessor, selected=["lemma"]), ProcessorPrototype())
>>> processor.set_tasks(["lemma", "pos"])
['lemma']
"""
from typing import Iterable, Optional
from .iterators.proto import DataIterator
from .postprocessor.proto import ProcessorPrototype
[docs]
def chained(*links) -> ProcessorPrototype:
""" A chain of processors: the outermost first (the first to see the annotations of a token), the innermost last, as
an instance. Each other link makes its processor from `head_processor=`, the one that follows it: a class, a `partial`...
"""
*outer, processor = links
for link in reversed(outer):
processor = link(head_processor=processor)
return processor
[docs]
def iterator_for(tokenizer, max_tokens: int = 256, exclude: Optional[Iterable] = None, *,
excluded_by_tokenizer: bool = True) -> DataIterator:
""" The data iterator of a model: the tokens that the excluders of the tokenizer put aside are not tagged, nor the
patterns of `exclude`
:param excluded_by_tokenizer: False for a tokenizer with no excluders (the iterator then only has `exclude`)
"""
patterns = (list(tokenizer.exclude_patterns()) if excluded_by_tokenizer else []) + list(exclude or [])
return DataIterator(tokenizer=tokenizer, exclude_patterns=patterns or None, max_tokens=max_tokens)
__all__ = ["chained", "iterator_for"]