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]'

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.id is None:
        self.id = _first_target_string(self.targets)

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)