A Multi-Perspective Architecture for Semantic Code Search (2020)

Abstract: The ability to match pieces of code to their corresponding natural language descriptions and vice versa is fundamental for natural language search interfaces to software repositories. In this paper, we propose a novel multi-perspective cross-lingual neural framework for code–text matching, inspired in part by a previous model for monolingual text-to-text matching, to capture both global and local similarities. Our experiments on the CoNaLa dataset show that our proposed model yields better performance on this cross-lingual text-to-code matching task than previous approaches that map code and text to a single joint embedding space.

## introduction in semantic code search or retrieval, the user provides a natural language query, and the system returns a ranked list of relevant code snippets from a database or repository for that query. this task is usually performed using a matching model that computes the similarity between code snippets and natural language descriptions by mapping code and natural language embeddings into a common space where the distance between a piece of code and its corresponding description is small @xcite . but current models do not explicitly model any interactions between the code and the description until the final step when their global similarity is calculated. in this paper, we propose a novel multiperspective neural framework for code-text matching that captures both global and local similarities. we show that it yields improved results on semantic code search. we apply our model to the conala benchmark dataset @xcite , which consists of python code snippets and their corresponding annotations in english. we believe that our model could be applied to other programming languages as well. we have made our code publicly available for research purpose foot_0 . ## background semantic code search is a cross-modal ranking problem where items in one modality (code) need to be ranked according to how well they match queries in another (natural language). one standard way to compute the similarity of items drawn from two different modalities or languages is to map each modality into a common "semantic" vector space such that matching pairs are mapped to vectors that are close to each other. @xcite propose a code retrieval framework that jointly embeds code snippets and nl descriptions into a high dimensional embedding space such that the vectors representing a code snippet and its corresponding description have high similarity. a variety of different approaches for learning embeddings for code have been proposed. because source code is less ambiguous than natural language, there are ways to exploit the underlying structure of code to obtain better representations. wan et al. (2019); leclair et al. (2020) show that using features extracted from abstract syntax trees (ast's) and control flow graphs (cfg's) lead to creating better representations of code. @xcite ; @xcite show that asts represented as compact strings can be used to represent code. following these approaches, we developed a multi-modal framework that generates embeddings for code using both the code tokens and an ast representation. ## models we compare four models: a baseline model (ct) that only considers text and source code, a (cat) model that also includes embedding of abstract syntax trees, a multi-perspective model (mp) that leverages multi-perspective matching operations as defined in a bilateral multi-perspective model @xcite , and our mp-cat model that combines both mp and cat architectures. ## experiments the conala dataset the conala dataset @xcite has two parts, a manually curated parallel corpus of 2,379 training and 500 test examples, and a large automatically-mined dataset with 600k examples (which we ignore here). each example consists of a snippet of python code and its corresponding english description. pre-processing we pre-process the text representing both the source code and the natural language descriptions using sub-word regularization based on unigram language modeling @xcite transforms the original tokens into sequences of shorter (and hence more common) substrings. we use the sentencepiece library @xcite and follow the same approach as used by @xcite for the conala dataset. training procedure during training, we use triplets consisting of a code snippet, a correct description, and an incorrect description (obtained by ## conclusions in this paper, we consider the task of semantic code search or retrieval using a code-text similarity model. we propose mp-cat, a novel multiperspective deep neural network framework for this task. in contrast to previous approaches, the multiperspective nature of our model allows it to capture richer similarities between the two sequences.