Skip to content

feat(query): split camelCase/snake_case identifiers as fallback - #83

Merged
Karmenzind merged 2 commits into
Karmenzind:mainfrom
bighu630:feat/identifier-splitting
Sep 16, 2026
Merged

Karmenzind merged 2 commits into
Karmenzind:mainfrom
bighu630:feat/identifier-splitting

Conversation

@bighu630

@bighu630 bighu630 commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Identifier-style English input cannot be looked up:

  • kd helloWorld — normalizeQuery lowercases it to helloworld, so the camel boundaries are lost and Youdao has no entry.
  • kd hello_world — the underscore is not in the IsEN charset (^[A-Za-z0-9 -.?]+$), so it is routed to the ch table.

Change

Detect camelCase/snake_case identifiers and fall back to machine translation of the space-separated words.

Note: the whole feature is gated behind the new split_camelcase_and_snakecase config option (default false), because splitting is programming-oriented and could affect ordinary lookup intent. When disabled, behavior is identical to before this change.

  • Short query: look up the whole string first; if it is not found, split it into words and translate the joined phrase (helloWorld -> hello world).
  • Long text (-t): rewrite every identifier token in the segment before translating; if the rewrite yields nothing, retry with the original text.
  • Splitting rules: acronyms stay together (HTTPServer -> HTTP Server, getHTTPResponse -> get HTTP Response); underscores are split with empty segments dropped (hello_world -> hello world, _private -> private); single letters are kept (getUserByID -> get User By ID); digits stay attached to the previous segment (user2FA -> user2 FA). Plain words, phrases and all-caps words are left untouched.

Examples:

$ kd helloWorld
hello world
你好,世界:一个用于展示编程语言基本语法和结构的示例程序。

$ kd getUserByID
get user by id
通过id获取用户

Compatibility

  • The feature is opt-in via split_camelcase_and_snakecase (default false); --generate-config emits the option.
  • IsEN detection, the en/ch schema and the daemon protocol are unchanged.
  • The whole-string short-query cache key stays lowercased, so existing cache entries keep hitting.
  • Fallback results are written to the long-text cache under the rewritten lowercase phrase. Old long-text keys that contain identifiers become orphans but are not deleted.
  • The fallback result reuses the primary query's counter channel so that an enabled freq_alert does not block on a long-text result.

Implementation notes

  • New pure helpers in pkg/str: SplitIdentifier, IdentifierPhrase, RewriteIdentifiers.
  • internal/query.go extracts the existing body into querySingle and adds the fallback orchestration in QueryWithProgress.
  • README updated with the new behavior.

Tests

Table-driven tests cover every splitting rule, the fallback decision and long-text rewriting.

gofmt clean, CGO_ENABLED=0 go test -count=1 ./..., go vet ./... and CGO_ENABLED=0 go build ./... all pass.

英文标识符式输入此前无法识别:`helloWorld` 归一化后被小写为
`helloworld` 而查不到,`hello_world` 因下划线不在 IsEN 字符集内落到
ch 表。现在:

- 短词查询先按整体查询,未找到时把标识符拆成空格分隔词组
  (如 helloWorld -> hello world),走长文本机器翻译通道展示。
- 长文本(-t)对整段中每个标识符 token 拆词重写后再翻译,重写无结果
  时退回原文重试。
- 拆分规则:连续大写视为整体、下划线切分并忽略空段、单字母保留、
  数字不单独成段;普通单词/词组/全大写词不触发。

不改 IsEN 判定、en/ch 表结构与 daemon 协议,短词整体查询键保持小写,
旧缓存继续命中。回退结果复用主查询的计数通道,避免 freq_alert 开启时
在长文本结果上永久阻塞。
@Karmenzind

Copy link
Copy Markdown
Owner

感谢贡献🙏,挺实用的功能
请增加一个配置项吧:split_camelcase_and_snakecase,默认为false,因为这个拆词处理贴近编程语境,可能会影响普通查询意图

@bighu630

Copy link
Copy Markdown
Contributor Author

已按建议补上配置项 split_camelcase_and_snakecase(默认 false)并推送到本分支。

关闭时短词回退与长文本拆词重写完全禁用,行为与本次改动前一致;开启后才会拆分。--generate-config 会输出该项,README 配置说明也已同步,并补了 config 默认值与生成/回读测试。

辛苦再看一下🙏

…akecase

拆词贴近编程语境,可能影响普通查询意图,因此改为默认关闭的配置项:

- Config 新增 SplitCamelcaseAndSnakecase(默认 false),
  toml 键为 split_camelcase_and_snakecase,会随 --generate-config 输出。
- QueryWithProgress 用该开关门控短词回退与长文本重写;关闭时行为与
  该功能引入前完全一致。
- README 配置示例与说明同步,并补 config 测试覆盖默认值与生成/回读。
@bighu630
bighu630 force-pushed the feat/identifier-splitting branch from 3d931ae to 9c419c0 Compare September 16, 2026 03:19
@Karmenzind
Karmenzind merged commit 88e7ea5 into Karmenzind:main Sep 16, 2026
4 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