Tagging IK Analyzer Tokens with Business Types: A Source Code Modification Log
IK Analyzer can split a Chinese string into words, but in practice knowing what got segmented isn’t always enough. Given text like "施耐德继电器" (“Schneider relay”), I also wanted the analyzer output to tell downstream code that 施耐德 (“Schneider”) is a brand and 继电器 (“relay”) is a category. When it sees a material number, it should keep that business type too.
Standard extension dictionaries let IK recognize new words, but they won’t distinguish brands, categories, and material numbers on their own. To fill that gap, I traced IK’s dictionary loading and token emission paths and modified the source code. This note covers only the main path and the parts I actually changed.
Extension dictionaries recognize new words, not what they mean
Out of the box, IK supports a main dictionary, extension dictionaries, and stop-word dictionaries. Adding "施耐德" to an extension dictionary makes sure it comes out as a single intact token instead of getting split into meaningless fragments.
The problem is that all extension terms get merged into the main dictionary. As far as the tokenizer is concerned, "施耐德", "继电器", and a material number are all just dictionary hits. Downstream code still has no idea which token is a brand and which one is a category.
If improving word segmentation were the only goal, an extension dictionary would be plenty. Because this change also needed to keep each term’s business classification, I couldn’t keep dumping every word into the same dictionary trie. Instead, I added separate dictionaries for brands, categories, and material numbers so each class of terms has its own matching entry point.
First, find out how dictionaries get into memory
IK’s codebase is fairly sizeable, but the parts relevant to this change are in three areas: configuration, the core segmenter, and dictionary management. Following the calls down, everything lands on Dictionary and DictSegment.
Dictionary reads the configuration and initializes the dictionaries. On first use, the main dictionary, quantifier dictionary, and stop-word dictionary are loaded into memory, and any extension dictionaries declared in the config are written into the main dictionary as well. The terms aren’t stored in a flat list of strings, but in a trie built out of DictSegment nodes.

Taking "施耐德继电器" as an example, matching starts at the character 施:
1 | 施 |
As long as you walk down the character nodes 施 → 耐 → 德 and the last node is marked as a word ending, you know "施耐德" is a complete term. If you hit a dead end partway through, matching stops.
One implementation detail is worth keeping in mind. When a node has only a few children, IK stores them in an array; once the count passes a threshold, it moves them into a Map. The array avoids paying HashMap overhead on every tiny branch, while switching to a Map on larger branches keeps lookups from slowing down as children multiply.
Once that loading path was clear, adding business dictionaries didn’t mean touching the segmentation algorithm itself. I followed the existing pattern: I created separate DictSegment trees for brands, categories, and material numbers, read their dictionary files on startup, and loaded each term into its own trie with fillSegment.

With dictionaries split, where do we tag each token?
Loading separate business dictionaries only answers where the data lives. Next, before tokens are returned to callers, each token needs to be matched against those dictionaries.
IKSegmenter.next() emits tokens one by one and filters out stop words along the way. At that point, we already have the token’s character boundaries, and we’re right before the result gets handed back to the caller. That made it the right place to plug in business-type tagging: check the brand dictionary first and tag the token as brand on a hit; if it misses, move on to the category dictionary, material-number dictionary, and so on.
A dictionary lookup produces one of three states:
UNMATCH: the character path does not exist;MATCH: an exact complete term was matched;PREFIX: the current characters form the prefix of a longer term, so matching can continue deeper.
All three states come from the same dictionary trie. After matching the first character, if more characters remain, the lookup continues down the current node. Once it reaches the last character, it marks an exact match or a prefix match depending on whether the node is flagged as a word ending and whether it still has child nodes. For business-type tagging, all we care about is whether we got a complete match.
The whole change follows this flow:
1 | IK emits a token |
With that in place, tokenizing "施耐德继电器" produces tokens that carry tags like brand and category, not just raw text. Downstream indexing, aggregations, and business rules no longer have to guess what kind of entity a string represents.
Separate dictionaries bring new trade-offs
Mixing different business terms into the main dictionary makes sure they get segmented intact, at the cost of losing their category at load time. Keeping business terms in separate dictionaries lets IK’s original main dictionary stay focused on segmentation, while the business dictionaries attach classifications during token emission.
The trade-off is that every emitted token may have to probe multiple tries in sequence. If the number of business dictionaries keeps growing, both the matching order and the extra lookup cost will need a fresh look. An alternative would be storing a type field directly on main-dictionary nodes, though a single word can belong to multiple categories, which complicates dictionary loading and updates as well.
You also have to settle priority rules between dictionaries up front. If a term appears in both the brand and category dictionaries, deciding whether to stop at the first match or allow multiple types per token directly shapes the downstream data model. Since this implementation matches sequentially, the dictionary order itself becomes a business rule.
Updating dictionaries is harder than loading them once
Loading dictionary files only at startup works fine when the terms rarely change. If every new brand, category, or material number required a service restart, maintenance would get painful fast, so this change also included dictionary hot reload.
Hot reload isn’t just a matter of re-reading files from disk. Whether the old dictionary keeps serving queries while the new one loads, when the swap happens, how multiple dictionaries stay consistent with each other, and whether old data stays intact if a reload fails all directly affect live tokenization output. Because that part falls under confidentiality terms for the project’s digital assets, I won’t show the implementation here, only note where the boundary is.
In the end, this change didn’t alter how IK handles segmentation. The main dictionary still decides which words get recognized, and the separate business dictionaries attach classifications onto the tokens already produced. As more business types get added down the road, the next things to watch are multi-dictionary lookup cost and how to represent type conflicts cleanly.