A method and system for creating code visualization representation based on word embedding

The method addresses inefficiencies in code visualization by using word embedding to generate structured visual representations, enhancing neural network learning and improving software engineering tasks.

CN118642753BActive Publication Date: 2024-12-03GUANGDONG UNIV OF TECH
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202410622117.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-05-20
Publication Date
2024-12-03
Estimated Expiration
2044-05-20

AI Technical Summary

Technical Problem

Existing code visualization methods suffer from high noise levels, large image sizes, and long rendering times, which affect the efficiency of downstream software engineering tasks, and fail to effectively utilize semantic information of code structure.

Method used

A method and system for creating code visualization representation using word embedding, involving preprocessing, segmenting code snippets into word-unit sequences, training a word-unit-vector mapping model, and generating embedding matrices to fill a visual block matrix based on word appearance order.

Benefits of technology

The method effectively encodes semantic information of code units, reducing noise and rendering time, and enhances the learning of code structure by neural networks, improving performance in tasks like code clone detection and classification.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118642753B_ABST
    Figure CN118642753B_ABST
Patent Text Reader

Abstract

The present invention discloses a method and system for creating a code visualization representation based on word embedding, the method comprising: S1, obtaining a code library to remove code comments in the code snippet to obtain a processed code snippet; formatting the processed code snippet to obtain a formatted code snippet; S2, establishing a word-value mapping table for all word-grams in the code library; S3, using the word-value mapping table to train a pre-built word-gram-vector mapping model to obtain a trained word-gram-vector mapping model; S4, transforming the embedding vectors corresponding to all word-grams in the code into embedding matrices corresponding to each word-gram; S5, generating a code visualization expression based on word embedding according to the order in which each word-gram appears in each word-gram sequence in the corpus. The present invention transforms a one-dimensional word embedding vector into a two-dimensional word embedding matrix, so that the elements in the visualization expression are efficiently utilized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of code semantic analysis, and more specifically to a method and system for creating a code visualization representation based on word embedding. Background Art

[0002] The purpose of code visualization is to generate images or image-like intermediate expressions for program source code, so as to extract visual features of source code using methods in the field of computer vision, and apply the visual features to downstream software engineering tasks, such as code clone detection, code classification, and code vulnerability prediction.

[0003] Currently, there are two main types of intermediate expressions for code visualization: one is the code image rendered by a code image rendering tool, and the other is an image encoded based on the code character value.

[0004] Code images are generated by rendering tools from program source code, and are directly recognizable as code. They are similar to the code that programmers see in an integrated development environment. Code images can be rendered in different ways, for example, monochrome code images can be generated, or visual information such as syntax highlighting can be added when rendering the code, as in current integrated development environments. Code images are the most intuitive visual representation of code, and include both character information and syntax information of the code.

[0005] An image or matrix encoded based on code character values ​​is a code visualization representation formed by using the code value of a character in a character set as a color channel value of a pixel point in a visualization image and encoding it in a certain order. For example, the ASCII value of a character in the code can be used as a channel value of the RGB channel of a single image pixel; after determining the image size, the image is encoded row by row, pixel by pixel, and channel by channel and in reverse order; for another example, the ASCII code of a single character can be used as the value of a pixel in a single-channel grayscale image to encode a single-channel grayscale image for the code.

[0006] Word embedding is a technology in the field of natural language processing. It relies on the corpus and uses a probabilistic model to model the word origin, and can train a word-to-vector mapping model. Since each element of the word embedding vector reflects the information of the different semantic dimensions of the word, the trained word-to-vector mapping model can convert the word into a word embedding vector.

[0007] In the process of solving software engineering tasks by using code visualization, the creation of code visualization representation is an important link. If a sufficiently effective code visualization intermediate expression cannot be created, the effect of downstream software engineering tasks will be affected. However, existing code visualization methods still have obvious shortcomings: on the one hand, the code visualization representation generation method based on code image rendering tools uses a large number of pixels to render each code character, which not only has a large amount of noise, but also may have the problem of too large image when the code segment is long, which will further cause the code image rendering time to be too long and reduce the efficiency of completing related software engineering tasks. On the other hand, most code visualization methods based on code character value encoding only retain the encoding value of each character and fail to effectively utilize semantic information such as the structure of the code. Therefore, the existing technology is not sufficient to generate a sufficiently effective code visualization intermediate expression to support the reliable completion of downstream software engineering tasks. Summary of the invention

[0008] In view of this, the purpose of the present invention is to provide a method and system for creating a code visualization representation based on word embedding, so as to solve the problems of a large amount of noise, too large images and long code image rendering time in the code visualization expression generation method in the prior art.

[0009] To achieve the above object, the present invention provides the following technical solutions:

[0010] The present invention provides a method for creating a code visualization representation based on word embedding, which is specifically:

[0011] S1. Obtain a code library and preprocess the code snippets in the code library, wherein the preprocessing includes removing code comments and formatting to obtain preprocessed code snippets;

[0012] S2, segmenting the preprocessed code snippet into word-unit sequences to obtain a corpus for training word embeddings, and establishing a word-unit-value mapping table for all word-units in the code base based on the corpus;

[0013] S3, using the corpus and the word-unit-value mapping table to train the pre-built word-unit-vector mapping model to obtain a trained word-unit-vector mapping model;

[0014] S4. Based on the trained word-to-vector mapping model, all words in the code base are generated with corresponding embedding vectors;

[0015] Transform the embedding vectors corresponding to all the words in the code into the embedding matrices corresponding to each word;

[0016] S5. According to the order in which each word in each word sequence in the corpus appears, the embedding matrix corresponding to each word is used to fill the visual block matrix of the code to generate a visual representation of the code based on word embedding.

[0017] Furthermore, in the step S1, the removing of code comments is specifically: removing the code comments by using a python script.

[0018] Furthermore, in the step S1, the formatting process is specifically: using Google Java Format to format the Java code in the code data, and using GNU Indent to format the C language code in the code data.

[0019] Furthermore, the formatted code snippets are segmented into word sequences, specifically: the code snippets in the code library are segmented into word sequences using regular expressions.

[0020] Furthermore, the embedding vectors corresponding to all word units in the code are transformed into embedding matrices corresponding to each word unit, specifically, indentation and line break processing for each word unit.

[0021] Furthermore, the number of blocks in each row and the number of blocks in each column of the visualization block matrix are set to fixed values ​​or determined according to the properties of the code itself.

[0022] The present application further provides a system for creating a code visualization representation based on word embedding, the system comprising:

[0023] The data preprocessing module is used to remove and format code comments in the code library, specifically: obtain the code library, preprocess the code snippets in the code library, the preprocessing includes removing code comments and formatting, and obtaining the preprocessed code snippets;

[0024] The preprocessed code snippets are divided into word-unit sequences to obtain a corpus for training word embeddings, and a word-unit-value mapping table is established for all word-units in the code base based on the corpus;

[0025] The word embedding module is used to train the word-to-vector mapping model and use the trained word-to-vector mapping model to generate embedding vectors for all words in the code base, which are then transformed into the embedding matrix corresponding to each word. Specifically:

[0026] The pre-built word-to-vector mapping model is trained using the corpus and the word-to-value mapping table to obtain a trained word-to-vector mapping model;

[0027] Based on the trained word-to-vector mapping model, all words in the code base are generated with corresponding embedding vectors;

[0028] Transform the embedding vectors corresponding to all the words in the code into the embedding matrices corresponding to each word;

[0029] Visual representation generation module: used to generate a code visualization expression based on word embedding for all the words in the corpus; specifically: according to the order of appearance of each word in each word sequence in the corpus, the code visualization block matrix is ​​filled with the embedding matrix corresponding to each word to generate a code visualization representation based on word embedding.

[0030] According to the specific embodiments provided by the present invention, the present invention has the following technical effects:

[0031] The present invention uses word units as units to perform visual expression of codes. Word embedding technology is used in word unit visualization to effectively encode the semantic information of word units, and by transforming the one-dimensional word embedding vector into a two-dimensional word embedding matrix, the elements in the visual expression are efficiently utilized. The generated word unit embedding matrix has a unified format and can be arranged regularly in the visual expression, which is conducive to the ResNet neural network learning the pattern of this visual representation. By adjusting the filling strategy of the word unit embedding matrix, significant indentation and line break signals can be generated in the embedding matrix, which helps to enhance the ResNet neural network's learning of code structure information. BRIEF DESCRIPTION OF THE DRAWINGS

[0032] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying creative work.

[0033] The following is a further description of the code visualization representation creation method and system based on word embedding of the present invention in conjunction with the accompanying drawings;

[0034] Figure 1 It is a general framework diagram of the code visualization representation creation method based on word embedding provided by the present invention;

[0035] Figure 2 It is a flow chart of generating a word unit embedding matrix in the code visualization representation creation method based on word embedding provided by the present invention;

[0036] Figure 3 It is a schematic diagram of code visualization generated in the first embodiment of the method for creating code visualization based on word embedding provided by the present invention;

[0037] Figure 4It is a schematic diagram of code visualization generated by Example 2 of the code visualization creation method based on word embedding provided by the present invention;

[0038] Figure 5 It is a schematic diagram of code visualization generated by Example 3 of the code visualization creation method based on word embedding provided by the present invention. DETAILED DESCRIPTION

[0039] The specific implementation of the present invention is further described in detail below in conjunction with the accompanying drawings and examples. The following examples are used to illustrate the present invention, but are not intended to limit the scope of the present invention.

[0040] In order to better understand the purpose, structure and function of the present invention, the present invention is further described in detail below in conjunction with the accompanying drawings.

[0041] The present invention provides a method for creating a code visualization representation based on word embedding, which is specifically expressed through five main processes:

[0042] (1) Remove the code comments in the code snippet and then format it; (2) Segment the formatted code snippet into word sequences to obtain a corpus, and establish a word-value mapping table based on the corpus; (3) Use the word-value mapping table to train a word-vector mapping model; (4) Based on the trained word-vector mapping model, generate word embedding vectors for all word sequences, and transform the word embedding vectors into a word embedding matrix; (5) Fill the word embedding matrix according to the order in which each word in all word sequences appears, and obtain a visual block matrix of the code: and give the following detailed description:

[0043] S1. Obtain a code library and preprocess the code snippets in the code library, wherein the preprocessing includes removing code comments and formatting to obtain preprocessed code snippets;

[0044] S2, segmenting the preprocessed code snippet into word-unit sequences to obtain a corpus for training word embeddings, and establishing a word-unit-value mapping table for all word-units in the code base based on the corpus;

[0045] S3, using the corpus and the word-unit-value mapping table to train the pre-built word-unit-vector mapping model to obtain a trained word-unit-vector mapping model;

[0046] It should be noted that the detailed process of training the pre-built word-to-vector mapping model using the corpus and the word-to-value mapping table is as follows:

[0047] 1. Construct training samples: Using the Skip-gram model, the training samples are composed of the input word and its context words. The context words can be determined by defining a window size. For example, for a given word, if the window size is set to 5, then its context words are the 5 words before and after it.

[0048] 2. Model construction: Establish a word-to-vector mapping model. The input layer of the model is a one-hot encoded vector whose dimension is equal to the size of the word-to-vector mapping table (vocabulary). The hidden layer is an embedding layer, which is used to map the input one-hot encoded vector to a low-dimensional word vector space. The output layer is a softmax layer, which is used to predict context words.

[0049] 3. Training model: The model is trained by performing forward propagation and back propagation on training samples. For each training sample, the input of the model is a one-hot encoded vector of the center word, and the goal is to predict the probability distribution of its context words. The model parameters are optimized by using optimization algorithms such as stochastic gradient descent (SGD), Adam, etc. to minimize the cross entropy loss between the predicted probability and the actual context words.

[0050] S4. Based on the trained word-to-vector mapping model, all words in the code base are generated with corresponding embedding vectors;

[0051] Transform the embedding vectors corresponding to all the words in the code into the embedding matrices corresponding to each word;

[0052] S5. According to the order in which each word in each word sequence in the corpus appears, the embedding matrix corresponding to each word is used to fill the visual block matrix of the code to generate a visual representation of the code based on word embedding.

[0053] The code comments are removed in step S1, specifically: the code comments are removed using a python script;

[0054] It should be noted that the Python script is written according to the comment specifications of C language or Java language, and is used to remove comments. The Python code is as follows: defremove_comments(code):

[0055] code=re.sub(r' / \*.*?\* / ',”,code,flags=re.DOTALL)

[0056] code=re.sub(r' / / .*',",code)

[0057] code=re.sub(r'\n\s*\n','\n',code)

[0058] return code

[0059] The layout of the processed code data is formatted, specifically: the Java code in the code data is formatted using Google Java Format, and the C language code in the code data is formatted using GNU Indent.

[0060] The step of segmenting the formatted code snippets into word sequences specifically includes segmenting the code snippets in the code library into word sequences using regular expressions.

[0061] It should be noted that the following is the regular expression code used to split the code snippets in the code library into word sequences:

[0062] deftokenize_code(code):

[0063] pattern=r'\b\w+\b|[-+* / =<>()[\]{};]|[\n\t]'

[0064] tokens=re.findall(pattern,str(code))

[0065] return tokens

[0066] The number of blocks in each row and the number of blocks in each column of the visualization block matrix are set to fixed values ​​and / or determined according to the properties of the code itself.

[0067] This application further provides a system for creating a code visualization representation based on word embedding:

[0068] The system comprises:

[0069] The data preprocessing module is used to remove and format code comments in the code library, specifically: obtain the code library, preprocess the code snippets in the code library, the preprocessing includes removing code comments and formatting, and obtaining the preprocessed code snippets;

[0070] The preprocessed code snippets are divided into word-unit sequences to obtain a corpus for training word embeddings, and a word-unit-value mapping table is established for all word-units in the code base based on the corpus;

[0071] The word embedding module is used to train the word-to-vector mapping model and use the trained word-to-vector mapping model to generate embedding vectors for all words in the code base, which are then transformed into the embedding matrix corresponding to each word. Specifically:

[0072] The pre-built word-to-vector mapping model is trained using the corpus and the word-to-value mapping table to obtain a trained word-to-vector mapping model;

[0073] Based on the trained word-to-vector mapping model, all words in the code base are generated with corresponding embedding vectors;

[0074] Transform the embedding vectors corresponding to all the words in the code into the embedding matrices corresponding to each word;

[0075] Visual representation generation module: used to generate a code visualization expression based on word embedding for all the words in the corpus; specifically: according to the order of appearance of each word in each word sequence in the corpus, the code visualization block matrix is ​​filled with the embedding matrix corresponding to each word to generate a code visualization representation based on word embedding.

[0076] The present invention further provides the following embodiments to illustrate the technical solutions in the present invention:

[0077] Embodiment 1:

[0078] In the method for creating a code visualization representation based on word embedding of the present invention, the number of rows and columns of the word embedding matrix of the visualization representation is variable, and the arrangement of the word embedding matrix depends on the code layout, and includes the following steps:

[0079] Step 1: For all code snippets C in the code base i Perform preprocessing to remove comments in the code snippet and use the code formatting tool to format all the code so that all code snippets in the code repository conform to the same format specification to obtain the processed code snippet C' i .

[0080] Step 2: Use the word segmentation tool to sort all the code snippets C' in the code repository i , divided into word sequence S i , to obtain a corpus for training word embeddings. At the same time, based on the corpus, a vocabulary-value mapping table (vocabulary) is established for all vocabulary in the code base to map vocabulary to numerical indexes (to digitize vocabulary).

[0081] Step 3: Using the corpus obtained in step 2 and the word-to-value mapping table, train a word-to-vector (word2vec) mapping model with an embedding dimension of d.

[0082] Step 4: Based on the word-to-vector mapping model, for each word w j Generate embedding vector v j Then, the embedding vector of each word is transformed into an a×b embedding matrix m j , where d = a×b.

[0083] Step 5: Create a visualization block matrix M for each code snippet in the code base i .M i is a block matrix with y×x blocks, each block is of size a×b.

[0084] Among them, create a visual representation M for a code snippet i The steps are as follows:

[0085] Step S1: Assuming the code snippet has r lines, replace the code snippet C' i , using behavior as the segmentation unit, and dividing into several word sequences s k ;

[0086] Step S2: Let y be the code fragment C i Let x be the number of tokens in the line with the most tokens in the code snippet, that is:

[0087] x=max(numtoken(s_1), numtoken(s_2),..., numtoken(s_r))

[0088] Step S3: For the matrix M i The nth matrix block of each row in , and the content of each matrix block, and the row word sequence s k The embedding matrix corresponding to the nth word is corresponding. The matrix blocks without corresponding are filled with zero matrices. When all rows are matched, the code segment C is obtained i Visualization matrix M i .

[0089] This embodiment uses word embedding technology to generate a visual representation of the code in units of words. The shape of the words in the visual representation is uniform, which is easy for the ResNet neural network to learn. The arrangement of words in the visual representation reflects the layout information of the code, which can be learned by the ResNet neural network as important information for solving software engineering tasks.

[0090] This example uses the code snippets in the Big Clone Bench dataset: (https: / / github.com / clonebench / BigCloneBench) to generate visual representations for the code snippets in the dataset, and then uses a neural network to extract features and classify them to complete the code clone detection task in software engineering. During implementation, the dataset is randomly divided into a training dataset, a validation dataset, and a test dataset in a ratio of 8:1:1. After training, the model is tested on the test dataset.

[0091] Using the visualization method in Example 1 and the ResNet neural network model, the scores of the four indicators of Accuracy, Precision, Recall, and F1 on the test data set can reach 94.33, 99.80, 93.06, and 96.32, while the scores of only 93.84, 98.20, 93.70, and 95.90 are achieved by using the method in "What You See is What it Means! Semantic Representation Learning of Codebased on Visualization and Transfer Learning" to complete this task, and the scores of only 92.39, 96.24, 94.13, and 95.17 are achieved by using the method in "Software visualization and deep transfer learning for effective soft-ware defect prediction" to complete this task, and the scores of only 93.68, 98.25, 93.74, and 95.94 are achieved by using the method in "Codegrid: A grid representation of code" to complete this task.

[0092] In Example 1, code snippets in the OpenJudge (OJ) data set (see the article "Convolutional neural networks over tree structures for programming language processing") are used to generate visual representations for the code snippets in the data set, and then neural networks are used to extract features and classify them to complete the code classification task in software engineering. During implementation, the data set is randomly divided into a training data set, a verification data set, and a test data set in a ratio of 8:1:1. After training, the ResNet neural network model is tested on the test data set.

[0093] Using the visualization method in Example 1 and the ResNet neural network model, the scores of the four indicators of Accuracy, Precision, Recall, and F1 on the test data set can reach 96.3596.5496.4096.43, while the scores of only 89.86, 89.99, 89.89, and 89.80 are obtained by using the method in "What You See is What it Means! Semantic Representation Learning of Code based on Visualization and Transfer Learning" to complete this task, and the scores of only 90.18, 90.30, 90.26, and 90.16 are obtained by using the method in "Software visualization and deep transfer learning for effective soft-ware defect prediction" to complete this task, and the scores of only 97.11, 97.14, 97.16, and 97.12 are obtained by using the method in "Codegrid: A grid representation of code" to complete this task.

[0094] In summary, the present invention can generate an effective word embedding-based visual representation for a piece of code by using a data preprocessing module, a word embedding module, an embedding matrix generation module and a visual representation generation module. The visual representation contains the embedding information of different word units and the layout information of the code. This information can be used by the ResNet neural network model to improve the performance of completing related software engineering tasks.

[0095] Embodiment 2:

[0096] The method for creating a code visualization representation based on word embedding of the present invention also includes the following method steps: Step 1: For all code snippets C in the code library i Perform preprocessing to remove comments in the code snippet and use the code formatting tool to format all the code so that all code snippets in the code repository conform to the same format specification to obtain the processed code snippet C' i .

[0097] Step 2: Use the word segmentation tool to sort all the code snippets C' in the code repository i , divided into word sequence S i , to obtain a corpus for training word embeddings. At the same time, based on the corpus, a vocabulary-value mapping table (vocabulary) is established for all vocabulary in the code warehouse to map vocabulary to numerical index (to digitize vocabulary).

[0098] Step 3: Use the corpus obtained in step 2 and the word-to-value mapping table to train a word-to-vector (word2vec) mapping model with an embedding dimension of d.

[0099] Step 4: According to the word-to-vector mapping model, for each word w j Generate embedding vector v j Then, the embedding vector of each word is transformed into an a×b embedding matrix m j , where d = a×b.

[0100] Step 5: Create a visual block matrix M for each code snippet in the code repository i .M i is a block matrix with y×x blocks, each block is of size a×b. x, y are manually specified values ​​and are not affected by code snippet C' i And change.

[0101] Among them, creating a visual representation M for a code snippet i The steps are as follows:

[0102] Step S1: Assuming the code snippet has r lines, replace the code snippet C' i , using behavior as the segmentation unit, and dividing into several word sequences s k

[0103] Step S2: For the matrix M i The nth matrix block of each row in , and the content of each matrix block, and the row word sequence s k The embedding matrix corresponding to the nth word is corresponding. The matrix blocks without corresponding blocks are filled with zero matrices. If the number of words in this row exceeds x, the words exceeding the number will be discarded. When all rows are matched, the code segment C is obtained. i Visualization matrix M i .

[0104] The experimental part of the second implementation is the same as that of the first implementation, and the effect of the second implementation is as follows: the code snippets in the Big CloneBench dataset (https: / / github.com / clonebench / BigCloneBench) are used to generate visual representations for the code snippets in the dataset, and then neural networks are used to extract features and classify them to complete the code clone detection task in software engineering. During implementation, the dataset is randomly divided into a training dataset, a validation dataset, and a test dataset in a ratio of 8:1:1. After training, the model is tested on the test dataset.

[0105] Using the visualization method in the second embodiment and the ResNet neural network model, the scores of the four indicators of Accuracy, Precision, Recall, and F1 on the test data set can reach 94.49, 99.81, 93.25, and 96.42, while using the method in "What You See is What it Means! Semantic Representation Learning of Code based on Visualization and Transfer Learning" to complete this task, the scores are only 93.84, 98.20, 93.70, and 95.90, using the method in "Software visualization and deep transfer learning for effective soft-ware defect prediction" to complete this task, the scores are only 92.39, 96.24, 94.13, and 95.17, and using the method in "Codegrid: A grid representation of code" to complete this task, the scores are only 93.68, 98.25, 93.74, and 95.94.

[0106] This second embodiment uses code snippets in the OpenJudge (OJ) data set (see the article "Convolutional neural networks over tree structures for programming language processing") to generate visual representations for the code snippets in the data set, and then uses neural networks to extract features and classify them to complete the code classification task in software engineering. During implementation, it is randomly divided into training data set, verification data set, and test data set in a ratio of 8:1:1. After training, the model is tested on the test data set.

[0107] Using the visualization method of the second embodiment and the ResNet neural network model, the scores of the four indicators of Accuracy, Precision, Recall, and F1 on the test data set can reach 97.22, 97.26, 97.23, and 97.20. However, using the method in "What You See is What it Means! Semantic Representation Learning of Code based on Visualization and Transfer Learning" to complete this task, the scores are only 89.86, 89.99, 89.89, and 89.80, using the method in "Software visualization and deep transfer learning for effective soft-ware defect prediction" to complete this task, the scores are only 90.18, 90.30, 90.26, and 90.16, and using the method in "Codegrid: A grid representation of code" to complete this task, the scores are only 97.11, 97.14, 97.16, and 97.12.

[0108] Embodiment three:

[0109] The method for creating a code visualization representation based on word embedding of the present invention also includes the following method steps: Step 1: For all code snippets C in the code library i Perform preprocessing to remove comments in the code snippet and use the code formatting tool to format all the code so that all code snippets in the code repository conform to the same format specification to obtain the processed code snippet C' i .

[0110] Step 2: Use the word segmentation tool to sort all the code snippets C' in the code repository i , divided into word sequence S i , to obtain a corpus for training word embeddings. At the same time, based on the corpus, a vocabulary-value mapping table (vocabulary) is established for all vocabulary in the code warehouse to map vocabulary to numerical index (to digitize vocabulary).

[0111] Step 3: Use the corpus obtained in step 2 and the word-to-value mapping table to train a word-to-vector (word2vec) mapping model with an embedding dimension of d.

[0112] Step 4: According to the word-to-vector mapping model, for each word w j Generate embedding vector v jThen, the embedding vector of each word is transformed into an a×b embedding matrix m j , where d = a × b.

[0113] Step 5: Create a visual block matrix M for each code snippet in the code repository i .M i is a block matrix with y×x blocks, each block is of size a×b.

[0114] A visual representation M is created for a code snippet i The steps are as follows:

[0115] Step S1: Count code snippets C' i The number of words n, let

[0116] Step S2: For the matrix M i The matrix blocks in S i The embedding matrix m corresponding to the word unit is filled row by row and column by column in the order of the word units in , and the remaining matrix blocks are filled with zero matrices.

[0117] The experimental part of this implementation three is the same as that of the implementation one, but the effect of this implementation three is as follows: the code snippets in the BigClone Bench dataset (https: / / github.com / clonebench / BigCloneBench) are used to generate visual representations for the code snippets in the dataset, and then neural networks are used to extract features and classify them to complete the code clone detection task in software engineering. During implementation, the dataset is randomly divided into a training dataset, a validation dataset, and a test dataset in a ratio of 8:1:1. After training, the model is tested on the test dataset.

[0118] Using the visualization method of the third embodiment and the ResNet neural network model, the scores of the four indicators of Accuracy, Precision, Recall, and F1 on the test data set can reach 94.58, 99.90, 93.29, and 96.48, while the method in "What You See is What it Means! Semantic Representation Learning of Code based on Visualization and Transfer Learning" can only achieve scores of 93.84, 98.20, 93.70, and 95.90 for this task, the method in "Software visualization and deep transfer learning for effective soft-ware defect prediction" can only achieve scores of 92.39, 96.24, 94.13, and 95.17 for this task, and the method in "Codegrid: A grid representation of code" can only achieve scores of 93.68, 98.25, 93.74, and 95.94 for this task.

[0119] This embodiment 3 uses the code snippets in the OpenJudge (OJ) data set (see the article "Convolutional neural networks over tree structures for programming language processing.") to generate visual representations for the code snippets in the data set, and then uses neural networks to extract features and classify them to complete the code classification task in software engineering. During implementation, it is randomly divided into training data set, verification data set, and test data set in a ratio of 8:1:1. After training, the model is tested on the test data set.

[0120] Using the visualization method of the third embodiment and the ResNet neural network model, the scores of the four indicators of Accuracy, Precision, Recall, and F1 on the test data set can reach 97.90, 98.00, 97.91, and 97.91, while the scores of only 89.86, 89.99, 89.89, and 89.80 are achieved by using the method in "What You See is What it Means! Semantic Representation Learning of Codebased on Visualization and Transfer Learning" to complete this task, and only 90.18, 90.30, 90.26, and 90.16 are achieved by using the method in "Software visualization and deep transfer learning for effective soft-ware defect prediction" to complete this task, and only 97.11, 97.14, 97.16, and 97.12 are achieved by using the method in "Codegrid: A grid representation of code" to complete this task.

[0121] The above description of the disclosed embodiments enables one skilled in the art to implement or use the present invention. Various modifications to these embodiments will be apparent to one skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features disclosed herein.

Claims

1. A method for creating a code visualization expression based on word embedding, characterized in that: The following steps are involved: S1. Obtain a code library and preprocess the code snippets in the code library, wherein the preprocessing includes removing code comments and formatting to obtain preprocessed code snippets; S2, segmenting the preprocessed code snippet into word-unit sequences to obtain a corpus for training word embeddings, and establishing a word-unit-value mapping table for all word-units in the code base based on the corpus; S3, using the corpus and the word-unit-value mapping table to train the pre-built word-unit-vector mapping model to obtain a trained word-unit-vector mapping model; S4. Based on the trained word-to-vector mapping model, all words in the code base are generated with corresponding embedding vectors; Transform the embedding vectors corresponding to all the words in the code into the embedding matrices corresponding to each word; S5. Fill the visual block matrix of the code with the embedding matrix corresponding to each word according to the order in which each word appears in each word sequence in the corpus, and generate a visual representation of the code based on word embedding; The number of blocks per row and per column of the visualization block matrix is ​​set to a fixed value or determined according to the properties of the code itself; The creation process of the code visualization is as follows: According to the order in which each word in each word sequence in the corpus appears, the embedding matrix corresponding to each word is filled row by row and column by column, and the remaining matrix blocks are filled with zero matrices.

2. The method for creating a code visualization representation based on word embedding according to claim 1, characterized in that: In the step S1, the code comments are removed by using a python script to remove the code comments.

3. The method for creating a code visualization representation based on word embedding according to claim 1, characterized in that: In the step S1, the formatting process specifically includes: using Google Java Format to format the Java code in the code data, and using GNU Indent to format the C language code in the code data.

4. The method for creating a code visualization representation based on word embedding according to claim 1, characterized in that: The step of segmenting the formatted code snippets into word sequences specifically includes segmenting the code snippets in the code library into word sequences using regular expressions.

5. The method for creating a code visualization representation based on word embedding according to claim 1, characterized in that: The method of transforming the embedding vectors corresponding to all word units in the code into the embedding matrices corresponding to each word unit includes: indentation and line break processing for each word unit.

6. A system for creating a visual representation of code based on word embedding, characterized in that: The system is applied to the method for creating a code visualization representation based on word embedding according to any one of claims 1 to 5, and the system comprises: The data preprocessing module is used to remove and format code comments in the code library, specifically: obtain the code library, preprocess the code snippets in the code library, the preprocessing includes removing code comments and formatting, and obtaining the preprocessed code snippets; The preprocessed code snippets are divided into word-unit sequences to obtain a corpus for training word embeddings, and a word-unit-value mapping table is established for all word-units in the code base based on the corpus; The word embedding module is used to train the word-to-vector mapping model and use the trained word-to-vector mapping model to generate embedding vectors for all words in the code base, which are then transformed into the embedding matrix corresponding to each word. Specifically: The pre-built word-to-vector mapping model is trained using the corpus and the word-to-value mapping table to obtain a trained word-to-vector mapping model; Based on the trained word-to-vector mapping model, all words in the code base are generated with corresponding embedding vectors; Transform the embedding vectors corresponding to all the words in the code into the embedding matrices corresponding to each word; The visualization generation module is used to generate a code visualization expression based on word embedding for all the words in the corpus. Specifically, according to the order in which each word appears in each word sequence in the corpus, the visualization block matrix of the code is filled with the embedding matrix corresponding to each word to generate a code visualization expression based on word embedding. The number of blocks per row and per column of the visualization block matrix is ​​set to a fixed value or determined according to the properties of the code itself; The creation process of the code visualization is as follows: According to the order in which each word in each word sequence in the corpus appears, the embedding matrix corresponding to each word is filled row by row and column by column, and the remaining matrix blocks are filled with zero matrices.

Citation Information

Patent Citations

  • Method and device for generating front-end code generation model

    CN111562915A

  • Tree serialization embedded software code recommendation method

    CN114647418A

  • Code abstract generation method and system based on KNN decoding enhancement

    CN115237424A

  • APT malicious sample attack organization tracing method based on fusion features

    CN117237688A