用占位符替换长变量名,提升大段代码翻译准确率
LLM Based Long Code Translation using Identifier Replacement
- 用通用符号替换长变量名,降低模型负担
- 翻译后代码语法和结构保持完整,令牌数减少
- 适合处理超长代码片段的零样本翻译任务
在软件开发领域,大型语言模型(LLMs)被用于自动化代码翻译,即将一种编程语言的源代码转换为另一种语言并保持功能一致。然而,当源代码过长无法放入上下文窗口时,LLMs 常产生不准确的翻译。为此,我们提出一种新颖的零样本代码翻译方法,引入标识符替换机制。通过将用户提供的长标识符替换为通用占位符,该方法使 LLM 能聚焦于代码逻辑结构,降低令牌数量与内存消耗,从而提升长代码翻译的效率与成本效益。实验结果表明,该方法能有效保留代码的语法与层次信息,并生成更短的翻译结果。
原文摘要 · Abstract (English)
In the domain of software development, LLMs have been utilized to automate tasks such as code translation, where source code from one programming language is translated to another while preserving its functionality. However, LLMs often struggle with long source codes that don't fit into the context window, which produces inaccurate translations. To address this, we propose a novel zero-shot code translation method that incorporates identifier replacement. By substituting user-given long identifiers with generalized placeholders during translation, our method allows the LLM to focus on the logical structure of the code, by reducing token count and memory usage, which improves the efficiency and cost-effectiveness of long code translation. Our empirical results demonstrate that our approach preserves syntactical and hierarchical information and produces translation results with reduced tokens.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。