Using Document Similarity Methods to create Parallel Datasets for Code Translation

Mayank Agarwal, Kartik Talamadupula, Fernando Martinez, Stephanie Houde, Michael Muller, John Richards, Steven I Ross, Justin D. Weisz

TL;DR

Supervised neural machine translation is powerful for migrating code between programming languages, but finding exact parallel code corpora across diverse languages is notoriously difficult. This paper introduces an automatic parallel corpus mining pipeline using document similarity metrics—specifically Word Mover’s Distance (WMD)—and reveals that code translation models are remarkably resilient to noisy training pairs, successfully enabling translation across 10 programming languages.

What Problem Does This Paper Address?

Modernizing legacy software (such as multi-decade-old COBOL systems powering banks and government platforms) costs billions of dollars and suffers from severe talent shortages. While natural language processing (NLP) techniques have recently been adapted to automate source-to-source translation, supervised models require parallel data: identical functionality implemented in both the source and target languages.

Unlike human languages, where translated books and parliamentary transcripts are abundant, parallel source code is exceedingly rare. As a result, prior work either restricted itself to a few curated pairs (like Java $\leftrightarrow$ C#) or relied on unsupervised neural machine translation, which struggles when languages differ drastically in structure or domain. The authors address this training bottleneck by asking: Can we automatically mine "noisy" parallel datasets from unaligned code repositories using document similarity?

Methods

The authors treat source files as text documents and formulate parallel corpus creation as a similarity-matching task:

  1. Corpus Mining Algorithm: Given two unaligned sets of programs in two languages, the algorithm computes pairwise document distances and pairs the most similar candidates under a strict distance threshold $\delta$, ensuring each document is paired at most once (1-to-1 mapping).
  2. Similarity Metrics Evaluated: The paper benchmarks traditional text-retrieval and semantic methods:
  3. Token-level / Lexical: TF-IDF, Okapi-BM25.
  4. Latent space / Topic modeling: Latent Dirichlet Allocation (LDA), Latent Semantic Indexing (LSI).
  5. Semantic distance: Word Mover's Distance (WMD), which uses word embeddings to calculate the minimum cumulative distance required to transport word distributions from one document to another.
  6. Supervised Fine-Tuning: Pre-trained sequence-to-sequence code models (CodeBERT, GraphCodeBERT, and PLBART) are fine-tuned on the automatically aligned, noisy datasets.

Datasets

The authors evaluate their approach on both existing curated benchmarks and a massive multi-language corpus:

  • Curated Parallel Benchmarks (Ground Truth):
  • Java $\leftrightarrow$ C#: 10,300 paired methods mined from open-source projects.
  • GeeksForGeeks (Java, C++, Python): 1,418 function-level algorithmic solutions per language pair.
  • In-the-Wild Mining Corpus:
  • Project CodeNet: A large-scale competitive programming dataset spanning 55 languages and ~4,000 problems. The authors sample 2,500 problem sets across 10 languages (C, C#, C++, Go, Java, JavaScript, PHP, Python, Ruby, and Scala) to mine new parallel datasets.

Key Insights and Ideas

  • Word Mover's Distance dramatically outperforms latent topic models: Latent models (LDA: 16–47% match accuracy; LSI: 57–88%) performed poorly because programming functions do not share continuous thematic "topics" like news articles. Instead, exact lexical semantics matter. WMD consistently achieved the highest match accuracy (89.5%–95.1%), gracefully handling different syntax while recognizing shared algorithmic operations and variables.
  • Code translation models are inherently noise-resistant: By systematically injecting misalignment noise (0% to 100%), the authors found that performance degrades gradually up to ~30% noise. GraphCodeBERT trained on a 30% misaligned Java $\leftrightarrow$ C# dataset lost only about 20 percentage points across metrics.
  • Why noise doesn't break code translation: In natural language, misaligned sentence pairs teach nonsense associations. In code, even if two paired functions solve slightly different tasks, structural invariants remain intact—variable declarations, function wrappers, control-flow blocks, and idioms are preserved, allowing models to learn cross-language syntax mappings effectively.

Key Results

  • Near-Ground-Truth Translation Quality:
  • On Java $\rightarrow$ C#, GraphCodeBERT trained on WMD-mined data achieved 79.47 BLEU, 83.72 CodeBLEU, and 59.0% Exact Match, nearly matching the model trained on pure ground truth (80.89 BLEU / 61.1% EM), and vastly outperforming a random alignment baseline (6.88 BLEU / 0.0% EM).
  • Scaling to 10 Languages:
  • Using PLBART fine-tuned on WMD-aligned CodeNet pairs, the authors evaluated Computational Accuracy @ 5 (CA@5, passing unit tests).
  • WMD-aligned models substantially outperformed problem-level random baselines across almost all language directions, achieving dramatic gains on pairs such as C $\rightarrow$ C++ (+33.05 CA@5), Python $\rightarrow$ C++ (+28.60), and Ruby $\rightarrow$ C++ (+23.25), as well as under-explored pairs like PHP $\rightarrow$ Ruby (+6.91) and JavaScript $\rightarrow$ C++ (+19.86).

Limitations and Future Work

  • Algorithmic vs. Enterprise Code: CodeNet consists of self-contained competitive programming solutions. It remains unverified how well this mining strategy transfers to complex enterprise codebases with extensive dependencies and boilerplate (e.g., enterprise COBOL).
  • Sequence Length Bounds: Due to the 512-token context window of PLBART, programs longer than 512 tokens were filtered out.
  • Computational Cost: Calculating WMD across millions of code pairs is computationally intensive, limiting practical scaling without optimization.
  • Future Directions: The authors suggest investigating computationally lighter similarity metrics, incorporating continuous similarity scores directly as sample loss weights during model training, and expanding to legacy industrial languages.

Summary updated

BibTeX
@inproceedings{agarwal2022using,
  author = {Mayank Agarwal and Kartik Talamadupula and Fernando Martinez and Stephanie Houde and Michael Muller and John Richards and Steven I Ross and Justin D. Weisz},
  title = {{Using Document Similarity Methods to create Parallel Datasets for Code Translation}},
  booktitle = {International Conference on Learning Representations},
  year = {2022},
  url = {https://openreview.net/forum?id=CO0ZuH5vaMu},
}

Loading related content...

Figure