Code search method, system, and storage medium
By training a first search model and an annotation generation model to enhance semantic description from the code perspective, and combining a query enhancement model to enhance intent expression from the query perspective, the semantic gap between user queries and code snippets is resolved, improving the accuracy and reliability of code search.
Patent Information
- Application Number
- CN202410825734.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-25
- Publication Date
- 2025-12-16
- Estimated Expiration
- 2044-06-25
AI Technical Summary
In existing technologies, there is a semantic gap between user queries and code snippets, resulting in low accuracy and reliability of code search, which existing methods struggle to address effectively.
By training a first search model and an annotation generation model, semantic description is enhanced from the code perspective, and a query enhancement model is trained to enhance intent expression from the query perspective. Combining four vector representation sets for code search, the semantic gap is bridged and search performance is improved.
It effectively shortens the semantic gap between user queries and code snippets, improving the accuracy, effectiveness, and reliability of code search.
Smart Images

Figure CN118860482B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present disclosure relates to the technical field of artificial intelligence, and particularly relates to a code search method and system and a storage medium. BACKGROUND
[0002] In the process of software development, developers often use code search to improve development efficiency.
[0003] In the related art, a code snippet can be regarded as a common character text, so as to perform natural language character text matching between the character text and a user query, so as to obtain a code snippet corresponding to the user query, and then implement code search.
[0004] However, the code snippet is a text expressed in a programming language, and has specific keywords, syntax rules and specific program structures, while the user query is a text expressed in a brief natural language, and thus there is a semantic gap between the user query and the code snippet. Therefore, the method in the above related art has the technical problem of low search accuracy and reliability.
[0005] The content in the background section is only information known to the inventor personally, and does not represent that the above information has entered the public domain before the filing date of the present disclosure, nor does it represent that it can be prior art of the present disclosure. SUMMARY
[0006] The present disclosure provides a code search method, system and storage medium to solve at least one of the above technical problems.
[0007] In a first aspect, the present disclosure provides a code search method, comprising:
[0008] extracting a to-be-queried code snippet from a to-be-queried code base, and determining a first set of vector representations corresponding to the to-be-queried code snippet based on a first search model, wherein the first search model is trained to predict vector representations corresponding to a code snippet and vector representations corresponding to a query;
[0009] generating a to-be-queried semantic annotation corresponding to the to-be-queried code snippet based on an annotation generation model, and determining a second set of vector representations corresponding to the to-be-queried semantic annotation based on a second search model, wherein the annotation generation model is trained to predict a semantic annotation corresponding to a code snippet, and the second search model is trained to predict vector representations of a semantic annotation and vector representations corresponding to a query;
[0010] obtaining a user query input by a user;
[0011] determining an enhanced query corresponding to the user query based on a query enhancement model, wherein the query enhancement model is trained to convert a query into an alternative query with enhanced intent expression.
[0012] determining a third set of vector representations corresponding to the enhanced query based on the first search model, and determining a fourth set of vector representations corresponding to the enhanced query based on the second search model;
[0013] determining and outputting a target code snippet corresponding to the user query from the code snippets to be queried according to the first set of vector representations, the second set of vector representations, the third set of vector representations, and the fourth set of vector representations.
[0014] In some embodiments, the determining and outputting the target code snippet corresponding to the user query from the code snippets to be queried according to the first set of vector representations, the second set of vector representations, the third set of vector representations, and the fourth set of vector representations comprises:
[0015] calculating a first cosine similarity between the first set of vector representations and the third set of vector representations;
[0016] calculating a second cosine similarity between the second set of vector representations and the fourth set of vector representations;
[0017] determining and outputting the target code snippet according to the first cosine similarity and the second cosine similarity.
[0018] In some embodiments, the determining the target code snippet according to the first cosine similarity and the second cosine similarity comprises:
[0019] obtaining weight parameters corresponding to the first search model and the second search model respectively;
[0020] determining a cosine similarity score of the enhanced query corresponding to the code snippets to be queried according to the first cosine similarity, the second cosine similarity, and the weight parameters;
[0021] determining and outputting the target code snippet according to the cosine similarity score.
[0022] In some embodiments, the process of training the first search model, the second search model, and the query enhancement model comprises:
[0023] extracting a training data set from a source code file data set, the training data set comprising training code snippets and training description information for describing the training code snippets;
[0024] training the first search model, the second search model, and the query enhancement model according to the training data set.
[0025] In some embodiments, the first search model is trained according to the training data set, including:
[0026] The training description information is taken as a training query, and a first data set is generated, where the first data set represents query-code pairs of corresponding relationships between the training query and the training code snippet;
[0027] The first search model is trained based on the first data set.
[0028] In some embodiments, the second search model is trained according to the training data set, including:
[0029] The training semantic annotation corresponding to the training code snippet is generated based on an annotation generation model, and a second data set is generated, where the annotation generation model is trained to predict semantic annotations corresponding to code snippets, and the second data set represents query-annotation pairs of corresponding relationships between the training query and the training semantic annotation;
[0030] The second search model is trained based on the second data set.
[0031] In some embodiments, the query enhancement model is trained according to the training data set, including:
[0032] The training query is generalized to obtain a generalized training query, and a third data set is generated, where the third data set represents query-generalized query pairs of corresponding relationships between the training query and the generalized training query;
[0033] The query enhancement model is trained based on the third data set.
[0034] In some embodiments, the training query is generalized to obtain a generalized training query, including at least one of the following:
[0035] The training query is subjected to part-of-speech recognition to obtain part-of-speech classification information of the training query, and the training query is subjected to default processing based on the part-of-speech classification information;
[0036] The training query is subjected to synonym replacement processing.
[0037] In a second aspect, the present disclosure provides a code search system, including a processor and a memory connected in communication with the processor;
[0038] The memory stores computer execution instructions;
[0039] The processor executes the computer execution instructions stored in the memory to implement the method of any one of the first aspect.
[0040] In a third aspect, the present disclosure provides a processor-readable storage medium storing a computer program for causing a processor to perform the method of any one of the first aspect.
[0041] In a fourth aspect, the present disclosure provides a computer program product comprising a computer program for causing a processor to perform the method of any one of the first aspect when executed by the processor.
[0042] The code search method, system and storage medium provided by the embodiment include: extracting a to-be-queried code snippet from a to-be-queried code library, and determining a first vector representation set corresponding to the to-be-queried code snippet based on a first search model, wherein the first search model is trained to predict a vector representation corresponding to a code snippet and a vector representation corresponding to a query; generating a to-be-queried semantic annotation corresponding to the to-be-queried code snippet based on an annotation generation model, and determining a second vector representation set corresponding to the to-be-queried semantic annotation based on a second search model, wherein the annotation generation model is trained to predict a semantic annotation corresponding to a code snippet, and the second search model is trained to predict a vector representation of a semantic annotation and a vector representation corresponding to a query; obtaining a user query input by a user; determining an enhanced query corresponding to the user query based on a query enhancement model, wherein the query enhancement model is trained to convert a query into an alternative query with enhanced intent expression; determining a third vector representation set corresponding to the enhanced query based on the first search model, and determining a fourth vector representation set corresponding to the enhanced query based on the second search model; and determining and outputting a target code snippet corresponding to the user query from the to-be-queried code snippet according to the first vector representation set, the second vector representation set, the third vector representation set and the fourth vector representation set, so as to simultaneously perform semantic description enhancement from a code perspective and a user query perspective, and effectively solve the semantic gap problem between a user query and a code snippet and break through the performance limitation of code search. BRIEF DESCRIPTION OF DRAWINGS
[0043] The accompanying drawings, which are incorporated herein and form part of the specification, illustrate embodiments consistent with the present disclosure and, together with the description, further serve to explain the principles of the present disclosure.
[0044] Figure 1 A flowchart of the code search method of the present disclosure is shown in FIG. 1.
[0045] Figure 2 A principle diagram of the code search method of the present disclosure is shown in FIG. 2.
[0046] Figure 3 A principle diagram of the EMDQ model of the present disclosure is shown in FIG. 3.
[0047] Figure 4 A schematic diagram of a training principle of the QCCR model and the QACR model of the present disclosure;
[0048] Figure 5 A schematic diagram of an application of the code search method of the present disclosure;
[0049] Figure 6 A schematic diagram of a structure of the code search system of the present disclosure.
[0050] The specific embodiments of the present disclosure have been shown through the above-described drawings, and will be described in more detail hereinafter. The drawings and the written description are not intended to limit the scope of the present disclosure in any way, but to explain the concept of the present disclosure to those skilled in the art by referring to specific embodiments. DETAILED DESCRIPTION
[0051] The exemplary embodiments will be described in detail herein with reference to the attached drawings. In the following description, the same numbers are used to indicate the same or similar components. The embodiments described in the following exemplary embodiments are not meant to represent all embodiments consistent with the present disclosure. Rather, they are merely examples of apparatuses and methods consistent with some aspects of the present disclosure as detailed in the appended claims.
[0052] It should be understood that the terms "comprise" and "have", and any variations thereof, used in the embodiments of the present disclosure are intended to cover but not limited to, for example, the products or devices comprising a series of components, and can include other components not clearly listed or inherent to these products or devices.
[0053] In the embodiments of the present disclosure, the term "and / or" describes the association relationship of the associated objects, which means that there can be three relationships, for example, A and / or B can represent the three cases of A alone, A and B together, and B alone. The character " / " generally represents an "or" relationship between the associated objects before and after it.
[0054] In the embodiments of the present disclosure, the term "a plurality of" means two or more, and other quantifiers are similar.
[0055] The terms "first", "second", "third", and the like in the present disclosure are used to distinguish similar or similar objects or entities, and do not necessarily mean to limit the specific order or sequence, unless otherwise indicated. It should be understood that the terms used in this way can be interchanged under appropriate circumstances, for example, those other than the order given in the embodiments of the present disclosure or the description can be implemented.
[0056] The term "unit / module" used in the present disclosure refers to any known or later developed hardware, software, firmware, artificial intelligence, fuzzy logic, or a combination of hardware or / and software code capable of performing the functions associated with that element.
[0057] The technical solutions in the embodiments of the present disclosure will be described clearly and completely below in combination with the drawings in the embodiments of the present disclosure. Obviously, the described embodiments are only part of the embodiments of the present disclosure, not all the embodiments. Based on the embodiments in the present disclosure, all other embodiments obtained by those of ordinary skill in the art without creative labor are within the scope of protection of the present disclosure.
[0058] In the process of software development, developers often use code search to improve development efficiency. Code search refers to inputting a user query described in natural language to find matching code snippets from a code base (such as a pre-built code base to be queried) as search results returned to the user, providing reusable code snippets for the user.
[0059] In some related technologies, many research works use text retrieval techniques to treat code snippets as ordinary character texts, and directly match natural language character texts with user queries. However, code snippets are texts expressed in programming languages, with specific keywords, syntax rules, and specific program structures, while user queries are texts expressed in brief natural languages. There is a semantic gap between user queries and code snippets.
[0060] Therefore, such natural language character text retrieval-based methods ignore the program semantics and programming language syntax contained in code snippets, and have always been difficult to achieve good search performance.
[0061] In addition, user queries have problems such as query being too short, query words being inaccurate, and limited understanding of specific domain knowledge, which cannot effectively express the real query intention of the user.
[0062] In some other related technologies, there are also some code search methods based on extended queries, such as collecting questions and answers on programming forums, application programming interface (API) online documents, and community knowledge in code repositories to extend and refactor user queries to effectively understand and express the real query intention of the user.
[0063] However, these methods based on extended queries have limited effect, and the performance of code search does not show significant improvement, and even produces negative factors, because it is difficult to efficiently distinguish relevant words from irrelevant words among a large number of candidate extended words, thereby limiting the performance of code search.
[0064] It should be noted that the above related technical content is only the information known by the inventors personally, and does not represent that the above information has entered the public domain before the filing date of the present disclosure, nor does it represent that it can be prior art of the present disclosure.
[0065] To avoid at least one of the above technical problems, the present disclosure provides a code search method, system and storage medium, which is a bidirectional description enhancement technology for code search. Specifically, the semantic description enhancement is performed from the code perspective and the user query perspective, so as to effectively solve the semantic gap problem between the user query and the code snippet and break through the performance limitation of code search. That is, by using the bidirectional enhancement method from the code perspective and the query perspective, the semantic gap between the code snippet and the natural language query is shortened, and the performance of code search is improved.
[0066] Please refer to Figure 1 , Figure 1 is a flowchart of the code search method of the present disclosure. As shown in Figure 1 , the method comprises the following steps.
[0067] S101: Extracting a to-be-queried code snippet from a to-be-queried code library, and determining a first vector representation set corresponding to the to-be-queried code snippet based on a first search model, wherein the first search model is trained to predict a vector representation corresponding to a code snippet and a vector representation corresponding to a query.
[0068] Exemplarily, the execution subject of the present embodiment can be a code search system, which can be a server, a terminal device, a processor, a chip, etc., and will not be listed one by one here.
[0069] If the code search system is a server, the code search system can be a standalone server or a cluster server; it can be a cloud server or a local server, and the present embodiment does not make any limitation.
[0070] The to-be-queried code library is a database used to determine a code snippet corresponding to a user query. The to-be-queried code library includes a plurality of to-be-queried code snippets. The first vector representation set includes a plurality of first vector representations, and one vector representation corresponds to one to-be-queried code snippet.
[0071] S102: Generating a to-be-queried semantic annotation corresponding to the to-be-queried code snippet based on an annotation generation model, and determining a second vector representation set corresponding to the to-be-queried semantic annotation based on a second search model, wherein the annotation generation model is trained to predict a semantic annotation corresponding to a code snippet, and the second search model is trained to predict a vector representation of the semantic annotation and a vector representation corresponding to a query.
[0072] Exemplarily, one to-be-queried code snippet corresponds to one to-be-queried semantic annotation. The to-be-queried semantic annotation annotates the to-be-queried code snippet from the semantic dimension. The second vector representation set includes a plurality of second vector representations, and one second vector representation corresponds to one to-be-queried semantic annotation.
[0073] S103: Obtain a user query input by a user.
[0074] In combination with the above example, the user query can be understood as a query for requesting to query a code snippet corresponding to the user query from a to-be-queried code library.
[0075] S104: Determine an enhanced query corresponding to the user query based on a query enhancement model, the query enhancement model being trained to convert a query into a substitute query with enhanced expression of intent.
[0076] In comparison, the enhanced query can more accurately express the real query intent of the user than the user query.
[0077] S105: Determine a third vector representation set corresponding to the enhanced query based on a first search model, and determine a fourth vector representation set corresponding to the enhanced query based on a second search model.
[0078] S106: Determine and output a target code snippet corresponding to the user query from the to-be-queried code snippet according to the first vector representation set, the second vector representation set, the third vector representation set, and the fourth vector representation set.
[0079] Correspondingly, the code search system can determine a code snippet (i.e., a target code snippet) corresponding to the real intent of the user query from the to-be-queried code library in combination with the above four vector representation sets.
[0080] In combination with the above analysis, the first vector representation set and the second vector representation set are semantic description enhancement from the code perspective by the code search system, and the third vector representation set and the fourth vector representation set are semantic description enhancement from the query perspective by the code search system. Therefore, the code search system determines the target code snippet in combination with the above four vector representation sets, which can effectively solve the semantic gap problem between the user query and the code snippet, break through the performance limitation of code search, and thus improve the accuracy, effectiveness, and reliability of the search.
[0081] In some embodiments, the process of training the first search model, the second search model, and the query enhancement model includes the following steps 1 and 2:
[0082] Step 1: Extract a training data set from a source code file data set, the training data set including training code snippets and training description information for describing the training code snippets.
[0083] Exemplarily, the source code file dataset can be understood as a collection of data of at least one source code file. The training dataset can be understood as each code snippet obtained from the source code file dataset, and the description information (or code description) corresponding to each obtained code snippet. As can be known from the above analysis, since the embodiments of the present disclosure involve a training phase and an application phase, in order to distinguish the code snippets and the description information involved in different phases, the code snippets in the training phase shown in step 1 are referred to as training code snippets, and the description information is referred to as training description information, while the code snippets in S101 are referred to as code snippets to be queried. Therefore, other similar representations in the embodiments of the present disclosure will not be described here.
[0084] It should be noted that the execution subject for completing the training phase and the execution subject for performing code search can be the same or different. In order to facilitate understanding and description, the same subject, i.e., the code search system, is described in the embodiments.
[0085] As shown in Figure 2 , the code search system can extract code snippets and description information from the collected source code file dataset, thereby extracting training code snippets and corresponding training description information.
[0086] Step 2: training a first search model, a second search model, and a query enhancement model according to the training dataset.
[0087] Correspondingly, after the code search system obtains the training dataset for training each model, each model can be trained based on the training requirement.
[0088] In some embodiments, the first search model is trained according to the training dataset, including: taking the training description information as a training query, and generating a first dataset, wherein the first dataset represents a query-code pair of the corresponding relationship between the training query and the training code snippet; and training the first search model based on the first dataset.
[0089] Exemplarily, for each training description information, the code search system takes the training description information as a natural language query (for the sake of distinction, referred to as a training query), and generates a query-code pair (Q-C) by taking the training query and the training code snippet corresponding to the training description information, thereby generating a Q-C dataset (i.e., a first dataset), as shown in Figure 2 .
[0090] Correspondingly, the code search system can train the first search model based on the Q-C dataset.
[0091] For example, as shown in Figure 2As shown, the code search system can train a code search neural network (CRN) model based on the QC dataset to obtain the first search model (Query-Code Based Code Retrieval, QCCR). For example, as shown... Figure 2 The "QCCR model (CRN trained with QC)" is shown in the image.
[0092] In some embodiments, a second search model is trained based on training data, including:
[0093] The annotation generation model generates training semantic annotations corresponding to the training code snippets, and generates a second dataset. The annotation generation model is trained to predict semantic annotations corresponding to the code snippets, and the second dataset represents query-annotation pairs that represent the correspondence between training queries and training semantic annotations. A second search model is trained based on the second dataset.
[0094] For example, such as Figure 2 As shown, the code search system can use an annotation generation model to generate corresponding semantic annotations (i.e., training semantic annotations) for training code snippets, which also represent the semantics of the training code snippets. And as... Figure 2 As shown, the annotation generation model can be a code annotation generation neural network based on codeT5 (CodeT5CG).
[0095] In this embodiment, the code search system generates annotation information (i.e., training semantic annotations) that can express the semantics of the training code snippets through the annotation generation model. The annotations are used to supplement and enrich the semantic information of the code, so as to achieve the purpose of code description enhancement.
[0096] For example, for each training code snippet, the code search system can generate corresponding training semantic annotations based on the annotation generation model, and combine the training code snippet and its corresponding training semantic annotations into a query-annotation pair (QA), generating a QA dataset (i.e., the second dataset), such as... Figure 2 As shown.
[0097] Correspondingly, the code search system can train a second search model based on the QA dataset.
[0098] For example, such as Figure 2 As shown, the code search system can train the CRN model based on the QA dataset to obtain a second search model (Query-Annotation Based Code Retrieval, QACR). For example, if... Figure 2 The "QACR model (CRN trained with QA)" is shown in the image.
[0099] Based on the above analysis, it can be seen that both the QCCR and QACR models are trained from the CRN model. To better learn the end-to-end semantic relationships between code snippets and queries, this disclosure uses a Tree-Transformer to construct the CRN model.
[0100] The given CRN model takes a sequence of tokens as input, and these tokens vary depending on the training task. For example, as shown in the previous example, the token could be a query token corresponding to a training query, a semantic annotation token corresponding to a training semantic annotation, or a code snippet token corresponding to a training code snippet.
[0101] For example, a sequence of tokens for code snippet identifiers or a sequence of tokens for semantic annotation identifiers can be represented as w = {w1, ..., w...} n The process involves converting the word vectors into word embeddings and adding positional encoding information. Then, the word vectors are processed by a predefined weight matrix W. Q W K Transform into vector matrices Q and K, where n is an integer greater than 1.
[0102] To represent w in w i with w j For trends of the same component, the query vector q is used here. i And value vector k i+1 To calculate a fraction r i,i+1 As shown in Formula 1, where d1 represents the preset scaling factor.
[0103]
[0104] In the ingredient attention module, use a i,i+1 Indicates w i For w i+1 The probability of attention is determined, and the sum of the probability of attention for the same token to other tokens before and after it is 1. Then, using a... i,i+1 With a i+1,i Multiplication yields a sequence {p1, ..., p}. N}, where p i The w in the code snippet identifier i its neighbor w i+1 The probability of being the same component is shown in Formulas 2 and 3.
[0105] a i,i+1 =softmax(r i,i+1 r i,i-1 ) (Formula 2)
[0106]
[0107] The representation of the component prior C can be obtained from the sequence p, C ij The product of all sequences p from w i to w j can be derived from equation 4.
[0108]
[0109] In the CRN model, the calculation of the attention probability matrix E is shown in equation 5, which is affected by Q, K and the component prior C generated by the component attention module. Where d2 is a preset dot product scaling factor, and T is the transpose.
[0110]
[0111] After obtaining the component prior using the component attention layer and acting on the attention layer, the numerical values are normalized through residual connection and normalization to prevent the feature values from exceeding a reasonable range as the number of neural network layers increases. Then, input to the feedforward fully connected layer to enhance the model's fitting ability for complex processes.
[0112] During the training process of the CRN model, the code search system additionally selects an unrelated query for each query-code pair to form a combination of code snippets c, positive sample queries q + , and negative sample queries q - for CRN model training and minimizing the ranking loss function, where the loss function L(Θ) is shown in equation 6. Where Θ is the CRN model parameter, and β is a fixed marginal constraint (such as can take the value of 0.05).
[0113]
[0114] In some embodiments, the query enhancement model is trained according to the training data, including: performing generalization processing on the training query to obtain a generalized training query, and generating a third data set, wherein the third data set represents the query-generalized query pair corresponding relationship between the training query and the generalized training query; and training the query enhancement model based on the third data set.
[0115] For example, for a training query, the code search system performs generalization processing on the training query to obtain the corresponding generalized training query (i.e., generalized training query), and groups the training query and the generalized training query corresponding to the training query to form a query-generalized query pair (Q-GQ), and generates a Q-GQ data set (i.e., a third data set), as shown in Figure 2 .
[0116] Correspondingly, the code search system can train the query enhancement model based on the Q-GQ data set.
[0117] For example, as shown in Figure 2 , the code search system can train a query description enhanced neural network (EMDQ) model based on the Q-GQ dataset to obtain a query enhancement model (e.g., “Q-GQ trained EMDQ” as shown in Figure 2 ).
[0118] In some embodiments, the above-mentioned “generating generalized training queries by generalizing the training queries” includes at least one of the following:
[0119] 1) performing part-of-speech recognition on the training queries to obtain part-of-speech classification information of the training queries, and performing default processing on the training queries based on the part-of-speech classification information.
[0120] 2) performing synonym replacement processing on the training queries.
[0121] For example, the above-mentioned generalization processing includes the above-mentioned two ways.
[0122] The code search system can identify the part-of-speech of the words of the training queries, such as verbs, nouns, etc., through a part-of-speech parser, and classify the query words into API nouns, verbs or non-API nouns, adjectives or adverbs, and other words. In the word default process of the training queries, the adjectives or adverbs are defaulted one by one to obtain multiple queries to simulate the possible short query word problem of the training queries. In the synonym replacement process, the code search system can replace the verbs or non-API nouns with synonyms generated by a dictionary one by one to simulate the possible inaccurate query word problem of the training queries.
[0123] That is, the code search system can improve the accuracy and reliability of the generalized training queries through the above-mentioned generalization processing.
[0124] For example, the EMDQ model can be a model constructed based on a Transformer neural network. As shown in Figure 3 , the code search system can first extract code descriptions from the source code file dataset to obtain training description information corresponding to the training code snippets. Then, as shown in Figure 3 , the code search system can perform query preprocessing on the training description information, such as the part-of-speech analysis in the above-mentioned example. Next, as shown in Figure 3 , the code search system can sequentially perform query word default and synonym replacement to obtain generalized training queries. Among them, the generalized training queries include the respective generalized training queries corresponding to each training description information.
[0125] Please continue to refer to Figure 3The code search system can use a dataset including training description information and generalized training queries as a training set, and train the EMDQ model based on the training set. Correspondingly, when the code search system obtains a user query, it can input the user query into the trained EMDQ model and output an enhanced query (i.e., an augmented query).
[0126] In this embodiment, the code search system uses the query-generalized query to train the query understanding neural network model EMDQ, enabling the EMDQ model to learn and understand the semantics of user queries, identify and express the user's true query intent, and achieve the purpose of query description enhancement.
[0127] In some embodiments, S106 may include the following S1061 to S1063:
[0128] S1061: Calculate the first cosine similarity between the first vector representation set and the third vector representation set.
[0129] S1062: Calculate the second cosine similarity between the second vector representation set and the fourth vector representation set.
[0130] For example, in combination Figure 2 As can be seen from S101 above, the code search system can search for the code repository to be queried (such as...). Figure 2 The code snippets are extracted from the codebase shown, and corresponding vector representations are obtained based on the QCCR model, such as... Figure 2 The enhanced code V shown c The first vector representation set includes the enhanced code V. c .
[0131] Combination Figure 2 As shown in S102 above, the code search system can combine the annotation generation model and the QACR model to obtain the corresponding vector representation, such as... Figure 2 The enhanced code V shown a The second vector representation set includes the enhanced code V. a .
[0132] Combination Figure 2 As can be seen from S104 above, the code search system can determine enhanced user queries (i.e., enhanced queries) based on the EMDQ model.
[0133] Combination Figure 2 As can be seen from S104 above, the code search system can determine the vector representation corresponding to the enhanced user query based on the QCCR model and the QACR model respectively, such as obtaining... Figure 2 The enhanced query V shown q V q Including query Vq1 and V q2 . Wherein, the third vector representation set includes the enhanced query V q1 . The fourth vector representation set includes the enhanced query V q2 .
[0134] Correspondingly, after obtaining the above-mentioned enhanced vector representation set, the cosine similarity calculation can be performed. For example, the cosine similarity calculation between V c and V q1 ; the cosine similarity calculation between V a and V q2 .
[0135] S1063: determining and outputting the target code snippet according to the first cosine similarity and the second cosine similarity.
[0136] In some embodiments, S1063 can include S10631-S10633 as follows:
[0137] S10631: obtaining the weight parameter corresponding to the first search model and the second search model respectively.
[0138] S10632: determining the cosine similarity score of the enhanced query corresponding to the code snippet to be queried according to the first cosine similarity, the second cosine similarity, and the weight parameter.
[0139] The embodiment does not limit the weight parameter, which can be determined by the code search system based on demand, historical records, experiments, etc. The weight parameters of the first search model and the second search model can be the same or different. Generally, the weight parameters of the first search model and the second search model are different.
[0140] For example, combining the above-mentioned V Figure 2 , it can be known that the code search system can determine the cosine similarity score based on the similarity weighted calculation. For example, the code search system can determine the cosine similarity score S(Q, C) based on formula 7.
[0141] S(Q, C) = a * cos(V c , V q1 ) + (1-a) * cos(V a , V q2 ) Formula 7
[0142] Wherein, a is the weight parameter of the QCCR model, and (1-a) is the weight parameter of the QACR model.
[0143] S10633: determining and outputting the target code snippet according to the cosine similarity score.
[0144] For example, based on the above analysis, for each code snippet in the code repository to be queried, the code search system can calculate the cosine similarity score corresponding to that code snippet. This cosine similarity score represents the degree of correspondence between the code snippet and the user's query. For instance, the higher the cosine similarity score, the more likely the code snippet is to be the code snippet corresponding to the user's query.
[0145] In some embodiments, after obtaining each cosine similarity score, the code search system can obtain the highest cosine similarity scores from the cosine similarity scores and determine the code fragments corresponding to the highest cosine similarity scores as the target code fragments.
[0146] For example, a code search system can sort the cosine similarities, such as in ascending or descending order. If it's in ascending order, then... Figure 2 It can be seen that the code search system can select the first k in ascending order (i.e., Figure 2 The code snippets corresponding to the cosine similarity scores of "Tok k" shown in the figure are output as the target code snippets. Here, k is an integer greater than 1, such as k equals 10.
[0147] It is worth noting that, based on the above analysis, in the application stage described above, the code search system can calculate the cosine similarity score based on the output of the QCCR model and the QACR model, thereby obtaining the target code fragment.
[0148] And combined Figure 4 As can be seen, the inputs to the QCCR and QACR models have been described in detail previously and will not be repeated here. The augmented code output by the QCCR and QACR models is V. c′ and V a′ And during the training phase, such as Figure 4 As shown, the code search system can be based on enhanced code for V c′ and V a′ The parameters of the QCCR and QACR models are iteratively optimized until the QCCR and QACR models used in the training phase are obtained.
[0149] It is worth noting that in some embodiments, the code search method provided in this embodiment can be a plugin for a VSCode integrated development environment.
[0150] For example, in combination Figure 5 It can be seen that, Figure 5 The database shown can be the code repository in the example above, such as the code repository to be queried. During the offline process, based on the above analysis, it can be seen that data such as... can be extracted from the database.Figure 5 The code snippet shown in the middle.
[0151] In one aspect, the code snippet can be embedded into a code vector by the QCCR model, that is, to obtain the first vector representation set in the above example. On the other hand, the semantic annotation corresponding to the code snippet can be determined by the annotation generation model, and the semantic annotation can be embedded into an annotation vector by the QACR model, that is, to obtain the second vector representation set in the above example.
[0152] Continuing to refer to Figure 5 It can be known that, in the user query stage, the front-end tool can receive the input-user query of the user from the plug-in, and send the user query to the back-end interface. Then, the user query can be preprocessed, and the EMDO model can be used to understand the user query to generate an enhanced query (that is, the enhanced query in the above example), and the QCCR model and the QACR model can be used to embed the enhanced query into a vector, such as to obtain the third vector representation set and the fourth vector representation set in the above example. Then, the vectors obtained online are matched with the vectors stored in the database in terms of similarity (for details, refer to the principle of obtaining the cosine similarity score in the above example, which will not be described here), to obtain a code snippet result set (such as a target code snippet), and return to the front-end page, and all code snippet results (such as the target code snippet) are displayed in the plug-in page for the user to view and reuse the code.
[0153] In this embodiment, a plug-in of the VS Code code integrated development tool for code search is provided. By converting the code snippet and the semantic annotation into a vector representation in an offline process, the efficiency of online code search can be improved. The tool is implemented in the form of a plug-in, which is more in line with the habits of developers, and the search and reuse of code snippets can be directly performed in the development environment.
[0154] According to embodiments of the present disclosure, the present disclosure further provides a code search system, a readable storage medium, and a computer program product.
[0155] According to embodiments of the present disclosure, the present disclosure further provides a computer program product, which includes a computer program stored in a readable storage medium, at least one processor of an electronic device can read the computer program from the readable storage medium, and the at least one processor executes the computer program to enable the electronic device to perform the scheme provided in any of the above embodiments.
[0156] Figure 6A structural diagram of an example code search system 600 that can be used to implement embodiments of the present disclosure is shown. The electronic device is intended to represent various forms of digital computers, such as laptops, desktops, tablets, personal digital assistants, servers, blade servers, mainframes, and other appropriate computers. The electronic device can also represent various forms of mobile devices, such as personal digital assistants, cellular telephones, smartphones, wearable devices, and other similar computing devices. The components shown here, their connections and relationships, and their functions, are meant to be examples only, and are not intended to limit the implementations of the present disclosure described and / or claimed in this document.
[0157] As shown in Figure 6 The code search system 600 includes a computing unit 601 that can perform various appropriate actions and processes in accordance with a computer program stored in a read-only memory (ROM) 602 or a computer program loaded from a storage unit 608 into a random access memory (RAM) 603. In the RAM 603, various programs and data required for the operation of the code search system 600 can also be stored. The computing unit 601, the ROM 602, and the RAM 603 are connected to each other through a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0158] Various components in the code search system 600 are connected to the I / O interface 605, including an input unit 606, such as a keyboard, a mouse, etc.; an output unit 607, such as various types of displays, speakers, etc.; the storage unit 608, such as a magnetic disk, an optical disk, etc.; and a communication unit 609, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 609 allows the code search system 600 to exchange information / data with other devices through a computer network, such as the Internet, and / or various telecommunication networks.
[0159] The computing unit 601 can be various general and / or special purpose processing components with processing and computing capabilities. Some examples of the computing unit 601 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various specialized artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, a digital signal processor (DSP), and any appropriate processor, controller, microcontroller, etc. The computing unit 601 performs various methods and processes described above, such as the code search method. For example, in some embodiments, the code search method can be implemented as a computer software program tangibly embodied in a machine-readable medium, such as the storage unit 608. In some embodiments, part or all of the computer program can be loaded and / or installed onto the code search system 600 via the ROM 602 and / or the communication unit 609. When the computer program is loaded onto the RAM 603 and executed by the computing unit 601, one or more steps of the code search method described above can be performed. Alternatively, in other embodiments, the computing unit 601 can be configured to perform the code search method by any other appropriate means, such as by means of firmware.
[0160] Various implementations of the systems and techniques described above can be realized in digital electronic circuitry, integrated circuitry, a field programmable gate array (FPGA), an application specific integrated circuit (ASIC), a system on a chip (SOC), a complex programmable logic device (CPLD), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include implementation in one or more computer programs that are executable and / or interpretable on a programmable system including at least one programmable processor, which can be special or general purpose, coupled to receive data and instructions from, and to transmit data and instructions to, a storage system, at least one input device, and at least one output device.
[0161] Program code for carrying out methods of the present disclosure can be written in any combination of one or more programming languages. The program code can be provided to a processor or controller of a general purpose computer, special purpose computer, or other programmable data processing apparatus to produce a machine, such that the program code, when executed by the processor or controller, produces the functions / operations specified in the flowcharts and / or the block diagrams. The program code can be executed entirely on a machine, partially on a machine, partially on a machine and partially on a remote machine or entirely on a remote machine or server.
[0162] In the context of this disclosure, a machine-readable medium can be a tangible medium that contains or stores a program for use by or in connection with an instruction execution system, apparatus, or device. The machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can include but is not limited to an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any suitable combination of the foregoing. More specific examples of the machine-readable storage medium will include one or more lines of electrical connections, portable computer disks, hard disk drives, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or Flash memory), optical fibers, portable compact disc read-only memories (CD-ROMs), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.
[0163] To provide for interaction with a user, the systems and techniques described here can be implemented on a computer having a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user and a keyboard and a pointing device (e.g., a mouse or a trackball) by which the user can provide input to the computer. Other kinds of devices can be used to provide for interaction with a user as well; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form, including acoustic, speech, or tactile input.
[0164] The systems and techniques described here can be implemented in a computing system that includes a back end component (e.g., as a data server), or that includes a middleware component (e.g., an application server), or that includes a front end component (e.g., a user computer having a graphical user interface or a Web browser through which a user can interact with an implementation of the systems and techniques described here), or any combination of such back end, middleware, or front end components. The components of the system can be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include a local area network (LAN), a wide area network (WAN), and the Internet.
[0165] The computer system can include clients and servers. A client and server are generally remote from each other and typically interact through a communication network. The relationship of client and server arises by virtue of computer programs running on the respective computers and having a client-server relationship to each other. The server can be a cloud server, also known as cloud computing server or cloud host, which is a host product in the cloud computing service system, to solve the defects of large management difficulty and weak business scalability in traditional physical host and VPS (Virtual Private Server, or VPS for short) services. The server can also be a server of a distributed system, or a server combined with a blockchain.
[0166] Based on the above technical concept, the present disclosure further provides a processor-readable storage medium, which stores a computer program for causing a processor to execute the method according to any one of the above embodiments.
[0167] The processor-readable storage medium can be any available medium or data storage device that can be accessed by a processor, including but not limited to a magnetic storage (such as a floppy disk, a hard disk, a magnetic tape, a magneto-optical disk (MO), etc.), an optical storage (such as a CD, a DVD, a BD, a HVD, etc.), and a semiconductor memory (such as a ROM, an EPROM, an EEPROM, a NAND FLASH, a solid state disk (SSD)), etc.
[0168] Those skilled in the art should understand that the embodiments of the present disclosure can be provided as a method, a system, or a computer program product. Therefore, the present disclosure can take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present disclosure can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to magnetic disk storage and optical storage, etc.) containing computer-usable program code.
[0169] The present disclosure is described with reference to flowcharts and / or block diagrams of methods, apparatus (systems) and computer program products according to embodiments of the present disclosure. It should be understood that each flow and / or block in the flowcharts and / or block diagrams, as well as combinations of flows and / or blocks in the flowcharts and / or block diagrams, can be implemented by computer executable instructions. These computer executable instructions can be provided to a general purpose computer, a special purpose computer, an embedded processor, or other programmable data processing apparatus to produce a machine, so that the instructions executed by the computer or other programmable data processing apparatus produce a device that implements the functions specified in the flowcharts and / or block diagrams. Figure 1 An apparatus for performing each flow or a plurality of flows and / or blocks Figure 1 An apparatus for performing each flow or a plurality of flows and / or blocks An apparatus for performing each flow or a plurality of flows and / or blocks
[0170] These processor-executable instructions can also be stored in a processor-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the processor-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart Figure 1 one or more flows and / or blocks Figure 1 one or more blocks or multiple blocks.
[0171] These processor-executable instructions can also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the function specified in the flowchart Figure 1 one or more flows and / or blocks Figure 1 Figure 1 one or more blocks or multiple blocks.
[0172] Obviously, numerous modifications and variations of the present disclosure are possible in light of the above teachings. It is therefore to be understood that within the scope of the present disclosure, the disclosure can be practiced otherwise than as specifically set out herein. Accordingly, any one or more features of any embodiment are not essential to the practice of the disclosure.
Claims
1. A code search method, characterized in that, The method includes: Extract the code fragment to be queried from the code library to be queried, and determine the first vector representation set corresponding to the code fragment to be queried based on the first search model, wherein the first search model is trained to predict the vector representation corresponding to the code fragment and the vector representation corresponding to the query; The annotation generation model generates a semantic annotation corresponding to the code snippet to be queried, and the second search model determines a second vector representation set corresponding to the semantic annotation to be queried. The annotation generation model is trained to predict the semantic annotation corresponding to the code snippet, and the second search model is trained to predict the vector representation of the semantic annotation and the vector representation corresponding to the query. Obtain user queries input by the user; The query enhancement model determines the enhanced query corresponding to the user query, and the query enhancement model is trained to transform the query into an alternative query with enhanced intent expression. Based on the first search model, a third vector representation set corresponding to the enhanced query is determined, and based on the second search model, a fourth vector representation set corresponding to the enhanced query is determined; Based on the first vector representation set, the second vector representation set, the third vector representation set, and the fourth vector representation set, the target code segment corresponding to the user query is determined from the code segment to be queried and output. The step of determining and outputting the target code fragment corresponding to the user query from the code fragment to be queried based on the first vector representation set, the second vector representation set, the third vector representation set, and the fourth vector representation set includes: The first cosine similarity between the first vector representation set and the third vector representation set is calculated. The second cosine similarity between the second vector representation set and the fourth vector representation set is calculated. Obtain the weight parameters corresponding to the first search model and the second search model respectively; Based on the first cosine similarity, the second cosine similarity, and the weight parameter, the cosine similarity score between the enhanced query and the code segment to be queried is determined; The target code fragment is determined and output based on the cosine similarity score.
2. The method according to claim 1, characterized in that, The process of training the first search model, the second search model, and the query augmentation model includes: Extract a training dataset from the source code file dataset. The training dataset includes training code snippets and training description information for describing the training code snippets. The first search model, the second search model, and the query enhancement model are trained based on the training dataset.
3. The method according to claim 2, characterized in that, The first search model is trained based on the training dataset, including: The training description information is used as a training query to generate a first dataset, wherein the first dataset represents query-code pairs that correspond to the training query and the training code fragment; The first search model is obtained by training the first dataset.
4. The method according to claim 3, characterized in that, The second search model is trained based on the training dataset, including: Based on the annotation generation model, training semantic annotations corresponding to the training code snippets are generated, and a second dataset is generated. The annotation generation model is trained to predict semantic annotations corresponding to the code snippets, and the second dataset represents query-annotation pairs that represent the correspondence between the training queries and the training semantic annotations. The second search model is obtained by training the second dataset.
5. The method according to claim 3, characterized in that, The query enhancement model is trained based on the training dataset, including: The training query is generalized to obtain a generalized training query, and a third dataset is generated, wherein the third dataset represents the query-generalized query pair that represents the correspondence between the training query and the generalized training query. The query enhancement model is trained based on the third dataset.
6. The method according to claim 5, characterized in that, The generalization process performed on the training query to obtain a generalized training query includes at least one of the following: Part-of-speech tagging is performed on the training query to obtain the part-of-speech classification information of the training query, and the training query is processed by default based on the part-of-speech classification information; The training query is then subjected to synonym replacement processing.
7. A code search system, characterized in that, include: A processor, and a memory communicatively connected to the processor; The memory stores computer-executed instructions; The processor executes computer execution instructions stored in the memory to implement the method as described in any one of claims 1-6.
8. A processor-readable storage medium, characterized in that, The processor-readable storage medium stores a computer program for causing the processor to perform the method of any one of claims 1-6.
Citation Information
Patent Citations
Code search system and method based on pre-training model
CN117992572A
Semantic code search based on augmented programming language corpus
US20220236964A1