Nodes/Comfyui_PDuse/PD 字符串转json (语言自动检测)
ComfyUI Node

PD 字符串转json (语言自动检测)

Turn caption text into caption+lang JSON, with the language sniffed for you

By 7BEII·Created 2 years ago·Updated 15 days ago· 53
PD 字符串转json (语言自动检测)
    • json_string
    text
    langen

    The caption formatter your dataset prep is missing

    PD_StringToCaptionJSON (display name "PD 字符串转json (语言自动检测)" - "string to json, language auto-detect") takes a caption string and wraps it in a clean JSON object:

    {"caption": "一只猫坐在沙发上", "lang": "zh"}
    

    That's the whole trick, and it matters more than it sounds. Some LoRA trainers and caption-editing tools want captions as structured caption + lang records rather than raw text lines. If you're building a training set, this node is the formatting glue between "text I typed" and "caption the trainer can eat" - the kind of dataset-plumbing step the KB's lora-training.md keeps reminding you is where LoRA quality actually lives. Caption what you want to stay variable, keep the format consistent, and the training goes smoothly.

    How it works

    Read the source and it's refreshingly honest. It splits the text into a list (it's list-aware: INPUT_IS_LIST on text, so it batch-processes multiple captions in one run), then builds a dict per item and serializes it with json.dumps(..., ensure_ascii=False) - that last flag matters, because it keeps Chinese and other non-ASCII text readable in the JSON instead of escaping it to \uXXXX garbage.

    The "language auto-detect" is real character counting, not a model. It tallies CJK, kana, hangul and latin characters and applies thresholds: more than 30% Han characters → zh, more than 20% kana → ja, more than 20% hangul → ko, otherwise en. You don't need to trust it, though - the lang field lets you override.

    Inputs and outputs

    • text (required, multiline) - the caption. This is forceInput, so it's built to be wired from another node, not typed.
    • lang (optional, default en) - set it to auto, leave it empty, and the detector kicks in. Any other value (e.g. ja, de) is used verbatim.
    • Output: json_string - always a list, even for one caption. Remember that when you wire downstream: a plain STRING socket won't accept a list; you need a list-aware consumer or an unpack node.

    Installing it

    It's part of Comfyui_PDuse, so install the pack:

    cd ComfyUI/custom_nodes
    git clone https://github.com/7BEII/Comfyui_PDuse.git
    cd Comfyui_PDuse
    pip install -r requirements.txt
    

    Or search "Comfyui_PDuse" in ComfyUI Manager and restart. No models, no API keys, nothing to download.

    Where people get burned

    The output is a JSON string, not a native object - a node expecting a dict will not parse it. This node pairs with the pack's own PD_CaptionJSON editor/parser nodes (they're built to consume this exact format), so if you're mixing it with other packs, verify what they expect. Also, empty text produces {"caption": "", "lang": "en"} rather than skipping the item - harmless, but it can pad a dataset with blanks if you're not careful. And the auto-detection thresholds are rough: short mixed-language captions can get the language wrong, so when the language matters, set lang explicitly.

    CategoryPDuse/Text

    Inputs (2)

    NameTypeDefaultDescription
    textSTRING
    langoptSTRINGen

    Outputs (1)

    NameTypeDescription
    json_stringSTRING