Skip to content

Commit

Permalink
Merge pull request #8 from nickve28/feature/text-selection-by-regex
Browse files Browse the repository at this point in the history
add node to select text portions by regex
  • Loading branch information
nickve28 authored May 19, 2024
2 parents 1228c4e + 7ff8af8 commit 9b42260
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ This node will select a random image from the provided folder. It allows for
- Cycling controlnet poses
- Cycling only through a subset of images (regular expression knowledge required).

## Select Text with Regular Expression

This node allows selecting parts of text using a regular expression, with a delimiter (default = whitespace)

### Example Use cases:
- Select a subset of a prompt

# Open work
- Improve code
- Add tests
Expand Down
25 changes: 24 additions & 1 deletion nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@
from .utils.file_utils import filename_without_extension, list_images
from .utils.tensor_utils import pil_to_tens

class SelectPortionsFromText:
CATEGORY = 'Nich/utils'
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("selected_text",)
FUNCTION = "execute"

@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input": ("STRING", {}),
"regexp_filter": ("STRING", { "default": None, "multiline": True }),
"delimiter": ("STRING", { "default": " ", "multiline": False })
}
}

def execute(self, input, regexp_filter, delimiter):
filename_filter_regexp = re.compile(regexp_filter)

return (delimiter.join(re.findall(filename_filter_regexp, input)),)


class ImageFromDirSelector:
def __init__(self) -> None:
self.current_image = None
Expand Down Expand Up @@ -92,5 +114,6 @@ def sample_images(self, *_args, **kwargs):


NODE_CLASS_MAPPINGS = {
"Image from Dir Selector (Nich)": ImageFromDirSelector
"Image from Dir Selector (Nich)": ImageFromDirSelector,
"Select Text with Regular Expression (Nich)": SelectPortionsFromText
}

0 comments on commit 9b42260

Please sign in to comment.