Merge pull request #370 from thayol/sort-text-node

feat: Add "Sort Text" Node
This commit is contained in:
WAS
2024-06-16 18:32:27 -07:00
committed by GitHub
4 changed files with 127 additions and 0 deletions
+46
View File
@@ -10257,6 +10257,51 @@ class WAS_Text_Shuffle:
return (new_text, )
# Text Sort
class WAS_Text_Sort:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"text": (TEXT_TYPE, {"forceInput": (True if TEXT_TYPE == 'STRING' else False)}),
"separator": ("STRING", {"default": ', ', "multiline": False}),
}
}
RETURN_TYPES = (TEXT_TYPE,)
FUNCTION = "sort"
CATEGORY = "WAS Suite/Text/Operations"
def sort(self, text, separator):
tokens = WAS_Text_Sort.split_using_protected_groups(text.strip(separator + " \t\n\r"), separator.strip())
sorted_tokens = sorted(tokens, key=WAS_Text_Sort.token_without_leading_brackets)
return (separator.join(sorted_tokens), )
@staticmethod
def token_without_leading_brackets(token):
return token.replace("\\(", "\0\1").replace("(", "").replace("\0\1", "(").strip()
@staticmethod
def split_using_protected_groups(text, separator):
protected_groups = ""
nesting_level = 0
for char in text:
if char == "(": nesting_level += 1
if char == ")": nesting_level -= 1
if char == separator and nesting_level > 0:
protected_groups += "\0"
else:
protected_groups += char
return list(map(lambda t: t.replace("\0", separator).strip(), protected_groups.split(separator)))
# Text Search and Replace
@@ -13995,6 +14040,7 @@ NODE_CLASS_MAPPINGS = {
"Text String": WAS_Text_String,
"Text Contains": WAS_Text_Contains,
"Text Shuffle": WAS_Text_Shuffle,
"Text Sort": WAS_Text_Sort,
"Text to Conditioning": WAS_Text_to_Conditioning,
"Text to Console": WAS_Text_to_Console,
"Text to Number": WAS_Text_To_Number,
+1
View File
@@ -0,0 +1 @@
[pytest]
+69
View File
@@ -0,0 +1,69 @@
from was_mock import was_text_sort
def test_empty_text():
assert was_text_sort() == ""
def test_empty_text_with_separator_override():
assert was_text_sort(separator="|") == ""
def test_already_sorted_text():
assert was_text_sort("already, sorted, text") == "already, sorted, text"
def test_already_sorted_text_with_separator_override():
assert was_text_sort("already, sorted, text", separator="|") == "already, sorted, text"
def test_with_alternative_separator():
assert was_text_sort("test | with | alternative | separator", separator=" | ") == "alternative | separator | test | with"
def test_with_trailing_separators():
assert was_text_sort("test, with, trailing, separator,") == "separator, test, trailing, with"
def test_with_tabs():
assert was_text_sort("test,\t without, \tweights") == "test, weights, without"
def test_with_linefeed_newlines():
assert was_text_sort("test,\n without, \nweights") == "test, weights, without"
def test_with_macos_pre_cheetah_newlines():
assert was_text_sort("test,\r without, \rweights") == "test, weights, without"
def test_with_windows_newlines():
assert was_text_sort("test,\r\n without, \r\nweights") == "test, weights, without"
def test_without_weights():
assert was_text_sort("test, without, weights") == "test, weights, without"
def test_with_weights():
assert was_text_sort("(test:1), (with:2.0), (weights:3.1)") == "(test:1), (weights:3.1), (with:2.0)"
def test_with_some_weights():
assert was_text_sort("(test:1), with, some, (weights:3.1)") == "some, (test:1), (weights:3.1), with"
def test_with_half_weights():
assert was_text_sort("(test:1), with, half (weights:3.1)") == "half (weights:3.1), (test:1), with"
# ASCII "_" is after uppercase and before lowercase letters
def test_with_wildcards():
assert was_text_sort("test, with, __wildcards__") == "__wildcards__, test, with"
def test_with_weighted_wildcards():
assert was_text_sort("test, (with:2), (__wildcards__:3)") == "(__wildcards__:3), test, (with:2)"
# ASCII "{" is after all letters
def test_with_dynamic_prompts():
assert was_text_sort("test, {with|dynamic|prompts}") == "test, {with|dynamic|prompts}"
def test_with_weighted_dynamic_prompts():
assert was_text_sort("(test:1.1), with, ({weighted|dynamic|prompts}:0.9)") == "(test:1.1), with, ({weighted|dynamic|prompts}:0.9)"
def test_with_embeddings():
assert was_text_sort("test, with, embedding:my_embed.pt") == "embedding:my_embed.pt, test, with"
def test_with_lora():
assert was_text_sort("test, with, lora:my_lora.safetensors") == "lora:my_lora.safetensors, test, with"
def test_with_grouped_weights():
assert was_text_sort("(test, with:1), (grouped, weights:2.1)") == "(grouped, weights:2.1), (test, with:1)"
def test_with_nested_weights():
assert was_text_sort("(test, (with:1.2):1.1), ((nested:1), weights:2)") == "((nested:1), weights:2), (test, (with:1.2):1.1)"
+11
View File
@@ -0,0 +1,11 @@
# TODO: In case anyone that knows how to set up PyTest correctly comes around, this file can be scrapped.
from pathlib import Path
TEXT_TYPE = "STRING"
CLASS_NAME = "WAS_Text_Sort"
class_string = f"class {CLASS_NAME}:"
exec(class_string + Path("../WAS_Node_Suite.py").read_text().split(class_string)[1].split("class ")[0])
def was_text_sort(text = "", separator = WAS_Text_Sort.INPUT_TYPES()["required"]["separator"][1]["default"]):
return WAS_Text_Sort().sort(text, separator)[0]