Bring back character q-grams as a token generator - #52
Merged
Merged
Conversation
QgramGenerator(q) emits character q-grams over the whole normalized text, blanks included, so a window can span a word boundary; runs of blanks read as one, and no q-gram spans two fields of a multi-field document. Tokens are tagged 'q', which keeps the 3-gram "que" apart from the word "que" and keeps word-level stopwords and lemmas off q-grams. Several lengths are several generators, and they mix freely with word tokens. They live on the ordinary Vocabulary, so VectorModel, EntropyWeighting, RandomIndexing, LSI and filter_tokens work unchanged. A hash-keyed separate vocabulary was tried first and built the identical space (314,775 q-grams on 23k Markdown paragraphs) about 6x faster, but a Dict is needed either way and keeping the strings allows inspection; most of the gap is Vocabulary taking a lock per token, which the word path pays too. Profiles with q-gram generators save as format "1.2", so a "1.1"-only build refuses them instead of silently tokenizing without the q-grams. Profiles without generators, the published ones included, still write "1.1" with the same bytes and profile_id. Other custom generators still refuse to save, and merge_profiles still declines any generator list. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
QgramGenerator(q), a token generator that emits character q-grams. They are meant as encoders for document-vs-document tasks (classification, clustering,RandomIndexinginto bit or multi-bit sketches), not for short-query search.'q'. The tag keeps the 3-gramqueapart from the wordque, and keeps word-level stopwords and lemmas from matching q-grams.Vocabulary, soVectorModel,EntropyWeighting,RandomIndexing, LSI andfilter_tokenswork unchanged. Because thefilter_tokenspredicate sees the token, words and q-grams can be pruned with separate thresholds by checking the tag.Saving
"1.2". A build that only reads"1.1"refuses it, instead of silently tokenizing without the q-grams."1.1"with the same bytes and the sameprofile_id.merge_profilesstill rejects any config with generators.Cost
A hash-keyed separate vocabulary was tried first. On 23,186 Markdown paragraphs with 16 threads, both versions built the same space: 314,775 distinct q-grams and the same number of nonzero entries.
QgramGeneratorWe kept the strings because a Dict is needed either way, and strings can be inspected. Most of the build-time gap comes from
Vocabularytaking a lock per token (_locked_tokenize_and_push), which slows the word path equally. That is left for a separate change.Test plan
Pkg.test()), including 33 new tests intest/testqgrams.jl: tokens, tagging, stopwords, pruning, entropy weighting,RandomIndexing, and a save/load round trip in both format versions.🤖 Generated with Claude Code