papie lemmatization · tagging · any language

Candidate features

Sources of candidate (lemma, POS) readings and their feature encoding.

Generated from the source by tools/make_api.py; do not edit by hand.

papie.lexicon.sources

Candidate sources: external analyzers or dictionaries that propose the possible
(lemma, POS) readings of a word form.

A source is anything with candidates(form) -> list[Candidate] (and optionally
pos_tags() to advertise its tagset). Sources are described in YAML as ::

    - name: dictionary            # built-in: dictionary | collatinus | callable
      path: latin/train.tsv       # ... + that source's own options
    - class_path: my_pkg.MySource # or any importable class (Lightning style)
      init_args: {...}

build_source(spec) turns one such spec into an instance; CachedSource wraps it
so each distinct form is analysed once (and can be persisted to JSON for offline
precomputation).

class Candidate

    lemma: str
    pos: Optional[str] = None
    count: int = 1

class CandidateSource

Interface. Implement candidates; pos_tags is optional.

CandidateSource.candidates

candidates(self, form: str) -> List[Candidate]

CandidateSource.pos_tags

pos_tags(self) -> Optional[List[str]]

class DictionarySource (CandidateSource)

DictionarySource(self, path=None, form_col=0, lemma_col=1, pos_col=2, header=True, sep='\t', lowercase_fallback=True, min_count=1, normalization=None, table=None)
form -> {(lemma, pos): count} read from a TSV (e.g. the training data).

Leakage warning: built from the *training* set, a word's own gold reading is always
in its candidates, so the model can learn to over-trust them. Mitigate with
min_count (drop rare readings), candidate dropout in CandidateFeatures, or by
pointing path at a *different* split/corpus than the one being trained on.

DictionarySource.pos_tags

pos_tags(self)

DictionarySource.candidates

candidates(self, form)

class CollatinusSource (CandidateSource)

CollatinusSource(self, tag_map=None, lemma_map=None, compiled=True, normalization=None, lowercase=False, package_path=None)
Latin candidates from pycollatinus (a port of Collatinus; pip install pycollatinus).

tag_map maps Collatinus' POS / morphology strings onto the model's tagset: a list of
[regex, tag-or-list-of-tags] applied in order to "<pos> <morph>[ UPPER]" (first match wins); unmatched
readings keep pos=None (they still count for lemma features). lemma_map is an
optional {collatinus_lemma: dataset_lemma} alias table (conventions differ).

CollatinusSource.pos_tags

pos_tags(self)

CollatinusSource.candidates

candidates(self, form)

class IkelosSource (CandidateSource)

IkelosSource(self, tag_map=None, normalization='NFC', strip_homograph_digits=True)
Ancient Greek candidates from ikelos (a Python port of the Morpheus analyzer; pip install ikelos).

Each reading's label is "<pos> <stem class>" (noun, verb, participle, or for indeclinable words
indeclinable article / indeclinable prep / indeclinable conj ...). **Without tag_map these native labels
are used as they are** (the feature vocabulary is then given by pos_vocab in the candidates config); a tag_map (a
list of [regex, tag-or-list-of-tags], first match wins) can map them onto another tagset instead, but a learned
projection does not need that. Lemmas lose Morpheus' trailing homograph digit (ἐράω1 -> ἐράω); input is NFC.

IkelosSource.pos_tags

pos_tags(self)

IkelosSource.candidates

candidates(self, form)

class CallableSource (CandidateSource)

CallableSource(self, function, pos_tags=None)
Wrap any module:function mapping a form to a list of lemma, (lemma, pos),
or {"lemma":..,"pos":..} readings.

CallableSource.pos_tags

pos_tags(self)

CallableSource.candidates

candidates(self, form)

class CachedSource (CandidateSource)

CachedSource(self, source: CandidateSource, cache_path=None)
Analyse every distinct form once; optionally load/persist the results as JSON.

CachedSource.pos_tags

pos_tags(self)

CachedSource.candidates

candidates(self, form)

CachedSource.save

save(self, path=None)

build_source

build_source(spec) -> CandidateSource
Instantiate a source from a config dict (see module docstring); instances pass through.

papie.lexicon.features

Categorical encoding of candidate (lemma, POS) readings as model features.

For each word and each source the features are ::

    [ POS multi-hot (|pos_vocab|) | stats (4) | hashed lemma bag (lemma_hash_dim) ]

stats = [log1p(#lemmas), log1p(#pos), unknown (no candidate), ambiguous (>1 lemma or POS)].
Rows are concatenated across sources and projected (zero-initialised, so training starts
exactly at the no-candidates model) to the embedding dimension, then *added* to the word
vector (inject: emb) and/or to every byte/char state of the word (inject: outs).
dropout zeroes a word's candidates in training (so the model copes with missing /
wrong analyses; the row then looks like an unknown word).

class CandidateFeatures (nn.Module)

CandidateFeatures(self, sources: Sequence, emb_dim: int, outs_dim: Optional[int]=None, pos_vocab: Optional[List[str]]=None, inject: Sequence[str]=('emb',), lemma_hash_dim: int=0, dropout: float=0.0, lowercase: bool=False)

CandidateFeatures.featurize

featurize(self, raw_words, device=None)
(N words, feature_dim), sentence-major flat order (same as the char/byte encoders).

CandidateFeatures.forward

forward(self, emb, cemb_outs, src_lengths, raw_words, wlen)
Return (emb, cemb_outs) with the candidate features added.

emb is (words, batch, d) time-major; cemb_outs is (T, N flat words, d_outs).

papie.lexicon.precompute

Precompute candidates for every form of a config's data, save the caches, and print an
*oracle* report (how often the gold lemma / POS is among the candidates).

    python -m papie.lexicon.precompute -c configs/x.yaml [--split dev] [--no-save]

Reads model.candidates.sources and the data paths / task columns from the
same config the model trains with; sources with a cache_path are persisted there.

read_tsv

read_tsv(path, cols, header=True)

deep_merge

deep_merge(a, b)

main

main(argv=None)