让代码生成可控制风格,保持功能正确性
Style2Code: A Style-Controllable Code Generation Framework with Dual-Modal Contrastive Representation Learning
- 用对比学习对齐代码风格与语义结构
- 支持风格插值和用户个性化,生成准确率高
- 适合需要定制化代码的开发者使用
可控代码生成旨在合成符合特定风格但功能正确的代码,仍具挑战。本文提出两阶段训练框架,结合对比学习与条件解码实现灵活风格控制。第一阶段将代码风格表示与语义、结构特征对齐;第二阶段在已学习风格向量条件下微调语言模型(如Flan-T5)以引导生成。方法支持风格插值与轻量级混合,实现用户个性化。相比先前工作,该统一框架在不牺牲代码正确性的前提下提升风格控制能力。这是首个将对比对齐与条件解码结合用于风格引导代码生成的方法。
原文摘要 · Abstract (English)
Controllable code generation, the ability to synthesize code that follows a specified style while maintaining functionality, remains a challenging task. We propose a two-stage training framework combining contrastive learning and conditional decoding to enable flexible style control. The first stage aligns code style representations with semantic and structural features. In the second stage, we fine-tune a language model (e.g., Flan-T5) conditioned on the learned style vector to guide generation. Our method supports style interpolation and user personalization via lightweight mixing. Compared to prior work, our unified framework offers improved stylistic control without sacrificing code correctness. This is among the first approaches to combine contrastive alignment with conditional decoding for style-guided code generation.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。