Taggers

A tagger turns tokens into predictions, and drives the whole pipeline: data iterator, disambiguation, processor.

The base

class pie_extended.tagger.ExtensibleTagger(device='cpu', batch_size=100, lower=False, disambiguation=None, quantize=True, cache=True, cache_size=None, num_threads=None)[source]

Bases: object

What every backend shares: the tokenization and exclusion (iterator), the post-processing, SpaceAfter, the disambiguation and the formatting. A backend only has to give add_model and tag (see SpacyTagger and CompositeTagger, whose engines include the models of PaPie 1.0).

Nothing here needs PaPie: it is imported when a model is loaded.

Parameters:
  • quantize – Use Int8 quantization

  • cache – Use cache

add_model(model_path, *tasks)[source]

Adds a model that gives tasks: what a backend implements

Parameters:
  • model_path (str)

  • tasks (str)

Return type:

None

tag(sents, lengths=None, **kwargs)[source]

Tags tokenized sentences: ([[(token, (a value for each task))]], tasks). What a backend implements

tag_file(fpath, iterator, processor, no_tokenizer=False)[source]
Parameters:
tag_str(data, iterator, processor, no_tokenizer=False)[source]
Parameters:
Return type:

List[Dict[str, str]]

iter_tag_token(data, iterator, processor, no_tokenizer=False, empty_token_on_sent_break=False)[source]

Reads the string in [DATA] with [ITERATOR] and [PROCESSOR], then returns each token as a dict

Parameters:
  • data (str) – Textual

  • iterator (DataIterator) – Iterator used to read data

  • processor (ProcessorPrototype) – Processor used to post-process data

  • no_tokenizer (bool) – Disable the tokenizer inside the iterator

  • empty_token_on_sent_break (bool) – Returns a None token when going into a new sequence.

Yield:

Token in the form of a dict or, if [empty_token…] is True, a None value when changing “sentences”

Return type:

Generator[Dict[str, str] | None, None, None]

iter_tag(data, iterator, processor, formatter_class=<class 'pie_extended.pipeline.formatters.proto.Formatter'>, no_tokenizer=False)[source]
Parameters:
pie_extended.tagger.BaseTagger

alias of ExtensibleTagger

Composite (PaPie 1.0, Sôtêr, spaCy)

class pie_extended.composite_tagger.CompositeTagger(device='cpu', batch_size=100, lower=False, disambiguation=None, quantize=False, cache=False, num_threads=None, cache_size=None, **ignored)[source]

Bases: ExtensibleTagger

Tagger whose tasks come from different engines: each task is given by one of them.

An engine is added like a model, with add_model(name, *tasks), and in the same syntax as the other models (<name,task,task>), which is how it is chosen from the command (–model_path) or in a package of a model:

  • soter:Zual/soter-mini is the lemmatizer Sôtêr (it only gives lemma). soter:Zual/soter-mini#proiel gives the lemmas of the convention of PROIEL, #ptnk of PTNK (Perseus is the default);

  • any other name is a spaCy pipeline (grc_perseus_sm), see SpacyTagger.

>>> CompositeTagger.engine_of("soter:Zual/soter-mini#proiel")
('soter', 'Zual/soter-mini', 'proiel')
>>> CompositeTagger.engine_of("grc_perseus_sm")
('spacy', 'grc_perseus_sm', None)
Parameters:
  • device (str)

  • batch_size (int)

  • lower (bool)

  • quantize (bool)

  • cache (bool)

static engine_of(model_path)[source]

The kind of engine that a name designates, the name of the model, and its option

Parameters:

model_path (str)

Return type:

Tuple[str, str, str | None]

add_model(model_path, *tasks)[source]

Adds an engine, that gives tasks

Raises:

ValueError – If a task is already given by another engine, or if the engine does not give it

Parameters:
  • model_path (str)

  • tasks (str)

Return type:

None

tag(sents, lengths=None, **kwargs)[source]

Same contract as PaPie’s Tagger.tag: each engine tags the sentences, and the tasks are put together

Parameters:
  • sents (Sequence[Sequence[str]])

  • lengths (Sequence[int] | None)

Return type:

Tuple[List[List[Tuple[str, Tuple[str, …]]]], List[str]]

Backends

BACKENDS maps the Backend of a model (composite, spacy) to its tagger, and pie_extended.backends is the one place to import them from (ExtensibleTagger, SpacyTagger, CompositeTagger, PapieTagger, SoterLemmatizer): importing it needs neither PaPie, spaCy nor transformers.

spaCy

exception pie_extended.spacy_tagger.PipelineNotFound[source]

Bases: FileNotFoundError

The spaCy pipeline of a model is not installed (its message tells how to install it)

class pie_extended.spacy_tagger.SpacyTagger(device='cpu', batch_size=100, lower=False, disambiguation=None, pipeline=None, tasks=None, **ignored)[source]

Bases: ExtensibleTagger

Tagger that runs a spaCy pipeline (GreCy, for example) instead of PaPie models.

Everything that is not the prediction itself is inherited from ExtensibleTagger: tokenization and exclusion (iterator), post-processing, SpaceAfter, disambiguation, formatting. Only add_model and tag change: the data iterator already gives pre-tokenized sentences, they are given to spaCy as Doc objects so spaCy’s own tokenizer is never used.

spaCy is an optional dependency (pip install pie-extended[grecy]), it is only imported when a model is loaded.

The tasks are the spaCy token attributes: lemma, pos (universal POS), tag (language specific POS) and morph (UD features, _ if empty).

Parameters:
  • device (str)

  • batch_size (int)

  • lower (bool)

  • pipeline (str | None)

  • tasks (Sequence[str] | None)

EXTRACTORS: Dict[str, Callable] = {'lemma': <function SpacyTagger.<lambda>>, 'morph': <function SpacyTagger.<lambda>>, 'pos': <function SpacyTagger.<lambda>>, 'tag': <function SpacyTagger.<lambda>>}
UNUSED_COMPONENTS: Set[str] = {'coref', 'entity_linker', 'entity_ruler', 'ner', 'parser', 'span_resolver', 'span_ruler', 'spancat', 'textcat', 'textcat_multilabel'}
add_model(model_path, *tasks)[source]

Loads a spaCy pipeline (without the components that do not produce a task, see UNUSED_COMPONENTS)

Parameters:
  • model_path (str) – Name of an installed spaCy pipeline (grc_proiel_sm) or path to one

  • tasks (str) – Tasks to output (see EXTRACTORS)

Return type:

None

tag(sents, lengths=None, **kwargs)[source]

Same contract as PaPie’s Tagger.tag: tagged sentences ([(token, (tag for each task))]) and the tasks

Parameters:
  • sents (List[List[str]])

  • lengths (List[int] | None)

Return type:

Tuple[List[List[Tuple[str, Tuple[str, …]]]], List[str]]