Nodes/Nilor Nodes by Nilor Corp/๐Ÿ‘บ Categorize String
ComfyUI Node

๐Ÿ‘บ Categorize String

A keyword classifier that turns strings into category indexes

By nilor-corpยทCreated 2 years agoยทUpdated 5 months agoยท 6
๐Ÿ‘บ Categorize String
    • category_index
    โ—„input_stringโ–บ
    โ—„number_of_categories2โ–บ
    โ—„category_0apple, red fruitโ–บ
    โ—„category_1banana, yellow fruitโ–บ
    โ—„category_2โ–บ
    โ—„category_3โ–บ
    โ—„category_4โ–บ
    โ—„category_5โ–บ
    โ—„category_6โ–บ
    โ—„category_7โ–บ
    โ—„category_8โ–บ
    โ—„category_9โ–บ

    Sometimes you don't want a model to decide what category a string belongs to - you want a deterministic rule that gives the same answer every time. Nilor Categorize String is a tiny keyword classifier: you define up to ten categories, each with a comma-separated list of keywords, feed it a string, and it returns the index of the first category that matches. That's the entire node, and for routing strings in an automated pipeline it's exactly the right tool.

    How it works

    The inputs are simple:

    • input_string - what you're classifying.
    • number_of_categories - how many of the category slots are active (default 2, max 10).
    • category_0 โ€ฆ category_9 - each a comma-separated keyword list, e.g. apple, red fruit and banana, yellow fruit (the defaults).

    The matching is deliberately naive: it lowercases everything, splits each category's string on commas, strips whitespace, and checks whether any keyword is a substring of the input. First matching category wins, and it returns its index as the category_index INT output. If nothing matches, you get -1.

    The substring detail is where people get surprised in both directions. Because it's substring matching, apple will match "apple pie" - good. But it will also match "pineapple" and "applesauce", because those contain the letters apple. It's case-insensitive, but it's not a word-boundary match, so if your keywords are short or common, you'll get false positives. Keep keywords distinctive, and remember: no matches โ†’ -1, which is a silent failure if you don't handle it.

    When you'd reach for it

    The classic use is routing in a batch pipeline: you have a filename or a caption string, and downstream behavior should depend on which "type" it is. Wire category_index into a if-style router or a select node, and the graph picks a different branch (different model, different upscaler, different prompt) per category. It also works as a quick tag normalizer - mapping arbitrary user input onto a fixed set of buckets before it hits anything that cares about consistent labels.

    Two design notes. Categories are checked in index order, so the first one in the list wins ties - order your most specific category first. And unlike the pack's ๐Ÿ‘บ Random String node, this one is fully deterministic: same input, same categories, same index, forever. That reproducibility is the whole point of using it over an LLM classifier, which would be overkill and non-deterministic for "is this filename a cat or a dog".

    Install

    ComfyUI Manager (search "Nilor Nodes") or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/nilor-corp/nilor-nodes
    cd nilor-nodes && pip install -r requirements.txt
    

    Restart ComfyUI; find it under Nilor Nodes ๐Ÿ‘บ โ†’ Utilities. Pure stdlib, no extra dependencies.

    Bottom line

    This is a rule engine wearing a node costume, and that's fine - it's honest, fast, and predictable. Use it when you need deterministic string routing and a one-liner classification beats building the same matching logic out of text-processing nodes. Just respect the substring trap and the -1 sentinel, and it'll quietly do its job forever. If you need real semantic classification with synonyms and context, that's a different tool entirely - this one will happily call "grand theft auto" a car.

    CategoryNilor Nodes ๐Ÿ‘บ/Utilities

    Inputs (12)

    NameTypeDefaultDescription
    input_stringSTRINGโ€”
    number_of_categoriesINT21โ€“10โ€”
    category_0STRINGapple, red fruitโ€”
    category_1STRINGbanana, yellow fruitโ€”
    category_2optSTRINGโ€”
    category_3optSTRINGโ€”
    category_4optSTRINGโ€”
    category_5optSTRINGโ€”
    category_6optSTRINGโ€”
    category_7optSTRINGโ€”
    category_8optSTRINGโ€”
    category_9optSTRINGโ€”

    Outputs (1)

    NameTypeDescription
    category_indexINTโ€”