Nanyang Technological University
Semantic alignment between natural language and programming language
Abstract
dc:description.abstractBridging natural language and programming language is fundamental to translating human intentions into executable actions understandable by machines. Such alignment underpins human--AI collaborative software engineering, encompassing tasks such as code generation, retrieval, refactoring, documentation, and testing. Natural language and programming language inherently share a many-to-many mapping relationship: a natural language description may correspond to multiple valid code implementations, and a code snippet may be described through multiple linguistic expressions. However, training and evaluation datasets are commonly constructed as individual description--code pairs and are often used under a one-to-one supervision setting. In this thesis, we refer to this data-induced simplification as the one-to-one mapping assumption. Against this background, this thesis addresses three research questions. The first asks how contrastive learning can be improved from the positive- and negative-pair sides to achieve more efficient and effective representation alignment under the one-to-one mapping assumption. The second asks whether relaxing the one-to-one mapping assumption to account for the many-to-many mapping relationship benefits representation alignment. The third asks whether the many-to-many mapping relationship also benefits generative alignment. The capabilities of LLMs make it practical to synthesize alternative descriptions and code implementations for studying the many-to-many mapping relationship. To answer the first research question, we improve contrastive learning from both the positive- and negative-pair sides. On the positive-pair side, existing raw-data augmentation is resource-intensive and limited in variation. We therefore introduce representation-level augmentation, which directly augments representations and theoretically tightens the mutual-information lower bound of InfoNCE. On the negative-pair side, vanilla InfoNCE treats all negative pairs equally and ignores their potential relevance. We propose Soft-InfoNCE to differentiate negative pairs through reweighting, which controls the distribution of learned representations and reduces bias in mutual-information estimation. Experiments across multiple language models and programming languages demonstrate consistent improvements in code search. After improving representation alignment under the one-to-one mapping assumption, we relax this assumption to investigate whether addressing the many-to-many mapping relationship can further enhance representation alignment. We find that the generation-augmented retrieval framework often yields limited gains in code search because it fails to account for the many-to-many mapping relationship. The style variance of code poses challenges in representation alignment. By additionally rewriting the code for style normalization, the side effect is largely mitigated. Experimental results show that simply rewriting the code significantly enhances code search performance across multiple retrieval systems in diverse search scenarios, which indicates that addressing the many-to-many mapping relationship can enhance representation alignment. Finally, we retain the many-to-many mapping relationship and extend the investigation from representation alignment to generative alignment, which evaluates whether the output from language models is semantically aligned with the input. Yet it still remains unknown whether leveraging the many-to-many mapping relationship for data synthesis can further enhance generative alignment. We theoretically show that data that satisfies the many-to-many mapping relationship tends to have higher diversity, which benefits LLM training. And by iteratively translating between natural language descriptions and codes, we can construct training pairs that satisfy the relationship. Empirically, LLMs trained with such pairs achieve superior performance, particularly on more challenging benchmarks. Overall, this thesis demonstrates that semantic alignment can be improved by optimizing positive and negative relationships under the one-to-one mapping assumption and by modeling semantic variation under the many-to-many mapping relationship. The effectiveness observed in both representation alignment and generative alignment indicates the importance of accounting for mapping relationships when aligning natural language and programming language.
Degree
thesis:*- College dc:contributor.school
- College of Computing and Data Science
- Grantor dc:publisher
- Nanyang Technological University
- Year dc:date.issued
- 2026
Author and committee
dc:creator, dc:contributor.*- Author dc:creator
-
- Li, Haochen
- Advisor dc:contributor.supervisor
-
- Miao Chun Yan
Subjects
dc:subject × 1Rights
dc:rights- Statement dc:rights
-
- This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License (CC BY-NC 4.0).
- Language dc:language.iso
- en
Identifiers
dc:identifier.*- Identifier URI
- https://hdl.handle.net/10356/232765