一句话:提出 TranCS,通过将代码翻译为自然语言并在机器指令级收集执行上下文,结合共享词表嵌入,显著提升代码搜索效果。
现有代码搜索技术通常用深度学习模型分别学习代码片段和查询的嵌入表示,并依赖抽象语法树、控制流图等结构特征。然而相同结构并不一定对应相同语义,且查询词与代码 token 使用不同词映射函数,导致同一词或 token 的嵌入发散。本文提出上下文感知代码翻译技术 TranCS,将代码片段翻译为自然语言描述;翻译在机器指令上进行,并通过模拟指令执行收集上下文信息。同时,设计共享词映射函数,用单一词汇表为翻译和查询生成嵌入。在 CodeSearchNet 语料上的 1000 条查询实验表明,TranCS 在 MRR 指标上较当前最优技术提升 49.31% 至 66.50%。
Code search is a widely used technique by developers during software development. It provides semantically similar implementations from a large code corpus to developers based on their queries. Existing techniques leverage deep learning models to construct embedding representations for code snippets and queries, respectively. Features such as abstract syntactic trees, control flow graphs, etc., are commonly employed for representing the semantics of code snippets. However, the same structure of these features does not necessarily denote the same semantics of code snippets, and vice versa. In addition, these techniques utilize multiple different word mapping functions that map query words/code tokens to embedding representations. This causes diverged embeddings of the same word/token in queries and code snippets. We propose a novel context-aware code translation technique that translates code snippets into natural language descriptions (called translations). The code translation is conducted on machine instructions, where the context information is collected by simulating the execution of instructions. We further design a shared word mapping function using one single vocabulary for generating embeddings for both translations and queries. We evaluate the effectiveness of our technique, called TranCS, on the CodeSearchNet corpus with 1,000 queries. Experimental results show that TranCS significantly outperforms state-of-the-art techniques by 49.31% to 66.50% in terms of MRR (mean reciprocal rank).
@inproceedings{paperbot6946,
title = {Code search based on context-aware code translation},
author = {Weisong Sun and Chunrong Fang and Yuchen Chen and Guanhong Tao and Tingxu Han and Quanjun Zhang},
booktitle = {ICSE},
year = {2022},
doi = {10.1145/3510003.3510140}
}