IP Library › Granted Patent US 12,346,690
Granted Patent B2
US 12,346,690 · App. 18/113,663 · Granted Jul 1, 2025

Context aware code snippet recommendation

Inventors: Mu Qiao (San Jose, CA); Martin G. Keen (Durham, NC); Jeremy R. Fox (Austin, TX)
Assignee: International Business Machines Corporation
G06F8/71G06F8/73
View Patent ↗
Loading inventors, assignments & file history…
Monitor This Case
Get email alerts when status or documents change.
Order Certified Copies
Most orders are placed with the USPTO same day — all within 24 business hours.
Order via The Patent Place →
Pre-filled with this patent's details
Quick Facts
Patent No.
US 12,346,690
App. No.
18/113,663
Granted
Jul 1, 2025
Kind
B2
Abstract

A context aware code snippet recommendation method, system, and computer program product that includes generating comments for source code, learning semantically meaningful sentence embeddings based on the comments, and retrieving code snippets from the source code based on the learned semantically meaningful sentence embeddings.

Claims (28)

1. A computer-implemented context aware code snippet recommendation method, the method comprising:

ingesting a snippet of source code characters by an encoder-decoder model, wherein the encoder-decoder converts the characters to vectors;

translating, by the encoder-decoder model, the vectors into natural language comments;

learning semantically meaningful sentence embeddings based on the natural language comments and a comparison between the learned semantically meaningful sentence embeddings and a developer-provided comment that is converted into a sentence embedding, wherein the semantically meaningful sentence embeddings are learned by:

building a code file as a map between each code snippet of the source code and a corresponding generated natural language comment;

converting the code file to a natural language document; and

calculating the semantically meaningful sentence embeddings as vectors by inputting the code file through a Sentence-Bidirectional Encoder Representations from Transformers (BERT) model; and

retrieving code snippets from the source code based on the learned semantically meaningful sentence embeddings, wherein the code snippets are retrieved based on a comparison between the learned semantically meaningful sentence embeddings and a developer-provided comment that is converted into a sentence embedding.

2. The computer-implemented context aware code snippet recommendation method of claim 1 , wherein the comparison between the semantically meaningful sentence embeddings and the sentence embedding of the developer-provided comment is based on the code snippet corresponding to the semantically meaningful sentence embeddings having a largest cosine-similarity to the sentence embedding of the developer-provided comment.

3. The computer-implemented context aware code snippet recommendation method of claim 1 , wherein the comparison between the semantically meaningful sentence embeddings and the sentence embedding of the developer provided comment further includes utilizing project text descriptions in metadata and comments in a current code file that correspond to the developer-provided comment.

4. The computer-implemented context aware code snippet recommendation method of claim 1 , wherein an encoder-decoder model is utilized to generate the comments for the source code with an output of natural language documents including the generated comments.

5. The computer-implemented context aware code snippet recommendation method of claim 4 , wherein the generating repeatedly generates comments for a plurality of source codes to create a repository of the natural language documents.

6. The computer-implemented context aware code snippet recommendation method of claim 5 , wherein the semantically meaningful sentence embeddings include a numerical vector representation of the natural language documents.

7. The computer-implemented context aware code snippet recommendation method of claim 6 , wherein a developer comment for a code snippet is run through a model to generate an embedding including a numerical vector representation of the developer comment, and

wherein the numerical vector representation of the developer comment is compared with the semantically meaningful sentence embeddings in the repository to output a code snippet having the closest cosine-similarity to the numerical vector representation of the developer comment.

8. The computer-implemented context aware code snippet recommendation method of claim 1 , wherein a developer comment for a code snippet is run through the Sentence-Bidirectional Encoder Representations from Transformers (BERT) model to generate an embedding including a numerical vector representation of the developer comment, and

wherein the numerical vector representation of the developer comment is compared with the semantically meaningful sentence embeddings in the repository to output a code snippet having the closest cosine-similarity to the numerical vector representation of the developer comment.

9. The computer-implemented context aware code snippet recommendation method of claim 1 , embodied in a cloud-computing environment.

10. A context aware code snippet recommendation computer program product, the context aware code snippet recommendation computer program product comprising a computer-readable storage medium having program instructions embodied therewith, the program instructions executable by a computer to cause the computer to perform:

ingesting a snippet of source code characters by an encoder-decoder model, wherein the encoder-decoder converts the characters to vectors;

translating, by the encoder-decoder model, the vectors into natural language comments;

learning semantically meaningful sentence embeddings based on the comments, wherein the semantically meaningful sentence embeddings are learned by:

building a code file as a map between each code snippet of the source code and a corresponding generated natural language comment;

converting the code file to a natural language document; and

calculating the semantically meaningful sentence embeddings as vectors by inputting the code file through a sentence-Bidirectional Encoder Representations from Transformers (BERT) model; and

retrieving code snippets from the source code based on the learned semantically meaningful sentence embeddings, wherein the code snippets are retrieved based on a comparison between the learned semantically meaningful sentence embeddings and a developer-provided comment that is converted into a sentence embedding.

11. The context aware code snippet recommendation computer program product of claim 10 , wherein the comparison between the semantically meaningful sentence embeddings and the sentence embedding of the developer-provided comment is based on the code snippet corresponding to the semantically meaningful sentence embeddings having a largest cosine-similarity to the sentence embedding of the developer-provided comment.

12. The context aware code snippet recommendation computer program product of claim 10 , wherein the comparison between the semantically meaningful sentence embeddings and the sentence embedding of the developer-provided comment further includes utilizing project text descriptions in metadata and comments in a current code file that correspond to the developer-provided comment.

Assignments (1)
ASSIGNMENT OF ASSIGNOR'S INTEREST Recorded Feb 24, 2023
From: QIAO, MU; KEEN, MARTIN G.; FOX, JEREMY R.
To: INTERNATIONAL BUSINESS MACHINES CORPORATION
Reel/Frame 062797/0652 →
Continuity (1)
Related Publication 20240289124A1 · Aug 29, 2024
References Cited (33)
US 7197739B2 · Preston et al. · 2007 [cited by applicant]
US 9519464B2 · Dang · 2016 [cited by examiner]
US 10140101B2 · Allen et al. · 2018 [cited by applicant]
US 11074048B1 · Wilson-Thomas · 2021 [cited by examiner]
US 11157246B2 · Zhang · 2021 [cited by examiner]
US 11481211B1 · Karri · 2022 [cited by examiner]
US 11604626B1 · Sawant · 2023 [cited by examiner]
US 20160357519A1 · Vargas · 2016 [cited by applicant]
US 20180129544A1 · Ekambaram · 2018 [cited by examiner]
US 20210073632A1 · Iyer · 2021 [cited by examiner]
US 20210200958A1 · Liu · 2021 [cited by examiner]
CN 101930428A · 2010 [cited by applicant]
CN 108388425B · 2021 [cited by applicant]
WO WO201486101A1 · 2014 [cited by applicant]
WO WO202100512A1 · 2021 [cited by applicant]
WO WO202117025A1 · 2021 [cited by applicant]
Abid, Shamsa, Hamid Abdul Basit, and Shafay Shamail. “Context-aware code recommendation in Intellij IDEA.” Proceedings of the 30th ACM Joint European Software Engineering Conference and Symposium on the Foundations of S… [cited by examiner]
Gao, Zhipeng, et al. “I know what you are searching for: Code snippet recommendation from stack overflow posts.” ACM Transactions on Software Engineering and Methodology 32.3 (2023): pp. 1-42. (Year: 2023). [cited by examiner]
Tao, Chuanqi, et al. “Cram: Code recommendation with programming context based on self-attention mechanism.” IEEE Transactions on Reliability 72.1 (2022): pp. 302-316. (Year: 2022). [cited by examiner]
Gu, Xiaodong, Hongyu Zhang, and Sunghun Kim. “Deep code search.” Proceedings of the 40th International Conference on Software Engineering. 2018.pp. 933-944 (Year: 2018). [cited by examiner]
Lutellier, Thibaud, et al. “Coconut: combining context-aware neural translation models using ensemble for program repair.” Proceedings of the 29th ACM SIGSOFT international symposium on software testing and analysis. 20… [cited by examiner]
Verbert, Katrien, et al. “Context-aware recommender systems for learning: a survey and future challenges.” IEEE transactions on learning technologies 5.4 (2012): pp. 318-335. (Year: 2012). [cited by examiner]
Bruch, Marcel, Martin Monperrus, and Mira Mezini. “Learning from examples to improve code completion systems.” Proceedings of the 7th joint meeting of the European software engineering conference and the ACM SIGSOFT sym… [cited by examiner]
Mel, et al. “The NIST Definition of Cloud Computing”. Recommendations of the National Institute of Standards and Technology. Nov. 16, 2015. [cited by applicant]
Sifei Luan, Di Yang, Celeste Barnaby, Koushik Sen, and Satish Chandra. 2019. “Aroma: Code Recommendation via Structural Code Search”. Proc. ACM Program. Lang. 3, OOPSLA, Article 152 (Oct. 2019), 28 pages. https://doi.or… [cited by applicant]
Weisong Sun, Chunrong Fang, Yuchen Chen, Guanhong Tao, Tingxu Han, and Quanjun Zhang. 2022. “Code Search based on Context-aware Code Translation”. In 44th International Conference on Software Engineering (ICSE '22), May… [cited by applicant]
Zhangyin Feng, et al. “CodeBERT: A Pre-Trained Model for Programming and Natural Languages”, Research Center for Social Computing and Information Retrieval, Harbin Institute of Technology, China, The School of Data and … [cited by applicant]
Xiaodong Gul, Hongyu Zhang2, and Sunghun Kim1,3. 2018. “Deep Code Search”. In ICSE '18: ICSE '18: 40th International Conference on Software Engineering, May 27-Jun. 3, 2018, Gothenburg, Sweden. ACM, New York, NY, USA, 1… [cited by applicant]
Ruchir Puri, et al. IBM CodeNet: “A Large-Scale AI for Code Dataset for Learning a Diversity of Coding Tasks”, IBM Research, MIT-IBM Watson AI Lab, IBM Worldwide Ecosystems, Aug. 29, 2021. [cited by applicant]
Geert Heyman, Rafael Huysegems, Pascal Justen, and Tom Van Cutsem. 2021. “Natural Language-Guided Programming”. In Proceedings of the 2021 ACM SIGPLAN International Symposium on New Ideas, New Paradigms, and Reflections… [cited by applicant]
Mark Chen, et al. “Open AI Codex: Evaluating Large Language Models Trained on Code”, Jul. 14, 2021. [cited by applicant]
Manchanda Naman. “RNN in PyTorch”, retrieved from web https://www.kaggle.com/code/namanmanchanda/rnn-in-pytorch/notebook, dated Feb. 27, 2025, 7 pages. [cited by applicant]
Pham Hoang. “Text Classification Pytorch: Torchtext + LSTM”, retrieved from web https://www.kaggle.com/code/hoangpham51/text-classification-pytorch-torchtext-Istm, dated Feb. 27, 2025, 17 pages. [cited by applicant]
Cited By (1)
US 12,602,226