-
Notifications
You must be signed in to change notification settings - Fork 0
feat(tokens): port identifier-aware tokenizer from semble #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // Port of src/semble/tokens.py tests | ||
|
|
||
| import { describe, expect, it } from 'bun:test' | ||
| import { splitIdentifier, tokenize } from './tokens.ts' | ||
|
|
||
| describe('splitIdentifier', () => { | ||
| it('splits PascalCase identifiers', () => { | ||
| expect(splitIdentifier('HandlerStack')).toEqual([ | ||
| 'handlerstack', | ||
| 'handler', | ||
| 'stack', | ||
| ]) | ||
| }) | ||
|
|
||
| it('preserves runs of capitals as a single sub-token', () => { | ||
| expect(splitIdentifier('getHTTPResponse')).toEqual([ | ||
| 'gethttpresponse', | ||
| 'get', | ||
| 'http', | ||
| 'response', | ||
| ]) | ||
| }) | ||
|
|
||
| it('handles leading run of capitals', () => { | ||
| expect(splitIdentifier('XMLParser')).toEqual([ | ||
| 'xmlparser', | ||
| 'xml', | ||
| 'parser', | ||
| ]) | ||
| }) | ||
|
|
||
| it('splits snake_case identifiers', () => { | ||
| expect(splitIdentifier('my_func')).toEqual(['my_func', 'my', 'func']) | ||
| }) | ||
|
|
||
| it('returns only the lowered token when there is no boundary', () => { | ||
| expect(splitIdentifier('simple')).toEqual(['simple']) | ||
| }) | ||
|
|
||
| it('lowercases an already lower-case token', () => { | ||
| expect(splitIdentifier('Already')).toEqual(['already']) | ||
| }) | ||
|
|
||
| it('keeps consecutive underscores from collapsing into duplicate parts', () => { | ||
| // Python `split('_')` produces empty strings between consecutive | ||
| // underscores; the upstream filter drops them. | ||
| expect(splitIdentifier('foo__bar')).toEqual(['foo__bar', 'foo', 'bar']) | ||
| }) | ||
|
|
||
| it('treats a leading underscore as snake_case with one effective part', () => { | ||
| // `_foo`.split('_') === ['', 'foo'] -> filtered to ['foo'] -> len < 2 | ||
| expect(splitIdentifier('_foo')).toEqual(['_foo']) | ||
| }) | ||
|
|
||
| it('splits digit runs as their own camel sub-token', () => { | ||
| expect(splitIdentifier('abc123Def')).toEqual([ | ||
| 'abc123def', | ||
| 'abc', | ||
| '123', | ||
| 'def', | ||
| ]) | ||
| }) | ||
| }) | ||
|
|
||
| describe('tokenize', () => { | ||
| it('splits plain space-separated words', () => { | ||
| expect(tokenize('foo bar baz')).toEqual(['foo', 'bar', 'baz']) | ||
| }) | ||
|
|
||
| it('expands compound identifiers and drops non-identifier digits', () => { | ||
| // Numbers that do not start an identifier (e.g. "123") are not matched by | ||
| // TOKEN_RE, which mirrors the upstream Python behaviour. | ||
| expect(tokenize('camelCase_snake_case 123')).toEqual([ | ||
| 'camelcase_snake_case', | ||
| 'camelcase', | ||
| 'snake', | ||
| 'case', | ||
| ]) | ||
| }) | ||
|
|
||
| it('returns an empty array for input with no identifiers', () => { | ||
| expect(tokenize(' !!! 123 ???')).toEqual([]) | ||
| }) | ||
|
|
||
| it('preserves multiple identifiers and expands each', () => { | ||
| expect(tokenize('HandlerStack my_func')).toEqual([ | ||
| 'handlerstack', | ||
| 'handler', | ||
| 'stack', | ||
| 'my_func', | ||
| 'my', | ||
| 'func', | ||
| ]) | ||
| }) | ||
| }) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Port of src/semble/tokens.py | ||
|
|
||
| const TOKEN_RE = /[a-zA-Z_][a-zA-Z0-9_]*/g | ||
|
|
||
| // Split on camelCase/PascalCase boundaries: | ||
| // "HandlerStack" -> ["Handler", "Stack"] | ||
| // "getHTTPResponse" -> ["get", "HTTP", "Response"] | ||
| // "XMLParser" -> ["XML", "Parser"] | ||
| const CAMEL_RE = /[A-Z]+(?=[A-Z][a-z])|[A-Z]?[a-z]+|[A-Z]+|[0-9]+/g | ||
|
|
||
| /** | ||
| * Split a single identifier into sub-tokens via camelCase/snake_case. | ||
| * | ||
| * Returns the original token (lowered) plus any sub-tokens. | ||
| * E.g. "HandlerStack" -> ["handlerstack", "handler", "stack"] | ||
| * "my_func" -> ["my_func", "my", "func"] | ||
| * "simple" -> ["simple"] | ||
| */ | ||
| export function splitIdentifier(token: string): string[] { | ||
| const lower = token.toLowerCase() | ||
|
|
||
| // Fast-path: pure-lowercase tokens with no underscores/digits cannot split | ||
| // further. TOKEN_RE only matches [a-zA-Z0-9_], so the absence of `_`, | ||
| // uppercase, and digits means the token is already a single sub-token. | ||
| if (!token.includes('_') && !/[A-Z0-9]/.test(token)) { | ||
| return [lower] | ||
| } | ||
|
|
||
| let parts: string[] | ||
|
|
||
| if (token.includes('_')) { | ||
| // snake_case splitting | ||
| parts = lower.split('_').filter(p => p.length > 0) | ||
| } | ||
| else { | ||
| // camelCase / PascalCase splitting | ||
| parts = Array.from(token.matchAll(CAMEL_RE), ([m]) => m.toLowerCase()) | ||
| } | ||
|
|
||
| if (parts.length >= 2) { | ||
| return [lower, ...parts] | ||
| } | ||
| return [lower] | ||
| } | ||
|
|
||
| /** | ||
| * Split text into lowercase identifier-like tokens for BM25 indexing. | ||
| * | ||
| * Compound identifiers (camelCase, PascalCase, snake_case) are expanded | ||
| * into sub-tokens so that partial matches work. The original compound | ||
| * token is preserved for exact-match boosting. | ||
| */ | ||
| export function tokenize(text: string): string[] { | ||
| const result: string[] = [] | ||
| for (const [match] of text.matchAll(TOKEN_RE)) { | ||
| result.push(...splitIdentifier(match)) | ||
| } | ||
| return result | ||
| } | ||
|
amondnet marked this conversation as resolved.
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For simple lowercase tokens (which make up the vast majority of words in typical text), we can bypass the expensive
matchAllregex execution and array allocation entirely. SinceTOKEN_REonly matches[a-zA-Z0-9_], any token that does not contain underscores, uppercase letters, or digits consists solely of lowercase letters and cannot be split further. Adding a fast-path check at the beginning ofsplitIdentifiersignificantly improves tokenization performance.