Nodes/Comfy Text Stats/Text -> Char, Word & Line Count
ComfyUI Node

Text -> Char, Word & Line Count

Know exactly how many words are in your prompt or caption

By scofano·Created 11 months ago·Updated 3 months ago· 3
Text -> Char, Word & Line Count
    • char_count
    • word_count
    • line_count
    text

    Three numbers, one input. TextStats takes any string and gives you char_count, word_count, and line_count - trivial in Python, genuinely useful in a graph, because it lets other nodes make decisions about your text without you eyeballing it. It's one of the eight utilities in the Comfy Text Stats pack from scofano: pure Python, deterministic, nothing to install beyond the pack.

    What the counts actually mean - read this before trusting them

    • char_count is len(text) - every character, including spaces, punctuation, and newlines. A multiline caption's char count is bigger than you think, because the newlines count.
    • word_count uses the Unicode-aware regex \b\w+\b. That's solid for ordinary prose, but it's a mechanical definition, not a language one: well-known counts as two words, it's counts as two (it and s), a run like ... counts as zero, and emoji or CJK text won't count the way you'd expect.
    • line_count is text.splitlines(), so an empty string is 0 lines and a single line with no trailing newline is 1.

    None of this is a bug - it's just worth knowing you're getting regex counts, not a grammarian's opinion.

    Why you'd reach for it

    Caption analytics on dataset builds is the big one. When a LoRA training script or an LLM pipeline has a hard cap on caption length, wire word_count into a comparison or a switcher and let the graph decide which captions need trimming - that's what Text String Truncate, in the same pack, is for. Prompt inspection is a close second: verify your positive prompt isn't accidentally way longer than intended. And it's handy for metadata - stamp the counts into a filename or a note node so your outputs carry their own stats.

    How to use it

    One input, text (multiline). Three INT outputs: char_count, word_count, line_count. They're just numbers, so they feed any INT input - an integer compare, a format string, a text concat. It's deterministic and cheap (its change signal is just the input length), so you can drop it into a workflow and run it on every execution without worrying about cost or cache weirdness.

    Installing it

    It ships in the Comfy Text Stats pack, so you get all eight nodes in one install. ComfyUI Manager route: Manager → Custom Nodes Manager → search "Comfy Text Stats" → Install, then restart ComfyUI. Or manually:

    cd ComfyUI/custom_nodes
    git clone https://github.com/scofano/comfy-text-stats
    

    Restart after. No dependencies, no model downloads - the pack's requirements file is a comment saying there are none.

    Where people get burned

    The only real trap is treating the numbers as human counts on non-prose text. Punctuation-heavy or non-Latin content will disagree with you, and the regex - not the node - is the one that's right. When your caption length checks matter for a training run, spot-check a couple of counts against what you'd count by hand first, and you'll know exactly which definition you're working with.

    CategoryText/Utils

    Inputs (1)

    NameTypeDefaultDescription
    textSTRING

    Outputs (3)

    NameTypeDescription
    char_countINT
    word_countINT
    line_countINT