TextPredictionDataCreator
Create masked-token text-prediction datasets from base corpora.
The creator scans text, finds configured target tokens, masks each match, and
produces either per-class DataFrames or a unified training table consumable by
:class:~gradiend.trainer.text.prediction.trainer.TextPredictionTrainer.
It can also create neutral evaluation rows by excluding all configured target
words and optional spaCy tag patterns.
The main public workflow is:
TextPredictionDataCreator(...).generate_training_data(...) and, when
neutral evaluation data is needed,
TextPredictionDataCreator(...).generate_neutral_data(...).
Initialize with shared config for both generate methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_data
|
Union[str, DataFrame, List[str]]
|
HF id, pandas df, csv path, or List[str]. |
required |
text_column
|
str
|
Column name for text (default "text"). |
'text'
|
base_max_size
|
Optional[int]
|
Cap on base data (after shuffle, before preprocessing). |
None
|
split
|
str
|
HF split (default "train"). |
'train'
|
hf_config
|
Optional[str]
|
HF dataset config/subset (e.g. "20220301.en" for wikipedia). |
None
|
trust_remote_code
|
Optional[bool]
|
Optional value passed to load_dataset when base_data is HF id. None means do not pass the keyword. |
None
|
preprocess
|
Optional[TextPreprocessConfig]
|
Optional TextPreprocessConfig. |
None
|
spacy_model
|
Optional[str]
|
Spacy model name (e.g. "de_core_news_sm"); lazy-loaded. |
None
|
feature_targets
|
Optional[List[TextFilterConfig]]
|
List of TextFilterConfig. Each config's id (or first target) names the class. |
None
|
min_left_context_words
|
int
|
Default minimum word-like strings required before a matched target. Per-config overrides via TextFilterConfig.min_left_context_words are still supported. Mainly useful for decoder-only models where masked targets need left context. |
10
|
seed
|
int
|
Random seed for shuffle and sampling. |
42
|
download_if_missing
|
bool
|
If True, auto-download spacy model when not found. |
True
|
output_dir
|
Optional[str]
|
If set, generate_training_data/generate_neutral_data write to this folder when output= is not passed. Default filenames: training_basename + ext, neutral_basename + ext. |
None
|
training_basename
|
str
|
Base name for training output (default "training"); extension from output_format. |
'training'
|
neutral_basename
|
str
|
Base name for neutral output (default "neutral"). |
'neutral'
|
output_format
|
Literal['csv', 'parquet', 'hf']
|
"csv" (default), "parquet", or "hf" (HuggingFace datasets; per_class saves as subsets). "hf" requires the datasets library; falls back to csv with a warning if not installed. |
'csv'
|
use_cache
|
bool
|
If True and output_dir is set, generate_training_data and generate_neutral_data load from existing files in output_dir when available instead of regenerating. |
False
|
split_group_col
|
Optional[str]
|
Column used for vocabulary-held-out splits (e.g. |
None
|
split_group_key
|
SplitGroupKey
|
Callable or sequence of callables applied to |
None
|
Source code in gradiend/data/text/prediction/creator.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
_get_all_target_words
Collect all target strings from feature_targets (for neutral exclusion).
Source code in gradiend/data/text/prediction/creator.py
_get_texts
Load base data as raw texts (no sentence splitting); cache when no override.
Source code in gradiend/data/text/prediction/creator.py
_load_cached_neutral
Load neutral data from path when use_cache and output_dir are set. Returns None if path does not exist.
Source code in gradiend/data/text/prediction/creator.py
_load_cached_training
Load training data from path when use_cache and output_dir are set. Returns None if path does not exist.
Source code in gradiend/data/text/prediction/creator.py
_resolve_output_path
Resolve output path: explicit path, or output_dir + basename + extension.
Source code in gradiend/data/text/prediction/creator.py
generate_neutral_data
generate_neutral_data(base_data=None, additional_excluded_words=None, excluded_spacy_tags=None, max_size=None, format='minimal', output=None)
Generate neutral data by excluding sentences with target tokens.
Excludes sentences containing:
- Any token in (target words + additional_excluded_words), deduplicated
- Any token matching any spec in excluded_spacy_tags
Use excluded_spacy_tags=[{"pos": "DET"}, {"pos": "PRON", "Person": "3"}] to exclude determiners and third-person pronouns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_data
|
Optional[Union[str, DataFrame, List[str]]]
|
Optional override (otherwise uses creator's base). |
None
|
additional_excluded_words
|
Optional[List[str]]
|
Extra words to exclude (in addition to target words from feature_targets). E.g. gendered articles or pronouns. |
None
|
excluded_spacy_tags
|
Optional[Union[SpacyTagSpec, List[SpacyTagSpec]]]
|
Spacy tag spec(s); exclude if any token matches any spec. Use list for multiple: [{"pos": "DET"}, {"pos": "PRON", "Person": "3"}]. |
None
|
max_size
|
Optional[int]
|
Global cap for neutral dataset. |
None
|
format
|
str
|
Return format ("minimal" = text column for eval). |
'minimal'
|
output
|
Optional[str]
|
If set, save neutral data to this path. When output_dir is set on the creator and output is None, uses output_dir/neutral_basename + extension. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with at least "text" column. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in gradiend/data/text/prediction/creator.py
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 | |
generate_training_data
generate_training_data(max_size_per_class=None, format='per_class', split_name='train', balance='try', output=None, train_ratio=0.8, val_ratio=0.1, test_ratio=0.1, min_rows_per_class_for_split=MIN_ROWS_PER_CLASS_FOR_SPLIT, min_rows_per_target_for_balance=1, raise_on_incomplete_classes=False, split_group_col=None, split_group_key=None)
Generate masked training data by filtering configured target tokens.
Each returned row contains the original text, a masked version,
the matched label token, token_count, and a split column.
format="per_class" returns one DataFrame per feature class. The
"minimal" and "unified" formats return a single DataFrame; the
unified format contains factual/alternative columns used by GRADIEND
trainers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_size_per_class
|
Optional[int]
|
Cap per feature class. |
None
|
format
|
str
|
Return structure: |
'per_class'
|
split_name
|
str
|
Value for split column when auto_split is not used (default "train"). |
'train'
|
balance
|
Union[bool, str]
|
"try" (default) shuffles and caps classes without changing target-token
counts; False disables all balancing; "strict" exactly balances target-token
counts within each class, using replacement for targets below
|
'try'
|
output
|
Optional[str]
|
If set, save the data to this path using |
None
|
train_ratio
|
float
|
Fraction of each class for train (default 0.8). |
0.8
|
val_ratio
|
float
|
Fraction of each class for validation (default 0.1). |
0.1
|
test_ratio
|
float
|
Fraction of each class for test (default 0.1). Must sum to 1.0 with train_ratio and val_ratio. |
0.1
|
min_rows_per_class_for_split
|
int
|
Minimum rows per class to perform train/val/test split. Splitting fewer rows yields meaningless splits (e.g. 80/10/10 of 5 rows). Default 10. Set to 0 to disable this check. |
MIN_ROWS_PER_CLASS_FOR_SPLIT
|
min_rows_per_target_for_balance
|
int
|
Floor used by |
1
|
raise_on_incomplete_classes
|
bool
|
If True, raise ValueError when non-empty classes have fewer than min_rows_per_class_for_split rows. Main and incomplete-class files are still saved before raising. Defaults to False, so incomplete classes are excluded from the main generated training data. |
False
|
split_group_col
|
Optional[str]
|
Override instance |
None
|
split_group_key
|
SplitGroupKey
|
Override instance |
None
|
Returns:
| Type | Description |
|---|---|
Union[Dict[str, DataFrame], DataFrame]
|
Per format: dict of DataFrames, or single DataFrame. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If split ratios do not sum to |
TypeError
|
If lower-level split-key normalization receives an invalid
|
Source code in gradiend/data/text/prediction/creator.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 | |