Repository navigation
Support Julia 1.10+, make package compatible with juliac --trim - #66
Conversation
- Require Julia 1.10; DataDeps compat widened to "0.7, 1" - Move DataDeps into a package extension (WordTokenizersDataDepsExt): `load(ALBERT_V1)` now requires `using DataDeps`; `load(path)` does not - Replace HTML_Entities/StrTables with an embedded entity table (src/words/html_entities.jl, regenerated by gen/generate_html_entities.jl) - Fix trim verifier errors: typed TokenBuffer constructor, no splatting in flush!, typed `atoms` call and return type in tweet_tokenize, top-level phone number helper instead of boxed closures, avoid readlines in load - Fix BoundsError in tweet_tokenize for inputs that reduce to nothing (e.g. "&bogus;", "@user" with strip_handle=true) - Keep all characters of multi-codepoint HTML entities - Replace Travis/AppVeyor/REQUIRE with GitHub Actions CI and CompatHelper - Document DataDeps requirement and static compilation in README - Bump version to 0.6.0
|
Looks reasonable to me. I'd move the version of this package to 1.0.0 however. |
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
Posting this review on behalf of @aviks.
I pushed b57e49f, which changes the README line "Since version 0.6
|
|
On behalf of @aviks: I edited the PR description so it matches the version bump to 1.0.0. The summary bullet now says 1.0.0, and the "Migration notes" example says |
Summary
This PR modernizes WordTokenizers.jl for current Julia and makes it compatible with static compilation via JuliaC
--trim=safe:DataDepsmoved to a package extension.load(ALBERT_V1)now needsusing DataDeps.HTML_EntitiesandStrTablesreplaced with an embedded entity table, plus a script to regenerate it.juliac --trim=safeverifier errors fixed. A trimmed executable that uses every tokenizer went from 391 errors to 0.tweet_tokenizefixed.DataDepschange.Important
Breaking change:
load(ALBERT_V1)andload(ALBERT_V2)now needusing DataDeps. Without it they raise an error explaining what to do.load(path)with a local vocabulary file works withoutDataDeps. All rule-based tokenizers are unaffected.Motivation
Julia 1.12+ can build small standalone executables and libraries with
juliac --trim. Trimming requires every call reachable from the entry point to be statically resolvable, including the__init__of every loaded package. Two dependencies made this impossible, and the package's own code had type-instability problems.Changes
1. Julia and dependency compatibility (
Project.toml)julia11.10DataDeps0.6.5, 0.7(hard dependency)0.7, 1(weak dependency + extension)HTML_Entities1StrTables1Unicode,Test1Manifest.tomlis added to.gitignore.2.
DataDepsas a package extensionDataDepsloads HTTP.jl, OpenSSL, MbedTLS and Scratch.jl. Their__init__functions cannot be trimmed, so any application depending on WordTokenizers could not be compiled with--trim.ext/WordTokenizersDataDepsExt.jlregisters the ALBERT vocabularies with DataDeps and providesWordTokenizers.datadep_path(name).tokenizer_files(ALBERT_V1/V2)at load time. Listing the available vocabularies doesn't need DataDeps.load(::Type{<:PretrainedTokenizer})checks whether the extension is loaded. If not, it throws:3. Embedded HTML entity table instead of
HTML_Entities.jlHTML_Entities.jl (latest release v1.0.2, Oct 2024) builds its table at load time:
defaultis an untyped global, andStrTables.loadinfers element types from file contents. Neither can be resolved statically. A minimal app that only callslookupname(HTML_Entities.default, "Delta")fails--trim=safewith 10 verifier errors. Because JuliaC compiles every package's__init__, simply depending on HTML_Entities breaks trimming.src/words/html_entities.jlholds aconst HTML_ENTITIES::Dict{String,String}with the same 2,125 named references.gen/generate_html_entities.jlregenerates it:julia gen/generate_html_entities.jlinstalls HTML_Entities into a temporary environment, so it is not a dependency. The script reproduces the committed file byte-for-byte on Julia 1.10 and 1.13.4. Trim / type-stability fixes
Found by compiling a test app that calls every public tokenizer with
juliac --trim=safeon Julia 1.13.1:nltk_phonenumbersi/j, which were boxed (Core.Box) and becameAnyphonenumber_end_index(ts, i)helper; the two closures were the same apart from their start indexTokenBuffer(input)TokenBuffer(input, [], [], 1)builtVector{Any}and converted itTokenBuffer(input, Char[], String[], 1)tweet_tokenizeatoms(ts, [])iterated aVector{Any}atoms(ts, ())tweet_tokenizereturn []made the return typeUnion{Vector{Any}, Vector{String}}return String[]flush!push!(ts.tokens, s...)splatting is unresolvablehtml_entityHTML_Entities.defaultglobalconst HTML_ENTITIESload(path)readlinesusesEachLine, whoseondone::Functionfield cannot be trimmedsplit(read(path, String), '\n'), with\rand empty lines strippedVerified results:
rev_tokenize/rev_detokenize, and the defaulttokenize/split_sentencescompiles with 0 errors. Its output is byte-identical to the same code run under regular Julia.load(path),tokenizer,ids_from_tokensandsentence_from_tokenscompiles with 0 errors and returns the same tokens and IDs as the test suite.Still not supported in trimmed binaries (documented in the README):
load(ALBERT_V1)and other pretrained downloads, because DataDeps can't be trimmed. Ship the vocabulary file and useload(path)instead.set_tokenizer/set_sentence_splitterat run time, because they use@eval.5. Bug fixes
replace_html_entitiesandpre_processreturnedts.tokens[1]on an empty vector:≂̸gave"≂"instead of"≂̸". All characters are now kept.Albert_Version1→ALBERT_V1andtokenizerfiles→tokenizer_files.6. CI
.travis.yml(Julia 1.0/1.3),appveyor.ymland the obsoleteREQUIREfile..github/workflows/CI.yml:1.10,1andpreon Ubuntu, plus1on macOS and Windows.preis allowed to fail..github/workflows/CompatHelper.yml.actions/checkout@v7,julia-actions/setup-julia@v3,julia-actions/cache@v3,codecov/codecov-action@v7julia-buildpkg,julia-runtest,julia-processcoverageandTagBotremain on@v1, their current major versions.7. Documentation
using DataDepsis now required for pretrained vocabularies.--trim)" section listing what is and isn't supported.Testing
Pkg.test(), 136 tests (incl. 1 pre-existing@test_broken)--depwarn=yestest_all: ambiguities, piracy, stale deps, compat bounds, unbound args, undefined exports, persistent tasksjuliac --trim=safe, all tokenizersjuliac --trim=safe, SentencePieceload(path)New tests:
test/sp_unigram.jl:load(ALBERT_V1)throws beforeusing DataDeps.tokenizer_filesis populated without DataDeps.using DataDeps.test/tweet_tokenize.jl:remove_illegal=true/false.Vector{String}return type, including for empty input.BoundsErrorcases.The only warnings in the test log are the existing "Method definition
tokenize(AbstractString)… overwritten" messages fromtest/set_method_api.jl. Theset_tokenizerdocstring says these are expected.Known issue, not changed in this PR
In
toktok_tokenize, the replacement rules for&→&,|→|and tab →	(rules_replacesinsrc/words/TokTok.jl) never fire.lookaheadcompares one-elementTuple{String}patterns againstChars, which never match. Making them work would change the output of the defaulttokenize("a & b"would yield"&"), so the current behavior is kept. This is left to the maintainers: either fix it to match NLTK's ToktokTokenizer, or remove the dead rules.Migration notes for users
The analysis, code changes, tests and this description were prepared by Claude (Anthropic's AI assistant, running in Claude Code).
🤖 Generated with Claude Code