Skip to content

Support Julia 1.10+, make package compatible with juliac --trim - #66

Merged
aviks merged 3 commits into
JuliaText:masterfrom
rssdev10:julia-1.10-trim
Nov 19, 2026
Merged

aviks merged 3 commits into
JuliaText:masterfrom
rssdev10:julia-1.10-trim

Conversation

@rssdev10

@rssdev10 rssdev10 commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR modernizes WordTokenizers.jl for current Julia and makes it compatible with static compilation via JuliaC --trim=safe:

  • Minimum Julia version raised to 1.10 (current LTS).
  • DataDeps moved to a package extension. load(ALBERT_V1) now needs using DataDeps.
  • HTML_Entities and StrTables replaced with an embedded entity table, plus a script to regenerate it.
  • All juliac --trim=safe verifier errors fixed. A trimmed executable that uses every tokenizer went from 391 errors to 0.
  • Two crashes in tweet_tokenize fixed.
  • Travis/AppVeyor replaced with GitHub Actions.
  • Version bumped to 1.0.0 because of the DataDeps change.

Important

Breaking change: load(ALBERT_V1) and load(ALBERT_V2) now need using DataDeps. Without it they raise an error explaining what to do.
load(path) with a local vocabulary file works without DataDeps. 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)

Before After
julia 1 1.10
DataDeps 0.6.5, 0.7 (hard dependency) 0.7, 1 (weak dependency + extension)
HTML_Entities 1 removed
StrTables 1 removed
Unicode, Test no compat entry 1

Manifest.toml is added to .gitignore.

2. DataDeps as a package extension

DataDeps loads 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.

  • New ext/WordTokenizersDataDepsExt.jl registers the ALBERT vocabularies with DataDeps and provides WordTokenizers.datadep_path(name).
  • The core package still records the file names in 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:
    Loading pretrained tokenizers requires DataDeps.jl: run `using DataDeps` first,
    or call `load(path)` with a local vocabulary file.
    

3. Embedded HTML entity table instead of HTML_Entities.jl

HTML_Entities.jl (latest release v1.0.2, Oct 2024) builds its table at load time:

function __init__()
    global default = HTML_Table(StrTables.load(DATA_PATH)...)
end

default is an untyped global, and StrTables.load infers element types from file contents. Neither can be resolved statically. A minimal app that only calls lookupname(HTML_Entities.default, "Delta") fails --trim=safe with 10 verifier errors. Because JuliaC compiles every package's __init__, simply depending on HTML_Entities breaks trimming.

  • src/words/html_entities.jl holds a const HTML_ENTITIES::Dict{String,String} with the same 2,125 named references.
  • gen/generate_html_entities.jl regenerates it: julia gen/generate_html_entities.jl installs 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.
  • Side effects: two fewer dependencies (three counting RelocatableFolders), and no file I/O when the package loads.

4. Trim / type-stability fixes

Found by compiling a test app that calls every public tokenizer with juliac --trim=safe on Julia 1.13.1:

Location Problem Fix Verifier errors
nltk_phonenumbers Two inner closures assigned to the outer i/j, which were boxed (Core.Box) and became Any One top-level phonenumber_end_index(ts, i) helper; the two closures were the same apart from their start index ~330
TokenBuffer(input) TokenBuffer(input, [], [], 1) built Vector{Any} and converted it TokenBuffer(input, Char[], String[], 1) 8
tweet_tokenize atoms(ts, []) iterated a Vector{Any} atoms(ts, ()) 12
tweet_tokenize return [] made the return type Union{Vector{Any}, Vector{String}} return String[] 2
flush! push!(ts.tokens, s...) splatting is unresolvable Explicit loop 2
html_entity Untyped HTML_Entities.default global Lookup in const HTML_ENTITIES 12
load(path) readlines uses EachLine, whose ondone::Function field cannot be trimmed split(read(path, String), '\n'), with \r and empty lines stripped 2

Verified results:

  • An executable using every tokenizer and sentence splitter, rev_tokenize/rev_detokenize, and the default tokenize/split_sentences compiles with 0 errors. Its output is byte-identical to the same code run under regular Julia.
  • A second executable using load(path), tokenizer, ids_from_tokens and sentence_from_tokens compiles 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 use load(path) instead.
  • set_tokenizer / set_sentence_splitter at run time, because they use @eval.

5. Bug fixes

  • Crash on inputs that reduce to nothing. replace_html_entities and pre_process returned ts.tokens[1] on an empty vector:
    tweet_tokenize("&bogus;")                       # BoundsError → now String[]
    tweet_tokenize("@someone", strip_handle=true)   # BoundsError → now String[]
  • Multi-codepoint entities were truncated. Only the first character was kept, e.g. &NotEqualTilde; gave "≂" instead of "≂̸". All characters are now kept.
  • README: fixed Albert_Version1 → ALBERT_V1 and tokenizerfiles → tokenizer_files.

6. CI

  • Removed .travis.yml (Julia 1.0/1.3), appveyor.yml and the obsolete REQUIRE file.
  • Added .github/workflows/CI.yml:
    • Julia 1.10, 1 and pre on Ubuntu, plus 1 on macOS and Windows.
    • Coverage uploaded to Codecov.
    • pre is allowed to fail.
  • Added .github/workflows/CompatHelper.yml.
  • Action versions:
    • actions/checkout@v7, julia-actions/setup-julia@v3, julia-actions/cache@v3, codecov/codecov-action@v7
    • julia-buildpkg, julia-runtest, julia-processcoverage and TagBot remain on @v1, their current major versions.

7. Documentation

  • README: using DataDeps is now required for pretrained vocabularies.
  • README: new "Static compilation (JuliaC --trim)" section listing what is and isn't supported.

Testing

Check Julia 1.10.12 Julia 1.13.1
Pkg.test(), 136 tests (incl. 1 pre-existing @test_broken) ✅ pass ✅ pass
--depwarn=yes no deprecation warnings no deprecation warnings
Aqua.jl test_all: ambiguities, piracy, stale deps, compat bounds, unbound args, undefined exports, persistent tasks — ✅ pass
juliac --trim=safe, all tokenizers n/a ✅ 0 errors (391 before)
juliac --trim=safe, SentencePiece load(path) n/a ✅ 0 errors

New tests:

  • test/sp_unigram.jl:
    • load(ALBERT_V1) throws before using DataDeps.
    • tokenizer_files is populated without DataDeps.
    • The existing tests still pass after using DataDeps.
  • test/tweet_tokenize.jl:
    • Multi-codepoint entity.
    • Unknown entity with remove_illegal=true/false.
    • Vector{String} return type, including for empty input.
    • The two former BoundsError cases.
    • A phone-number case.

The only warnings in the test log are the existing "Method definition tokenize(AbstractString) … overwritten" messages from test/set_method_api.jl. The set_tokenizer docstring says these are expected.

Known issue, not changed in this PR

In toktok_tokenize, the replacement rules for & → &amp;, | → &#124; and tab → &#9; (rules_replaces in src/words/TokTok.jl) never fire. lookahead compares one-element Tuple{String} patterns against Chars, which never match. Making them work would change the output of the default tokenize ("a & b" would yield "&amp;"), 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

# Before (≤ 0.5.x)
using WordTokenizers
spm = load(ALBERT_V1)

# After (1.0.0)
using WordTokenizers, DataDeps   # `] add DataDeps` if it is not installed
spm = load(ALBERT_V1)

# Or, without DataDeps (works in trimmed executables):
spm = load("/path/to/albert_base_v1_30k-clean.vocab")

The analysis, code changes, tests and this description were prepared by Claude (Anthropic's AI assistant, running in Claude Code).
🤖 Generated with Claude Code

- 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
@aviks

aviks commented Nov 16, 2026

Copy link
Copy Markdown
Member

Looks reasonable to me. I'd move the version of this package to 1.0.0 however.

rssdev10 and others added 2 commits November 16, 2026 21:27
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@aviks

aviks commented Nov 16, 2026

Copy link
Copy Markdown
Member

Posting this review on behalf of @aviks.

Pkg.test() passes on Julia 1.10 and 1.13. Running gen/generate_html_entities.jl reproduces src/words/html_entities.jl byte for byte. I also ran about 3,000 random inputs through every tokenizer and sentence splitter on both master and this branch. The only output changes are the two intended fixes: multi-codepoint entities are now kept whole, and the BoundsErrors with strip_handle=true are gone.

I pushed b57e49f, which changes the README line "Since version 0.6 DataDeps is an optional dependency" to say 1.0. A few remaining points:

  1. Migration notes. The "Migration notes" section in the PR description still says 0.6.0 / ≤ 0.5.x. It should be updated to 1.0.0.
  2. Blank lines in vocab files. load(path) now silently drops blank lines (filter!(!isempty, …)). Before, a blank line in the middle of a file threw an error. Now it is skipped and the IDs after it shift by one. This is harmless for well-formed vocab files, but it is a behavior change worth knowing about.
  3. Test-order dependency. The "Pretrained without DataDeps" testset in test/sp_unigram.jl only passes because no earlier test file loads DataDeps in the same process. It will break if an earlier test file ever does using DataDeps. A short comment there would help.
  4. CompatHelper permissions. CompatHelper.yml has no permissions: block. On repos where the default GITHUB_TOKEN is read-only, CompatHelper can't open PRs. The standard template adds contents: write and pull-requests: write.
  5. Codecov token. The Codecov step needs a CODECOV_TOKEN repo secret. CI stays green without it (fail_ci_if_error: false), but coverage won't upload.
  6. toktok replacement rules. Agreed on leaving the dead rules_replaces in toktok out of this PR. We'll decide separately whether to make them match NLTK or delete them.

Written by Devin

@aviks

aviks commented Nov 16, 2026

Copy link
Copy Markdown
Member

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 # After (1.0.0) instead of 0.6.0. The "Before" label (≤ 0.5.x) was already right. These notes exist only in the PR description, so no commit was needed.

Written by Devin

@aviks
aviks merged commit 2cd18cb into JuliaText:master Nov 19, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants