Repository navigation
Add optional ICU-based InternationalTokenizer - #648
Conversation
`--tokenizer international` splits words with icu::BreakIterator so that languages written without spaces (Chinese, Japanese, Thai) and mixed-language text can be tokenized. Tokens are case-folded; whitespace, symbols and punctuation are dropped. ICU is optional and auto-detected (PISA_ENABLE_ICU, default ON). Without it, PISA still builds, CMake warns, and the tokenizer throws std::runtime_error. ICU state lives behind a pimpl, so no ICU headers appear in public headers. Relates to pisa-engine#464
3f4380e to
cb05ae1
Compare
|
@TheFabbest Thanks for the contribution, and sorry for the delay in reviewing. I'm hoping to find some time over the weekend 🤞 Thanks for your patience. |
elshize
left a comment
There was a problem hiding this comment.
Nice work! A few comments.
Documentation probably needs to be updated to mention the new tokenizer and its ICU dependency.
The guide doesn't seem to mention the tokenizer other than in the generated --help output. It would be ideal if we could add a section in the "Parsing" chapter of the guide, but it doesn't have to be in this PR.
If you want to test the new tokenizer in CI, the CI image needs the ICU development package. Otherwise, a simpler test will be run that just checks that the tokenizer can be constructed and throws an error when ICU is not available.
I think I'd like to see one image with icu, otherwise it's likely to regress eventually. We could add a separate dockerfile gcc-icu that uses, say, the latest gcc (or whatever the default is on the base image even), include icu + compile with icu support.
|
Looks good, just one question. Also, I think it's okay not to include CI in this PR, but if we're not, could you please open an issue for this so it's tracked? Thanks. |
|
@TheFabbest great work, thanks for contributing! |
|
@TheFabbest Would you mind having a look at the failing clang-tidy in trunk? I believe an explicit cast should fix it. There's a dockerfile for tidy to run it locally to verify it's passing. |
Summary
Relates to #464
Adds
InternationalTokenizer, selectable with--tokenizer international. It splits text into words with ICU'sicu::BreakIterator, which makes it possible to tokenize languages written without spaces (Chinese, Japanese, Thai and more) and mixed-language text.ICU is an optional dependency. Without it, PISA builds and works normally and
cmakeprints a warning, but--tokenizer internationalfails at runtime with an error.Most Linux distributions already have the ICU runtime installed, but PISA needs the development files (
libicu-devon Debian/Ubuntu,libicu-develon Fedora), which may be missing, for example in Docker images. As far as I know, macOS should work with-DICU_ROOT=$(brew --prefix icu4c), but I couldn't test it.Behavior
For
InternationalTokenizer:Straße->strasse). This is standard, but can be changed easily to lowercase only. Accents are kept.shouldn't->shouldn't). Dotted words stay whole, only a trailing.is dropped (U.S.A.->u.s.a,himself.image->himself.image).Dependency handling
ICU is large, but is available as a standard system package on common platforms. PISA does not vendor it, so unlike the
PISA_SYSTEM_*options (which choose between a system and a vendored copy of a dependency),PISA_ENABLE_ICUsimply enables or disables an optional feature. The default of ON follows the same convention as the vendored boost-cmake'sBOOST_LOCALE_ENABLE_ICU_BACKEND("enable if available", default ON). The table below summarizes the behavior:PISA_ENABLE_ICUPISA_HAS_ICU=1is defined, ICU is linked, tokenizer worksstd::runtime_errorstd::runtime_errorICU is already probed optionally by the existing build via
external/boost-cmake/libs/locale.cmake(find_package(ICU ...)withoutREQUIRED, falling back to iconv). So this PR does not add a new required dependency, but it is the first time PISA's own code uses ICU.No ICU headers are exposed by PISA’s public headers:
InternationalTokenStreamis declared intokenizer.hpplike the other streams, but its ICU state lives behind a pimpl defined intokenizer.cpp. Since PISA is a static library, binaries linking it when built with ICU also link ICU.ICU is widely used and well-maintained, and is the de facto standard for Unicode text processing.
Testing
Environment: PopOS! 24.04 (ubuntu-based), GCC 13.3, ICU 74.2 locally (was already installed).
Also an Ubuntu 24.04 docker container with no ICU headers installed.
PISA_ENABLE_ICUON or OFF) were checked.-DPISA_COMPILE_HEADERS=ONpasses, confirming thattokenizer.hppdoesn’t leak ICU includes.parse_collectiontool with all three tokenizers and manually verified that the output is as expected.EnglishTokenizeron English text, with little difference in memory usage.Notes
PISA_ENABLE_ICU. I chose ON (auto-detect) to match the existing Boost.Locale probe. If you prefer optional features to be strictly opt-in, flipping it to OFF is a one-line change.icu::BreakIteratorandicu::UnicodeString, with no shared mutable state. This suggests it is safe.InternationalTokenStreamis not copyable. Streams are only handed out asstd::unique_ptr<TokenStream>, so copying isn’t needed, and a correct copy would have to re-bind the ICU break iterator to the copied text. I can add copy support if you prefer consistency.