forked from MeeeyoAI/ComfyUI_StringOps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeyo_node_String.py
More file actions
1381 lines (1156 loc) · 49.8 KB
/
meyo_node_String.py
File metadata and controls
1381 lines (1156 loc) · 49.8 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import re, math, datetime, random, secrets, requests, string
from . import any_typ, note
#======文本输入
class SingleTextInput:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"text": ("STRING", {"default": "", "multiline": True}),
}
}
RETURN_TYPES = ("STRING",)
FUNCTION = "process_input"
OUTPUT_NODE = False
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(text): return float("NaN")
def process_input(self, text):
return (text,)
#======文本到列表
class TextToList:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"text": ("STRING", {"multiline": True, "default": ""}),
"delimiter": ("STRING", {"default": ""}),
}
}
RETURN_TYPES = ("STRING",)
FUNCTION = "split_text"
OUTPUT_IS_LIST = (True,)
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(text, delimiter): return float("NaN")
def split_text(self, text, delimiter):
if not delimiter:
parts = text.split('\n')
else:
parts = text.split(delimiter)
parts = [part.strip() for part in parts if part.strip()]
if not parts:
return ([],)
return (parts,)
#======文本拼接
class TextConcatenator:
@classmethod
def INPUT_TYPES(cls):
return {
"optional": {
"text1": ("STRING", {"multiline": False, "default": ""}),
"text2": ("STRING", {"multiline": False, "default": ""}),
"text3": ("STRING", {"multiline": False, "default": ""}),
"text4": ("STRING", {"multiline": False, "default": ""}),
"combine_order": ("STRING", {"default": ""}),
"separator": ("STRING", {"default": ","})
},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "combine_texts"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(text1, text2, text3, text4, combine_order, separator): return float("NaN")
def combine_texts(self, text1, text2, text3, text4, combine_order, separator):
try:
text_map = {
"1": text1,
"2": text2,
"3": text3,
"4": text4
}
if not combine_order:
combine_order = "1+2+3+4"
parts = combine_order.split("+")
valid_parts = []
for part in parts:
if part in text_map:
valid_parts.append(part)
else:
return (f"Error: Invalid input '{part}' in combine_order. Valid options are 1, 2, 3, 4.",)
non_empty_texts = [text_map[part] for part in valid_parts if text_map[part]]
if separator == '\\n':
separator = '\n'
result = separator.join(non_empty_texts)
return (result,)
except Exception as e:
return (f"Error: {str(e)}",)
#======多参数输入
class MultiParamInputNode:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"text1": ("STRING", {"default": "", "multiline": True}),
"text2": ("STRING", {"default": "", "multiline": True}),
"int1": ("INT", {"default": 0, "min": -1000000, "max": 1000000}),
"int2": ("INT", {"default": 0, "min": -1000000, "max": 1000000}),
}
}
RETURN_TYPES = ("STRING", "STRING", "INT", "INT")
FUNCTION = "process_inputs"
OUTPUT_NODE = False
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(text1, text2, int1, int2): return float("NaN")
def process_inputs(self, text1, text2, int1, int2):
return (text1, text2, int1, int2)
#======整数参数
class NumberExtractor:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_text": ("STRING", {"default": "2|3"}),
},
"optional": {},
}
RETURN_TYPES = ("INT", "INT")
FUNCTION = "extract_lines_by_index"
OUTPUT_TYPES = ("INT", "INT")
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_text): return float("NaN")
def extract_lines_by_index(self, input_text):
try:
data_list = input_text.split("|")
result = []
for i in range(2):
if i < len(data_list):
try:
result.append(int(data_list[i]))
except ValueError:
result.append(0)
else:
result.append(0)
return tuple(result)
except:
return (0, 0)
#======添加前后缀
class AddPrefixSuffix:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_string": ("STRING", {"default": ""}),
"prefix": ("STRING", {"default": "前缀符"}),
"suffix": ("STRING", {"default": "后缀符"}),
},
"optional": {},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "add_prefix_suffix"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_string, prefix, suffix): return float("NaN")
def add_prefix_suffix(self, input_string, prefix, suffix):
return (f"{prefix}{input_string}{suffix}",)
#======提取标签之间
class ExtractSubstring:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_string": ("STRING", {"multiline": True, "default": ""}),
"pattern": ("STRING", {"default": "标签始|标签尾"}),
},
"optional": {},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "extract_substring"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_string, pattern): return float("NaN")
def extract_substring(self, input_string, pattern):
try:
parts = pattern.split('|')
start_str = parts[0]
end_str = parts[1] if len(parts) > 1 and parts[1].strip() else "\n"
start_index = input_string.index(start_str) + len(start_str)
end_index = input_string.find(end_str, start_index)
if end_index == -1:
end_index = input_string.find("\n", start_index)
if end_index == -1:
end_index = len(input_string)
extracted = input_string[start_index:end_index]
lines = extracted.splitlines()
non_empty_lines = [line for line in lines if line.strip()]
result = '\n'.join(non_empty_lines)
return (result,)
except ValueError:
return ("",)
#======按数字范围提取
class ExtractSubstringByIndices:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_string": ("STRING", {"default": ""}),
"indices": ("STRING", {"default": "2-6"}),
"direction": (["从前面", "从后面"], {"default": "从前面"}),
},
"optional": {},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "extract_substring_by_indices"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_string, indices, direction): return float("NaN")
def extract_substring_by_indices(self, input_string, indices, direction):
try:
if '-' in indices:
start_index, end_index = map(int, indices.split('-'))
else:
start_index = end_index = int(indices)
start_index -= 1
end_index -= 1
if start_index < 0 or start_index >= len(input_string):
return ("",)
if end_index < 0 or end_index >= len(input_string):
end_index = len(input_string) - 1
if start_index > end_index:
start_index, end_index = end_index, start_index
if direction == "从前面":
return (input_string[start_index:end_index + 1],)
elif direction == "从后面":
start_index = len(input_string) - start_index - 1
end_index = len(input_string) - end_index - 1
if start_index > end_index:
start_index, end_index = end_index, start_index
return (input_string[start_index:end_index + 1],)
except ValueError:
return ("",)
#======分隔符拆分两边
class SplitStringByDelimiter:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_string": ("STRING", {"default": "文本|内容"}),
"delimiter": ("STRING", {"default": "|"}),
},
"optional": {},
}
RETURN_TYPES = ("STRING", "STRING")
FUNCTION = "split_string_by_delimiter"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_string, delimiter): return float("NaN")
def split_string_by_delimiter(self, input_string, delimiter):
parts = input_string.split(delimiter, 1)
if len(parts) == 2:
return (parts[0], parts[1])
else:
return (input_string, "")
#======常规处理字符
class ProcessString:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_string": ("STRING", {"multiline": True, "default": ""}),
"option": (["不改变", "取数字", "取字母", "转大写", "转小写", "取中文", "去标点", "去换行", "去空行", "去空格", "去格式", "统计字数"], {"default": "不改变"}),
},
"optional": {},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "process_string"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_string, option): return float("NaN")
def process_string(self, input_string, option):
if option == "取数字":
result = ''.join(re.findall(r'\d', input_string))
elif option == "取字母":
result = ''.join(filter(lambda char: char.isalpha() and not self.is_chinese(char), input_string))
elif option == "转大写":
result = input_string.upper()
elif option == "转小写":
result = input_string.lower()
elif option == "取中文":
result = ''.join(filter(self.is_chinese, input_string))
elif option == "去标点":
result = re.sub(r'[^\w\s\u4e00-\u9fff]', '', input_string)
elif option == "去换行":
result = input_string.replace('\n', '')
elif option == "去空行":
result = '\n'.join(filter(lambda line: line.strip(), input_string.splitlines()))
elif option == "去空格":
result = input_string.replace(' ', '')
elif option == "去格式":
result = re.sub(r'\s+', '', input_string)
elif option == "统计字数":
result = str(len(input_string))
else:
result = input_string
return (result,)
@staticmethod
def is_chinese(char):
return '\u4e00' <= char <= '\u9fff'
#======提取前后字符
class ExtractBeforeAfter:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_string": ("STRING", {"default": ""}),
"pattern": ("STRING", {"default": "标签符"}),
"position": (["保留最初之前", "保留最初之后", "保留最后之前", "保留最后之后"], {"default": "保留最初之前"}),
"include_delimiter": ("BOOLEAN", {"default": False}),
},
"optional": {},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "extract_before_after"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_string, pattern, position, include_delimiter): return float("NaN")
def extract_before_after(self, input_string, pattern, position, include_delimiter):
if position == "保留最初之前":
index = input_string.find(pattern)
if index != -1:
result = input_string[:index + len(pattern) if include_delimiter else index]
return (result,)
elif position == "保留最初之后":
index = input_string.find(pattern)
if index != -1:
result = input_string[index:] if include_delimiter else input_string[index + len(pattern):]
return (result,)
elif position == "保留最后之前":
index = input_string.rfind(pattern)
if index != -1:
result = input_string[:index + len(pattern) if include_delimiter else index]
return (result,)
elif position == "保留最后之后":
index = input_string.rfind(pattern)
if index != -1:
result = input_string[index:] if include_delimiter else input_string[index + len(pattern):]
return (result,)
return ("",)
#======简易文本替换
class SimpleTextReplacer:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_string": ("STRING", {"multiline": True, "default": "", "forceInput": True}),
"find_text": ("STRING", {"default": ""}),
"replace_text": ("STRING", {"default": ""})
},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "replace_text"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_string, find_text, replace_text): return float("NaN")
def replace_text(self, input_string, find_text, replace_text):
try:
if not find_text:
return (input_string,)
if replace_text == '\\n':
replace_text = '\n'
result = input_string.replace(find_text, replace_text)
return (result,)
except Exception as e:
return (f"Error: {str(e)}",)
#======替换第n次出现
class ReplaceNthOccurrence:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"original_text": ("STRING", {"multiline": True, "default": ""}),
"occurrence": ("INT", {"default": 1, "min": 0}),
"search_str": ("STRING", {"default": "替换前字符"}),
"replace_str": ("STRING", {"default": "替换后字符"}),
},
"optional": {},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "replace_nth_occurrence"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(original_text, occurrence, search_str, replace_str): return float("NaN")
def replace_nth_occurrence(self, original_text, occurrence, search_str, replace_str):
if occurrence == 0:
result = original_text.replace(search_str, replace_str)
else:
def replace_nth_match(match):
nonlocal occurrence
occurrence -= 1
return replace_str if occurrence == 0 else match.group(0)
result = re.sub(re.escape(search_str), replace_nth_match, original_text, count=occurrence)
return (result,)
#======多次出现依次替换
class ReplaceMultiple:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"original_text": ("STRING", {"multiline": True, "default": ""}),
"replacement_rule": ("STRING", {"default": ""}),
},
"optional": {},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "replace_multiple"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(original_text, replacement_rule): return float("NaN")
def replace_multiple(self, original_text, replacement_rule):
try:
search_str, replacements = replacement_rule.split('|')
replacements = [rep for rep in replacements.split(',') if rep]
def replace_match(match):
nonlocal replacements
if replacements:
return replacements.pop(0)
return match.group(0)
result = re.sub(re.escape(search_str), replace_match, original_text)
return (result,)
except ValueError:
return ("",)
#======批量替换字符
class BatchReplaceStrings:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"original_text": ("STRING", {"multiline": False, "default": "文本内容"}),
"replacement_rules": ("STRING", {"multiline": True, "default": ""}),
},
"optional": {},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "batch_replace_strings"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(original_text, replacement_rules): return float("NaN")
def batch_replace_strings(self, original_text, replacement_rules):
rules = replacement_rules.strip().split('\n')
for rule in rules:
if '|' in rule:
search_strs, replace_str = rule.split('|', 1)
search_strs = search_strs.replace("\\n", "\n")
replace_str = replace_str.replace("\\n", "\n")
search_strs = search_strs.split(',')
for search_str in search_strs:
original_text = original_text.replace(search_str, replace_str)
return (original_text,)
#======随机行内容
class RandomLineFromText:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_text": ("STRING", {"multiline": True, "default": ""}),
},
"optional": {"any": (any_typ,)}
}
RETURN_TYPES = ("STRING",)
FUNCTION = "get_random_line"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_text, any=None): return float("NaN")
def get_random_line(self, input_text, any=None):
lines = input_text.strip().splitlines()
if not lines:
return ("",)
return (random.choice(lines),)
#======判断是否包含字符
class CheckSubstringPresence:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_text": ("STRING", {"default": "文本内容"}),
"substring": ("STRING", {"default": "查找符1|查找符2"}),
"mode": (["同时满足", "任意满足"], {"default": "任意满足"}),
},
"optional": {},
}
RETURN_TYPES = ("INT",)
FUNCTION = "check_substring_presence"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_text, substring, mode): return float("NaN")
def check_substring_presence(self, input_text, substring, mode):
substrings = substring.split('|')
if mode == "同时满足":
for sub in substrings:
if sub not in input_text:
return (0,)
return (1,)
elif mode == "任意满足":
for sub in substrings:
if sub in input_text:
return (1,)
return (0,)
#======段落每行添加前后缀
class AddPrefixSuffixToLines:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_text": ("STRING", {"multiline": True, "default": ""}),
"prefix_suffix": ("STRING", {"default": "前缀符|后缀符"}),
},
"optional": {},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "add_prefix_suffix_to_lines"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(prefix_suffix, input_text): return float("NaN")
def add_prefix_suffix_to_lines(self, prefix_suffix, input_text):
try:
prefix, suffix = prefix_suffix.split('|')
lines = input_text.splitlines()
modified_lines = [f"{prefix}{line}{suffix}" for line in lines]
result = '\n'.join(modified_lines)
return (result,)
except ValueError:
return ("",)
#======段落提取指定索引行
class ExtractAndCombineLines:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_text": ("STRING", {"multiline": True, "default": ""}),
"line_indices": ("STRING", {"default": "2-3"}),
},
"optional": {},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "extract_and_combine_lines"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_text, line_indices): return float("NaN")
def extract_and_combine_lines(self, input_text, line_indices):
try:
lines = input_text.splitlines()
result_lines = []
if '-' in line_indices:
start, end = map(int, line_indices.split('-'))
start = max(1, start)
end = min(len(lines), end)
result_lines = lines[start - 1:end]
else:
indices = map(int, line_indices.split('|'))
for index in indices:
if 1 <= index <= len(lines):
result_lines.append(lines[index - 1])
result = '\n'.join(result_lines)
return (result,)
except ValueError:
return ("",)
#======段落提取或移除字符行
class FilterLinesBySubstrings:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_text": ("STRING", {"multiline": True, "default": ""}),
"substrings": ("STRING", {"default": "查找符1|查找符2"}),
"action": (["保留", "移除"], {"default": "保留"}),
},
"optional": {},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "filter_lines_by_substrings"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_text, substrings, action): return float("NaN")
def filter_lines_by_substrings(self, input_text, substrings, action):
lines = input_text.splitlines()
substring_list = substrings.split('|')
result_lines = []
for line in lines:
contains_substring = any(substring in line for substring in substring_list)
if (action == "保留" and contains_substring) or (action == "移除" and not contains_substring):
result_lines.append(line)
non_empty_lines = [line for line in result_lines if line.strip()]
result = '\n'.join(non_empty_lines)
return (result,)
#======根据字数范围过滤文本行
class FilterLinesByWordCount:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_text": ("STRING", {"multiline": True, "default": ""}),
"word_count_range": ("STRING", {"default": "2-10"}),
},
"optional": {},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "filter_lines_by_word_count"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_text, word_count_range): return float("NaN")
def filter_lines_by_word_count(self, input_text, word_count_range):
try:
lines = input_text.splitlines()
result_lines = []
if '-' in word_count_range:
min_count, max_count = map(int, word_count_range.split('-'))
result_lines = [line for line in lines if min_count <= len(line) <= max_count]
result = '\n'.join(result_lines)
return (result,)
except ValueError:
return ("",)
#======按序号提取分割文本
class SplitAndExtractText:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_text": ("STRING", {"multiline": True, "default": ""}),
"delimiter": ("STRING", {"default": "分隔符"}),
"index": ("INT", {"default": 1, "min": 1}),
"order": (["顺序", "倒序"], {"default": "顺序"}),
"include_delimiter": ("BOOLEAN", {"default": False}),
},
"optional": {},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "split_and_extract"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_text, delimiter, index, order, include_delimiter): return float("NaN")
def split_and_extract(self, input_text, delimiter, index, order, include_delimiter):
try:
if not delimiter:
parts = input_text.splitlines()
else:
parts = input_text.split(delimiter)
if order == "倒序":
parts = parts[::-1]
if index > 0 and index <= len(parts):
selected_part = parts[index - 1]
if include_delimiter and delimiter:
if order == "顺序":
if index > 1:
selected_part = delimiter + selected_part
if index < len(parts):
selected_part += delimiter
elif order == "倒序":
if index > 1:
selected_part += delimiter
if index < len(parts):
selected_part = delimiter + selected_part
lines = selected_part.splitlines()
non_empty_lines = [line for line in lines if line.strip()]
result = '\n'.join(non_empty_lines)
return (result,)
else:
return ("",)
except ValueError:
return ("",)
#======文本出现次数
class CountOccurrences:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_text": ("STRING", {"multiline": True, "default": ""}),
"char": ("STRING", {"default": "查找符"}),
},
"optional": {},
}
RETURN_TYPES = ("INT", "STRING")
FUNCTION = "count_text_segments"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_text, char): return float("NaN")
def count_text_segments(self, input_text, char):
try:
if char == "\\n":
lines = [line for line in input_text.splitlines() if line.strip()]
count = len(lines)
else:
count = input_text.count(char)
return (count, str(count))
except ValueError:
return (0, "0")
#======文本拆分
class ExtractLinesByIndex:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_text": ("STRING", {"multiline": True, "default": ""}),
"delimiter": ("STRING", {"default": "标签符"}),
"index": ("INT", {"default": 1, "min": 1}),
},
"optional": {},
}
RETURN_TYPES = ("STRING", "STRING", "STRING", "STRING", "STRING")
FUNCTION = "extract_lines_by_index"
OUTPUT_TYPES = ("STRING", "STRING", "STRING", "STRING", "STRING")
OUTPUT_NAMES = ("文本1", "文本2", "文本3", "文本4", "文本5")
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_text, index, delimiter): return float("NaN")
def extract_lines_by_index(self, input_text, index, delimiter):
try:
if delimiter == "" or delimiter == "\n":
lines = input_text.splitlines()
else:
lines = input_text.split(delimiter)
result_lines = []
for i in range(index - 1, index + 4):
if 0 <= i < len(lines):
result_lines.append(lines[i])
else:
result_lines.append("")
return tuple(result_lines)
except ValueError:
return ("", "", "", "", "")
#======提取特定行
class ExtractSpecificLines:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_text": ("STRING", {"multiline": True, "default": ""}),
"line_indices": ("STRING", {"default": "1|2"}),
"split_char": ("STRING", {"default": "\n"}),
},
"optional": {},
}
RETURN_TYPES = ("STRING", "STRING", "STRING", "STRING", "STRING", "STRING")
FUNCTION = "extract_specific_lines"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_text, line_indices, split_char): return float("NaN")
def extract_specific_lines(self, input_text, line_indices, split_char):
if not split_char or split_char == "\n":
lines = input_text.split('\n')
else:
lines = input_text.split(split_char)
indices = [int(index) - 1 for index in line_indices.split('|') if index.isdigit()]
results = []
for index in indices:
if 0 <= index < len(lines):
results.append(lines[index])
else:
results.append("")
while len(results) < 5:
results.append("")
non_empty_results = [result for result in results[:5] if result.strip()]
if not split_char or split_char == "\n":
combined_result = '\n'.join(non_empty_results)
else:
combined_result = split_char.join(non_empty_results)
return tuple(results[:5] + [combined_result])
#======删除标签内的内容
class RemoveContentBetweenChars:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_text": ("STRING", {"multiline": True, "default": ""}),
"chars": ("STRING", {"default": "(|)"}),
},
"optional": {},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "remove_content_between_chars"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_text, chars): return float("NaN")
def remove_content_between_chars(self, input_text, chars):
try:
if len(chars) == 3 and chars[1] == '|':
start_char, end_char = chars[0], chars[2]
else:
return input_text
pattern = re.escape(start_char) + '.*?' + re.escape(end_char)
result = re.sub(pattern, '', input_text)
return (result,)
except ValueError:
return ("",)
#======随机打乱
class ShuffleTextLines:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"input_text": ("STRING", {"multiline": True, "default": ""}),
"delimiter": ("STRING", {"default": "分隔符"}),
},
"optional": {"any": (any_typ,)}
}
RETURN_TYPES = ("STRING",)
FUNCTION = "shuffle_text_lines"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(input_text, delimiter, any=None): return float("NaN")
def shuffle_text_lines(self, input_text, delimiter, any=None):
if delimiter == "":
lines = input_text.splitlines()
elif delimiter == "\n":
lines = input_text.split("\n")
else:
lines = input_text.split(delimiter)
lines = [line for line in lines if line.strip()]
random.shuffle(lines)
if delimiter == "":
result = "\n".join(lines)
elif delimiter == "\n":
result = "\n".join(lines)
else:
result = delimiter.join(lines)
return (result,)
#======判断返回内容
class ConditionalTextOutput:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"original_content": ("STRING", {"multiline": True, "default": ""}),
"check_text": ("STRING", {"default": "查找字符"}),
"text_if_exists": ("STRING", {"default": "存在返回内容"}),
"text_if_not_exists": ("STRING", {"default": "不存在返回内容"}),
},
"optional": {},
}
RETURN_TYPES = ("STRING",)
FUNCTION = "conditional_text_output"
CATEGORY = "Meeeyo/String"
DESCRIPTION = note
def IS_CHANGED(original_content, check_text, text_if_exists, text_if_not_exists): return float("NaN")
def conditional_text_output(self, original_content, check_text, text_if_exists, text_if_not_exists):
if not check_text: