From 2154c6e165fde06acdec7c43df75e97b963c4f1c Mon Sep 17 00:00:00 2001 From: Wes McNamee Date: Sat, 16 Mar 2024 11:01:44 -0700 Subject: [PATCH] feat: add text search node --- README.md | 1 + WAS_Node_Suite.py | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a5ca723..fff0ba4 100644 --- a/README.md +++ b/README.md @@ -229,6 +229,7 @@ - Text Dictionary Keys: Returns the keys, as a list from a dictionary object - Text Dictionary To Text: Returns the dictionary as text - Text File History: Show previously opened text files *(requires restart to show last sessions files at this time)* + - Text Find: Find a substring or pattern within another string. Returns boolean - Text Find and Replace: Find and replace a substring in a string - Text Find and Replace by Dictionary: Replace substrings in a ASCII text input with a dictionary. - The dictionary keys are used as the key to replace, and the list of lines it contains chosen at random based on the seed. diff --git a/WAS_Node_Suite.py b/WAS_Node_Suite.py index 8b63274..ac40932 100644 --- a/WAS_Node_Suite.py +++ b/WAS_Node_Suite.py @@ -4714,7 +4714,7 @@ def image_batch(self, **kwargs): self._check_image_dimensions(batched_tensors, image_names) batched_tensors = torch.cat(batched_tensors, dim=0) return (batched_tensors,) - + # Latent TO BATCH @@ -10113,6 +10113,38 @@ def text_concatenate(self, delimiter, clean_whitespace, **kwargs): return (merged_text,) +# Text Find + + +# Note that these nodes are exposed as "Find", not "Search". This is the first class that follows the naming convention of the node itself. +class WAS_Find: + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "text": (TEXT_TYPE, {"forceInput": (True if TEXT_TYPE == 'STRING' else False)}), + "substring": ("STRING", {"default": '', "multiline": False}), + "pattern": ("STRING", {"default": '', "multiline": False}), + } + } + + RETURN_TYPES = ("BOOLEAN") + RETURN_NAMES = ("found") + FUNCTION = "execute" + + CATEGORY = "WAS Suite/Text/Search" + + def execute(self, text, substring, pattern): + if substring: + return substring in text + + return bool(re.search(pattern, text)) + + + # Text Search and Replace class WAS_Search_and_Replace: @@ -14005,6 +14037,7 @@ def count_places(self, int_input): "Text Find and Replace by Dictionary": WAS_Search_and_Replace_Dictionary, "Text Find and Replace Input": WAS_Search_and_Replace_Input, "Text Find and Replace": WAS_Search_and_Replace, + "Text Find": WAS_Find, "Text Input Switch": WAS_Text_Input_Switch, "Text List": WAS_Text_List, "Text List Concatenate": WAS_Text_List_Concatenate,