PD字符串转Caption JSON 节点 功能:将输入的字符串转换为包含caption和lang字段的JSON格式字符串 自动检测语言(中文/英文)
| 3 | |
| 4 | |
| 5 | class PD_StringToCaptionJSON: |
| 6 | """ |
| 7 | PD字符串转Caption JSON 节点 |
| 8 | 功能:将输入的字符串转换为包含caption和lang字段的JSON格式字符串 |
| 9 | 自动检测语言(中文/英文) |
| 10 | """ |
| 11 | |
| 12 | @classmethod |
| 13 | def INPUT_TYPES(cls): |
| 14 | return { |
| 15 | "required": { |
| 16 | "text": ("STRING", { |
| 17 | "default": "", |
| 18 | "multiline": True, |
| 19 | "placeholder": "输入文本内容...", |
| 20 | "forceInput": True |
| 21 | }), |
| 22 | }, |
| 23 | "optional": { |
| 24 | "lang": ("STRING", { |
| 25 | "default": "en", |
| 26 | "multiline": False, |
| 27 | "placeholder": "语言代码(en/zh/ja等),留空为en" |
| 28 | }), |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | RETURN_TYPES = ("STRING",) |
| 33 | RETURN_NAMES = ("json_string",) |
| 34 | FUNCTION = "convert_to_json" |
| 35 | CATEGORY = "PDuse/Text" |
| 36 | |
| 37 | # 支持列表输入和输出 |
| 38 | INPUT_IS_LIST = {"text": True} |
| 39 | OUTPUT_IS_LIST = (True,) |
| 40 | |
| 41 | def detect_language(self, text): |
| 42 | """ |
| 43 | 检测文本语言 |
| 44 | 返回语言代码:en(英文), zh(中文), ja(日文) 等 |
| 45 | """ |
| 46 | if not text or not text.strip(): |
| 47 | return "en" |
| 48 | |
| 49 | # 检测中文字符 |
| 50 | chinese_pattern = re.compile(r'[\u4e00-\u9fff]') |
| 51 | chinese_count = len(chinese_pattern.findall(text)) |
| 52 | |
| 53 | # 检测日文字符(平假名、片假名) |
| 54 | japanese_pattern = re.compile(r'[\u3040-\u309f\u30a0-\u30ff]') |
| 55 | japanese_count = len(japanese_pattern.findall(text)) |
| 56 | |
| 57 | # 检测韩文字符 |
| 58 | korean_pattern = re.compile(r'[\uac00-\ud7a3]') |
| 59 | korean_count = len(korean_pattern.findall(text)) |
| 60 | |
| 61 | # 检测英文字符 |
| 62 | english_pattern = re.compile(r'[a-zA-Z]') |
nothing calls this directly
no outgoing calls
no test coverage detected