用函数到风格的引导提升代码翻译的准确性和可读性
Function-to-Style Guidance of LLMs for Code Translation
- 分两阶段训练:先学功能正确性,再学代码风格
- 新基准测试中,小模型超越大模型和GPT-4
- 适合需要高质量代码转换的开发者和研究者
大型语言模型在代码翻译任务上已取得显著进展,但确保翻译后代码的正确性和可读性仍是挑战,限制了其在真实软件开发中的应用。本文提出F2STrans,一种函数到风格的引导范式,通过两个关键阶段逐步提升模型性能:(1) 功能学习,利用从在线编程平台挖掘的高质量源-目标代码对优化翻译正确性;(2) 风格学习,结合正负风格示例提升代码可读性。此外,我们构建了一个新基准,包含最新源码、广泛测试用例及人工标注的真实翻译结果,支持功能与风格的全面评估。在新基准和现有数据集上的实验表明,该方法显著提升代码翻译性能。值得注意的是,在20种不同代码翻译场景下,Qwen-1.5B模型平均表现优于提示增强的Qwen-32B和GPT-4。
原文摘要 · Abstract (English)
Large language models (LLMs) have made significant strides in code translation tasks. However, ensuring both the correctness and readability of translated code remains a challenge, limiting their effective adoption in real-world software development. In this work, we propose F2STrans, a function-to-style guiding paradigm designed to progressively improve the performance of LLMs in code translation. Our approach comprises two key stages: (1) Functional learning, which optimizes translation correctness using high-quality source-target code pairs mined from online programming platforms, and (2) Style learning, which improves translation readability by incorporating both positive and negative style examples. Additionally, we introduce a novel code translation benchmark that includes up-to-date source code, extensive test cases, and manually annotated ground-truth translations, enabling comprehensive functional and stylistic evaluations. Experiments on both our new benchmark and existing datasets demonstrate that our approach significantly improves code translation performance. Notably, our approach enables Qwen-1.5B to outperform prompt-enhanced Qwen-32B and GPT-4 on average across 20 diverse code translation scenarios.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。