PDstring:removecolorwords
Scrubbing unwanted words out of every caption file in a folder
- Result
You've got a folder of training images and their .txt captions, and the auto-tagger insisted on writing "green" into every caption when the dataset is supposed to be color-agnostic. This node walks a whole directory tree, finds every .txt file, deletes the words you list, and optionally prepends a phrase to the front of each caption. It's caption cleanup in bulk - the kind of dataset hygiene the LoRA-training doc keeps telling you matters more than any hyperparameter.
The default path in the widget is literally a Chinese dataset folder (G:\download\宫廷圣诞猫gongyan_V1 - "palace Christmas cat"), which is your first clue: this was built for a specific person's pipeline, then generalized. The generalization is genuinely useful, but the defaults are not yours.
How it works
It walks the directory recursively (os.walk), so subfolders are included. For each .txt file it:
- Removes every word in
words_to_remove, case-insensitively, using a regex that also eats a trailing parenthetical or an_suffixright after the word - so removing "red" catches "red (background)" and "red_dress" but leaves "fire" alone (word boundaries). - If
words_to_addis set, prepends it asword + " "to the cleaned caption. - Only rewrites the file if the content actually changed, then reports it.
The regex detail matters: it strips a following (anything) or _something that belongs to the removed word, which is what makes it beat a naive find-and-replace. words_to_remove is comma-separated; words_to_add is a single phrase.
The single output, Result, is a report: total files scanned, files actually modified, what was removed, and what was added.
The inputs that matter
directory_path- root folder to scan (recursive).words_to_remove- comma-separated words to delete.words_to_add- phrase to prepend to every caption.
Installing it
Part of Comfyui_PDuse by 7BEII. ComfyUI Manager (search "PDuse") or:
cd ComfyUI/custom_nodes
git clone https://github.com/7BEII/Comfyui_PDuse.git
cd Comfyui_PDuse
pip install -r requirements.txt
Restart ComfyUI. No model downloads - pack deps are numpy, Pillow, scipy, opencv-python, torchvision. Docs are Chinese-first.
Where people get burned
This overwrites your caption files in place, with no backup. If the regex eats something you didn't intend, the original is gone - copy the folder first. Second, it only touches .txt files, so captions stored as .json or embedded in the image are invisible to it. Third, removal is greedy around the word: a removed word that's part of a longer token (like "green" inside "greenscreen") will partially delete things if it isn't at a word boundary - the \b handles most of it, but test on one folder before letting it loose on the whole dataset. If words_to_remove is empty, the node just prepends words_to_add, which is a clean way to stamp a trigger word onto everything.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| directory_path | STRING | G:\\download\\宫廷圣诞猫gongyan_V1 | — |
| words_to_remove | STRING | — | |
| words_to_add | STRING | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| Result | STRING | — |