-
-
Notifications
You must be signed in to change notification settings - Fork 358
fix(search): preserve CJK relevance signals #232
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
Open
w-up
wants to merge
3
commits into
KnockOutEZ:main
Choose a base branch
from
w-up:fix/cjk-ranking-signals
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
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,41 @@ | ||
| const LATIN_OR_DIGIT_RE = /[\p{Script=Latin}\p{N}]/u; | ||
| const CJK_RE = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u; | ||
| // Include the prolonged sound mark explicitly so it stays inside Katakana runs, | ||
| // while punctuation such as 、, 。, and ・ still separates CJK token parts. | ||
| const CJK_RUN = String.raw`[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\u30FC]+`; | ||
| const TOKEN_PART_RE = new RegExp(String.raw`[\p{Script=Latin}\p{N}]+|${CJK_RUN}`, 'gu'); | ||
| const MAX_INPUT_CODE_UNITS = 4096; | ||
| const MAX_TOKENS = 256; | ||
|
|
||
| /** | ||
| * Tokenize ranking text with Unicode awareness so CJK characters are not discarded. | ||
| * Preserve lowercase word semantics for Latin/digits and emit overlapping bigrams for contiguous CJK text. | ||
| */ | ||
| export function tokenizeRankingText(text: string): string[] { | ||
| const tokens: string[] = []; | ||
| // Bound input before lowercasing, regex matching, or Array.from so an oversized query | ||
| // cannot allocate complete intermediate arrays or overflow the RegExp stack before the token cap applies. | ||
| const boundedText = text.slice(0, MAX_INPUT_CODE_UNITS).toLowerCase(); | ||
| const parts = boundedText.match(TOKEN_PART_RE) ?? []; | ||
|
|
||
| for (const part of parts) { | ||
| if (tokens.length >= MAX_TOKENS) break; | ||
| if (LATIN_OR_DIGIT_RE.test(part)) { | ||
| tokens.push(part); | ||
| continue; | ||
| } | ||
| if (!CJK_RE.test(part)) continue; | ||
|
|
||
| const chars = Array.from(part); | ||
| if (chars.length === 1) { | ||
| tokens.push(chars[0]); | ||
| continue; | ||
| } | ||
| for (let i = 0; i < chars.length - 1; i++) { | ||
| tokens.push(chars[i] + chars[i + 1]); | ||
| if (tokens.length >= MAX_TOKENS) break; | ||
| } | ||
| } | ||
|
|
||
| return tokens; | ||
| } | ||
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
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
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,38 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { tokenizeRankingText } from '../../../../src/search/core/text-tokenizer.js'; | ||
|
|
||
| describe('tokenizeRankingText', () => { | ||
| it('preserves Latin and digit runs while emitting CJK bigrams', () => { | ||
| expect(tokenizeRankingText('Hermes Agent 中文配置 2026')).toEqual([ | ||
| 'hermes', | ||
| 'agent', | ||
| '中文', | ||
| '文配', | ||
| '配置', | ||
| '2026', | ||
| ]); | ||
| }); | ||
|
|
||
| it('keeps Japanese prolonged sound marks inside Katakana bigrams', () => { | ||
| expect(tokenizeRankingText('AIニュース')).toEqual(['ai', 'ニュ', 'ュー', 'ース']); | ||
| }); | ||
|
|
||
| it('splits CJK runs around punctuation', () => { | ||
| expect(tokenizeRankingText('東京、京都。大阪・神戸')).toEqual([ | ||
| '東京', | ||
| '京都', | ||
| '大阪', | ||
| '神戸', | ||
| ]); | ||
| }); | ||
|
|
||
| it('emits Hangul bigrams for unsegmented Korean text', () => { | ||
| expect(tokenizeRankingText('인공지능뉴스')).toEqual(['인공', '공지', '지능', '능뉴', '뉴스']); | ||
| }); | ||
|
|
||
| it('caps token output for pathological unsegmented input', () => { | ||
| const tokens = tokenizeRankingText('中'.repeat(10_000_000)); | ||
|
|
||
| expect(tokens.length).toBeLessThanOrEqual(256); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.