A Bash code comment generation method based on dual information retrieval
Through dual information retrieval technology, combined with semantic and lexical similarity, the most similar code of the Bash code fragment is found and its comments are reused, which solves the problem of low efficiency of Bash code comment generation in the existing technology and realizes the generation and resource of high-quality comments. save.
Patent Information
- Application Number
- CN202111220663.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-10-20
- Publication Date
- 2024-08-20
- Estimated Expiration
- 2041-10-20
AI Technical Summary
Existing Bash code comment generation methods are inefficient, especially methods based on deep learning that require a lot of computing resources and time, and cannot effectively solve the problem of missing and mismatched code comments.
Using a method based on dual information retrieval, through semantic similarity and lexical similarity retrieval, we find the code fragments most similar to the target code from the corpus and reuse their annotations, use CodeBert and BERT-whitening models to extract semantic features, and use the FAISS library Perform vector retrieval and calculate lexical similarity combined with edit distance.
It improves the quality and efficiency of code annotations. Compared with the baseline method, the performance is significantly improved. It requires very little computing resources. It is simpler and more direct and can generate higher quality code annotations.
Smart Images

Figure CN113961237B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a Bash code comment generation method based on dual information retrieval. Background Art
[0002] Shell is the interface between developers and Linux operating system. The current Linux operating system supports different types of shells, among which Bash is the default shell command language of Linux and is widely used in the process of program development. As a scripting language, Bash usually includes three basic components: utilities (for example: find, mkdir, cd, grep), option tags (for example: -name, -i), and parameters (for example: "*.java", "TODO"). Compared with traditional programming languages such as C, Java, and Python, Bash has fewer usage scenarios, but its role in the development and maintenance of Linux systems cannot be ignored. In addition, Bash has the characteristics of wide application fields and flexible language. For developers who are not familiar with Bash, it is still difficult to use Bash code to complete development and maintenance tasks.
[0003] High-quality code comments can improve the readability and understandability of Bash code. Therefore, generating corresponding code comments plays an important role in Linux development and maintenance. However, due to limited project development budgets, developers' lack of programming experience, or failure to update comments synchronously when cloning code, there are often problems such as missing code comments and mismatches between code comments and code snippets. Therefore, there is an urgent need for an automatic method to generate relevant code comments for Bash code to assist developers in understanding Bash code.
[0004] At present, there is no method for automatically generating comments for Bash code. Among the existing methods for generating code comments, the early methods used information retrieval technology to generate code comments, but mainly used single-layer information retrieval to retrieve code comments, which was not very effective for Bash code. Recent studies modeled code comment generation as a neural machine translation problem, proposed a method based on deep learning, and achieved good results. However, this method usually requires a lot of computing resources to train the model, and requires a lot of time cost, and is inefficient.
[0005] How to solve the above technical problems becomes the subject faced by the present invention. Summary of the invention
[0006] The object of the present invention is to provide a Bash code comment generation method based on dual information retrieval, which can generate corresponding high-quality code comments according to Bash code snippets.
[0007] The idea of the present invention is: the present invention proposes a Bash code comment generation method based on dual information retrieval, that is, through semantic similarity retrieval and lexical similarity retrieval, the code snippet most similar to the target code is retrieved from the corpus, so as to reuse the code comment of the most similar code. The method proposed by the present invention achieves better performance than the benchmark method in the same corpus.
[0008] The present invention is achieved by the following measures: a Bash code comment generation method based on dual information retrieval, which includes the following steps:
[0009] (1) Collect data from the NL2Bash corpus and the data provided by the NLC2CMD competition to obtain a high-quality corpus, perform deduplication on the corpus, and obtain dataset D. Set the data format in dataset D to <code, comment>. The dataset contains data from 10,592 sources.
[0010] (2) Retrieve k candidate codes with the most similar semantics from the training set through semantic similarity retrieval, use CodeBert to extract the semantic information of Bash code, use the BERT-whitening model to reduce the dimension of the semantic vector, and calculate the semantic similarity between the target code segment and the code segment in the training set. The specific steps include:
[0011] (2-1) For a given Bash code segment, split it to obtain an input sequence;
[0012] (2-2) Input the sequence into CodeBert, extract the hidden states of the first and last layers in the output, sum them up and average them to obtain the final semantic feature vector X;
[0013] (2-3) Use the BERT-whitening model to process the semantic feature vector output by CodeBert, extract key features and reduce dimension through linear transformation, and convert the high-dimensional semantic feature vector X into a vector
[0014] (2-4) Calculate the vector of the target code segment through Euclidean distance and the vectors of the code snippets in the training set Get the semantic similarity between code segments The calculation formula is:
[0015] Where j is the vector dimension;
[0016] (3) Using the FAISS library as a vector retrieval tool, k code snippets with the most semantic similarity to the input code snippet are retrieved from the training set as candidate codes according to the calculated semantic similarity semantic_similarity;
[0017] (4) Retrieving the code fragment with the highest lexical similarity to the target code from the k candidate code fragments retrieved in step (3) through lexical similarity retrieval, specifically comprising the following steps:
[0018] (4-1) Treat the Bash code snippet as a set of tokens and split the code sequence based on spaces;
[0019] (4-2) performing a deduplication operation on the code sequence to obtain a word set of the code fragment;
[0020] (4-3) Calculate the edit distance dis between word set A and word set B by using the calculation method based on edit distance A,B ;
[0021] (4-4) Calculate the lexical similarity lexical_similarity(A,B) based on the calculated edit distance. The calculation formula is:
[0022] lexical_similarity(A,B)=1-dis A,B / max(len(A),len(B)), where len() represents the length of the set;
[0023] (4-5) according to the calculated lexical similarity lexical_similarity, retrieve the code fragment with the highest lexical similarity to the target code from the k candidate codes retrieved in step (3);
[0024] (5) After double information retrieval in step (3) and step (4), the code fragment most similar to the target code is retrieved from the training set, and the corresponding code annotation is reused as the corresponding annotation of the target code.
[0025] As a further optimization scheme of the Bash code comment generation method based on dual information retrieval provided by the present invention, the step (2) uses BERT-whitening in machine learning to further process the semantic vector, that is, the semantic vector extracted by CodeBert is subjected to key feature extraction and dimensionality reduction through linear transformation, thereby reducing the storage space, improving the model retrieval speed, and the training speed can be increased by more than two times.
[0026] The parameter settings of the Bash code comment generation method based on dual information retrieval are as follows:
[0027] Dimension d of BERT-whitening operation: 256;
[0028] The number of candidate code snippets for semantic similarity retrieval is k:8.
[0029] Compared with the prior art, the beneficial effects of the present invention are as follows: the Bash code comment generation method based on dual information retrieval proposed in the present invention takes into account the semantic similarity and lexical similarity of the code, and through dual information retrieval, finds the code segments most similar to the target code from the code library, and reuses their comments as the code comments of the target code. Previous code comment generation technologies were mostly based on deep learning or information retrieval. Compared with previous information retrieval-based methods, the method proposed in the present invention can better retrieve the most similar code through dual information retrieval and generate higher quality code comments. Compared with the method based on deep learning, the method proposed in the present invention uses only very few computing resources and is simpler, more direct and effective. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] The accompanying drawings are used to provide further understanding of the present invention and constitute a part of the specification. They are used to explain the present invention together with the embodiments of the present invention and do not constitute a limitation of the present invention.
[0031] Figure 1 A system framework diagram of a Bash code comment generation method based on dual information retrieval provided by the present invention.
[0032] Figure 2 This is a structural diagram of the semantic similarity retrieval according to an embodiment of the present invention.
[0033] Figure 3 This is a structural diagram of lexical similarity retrieval in an embodiment of the present invention. DETAILED DESCRIPTION
[0034] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. Of course, the specific embodiments described here are only used to explain the present invention and are not used to limit the present invention.
[0035] Example 1
[0036] See also Figure 1 As shown, the present invention provides a Bash code comment generation method based on dual information retrieval, which specifically includes the following contents:
[0037] (1) Collect data from the NL2Bash corpus and the data provided by the NLC2CMD competition to obtain a high-quality corpus. Duplicate data in the corpus is removed. The final corpus contains 10,592 pieces of data in the format <Bash code, natural language description>.
[0038] (2) Statistics are performed on the data in the corpus. Tables 1 and 2 respectively show the detailed statistical information on the lengths of code snippets and code comments in the corpus.
[0039] Table 1
[0040]
[0041] Table 2
[0042]
[0043] (3) To ensure a fair comparison with the baseline method, according to the data partitioning method in previous studies, 1,063 pairs of data are extracted from the corpus as the test set, and the remaining corpus is used as the training set.
[0044] (4) Input the target code: "find. -mindepth 1 -mmin -60 | xargs -r ls -ld", and calculate the semantic similarity between the code snippets in the training set and the target code: Use CodeBert to extract code semantic features, and perform key feature extraction and dimensionality reduction through BERT-whitening. Calculate the semantic similarity, and retrieve the top 8 code snippets with the highest semantic similarity to the target code as candidate codes. The semantic similarity retrieval process is as Figure 2 shown;
[0045] (4-1) Split the code segment according to spaces to obtain the input sequence where M is the length of the code sequence. Then input it into CodeBert to extract the first-layer hidden state h 0 and the last-layer hidden state h n , sum them and take the average to obtain the semantic feature vector of code X ∈ R d , where d is the dimension of CodeBert, set to 768. Finally, obtain the set of Bash code line vectors in the training set where N is the size of the training set;
[0046] (4-2) Further use BERT-whitening in machine learning to process the semantic vectors, and perform key feature extraction and dimensionality reduction through linear transformation, converting the 768-dimensional vector set into a 256-dimensional vector set By calculating the Euclidean distance between the semantic vectors of the target code segment and the code segment in the training set, the semantic similarity between the code segments is derived according to the semantic similarity calculation formula;
[0047] (4-3) Use the FAISS library as a vector retrieval tool to select the top 8 candidate similar codes in the training set based on semantic similarity;
[0048] (5) Calculate lexical similarity: treat the code snippet as a set of tokens and calculate the similarity between the sets using the text edit distance. The lexical similarity retrieval process is as follows: Figure 3 As shown;
[0049] (5-1) performing a deduplication operation on the code sequence to obtain a word unit set of the code fragment;
[0050] (5-2) Calculate the text editing distance between the target code and the candidate code;
[0051] (5-3) Derivation of the lexical similarity between codes based on the calculated text edit distance using the lexical similarity calculation formula;
[0052] (5-4) Retrieve the most similar code snippet from the candidate code based on lexical similarity, obtain the code comment corresponding to the code "list all files from the current directory tree that were modified less than 60 minutes ago", and then reuse the comment;
[0053] (6) The proposed method and existing code comment generation methods are evaluated on the same corpus, and three performance indicators (i.e., BLEU, METEOR, and ROUGE-L) from the field of neural machine translation are used to automatically evaluate the quality of the generated comments:
[0054] Table 4 Comparison of results between the method of the present invention and other methods
[0055]
[0056] Experiments show that the Bash code comment generation method based on dual information retrieval proposed in the present invention can generate higher quality code comments compared to 6 advanced benchmark methods based on deep learning and information retrieval. Specifically, the method of the present invention retrieves code snippets that are more similar to the target code from the training set through semantic similarity retrieval and lexical similarity retrieval, and this method can surpass these most advanced benchmark methods. Among them, for BLEU-1 / 2 / 3 / 4, the method of the present invention can at least improve the performance by 2.35%, 4.56%, 5.76%, and 6.35% respectively; for METEOR, the method of the present invention improves the performance by at least 2.74%; for ROUGE-L, the method of the present invention can improve the performance by at least 4.09%. These results show that the method proposed in the present invention has high competitiveness and advantages.
[0057] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
Claims
1. A Bash code comment generation method based on dual information retrieval, characterized in that: It includes the following steps: (1) Collect high-quality Bash code snippets and corresponding annotations as the original data to form an original corpus. Perform deduplication on this corpus to obtain a dataset D. Set the data format in dataset D as <Bash code, code annotation>. This corpus contains 10,592 Bash code data; (2) Use CodeBert to extract the semantic features of Bash code snippets, and perform key feature extraction and dimensionality reduction on the semantic feature vectors through BERT-whitening. Calculate the semantic similarity between Bash codes, and retrieve the top k Bash code snippets with the most similar semantics according to the semantic similarity ranking as candidate codes; (3) Regard the code snippets as sets composed of tokens, and calculate the lexical similarity between the sets through the text edit distance; (4) Among the k code snippets retrieved by semantic similarity, calculate the lexical similarity between the target code and the candidate codes, and retrieve the code snippet with the highest lexical similarity from the candidate codes; (5) According to the code snippets obtained by double retrieval, reuse the code annotation of this snippet as the code annotation of the target Bash code; In step (2), the code semantic information is extracted through CodeBert and BERT-whitening operations, which specifically includes the following steps: (2-1) For a given Bash code snippet, regard it as a text sequence, and split the sequence according to spaces to obtain an input sequence W; (2-2) Input the sequence W into CodeBert and extract the first hidden state h in the output 0 and the hidden state h of the last layer n , and take the average value of them to get the semantic feature vector X of the code segment; (2-3) Use BERT-whitening to process the semantic vector X, extract key features and reduce dimension through linear transformation, and convert the semantic feature vector X into a vector (2-4) By calculating the semantic vector of the target code segment and the semantic vector of the code snippet in the training set The Euclidean distance between them is used to calculate the semantic similarity between code segments. The formula for calculating semantic similarity is: (2-5) Use the FAISS library developed by FaceBook as a vector retrieval tool to retrieve the top k candidate similar codes in the training set according to the semantic similarity.
2. The Bash code comment generation method based on dual information retrieval according to claim 1 is characterized in that: In step (2), the code semantic information is extracted through CodeBert and BERT-whitening operations, which specifically includes the following steps: The hyperparameters of the model are as follows: The embedding dimension d of CodeBert: 768; The dimension K of the BERT-whitening operation: 256; When retrieving candidate codes according to the semantic similarity, the hyperparameter k is set to 8.
3. The Bash code comment generation method based on dual information retrieval according to claim 1 is characterized in that: In step (3), calculating the lexical similarity between code snippets specifically includes the following steps: (3-1) First, regard the code snippets as sets of tokens, and perform a deduplication deletion operation on the duplicate tokens in the code sequence to obtain sets A and B of the two code snippets; (3-2) Then calculate the text edit distance dis between the two sets A,B ; (3-3) The lexical similarity between the two code snippets is calculated based on the calculated text edit distance, lexical_similarity(A,B), and the calculation formula is: Where len() represents the size of the collection.
Citation Information
Cited By
Multi-agent-based Bash command annotation generation method
CN121597269A
