-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtext.py
144 lines (114 loc) · 3.68 KB
/
text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import string
_CATEGORY = "zuellni/text"
_MAPPING = "ZuellniText"
class Clean:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"text": ("STRING", {"default": "", "forceInput": True}),
"strip": (
("both", "punctuation", "whitespace", "none"),
{"default": "both"},
),
"case": (
("lower", "upper", "capitalize", "title", "none"),
{"default": "lower"},
),
"fix": ("BOOLEAN", {"default": True}),
}
}
CATEGORY = _CATEGORY
FUNCTION = "clean"
RETURN_NAMES = ("TEXT",)
RETURN_TYPES = ("STRING",)
def clean(self, text, strip, case, fix):
if strip == "both":
text = text.strip(string.punctuation + string.whitespace)
elif strip != "none":
text = text.strip(getattr(string, strip))
if case == "title":
text = string.capwords(text)
elif case != "none":
text = getattr(text, case)()
if fix:
text = "\n".join([t for t in text.splitlines() if t])
text = " ".join(text.split())
return (text,)
class Message:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"role": (("system", "user", "assistant"), {"default": "system"}),
"content": ("STRING", {"default": "", "multiline": True}),
},
"optional": {"messages": ("EXL_MESSAGES",)},
}
CATEGORY = _CATEGORY
FUNCTION = "add"
RETURN_NAMES = ("MESSAGES",)
RETURN_TYPES = ("EXL_MESSAGES",)
def add(self, role, content, messages=[]):
return (messages + [{"role": role, "content": content}],)
class Preview:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"text": ("STRING", {"default": "", "forceInput": True}),
"print_to_console": ("BOOLEAN", {"default": False}),
"output": ("STRING", {"default": "", "multiline": True}),
}
}
CATEGORY = _CATEGORY
FUNCTION = "preview"
OUTPUT_NODE = True
RETURN_TYPES = ()
def preview(self, text, print_to_console, output):
print_to_console and print(text)
return {"ui": {"text": [text]}}
class Replace:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"count": ("INT", {"default": 1, "min": 1, "max": 26}),
"text": ("STRING", {"default": "", "multiline": True}),
}
}
CATEGORY = _CATEGORY
FUNCTION = "replace"
RETURN_NAMES = ("TEXT",)
RETURN_TYPES = ("STRING",)
def replace(self, count, text="", **kwargs):
for index in range(count):
key = chr(index + 97)
if key in kwargs and kwargs[key]:
text = text.replace(f"{{{key}}}", kwargs[key])
return (text,)
class String:
@classmethod
def INPUT_TYPES(cls):
return {"required": {"text": ("STRING", {"default": "", "multiline": True})}}
CATEGORY = _CATEGORY
FUNCTION = "get"
RETURN_NAMES = ("TEXT",)
RETURN_TYPES = ("STRING",)
def get(self, text):
return (text,)
NODE_CLASS_MAPPINGS = {
f"{_MAPPING}Clean": Clean,
f"{_MAPPING}Message": Message,
f"{_MAPPING}Preview": Preview,
f"{_MAPPING}Replace": Replace,
f"{_MAPPING}String": String,
}
NODE_DISPLAY_NAME_MAPPINGS = {
f"{_MAPPING}Clean": "Clean",
f"{_MAPPING}Message": "Message",
f"{_MAPPING}Preview": "Preview",
f"{_MAPPING}Replace": "Replace",
f"{_MAPPING}String": "String",
}
WEB_DIRECTORY = "."