From a5ea9be75e9730b749b3f56d64b7b9c3bd7c015f Mon Sep 17 00:00:00 2001 From: "Eric W. Burns" Date: Sun, 20 Jul 2025 22:02:28 -0400 Subject: [PATCH] the rendomize node now supports weighted opions to make some options more common, e.g. "3>>red" is "red" but 3x as likely to come up --- nodes.py | 47 +++++++++++++++++++++++++++++++---------------- pyproject.toml | 2 +- readme.md | 12 ++++++++++-- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/nodes.py b/nodes.py index 73861df..260a639 100644 --- a/nodes.py +++ b/nodes.py @@ -357,7 +357,7 @@ class EbuPromptHelperRandomize: This node takes an input prompt and replaces occurrences of a specified target substring (word_to_replace) with a randomly selected option from a provided string of options. - The options string is split based on the chosen delimiter, which can be newlines, commas, or semi‑colons. + Supports weighted options: prefix an option with `N>>` (where N is an integer) to weight it N times. The seed input controls the random selection, allowing for deterministic output if desired. Inputs: @@ -392,34 +392,49 @@ class EbuPromptHelperRandomize: CATEGORY = "Prompts" def randomize_text(self, prompt_text, word_to_replace, replacement_options, seed, case_sensitive, delimit_options_with): + import re + import random random.seed(seed) - # Split the options string based on the selected delimiter. + # Split options based on chosen delimiter if delimit_options_with == "newlines": - options = [opt.strip() for opt in replacement_options.splitlines() if opt.strip()] + opts = [opt.strip() for opt in replacement_options.splitlines() if opt.strip()] elif delimit_options_with == "commas": - options = [opt.strip() for opt in replacement_options.split(',') if opt.strip()] + opts = [opt.strip() for opt in replacement_options.split(',') if opt.strip()] elif delimit_options_with == "semi-colons": - options = [opt.strip() for opt in replacement_options.split(';') if opt.strip()] + opts = [opt.strip() for opt in replacement_options.split(';') if opt.strip()] else: - options = [opt.strip() for opt in replacement_options.splitlines() if opt.strip()] + opts = [opt.strip() for opt in replacement_options.splitlines() if opt.strip()] - if not options: + if not opts: return (prompt_text, "") - selected = random.choice(options) + # Expand weighted options (syntax: N>>option) + expanded = [] + pattern = re.compile(r'^\s*(\d+)\s*>>(.*)$') + for item in opts: + m = pattern.match(item) + if m: + count = int(m.group(1)) + text = m.group(2).strip() + expanded.extend([text] * count) + else: + expanded.append(item) - # Handle multiple words using "|" - words_to_replace = word_to_replace.split("|") + if not expanded: + return (prompt_text, "") + # Select one option based on weights + selected = random.choice(expanded) + + # Perform replacement + words = word_to_replace.split("|") if case_sensitive: - for word in words_to_replace: - prompt_text = prompt_text.replace(word, selected) + for w in words: + prompt_text = prompt_text.replace(w, selected) else: - import re - for word in words_to_replace: - pattern = re.compile(re.escape(word), re.IGNORECASE) - prompt_text = pattern.sub(selected, prompt_text) + for w in words: + prompt_text = re.compile(re.escape(w), re.IGNORECASE).sub(selected, prompt_text) return (prompt_text, selected) diff --git a/pyproject.toml b/pyproject.toml index 5f9640b..0a075f8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "ComfyUI_EBU_PromptHelper" description = "This ComfyUI extension provides a suite of custom nodes for enhancing and manipulating prompts. Features include random color palette generation, prompt text replacement, prompt randomization using variable names and lists of options, list sampling, file loading, current datetime formatting, and season/weather/time-of-day generation." -version = "1.0.7" +version = "1.0.8" authors = [ {name = "burnsbert"} ] diff --git a/readme.md b/readme.md index 120425d..8242f18 100644 --- a/readme.md +++ b/readme.md @@ -94,7 +94,7 @@ Replaces a target substring with a randomly selected option from a list. **Inputs:** - `prompt_text` (STRING): Input prompt text -- `word_to_replace` (STRING): Target substring to replace (use | as delimiter for multiple) +- `word_to_replace` (STRING): Target substring to replace (you can include multiple strings to match separated by |, e.g. 'red|blue|yellow') - `replacement_options` (STRING): List of possible replacements - `random_seed` (INT): Seed for random selection - `case_sensitive` (BOOLEAN): Whether to match case when replacing @@ -104,6 +104,14 @@ Replaces a target substring with a randomly selected option from a list. - `updated_prompt_text` (STRING): The modified prompt - `word_selected` (STRING): The chosen replacement + +**Weighted Options:** +- Prefix any option with `N>>` (where `N` is an integer) to weight it `N` times. So '3>>>red' would be the same as listing 'red' 3 times. + +**Returns:** +- `updated_prompt_text` (STRING): The modified prompt. +- `word_selected` (STRING): The chosen replacement. + ### EBU PromptHelper Combine Two Strings Combines two strings using a specified join string. @@ -155,7 +163,7 @@ Returns the current date/time in various formats. Selects and removes an item from a list while updating a prompt. **Inputs:** -- `word_to_replace` (STRING): Target substring to replace +- `word_to_replace` (STRING): Target substring to replace (you can include multiple strings to match separated by |, e.g. 'red|blue|yellow') - `list` (STRING): Newline-separated list of options - `seed` (INT): Random seed - `prompt_text` (STRING, optional): Input prompt text