analysis
Part of Kronuz C++ Libraries, a collection of small, reusable C++ libraries.
Header-only text analyzer for C++20: turns text into index terms exactly as Xapian's
TermGenerator does. Words, case folding, Z-prefixed stems, stop words, CJK n-grams, wdf
and positions all match Xapian 2.1.0, at 1.8 to 2.7 times its speed. Reach for it when the terms
you index have to match a Xapian index or a Xapian-style query parser.
The term generator is header-only. It also splits Chinese, Japanese and Korean text into words exactly as ICU's word break
iterator does, with no ICU at runtime, in a small compiled library (CJK words).
✨ Highlights
- Xapian-exact. The same terms, wdf and positions as Xapian 2.1.0, checked against 966
recorded oracle vectors and sweeps over all 1,114,112 code points.
- 1.8 to 2.7 times Xapian's speed indexing English Wikipedia, with and without stemming
(Performance).
- Unicode-aware. UTF-8 with an ISO-8859-1 fallback, infixes such as
3.14 and
o'clock, suffixes such as c++, acronyms such as U.S.A., and CJK n-grams.
- Stemming and stop words. Four stem strategies (
none, some, all, all_z) with
any snowball stemmer, and three stop strategies.
- One word rule for both sides.
word_scanner is the indexer's word rule on its own,
so a query parser sees the same words the indexer saw.
- Fuzzed. A libFuzzer target checks that terms are valid UTF-8, bounded in length and
in increasing positions, another that segmenting untrusted text stays in bounds; CI runs
ASan and UBSan.
- CJK words.
cjk::segmenter gives ICU 78.3's word boundaries (UAX #29 rules plus ICU's
Chinese/Japanese dictionary), with ICU only as the test oracle (CJK words).
🚀 Quick start
#include "analysis.hh"
#include <cstdio>
int main() {
snowball::stemmer english("english");
analysis::term_generator g; // stem_strategy::some: terms plus Z-prefixed stems
g.stemmer = &english;
g.index_text("The quick brown foxes", [](std::string_view term, std::uint32_t wdf, std::uint32_t pos) {
std::printf("%.*s@%u ", int(term.size()), term.data(), pos);
});
// the@1 Zthe@0 quick@2 Zquick@0 brown@3 Zbrown@0 foxes@4 Zfox@0
}
The sink receives each term occurrence with its wdf increment and its position (0 for
none). docs/API.md covers the strategies, fields and word_scanner.
🀄 CJK words
#include "cjk/segmenter.hh" // target analysis::cjk
analysis::cjk::segmenter s("私は東京大学の学生です");
for (analysis::cjk::segment g; s.next(g);) {
// 私 は 東京 大学 の 学生 です, each word_kind::ideo; spaces and punctuation are word_kind::none
}
Segments cover the text in order, as byte offsets; kind is ICU's rule status
(none, number, letter, ideo). Rules decide every boundary and ICU's dictionary
divides runs of Han and kana by least total cost; Hangul stays whole, as in ICU. How it
works, what was verified, and where it differs from ICU (Thai, Lao, Khmer, Myanmar):
docs/cjk-segmenter.md.
📦 Installation
Requires a C++20 compiler and CMake 3.14 or newer. CMake fetches
unicode and snowball
at pinned commits. CI builds it on Linux (x86_64, arm64), macOS (arm64, x86_64), and
FreeBSD, plus ASan, UBSan, and TSan builds.
include(FetchContent)
FetchContent_Declare(analysis
GIT_REPOSITORY https://github.com/Kronuz/analysis.git
GIT_TAG 991ff3349d7e7d834b331c4b406139e76c76b24e) # main tracks the tip
FetchContent_MakeAvailable(analysis)
target_link_libraries(my_app PRIVATE analysis::analysis)
Then #include "analysis.hh"; link analysis::cjk for the segmenter.
🎯 When to use it
Use it to index text whose terms must equal a Xapian index's, or as a fast analyzer with
Xapian's rules.
Not for:
- Other analyzers' rules, such as Lucene's StandardTokenizer or ICU word breaks: it
reproduces Xapian's, quirks included.
- Keeping terms past the callback: the sink's
std::string_view is valid only during the
call, so copy what you keep.
⚡ Performance
analysis_bench indexes the "text" field of each line of a JSON-lines corpus into a
per-document map of terms to wdf and positions, and reports MB/s of input text (higher is faster).
The corpus is 10,000 English Wikipedia articles (9.6 MB) from the
search-benchmark-game corpus. The rows
are the stemming strategy: none (analysis::stem_strategy::none) indexes each word as it is,
and some (stem_strategy::some) also adds the Z-prefixed English stem of each word that
starts with a letter. The comparator is Xapian 2.1.0's TermGenerator with
STEM_NONE or STEM_SOME, indexing the same text into a Xapian::Document. Before it times
anything the benchmark checks that, for every document, the Xapian::Document holds exactly the
terms, wdfs and positions of the map, and it stops otherwise. × faster is analysis's speed
over Xapian's. Best of three passes, then the best of two runs, with Apple clang 17.0.0 and -O3
on an Apple M4 Pro (macOS 26.7) and an Intel Core i9-9980HK (macOS 15.8):
| Strategy |
analysis, M4 Pro |
Xapian 2.1.0, M4 Pro |
M4 Pro, × faster |
analysis, i9-9980HK |
Xapian 2.1.0, i9-9980HK |
i9-9980HK, × faster |
none |
81.8 MB/s |
30.6 MB/s |
2.7 |
32.3 MB/s |
17.0 MB/s |
1.9 |
some, English stemmer |
30.4 MB/s |
13.2 MB/s |
2.3 |
12.9 MB/s |
7.1 MB/s |
1.8 |
Xapian is found, never fetched, and only when asked for, so the library and a default build do not
need it (it needs Xapian 2.1 and pkg-config):
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DANALYSIS_BENCH_XAPIAN=ON
cmake --build build
build/analysis_bench enwiki.jsonl 10000
Without ANALYSIS_BENCH_XAPIAN the benchmark prints the analysis rows only.
📚 Documentation
🛠️ Building and testing
cmake -S . -B build
cmake --build build
ctest --test-dir build
The build includes analysis_bench; -DANALYSIS_FUZZ=ON adds the libFuzzer targets. The ICU
oracle tools need ICU 78 (-DANALYSIS_ICU_TOOLS=ON -DICU_ROOT=...):
build/analysis_icu_compare corpus test/corpora/*.txt # every boundary against ICU
build/analysis_icu_compare sweep # every code point in 14 contexts
build/analysis_icu_compare bench 20 test/corpora/*.txt # MB/s next to ICU
🤝 Contributing
Read AGENTS.md and the catalog's
rules of engagement
before opening a pull request; CI must pass on every platform.
📄 License
MIT, Copyright (c) 2026 Germán Méndez Bravo. See LICENSE.
The CJK segmenter carries ICU data: cjk/tables.hh (generated from ICU 78.3's word
break rules and Unicode data) and data/cjdict.txt (ICU 78.3's Chinese/Japanese
dictionary, unchanged), under the Unicode License v3 (Unicode-3.0), the dictionary also
under Google's BSD license with the notices of its sources, libtabe and IPADIC. Both are
quoted in LICENSE-ICU. The test corpora are public domain
(test/corpora).