TextPredictionDataCreator
Creates training and neutral data for text prediction from base corpora.
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
|
bool
|
Passed to load_dataset when base_data is HF id. Default False. |
False
|
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
|
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
|
Source code in gradiend/data/text/prediction/creator.py
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 173 174 175 176 177 178 179 | |
_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. |
Source code in gradiend/data/text/prediction/creator.py
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 | |
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)
Generate training data by filtering and masking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_size_per_class
|
Optional[int]
|
Cap per feature class. |
None
|
format
|
str
|
Return structure: "per_class" (dict), "unified", or "minimal". |
'per_class'
|
split_name
|
str
|
Value for split column when auto_split is not used (default "train"). |
'train'
|
balance
|
Union[bool, str]
|
"try" (default) attempt balance, fill with abundant; False no balancing; "strict" cap all to smallest. Uses TextFilterConfig.weight. |
'try'
|
output
|
Optional[str]
|
If set, save the data to this path as CSV (unified table when format is "per_class", otherwise the returned DataFrame). |
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
|
Returns:
| Type | Description |
|---|---|
Union[Dict[str, DataFrame], DataFrame]
|
Per format: dict of DataFrames, or single DataFrame. |
Source code in gradiend/data/text/prediction/creator.py
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 | |