Skip to content

Tokenizer 3.7.0

Latest

Choose a tag to compare

@vthorsteinsson vthorsteinsson released this 15 Oct 16:01

Tokenizer 3.7.0 is 15-19% faster on CPython and about 10% faster on PyPy. It also recognizes month ranges written as one word, and makes free-standing percent signs punctuation.

Changes in token output

  • Month ranges. A month range written as one word, such as ágúst-september or júlí–ágúst, is now split into two TOK.DATEREL tokens with the hyphen or en dash between them. Previously it was a single TOK.WORD. Ambiguous month names (mar-apr) and a capitalized Ágúst-... are left alone, as for single months. (#63, #65)
  • Free-standing %, ‰ and ° are now TOK.PUNCTUATION tokens, as they already were when attached to other punctuation, as in (%). Previously they were TOK.WORD tokens. A number followed by one of them is still coalesced into a TOK.PERCENT or TOK.MEASUREMENT token.
  • Percent spacing. A number followed by a separate % or ‰ now keeps the space in the text of the TOK.PERCENT token before punctuation as well. Previously 12,3 %. gave 12,3%, while 12,3 % í gave 12,3 %. The values and the original text are unchanged.

Otherwise, the output is identical to 3.6.4. On the large test set, 389 of 17,144 lines of shallow output differ from 3.6.4: 388 in percent spacing and 1 month range.

Performance

Time to deep-tokenize 1 MB of text with tokenize(), on test/toktest_large.txt using a 3.4 GHz Intel Core i7-4770 (best of 7 runs):

3.6.4 (sec) 3.7.0 (sec)
CPython 3.13 2.22 1.80
CPython 3.14 2.26 1.88
CPython 3.15 2.24 1.90
PyPy 3.11 1.17 1.04
PyPy 3.12 1.07 0.97

Profiling with the CPython 3.15 sampling profiler found two hot spots:

  • Zero-width and composite characters were searched for in every raw token. They are now searched for once per text.
  • The number parser called re.match() with pattern strings, paying the re cache lookup on each of up to about 25 attempts per numeric token. All regexes are now precompiled.

Neither change alters the output. (#64)

Development

  • bench/bench.py times several workloads and records a digest of their complete output, so that a change can be checked for both speed and identical output. bench/profile.sh runs the CPython 3.15 sampling profiler.
  • The gold file of the large test set, test/toktest_large_gold_acceptable.txt, dated from version 2.0.0. It has been regenerated, and a new test now fails when the output of the tokenize command differs from it.
  • uv.lock is now committed, and CI installs with --locked. The unused twine was removed from the dev extra and the release dependency group. It pulled urllib3 into the lockfile, which GitHub reported as a vulnerable dependency.
  • The code has been cleaned up for the default rule set of ruff 0.16.
  • The README has been updated: new performance figures, corrected token examples, and documentation of the -v option, original=True for split_into_sentences(), the one_sent_per_line and coalesce_percent options, and paragraph tokens.

Verification

  • The test suite (153 tests) passes on CPython 3.10 through 3.14 and PyPy 3.11 in CI, and mypy passes.
  • The sdist and wheel pass twine check. The wheel installs and works in clean environments on CPython 3.10, CPython 3.15 and PyPy 3.12.

Upgrade with:

pip install --upgrade tokenizer

Full changes: v3.6.4...v3.7.0