用自然语言重写代码检索,提升效果并判断何时值得做
Do not copy and paste! Rewriting strategies for code retrieval

- 三种重写策略:风格转换、带自然语言的伪代码、完整自然语言转写
- 全量自然语言重写使最高指标提升0.51(CT-Contest上MoSE-18)
- 提出熵值诊断法,可低成本预判重写是否有效,适合轻量编码器
基于嵌入的代码检索常因编码器过度依赖表面语法而表现不佳。现有方法通过大模型将查询和代码库重写为标准化风格来缓解,但未解决两个关键问题:表征迁移程度多大有益,以及每次查询调用大模型是否值得。本文系统评估了三种重写策略:风格化重写、带自然语言的伪代码、完整自然语言转写,在六组CoIR基准、五种编码器、三种跨模型家族(Qwen、DeepSeek、Mistral)重写器下,分别采用联合查询-代码库(QC,在线)与仅代码库(C,离线)增强。首次直接评估带自然语言的伪代码与片段级自然语言作为检索表示,而非临时中间态。结果表明,全量自然语言重写结合QC带来最大增益(CT-Contest上MoSE-18的NDCG@10绝对提升+0.51),而仅代码库重写在90种配置中有56种导致性能下降(约62%)。本文引入两项诊断指标:Delta H(token熵差)、Delta s(嵌入余弦差),发现Delta H在所有三类重写器中均能预测QC下的检索增益:DeepSeek+Codestral组合下皮尔逊等级相关系数rho=+0.436(p<0.001),Codestral单独为rho=+0.593,Qwen为rho=+0.356。这确立了Delta H作为廉价、无需依赖重写器的代理指标,可用于决策是否进行重写。分析表明,大模型重写本质上是成本收益权衡——对以代码为主导的查询,适合作为轻量编码器的补救层,对强编码器或含大量自然语言的查询,回报递减。
原文摘要 · Abstract (English)
Embedding-based code retrieval often suffers when encoders overfit to surface syntax. Prior work mitigates this by using LLMs to rephrase queries and corpora into a normalized style, but leaves two questions open: how much representational shift helps, and when is the per-query LLM call justified? We study a hierarchy of three rewriting strategies: stylistic rephrasing, NL-enriched PseudoCode, and full Natural-Language transcription, under joint query-corpus (QC, online) and corpus-only (C, offline) augmentation, across six CoIR benchmarks, five encoders, and three rewriters spanning independent model families (Qwen, DeepSeek, Mistral). We are the first to evaluate NL-enriched PseudoCode and snippet-level Natural Language as direct retrieval representations, rather than as transient intermediates. Full NL rewriting with QC yields the largest gains (+0.51 absolute NDCG@10 on CT-Contest for MoSE-18), while corpus-only rewriting degrades retrieval in 56 of 90 configurations, about 62%. We introduce two diagnostics, Delta H, token entropy, and Delta s, embedding cosine, and show that Delta H predicts retrieval gain under QC across all three rewriter families: pooled Spearman rho = +0.436, p < 0.001 on DeepSeek+Codestral; rho = +0.593 on Codestral alone; rho = +0.356 on Qwen. This establishes Delta H as a cheap, rewriter-agnostic proxy for deciding when rewriting pays off before running retrieval. Our analysis reframes LLM rewriting as a cost-benefit decision: it is most effective as a remediation layer for lightweight encoders on code-dominant queries, with diminishing returns for strong encoders or NL-heavy queries.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。