Skip to content

TextFilterConfig

Configuration for filtering and masking target tokens in text.

Simple use: TextFilterConfig(target="das") or TextFilterConfig(targets=["der", "die"], spacy_tags={...}) Advanced: TextFilterConfig(targets=[TextFilterConfig(["das"], spacy_tags={...}), "der"], spacy_tags={...}) Nested entries inherit parent spacy_tags and may add/override their own.

When used in TextPredictionDataCreator.feature_targets (a list), id names the output class. If id is None, it is inferred from the first target string.

id class-attribute instance-attribute

id = None

mask class-attribute instance-attribute

mask = '[MASK]'

min_left_context_words class-attribute instance-attribute

min_left_context_words = None

Per-config override for minimum left context (word-like strings before a match).

None inherits the default from :class:~gradiend.data.text.prediction.creator.TextPredictionDataCreator (or 0 when filtering outside a creator). Mainly useful for decoder-only/causal language models, where the gradient for a masked target is computed from the prefix before [MASK].

spacy_tags class-attribute instance-attribute

spacy_tags = None

target class-attribute instance-attribute

target = None

targets class-attribute instance-attribute

targets = None

use_lemma class-attribute instance-attribute

use_lemma = False

weight class-attribute instance-attribute

weight = 1.0

__post_init__

__post_init__()
Source code in gradiend/data/text/filter_config.py
def __post_init__(self) -> None:
    if self.target is not None and self.targets is not None:
        raise ValueError("TextFilterConfig: provide either target or targets, not both")
    if self.target is not None:
        self.targets = [self.target]
    elif isinstance(self.targets, str):
        self.targets = [self.targets]
    if self.targets is None:
        self.targets = []
    if not self.targets:
        raise ValueError("TextFilterConfig.targets must be non-empty")
    if self.min_left_context_words is not None:
        if not isinstance(self.min_left_context_words, int):
            raise TypeError(
                "TextFilterConfig.min_left_context_words must be an int or None, "
                f"got {type(self.min_left_context_words).__name__}"
            )
        if self.min_left_context_words < 0:
            raise ValueError(
                "TextFilterConfig.min_left_context_words must be >= 0, "
                f"got {self.min_left_context_words}"
            )
    if self.id is None:
        self.id = _first_target_string(self.targets)

effective_min_left_context_words

effective_min_left_context_words(default=0)

Return per-config override when set, otherwise default.

Source code in gradiend/data/text/filter_config.py
def effective_min_left_context_words(self, default: int = 0) -> int:
    """Return per-config override when set, otherwise ``default``."""
    if self.min_left_context_words is not None:
        return self.min_left_context_words
    return default

flatten_targets_with_tags

flatten_targets_with_tags(parent_tags=None)

Flatten nested config to list of (target_str, effective_spacy_tags).

Source code in gradiend/data/text/filter_config.py
def flatten_targets_with_tags(
    self,
    parent_tags: Optional[SpacyTagSpec] = None,
) -> List[tuple[str, Optional[SpacyTagSpec]]]:
    """Flatten nested config to list of (target_str, effective_spacy_tags)."""
    effective = self.get_effective_tags(parent_tags)
    out: List[tuple[str, Optional[SpacyTagSpec]]] = []
    for t in self.targets:
        if isinstance(t, str):
            out.append((t, effective))
        elif isinstance(t, TextFilterConfig):
            out.extend(t.flatten_targets_with_tags(effective))
        else:
            raise TypeError(f"target must be str or TextFilterConfig; got {type(t)}")
    return out

get_effective_tags

get_effective_tags(parent_tags=None)

Get merged spacy_tags (self + parent).

Source code in gradiend/data/text/filter_config.py
def get_effective_tags(self, parent_tags: Optional[SpacyTagSpec] = None) -> Optional[SpacyTagSpec]:
    """Get merged spacy_tags (self + parent)."""
    return _merge_spacy_tags(parent_tags, self.spacy_tags)