Any code snippet labeling method and retrieval system thereof
By combining multi-dimensional annotation and adaptive backtracking analysis in the code snippet retrieval system, the problem of low code snippet management and retrieval efficiency is solved, and the effect of high accuracy and efficient retrieval is achieved.
Patent Information
- Application Number
- CN202510035182.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-09
- Publication Date
- 2025-03-07
- Estimated Expiration
- 2045-01-09
AI Technical Summary
Due to the high complexity of code snippets in multi-dimensional feature annotation, the management efficiency and retrieval accuracy of code snippets are low.
By providing a labeling method and its search system for any code fragment, combining multi-dimensional annotation and adaptive backtracking analysis, the labeling accuracy and search efficiency of code fragments are improved.
It realizes high-accuracy annotation and efficient retrieval of code snippets, improving the efficiency of code management and optimization.
Smart Images

Figure CN119576388A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of machine learning, and in particular to a method for labeling arbitrary code snippets and a retrieval system thereof. Background Art
[0002] Open source software is widely used in software development. Many developers directly use code snippets of open source software in programming. In order to manage the risks brought by open source software, it is necessary to perform composition analysis (SCA) on the software. In the SCA analysis process, the identification and analysis of open source code snippets is very important. However, with the increasing complexity of software development, it becomes increasingly difficult to identify and manage these open source code snippets in the code.
[0003] On the other hand, modern software systems usually contain a large number of code snippets, which are repeatedly used in different functional modules, making code management, updating, optimization and retrieval an important issue in software engineering. Especially in large-scale distributed systems and multi-platform support environments, the requirements for code snippet annotation, classification, version management and performance optimization are particularly prominent.
[0004] In order to solve the above problems, we studied and proposed a method for annotating arbitrary code snippets and its retrieval system, thereby solving the problem that the traditional method lacks deep mining and intelligent analysis of the multi-dimensional features of code snippets, resulting in low efficiency in code recognition, reuse, error troubleshooting, and function optimization. Summary of the invention
[0005] The present application provides a method for labeling arbitrary code snippets and a retrieval system thereof, thereby solving the technical problem of low management efficiency and retrieval accuracy of code snippets due to the high complexity of code snippets in multi-dimensional feature labeling, and achieving the technical effect of improving the labeling accuracy and retrieval efficiency of code snippets by combining multi-dimensional labeling with adaptive backtracking analysis.
[0006] The present application provides a method for annotating arbitrary code snippets, which is applied to a retrieval system for arbitrary code snippets, including: establishing a code snippet database, activating a multidimensional feature annotation model, executing code snippet annotation, and establishing a first annotation result, wherein the code snippet annotation includes grammatical structure feature annotation, code style feature annotation, functional feature annotation, quality feature annotation, code performance feature annotation, and code language and platform feature annotation; calling an adaptive annotation network, using the adaptive annotation network to perform context backtracking analysis on the code snippets in the code snippet database, and establishing a second annotation result based on the context backtracking analysis result; performing static and dynamic fusion analysis on the code snippet database, establishing a joint functional similarity, and establishing a third annotation result based on the joint functional similarity; executing code version change annotation in the code snippet database to establish a fourth annotation result; and performing annotation retrieval management of the code based on the first annotation result, the second annotation result, the third annotation result, and the fourth annotation result.
[0007] The present application also provides a retrieval system for arbitrary code snippets, including: a code snippet annotation unit: establish a code snippet database, activate a multidimensional feature annotation model, execute code snippet annotation, and establish a first annotation result, wherein the code snippet annotation includes grammatical structure feature annotation, code style feature annotation, functional feature annotation, quality feature annotation, code performance feature annotation, code language and platform feature annotation; a context backtracking analysis unit: call an adaptive annotation network, use the adaptive annotation network to perform context backtracking analysis on the code snippets in the code snippet database, and establish a second annotation result based on the context backtracking analysis result; a fusion analysis unit: perform static and dynamic fusion analysis on the code snippet database, establish a joint functional similarity, and establish a third annotation result based on the joint functional similarity; a code version change annotation unit: execute code version change annotation in the code snippet database, and establish a fourth annotation result; an annotation retrieval management unit: perform annotation retrieval management of the code based on the first annotation result, the second annotation result, the third annotation result and the fourth annotation result.
[0008] One or more technical solutions provided in this application have at least the following technical effects or advantages:
[0009] The present application provides a method for labeling arbitrary code snippets and a retrieval system thereof, which relate to the field of machine learning technology, solve the technical problem of low management efficiency and retrieval accuracy of code snippets due to the high complexity of code snippets in multi-dimensional feature labeling, and achieve the technical effect of improving the labeling accuracy and retrieval efficiency of code snippets by combining multi-dimensional labeling with adaptive backtracking analysis. BRIEF DESCRIPTION OF THE DRAWINGS
[0010] In order to more clearly illustrate the technical scheme of the embodiment of the present application, the accompanying drawings of the embodiment of the present application will be briefly introduced below. A flow chart is used in the present application to illustrate the operations performed by the system according to the embodiment of the present application. It should be understood that the preceding or following operations are not necessarily performed accurately in order. On the contrary, various steps can be processed in reverse order or simultaneously as needed. At the same time, other operations can also be added to these processes, or a certain step or several steps of operations can be removed from these processes.
[0011] Figure 1 A flowchart of a method for marking arbitrary code snippets provided in an embodiment of the present application.
[0012] Figure 2 A schematic diagram of the structure of a system for retrieving arbitrary code snippets provided in an embodiment of the present application.
[0013] Explanation of the accompanying drawings: code snippet annotation unit 1, context backtracking analysis unit 2, fusion analysis unit 3, code version change annotation unit 4, annotation retrieval management unit 5. DETAILED DESCRIPTION
[0014] The above description is only an overview of the technical solution of the present application. In order to more clearly understand the technical means of the present application, it can be implemented in accordance with the contents of the specification. In order to make the above and other purposes, features and advantages of the present application more obvious and easy to understand, the specific implementation methods of the present application are listed below.
[0015] In order to make the purpose, technical solutions and advantages of the present application clearer, the present application will be further described in detail below in conjunction with the accompanying drawings. The described embodiments should not be regarded as limiting the present application. All other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of this application.
[0016] In the following description, reference is made to "some embodiments", which describe a subset of all possible embodiments, but it is understood that "some embodiments" may be the same subset or different subsets of all possible embodiments, and may be combined with each other without conflict, and the terms "first\second" involved are merely to distinguish similar objects and do not represent a specific ordering of objects. The terms "including" and "having" and any variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product, or server that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or modules that are not clearly listed or inherent to these processes, methods, products, or devices. Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those generally understood by technicians in the technical field of this application. The terms used herein are for the purpose of describing the embodiments of the present application only.
[0017] The embodiment of the present application provides a method for marking an arbitrary code fragment, and the method is applied to a retrieval system for an arbitrary code fragment, such as Figure 1 As shown, the method includes:
[0018] Step S100: Establish a code snippet database, activate a multidimensional feature annotation model, perform code snippet annotation, and establish a first annotation result, wherein the code snippet annotation includes grammatical structure feature annotation, code style feature annotation, functional feature annotation, quality feature annotation, code performance feature annotation, code language and platform feature annotation.
[0019] Specifically, first, a database containing a large number of code snippets is established. This code snippet database stores code snippets of various programming languages and different functional modules, with the purpose of providing comprehensive basic data for subsequent annotation and retrieval; when annotating the code snippets in the code snippet database, the system terminal will activate a pre-built multidimensional feature annotation model, which can identify multiple features of the code snippets and classify and annotate them through multiple channels including grammatical structure feature annotation channel, code style feature annotation channel, functional feature annotation channel, quality feature annotation channel, code performance feature annotation channel, code language and platform feature annotation channel, etc. The annotation content includes but is not limited to grammatical structure feature annotation, code style feature annotation, functional feature annotation, quality feature annotation, code performance feature annotation, code language and platform feature annotation; among them, grammatical structure feature annotation is to identify the grammatical structure of the code, such as variable definition, function declaration, control structure, etc., by analyzing the grammatical rules of the code. This helps to understand the basic syntax of the code and how to construct the logic; code style feature annotation is to evaluate whether the code style conforms to the specifications, such as naming rules, code indentation, comment specifications, etc., to ensure that the code meets the team's programming standards and is easy to read and maintain; functional feature annotation is to annotate according to the function and business logic of the code, such as for data processing, image recognition, network communication, etc., to quickly identify the actual use of the code snippet; quality feature annotation is to evaluate the quality of the code, including the readability, maintainability, modularity, etc. of the code, to indicate whether the code snippet conforms to best practices and whether it is easy to expand or modify; code performance feature annotation is to evaluate the execution efficiency of the code snippet, such as running time, memory usage, computational complexity, etc., in order to understand performance bottlenecks; code language and platform feature annotation is to annotate according to the programming language (such as Python, Java, C++, etc.) and platform (such as Web, mobile, embedded systems, etc.) used by the code, so as to quickly locate and apply it in different environments; finally, the system terminal will generate a first annotation result, which is the specific annotation and classification of each code snippet in these dimensions, to help subsequent retrieval, optimization and management.
[0020] Specifically, for the multidimensional feature annotation model, the system terminal constructs grammatical structure feature annotation channels, code style feature annotation channels, functional feature annotation channels, quality feature annotation channels, code performance feature annotation channels, code language and platform feature annotation channels, and integrates these channels in parallel to form this multidimensional feature annotation model. These channels are constructed in the same way and can be constructed based on deep neural networks, recurrent neural networks, BERT, CodeBERT, graph neural networks, etc.; taking CodeBERT as an example, the system terminal first obtains sample code snippet data groups and sample grammatical structure feature data groups from the sample annotation library. These sample data will serve as the basis for subsequent training; then use C CodeBERT structures the grammatical structure feature annotation channel. CodeBERT is a pre-trained model based on the Transformer architecture, specially designed for programming languages, which can understand the grammatical structure and contextual information in code snippets. By pre-training on a large-scale programming corpus, CodeBERT has mastered the common patterns and structures in programming languages, so it performs very well in code annotation tasks. In order to perform more accurate grammatical structure feature annotation, the system terminal processes the sample code snippet data group and uses CodeBERT's tokenizer to convert the code into token sequences, which will be passed as input to the CodeBERT model.In this process, the tokenizer converts the identifiers, keywords, operators, function calls and other elements in the code snippet into a format that the model can process. At the same time, in order to ensure the consistency of the input length, the system terminal will truncate the longer code snippets and pad the shorter code snippets to adapt to the maximum input length of the model (such as 512 tokens). In addition, the system terminal also generates an attention mask to indicate which parts are padding data and which are valid data, thereby ensuring that the model only pays attention to the actual code content and ignores the padding part. Subsequently, the sample grammatical structure feature data group corresponding to the sample code snippet data group is converted into a one-hot encoding form as the target label for model training. During the training process, the output of CodeBERT is the contextual representation of each token, which is connected through a fully connected layer. layer) is mapped to the corresponding grammatical feature category; the training goal is a multi-classification task, and the model needs to predict the grammatical category to which each token belongs; to this end, the system terminal uses the cross-entropy loss function to measure the gap between the predicted value and the actual label and optimize the model parameters; in order to improve training efficiency and accuracy, the optimizer usually uses the Adam optimizer to dynamically adjust the learning rate for better convergence; during the model training process, the system terminal uses the code snippet and its corresponding annotated data for supervised learning; during training, the model generates context-related representations based on the input code snippet, and predicts the grammatical feature category of each token through forward propagation, and then uses the cross-entropy loss function to calculate the loss value between the predicted result and the actual label; then, the gradient of the loss value is calculated through back-propagation and the model parameters are updated; through multiple rounds of training, the model gradually learns to how to accurately annotate the grammatical structure of code snippets according to the context; after training, the model can generate accurate annotation results and annotate the grammatical features of each token, such as function definitions, local variables, conditional statements, etc.; in the model evaluation stage, the system terminal measures the performance of the model by calculating the accuracy, precision, recall and F1 score on data not used for training. Especially for the grammatical feature annotation task, the F1 score is an important indicator, which can balance the precision and recall, and is especially suitable for cases of category imbalance; the system terminal can also use cross-validation to evaluate the generalization ability of the model, avoid overfitting, and further optimize the performance of the model through hyperparameter tuning; after the evaluation is passed, the system terminal uses the trained CodeBERT model as the final grammatical structure feature annotation channel to help the system terminal quickly understand the structure of the code.
[0021] Specifically, based on the same method as above, the system terminal obtains other sample data from the sample annotation library, and constructs code style feature annotation channels, functional feature annotation channels, quality feature annotation channels, code performance feature annotation channels, code language and platform feature annotation channels; then, all annotation channels are connected in parallel to form a multidimensional feature annotation model. Each channel in this model will analyze and annotate different code features; the system terminal inputs the code snippet into the multidimensional feature annotation model, and the multidimensional feature annotation model will flow the received code snippet into multiple internal feature annotation channels respectively. Each channel is analyzed and annotated independently, and finally generates its own annotation results; the annotation results of each channel will be merged by the multidimensional feature annotation model and output as the first annotation result, providing strong support for code management and optimization.
[0022] Step S200: calling an adaptive annotation network, using the adaptive annotation network to perform context backtracking analysis on code snippets in a code snippet database, and establishing a second annotation result according to the context backtracking analysis result.
[0023] Specifically, after obtaining the first annotation result, the system terminal will call the pre-built adaptive annotation network. This adaptive annotation network has an adaptive annotation logic inside, which is divided into a call record acquisition step, a similar usage clustering step and a context backtracking trust identification step. Through these steps, the system terminal can automatically annotate the code snippet database according to different context information, thereby generating a context backtracking analysis result, which includes the usage, performance and trust of the code snippet in different contexts; based on the result of the context backtracking analysis, the adaptive annotation network will annotate the code snippet, that is, annotate the content in the context backtracking analysis result to the corresponding code snippet to form a second annotation result; through this analysis process, the context information of the code snippet is fully mined, and the annotation result is more comprehensive, which can accurately reflect the actual performance of the code snippet in different environments, and provide strong support for subsequent code management, tuning and reuse.
[0024] Furthermore, step S200 of the present application includes:
[0025] Step S210: Obtain the call record of the code snippet in the code snippet database, wherein the call record includes a usage context and usage feedback; Step S220: Perform similar usage clustering according to the usage context in the call record, and establish a similar usage clustering result; Step S230: Time-mark the usage context, and use the time mark to construct a backtracking trust coefficient, perform a context backtracking trust identification of the similar usage clustering result through the backtracking trust coefficient and the usage feedback, and use the context backtracking trust identification to construct a context backtracking analysis result.
[0026] Specifically, in the adaptive annotation network, the system terminal will obtain the call records of the code snippet in the database. These call records include not only the basic information of the use of the code snippet, but also the use context and use feedback; among them, the use context refers to the running environment and background information of the code snippet in different scenarios, for example, the functional module where the code snippet is located, the input data received, and the calls in different functional modules, the execution status under different data inputs (recorded in the use feedback), etc. The use feedback refers to the feedback information on the running effect and performance of the code snippet, for example, performance data (such as CPU usage, code latency, network bandwidth occupancy, disk I / O operations, transaction processing rate, etc.), error reports, etc. These feedbacks can help us understand the performance of the code snippet in actual applications. After obtaining the context information in the call record, the system terminal will cluster the code snippet according to its usage pattern in similar contexts, and aggregate these similar usage scenarios together by calculating similar results. Clustering can help identify which usage scenarios have the greatest impact on the performance of the code snippet. Subsequently, based on similar usage scenarios, the system terminal will output similar usage clustering results to identify which usage scenarios of the code snippet are highly related, which helps to further understand the application characteristics of the code snippet in different scenarios. Afterwards, the system terminal will time-mark each usage context. The purpose of the first step is to identify the performance differences of code snippets at different time points. This time mark refers to the time when the code snippet is used; for example, the time when the code is called; through these time marks, the system terminal establishes a retrospective trust coefficient for each code snippet. This coefficient reflects the reliability of the code snippet in a specific context. It is obtained by filtering the number of successful runs and negative feedbacks in the most recent trust cycle through the time mark, adding the number of successful runs to the number of negative feedbacks to obtain the total number of runs, and then using the ratio of the number of successful runs to the total number of runs to calculate the result; among them, the trust cycle is determined based on the frequency of use of the code snippet and the frequency of feedback updates; once the retrospective trust coefficient is obtained, the system terminal The similar usage clustering results will be combined with usage feedback (such as performance data, error reports, etc.) to perform contextual backtracking trust identification; specifically, the system terminal extracts the execution time of the timed execution from the performance data, and uses the difference between each timed execution time and the expected execution time to perform ratio calculation with the expected execution time, and then adds all the calculation results and performs average calculation to obtain the performance penalty factor; the number of error reports is counted, and the number of error reports is used to perform ratio calculation with the total number of runs to obtain the error penalty factor; the performance penalty factor and the number of error reports are averaged to obtain the usage feedback penalty factor, and then the backtracking trust coefficient is subtracted from the product of the backtracking trust coefficient and the usage feedback penalty factor to obtain the contextual backtracking trust identification;Then, the system terminal stores the context backtracking trust mark in the context backtracking analysis result. This result shows the backtracking of the code snippet in different contexts and its performance reliability in these contexts, thus providing more comprehensive support for the management, optimization and reuse of code snippets. ;
[0027] Furthermore, step S220 of the present application includes:
[0028] Step S221: Establish a usage scenario based on the usage context, use the usage scenario to perform scenario similarity calculation, and establish a first similarity result; Step S222: Perform user backtracking based on the usage context, establish a user usage identifier, and establish a second similarity result based on the user usage identifier; Step S223: Perform similar usage clustering based on the first similarity result and the second similarity result, and establish a similar usage clustering result.
[0029] Specifically, the system terminal analyzes the usage context of each code snippet and establishes a usage scenario. The usage context includes information such as the environment in which the code is executed in different functional modules, input data, and call relationships. This information helps define the application scenarios of the code snippets in different situations. For example, in an e-commerce platform, a certain code segment may be used to process user logins, while another code segment is used for payment processes. Subsequently, the system terminal calculates the similarity between different usage scenarios to establish a first similarity result. The similarity calculation is based on the similarity of the input data, the similarity of the execution logic, and the similarity of the output results. Among them, the similarity of the input data refers to the similarity of different usage scenarios. Whether there are shared or similar parameters in the input data of the same scenario, the similarity of execution logic refers to whether the code logic in different scenarios is similar, that is, whether it contains the same function call or calculation process, and the similarity of output results refers to whether the output results of the code in different scenarios are comparable; the system terminal inputs each two code snippets into the first similarity comparison model, and performs similarity analysis according to the first similarity comparison channel in the first similarity comparison model. The first similarity comparison channel calculates the similarity of the two code snippets based on the knowledge learned from the sample data and stores it in the first similarity result; wherein the first similarity comparison channel is based on the sample code snippet and the sample code. The training method is similar to the above, which is carried out through the steps of forward propagation, loss calculation, back propagation, parameter optimization, etc. The second similarity comparison channel in the first similarity comparison model is also constructed in the same way; after obtaining the similarity of the usage scenario, the system terminal will also perform user backtracking, that is, analyzing how users use these code snippets in different time periods or contexts. The purpose of user backtracking is to understand the usage pattern of the same user for the code snippet at different times or situations, and establish a user usage identifier; for example, a user may use the same code snippet for different operations in different time periods, such as using a product discount code to calculate the discount price during a promotion, using a product discount code to calculate the member discount price, and using a product discount code to calculate the bulk discount price; the system terminal can obtain an identifier by backtracking and analyzing the user's behavior, indicating the user's usage pattern in different usage scenarios, which helps to further understand the user's needs and the scope of application of the code snippet; for example, the user usage identifier of the product discount code can be an ordinary user, a member user, or a volume user (wholesaler); based on the above user backtracking, the system terminal will input the backtracked user usage identifier into the second similarity comparison channel in the first similarity comparison model for similarity analysis, thereby obtaining a second similarity result;Afterwards, the system terminal performs similar usage clustering based on the first similarity result and the second similarity result, that is, the similarity of the same position in the first similarity result and the second similarity result is taken as a group. When the similarity of this group is greater than the corresponding similarity threshold, it means that the code snippets corresponding to this group of similarities have a high similarity. At this time, the two code snippets will be clustered together. This process will be repeated until the similarities in the first similarity result and the second similarity result are both involved in the comparison, thereby establishing a similar usage clustering result; through these steps, the system terminal can fully understand the performance of code snippets in different scenarios and the user's usage mode, thereby providing data support for further optimizing code management. ;
[0030] Step S300: performing static and dynamic fusion analysis on the code snippet database, establishing joint functional similarity, and establishing a third annotation result according to the joint functional similarity.
[0031] Specifically, the system terminal inputs each code snippet into the joint analysis channel for static and dynamic fusion analysis; in the static analysis process, the system terminal analyzes the structure and characteristics of the code through the static analysis sub-channel in the joint analysis channel, including checking the syntax of the code, function calls, variable usage, etc. Through static analysis, the functional structural characteristics of the code snippet can be extracted, for example, whether the code snippet implements a certain function (data sorting, file processing, API calls, etc.), and other modules or libraries it depends on; in the dynamic analysis process, the system terminal executes the code snippet through the dynamic analysis sub-channel in the joint analysis channel, and analyzes the running of the code. The system captures the behavior of the code under different input data (such as execution time, resource consumption, output results, etc.), and evaluates the performance characteristics, resource usage, execution path, etc. of the code in actual operation; then, the system terminal combines the results of the two analyses, evaluates the functional similarity of each two code snippets, and calculates their joint functional similarity; finally, based on the joint functional similarity, the system terminal generates a third annotation result, that is, annotates the code snippets to indicate their functional similarities; these annotations help the system terminal better understand and manage code snippets with different functions, and provide a basis for subsequent retrieval, optimization and reuse.
[0032] Furthermore, step S300 of the present application includes:
[0033] Step S310: input the code snippet database into the joint analysis channel, call the static analysis sub-channel of the joint analysis channel to perform code parsing, extract function, class, and module information, and generate a first functional feature; Step S320: call the dynamic analysis sub-channel of the joint analysis channel, use the dynamic analysis sub-channel to perform dynamic execution path analysis of the code snippet database, and establish a second functional feature; Step S330: after feature fusion of the first functional feature and the second functional feature, establish a joint functional similarity.
[0034] Specifically, the system terminal inputs each code snippet in the code snippet database into the joint analysis channel. The joint analysis channel contains a static analysis sub-channel and a dynamic analysis sub-channel, which are responsible for the structure analysis and operation analysis of the code respectively. The construction method of the two channels is similar to the above. When the joint analysis channel receives a code snippet, it will transmit the code snippet to the static analysis sub-channel and the dynamic analysis sub-channel respectively. In the static analysis sub-channel, the code snippet will be parsed according to the learned mapping relationship to extract the key information in the code, including function information, class information, and module information. Among them, the function information is the function defined in the code and its parameters, return value type, call relationship, etc., and the class information is the function defined in the code and its parameters, return value type, call relationship, etc. The first functional feature is a set of information describing the functional structure of the code fragment. For example, a code fragment defines a calculate_discount function. Static analysis will extract the function's name, input parameters (such as price, discount_rate), return value type (such as float), and identify the module it is in (such as discount_calculator). Before the code fragment enters the dynamic analysis subchannel, the system terminal will Generate virtual input data and input the virtual input data and the code snippet into the dynamic analysis subchannel for dynamic execution path analysis. The dynamic analysis subchannel analyzes the execution path and dynamic characteristics of the code through the learned mapping relationship, including dynamic execution path (function call chain and process path triggered at runtime), performance characteristics (execution time, memory usage, CPU usage, etc.), operating conditions (operating status under different inputs), etc.; through dynamic analysis, generate the second functional feature, which is the dynamic performance of the code snippet at runtime; for example, when running the calculate_discount function, the dynamic analysis subchannel will identify that the execution path triggers a function called ap The auxiliary function of ply_tax takes a long time to run under certain inputs (such as discount_rate=0.5). Subsequently, the system terminal fuses the first functional feature obtained by static analysis with the second functional feature obtained by dynamic analysis to form a comprehensive feature description. The feature fusion process integrates the structural information and runtime performance of the code to make the analysis result more comprehensive. After that, based on the fused features, the system terminal calculates the similarity of each two code fragments, that is, inputs the two fused features into the second similarity comparison model (similar to the aforementioned construction method), and calculates the joint functional similarity of each two code fragments to quantify their functional similarity.
[0035] Step S400: executing code version change annotation in the code snippet database to create a fourth annotation result.
[0036] Specifically, the system terminal obtains the version history of each code snippet from the code snippet database, including the timestamp, submission content, submitter, merge status, etc. of each version; by summarizing these version history records, a fourth annotation result is established to describe the changes in code snippets in different versions, which helps to better understand the evolution process of the code and provide support for subsequent code version management.
[0037] Furthermore, step S400 of the present application includes:
[0038] Step S410: Obtain the submission history data of the code snippets in the code snippet database, and establish a submission timing chain based on the submission history data; Step S420: Obtain the merge history of the code snippets in the code snippet database, perform merge identification of the submission timing chain based on the merge history, and establish an update timing chain; Step S430: Obtain the developer tags of the code snippets in the code snippet database, and construct additional tags based on the developer tags; Step S440: Use the submission timing chain, the update timing chain and the additional tags to establish a fourth annotation result.
[0039] Specifically, the system terminal will first extract the submission history of each code snippet from the code snippet database. This submission history describes all changes of the code snippet in the version library over time. These records include the timestamp, submission content, submitter and other information of each submission. For example, submission 1 fixes a calculation error, submission 2 optimizes performance and reduces memory usage, and submission 3 adds a new data processing function. Based on the submission history data extracted from the code snippet database, the system terminal will establish a submission timing chain according to the time sequence of each submission, that is, a sequence of submission records arranged by submission time. The role of the submission timing chain is to connect all submission records in chronological order to form an orderly historical trajectory, which is convenient for understanding the evolution of the code over time. Subsequently, the system terminal extracts the merge history of the code snippet from the code snippet database, that is, the merge record of the code snippet between different development branches. The merge history represents the process of different developers modifying and integrating the code in different time periods. The merge operation usually involves merging the code on different branches into a common version, so the merge history is very important for understanding how the code is integrated and evolved. For example, merge 1 merges the function of developer A. The branch is merged into the main branch, and merge 2 merges developer B's repair branch into the main branch. Through these merge histories, the system terminal identifies the submission timing chain, that is, the corresponding position of the merge history in the submission timing chain is identified, and an update timing chain is constructed, which records the version update order of the code snippet in different merge operations. After that, the system terminal also obtains the developer tag of each code snippet from the code snippet database. These tags are usually comments or tags added by developers when submitting the code, which are used to indicate the purpose or description of specific code changes. Developer tags can help understand the specific background of each submission and merge. For example, developers may indicate that performance bottlenecks have been fixed or new functions have been added when submitting. These tags help identify specific functions and important changes of code snippets. The system terminal uses developer tags to construct additional tags. These additional tags provide more contextual information about the changes in code snippets, which helps with subsequent annotation and classification. Then, the system terminal summarizes all the above information (submission timing chain, update timing chain, additional tags) to comprehensively obtain the fourth annotation result. This annotation result not only reflects the historical change trajectory of the code, but also includes the specific purpose, functional changes and other information of each submission and merge.
[0040] Step S500: performing annotation retrieval management of the code according to the first annotation result, the second annotation result, the third annotation result and the fourth annotation result.
[0041] Specifically, the system terminal integrates the first annotation result, the second annotation result, the third annotation result and the fourth annotation result, and adds them to the corresponding code snippet; each annotation result describes the characteristics or changes of the code snippet from different perspectives, the first annotation result includes the structural characteristics of the code snippet, the second annotation result involves the usage mode of the code snippet in different scenarios, the third annotation result combines the functional similarity of static and dynamic analysis to help identify the functional similarity of the code snippet, and the fourth annotation result records the version changes and developer tags of the code, and provides detailed information about the code evolution and developer intentions; by integrating these annotation information, the system terminal can realize the retrieval management of code snippets. The goal of retrieval management is to help developers quickly and accurately find the code snippets most relevant to current needs or problems through comprehensive analysis of the annotation results. For example, assuming that the developer is performing performance optimization, the system terminal can find the code snippets related to the performance bottleneck by matching the first annotation result (functional structure) and the third annotation result (functional similarity), so as to help developers find suitable solutions directly from the annotated code snippets when encountering similar problems, thereby improving the code reuse rate.
[0042] Furthermore, step S500 of the present application also includes:
[0043] Step S510: When executing the user's annotated search, obtain the user's search data, and set the independent search terms in the search data as independent search data; Step S520: Send a trust confirmation of the independent search data to the user, and receive the user's trust confirmation result; Step S530: Use the trust confirmation result to set the fuzzy range of the independent search data, and complete the annotated search based on the fuzzy range setting result and the mapped independent search data.
[0044] Specifically, when it is necessary to perform a user's annotated search, the system terminal will obtain the user's search data. The search data may be keywords, functional requirements or specific annotation information related to the code snippet entered by the user in response to a certain problem or need, and then separate search terms with independent meanings will be extracted from the search data as independent search data. These independent search terms are a simplification and concretization of user needs, and are used for more accurate searches. Subsequently, the system terminal will send the extracted independent search data to the user for trust confirmation. The purpose of this process is to confirm whether the extracted independent search data meets the user's needs or whether there is any misunderstanding. The user will confirm the independent search data to indicate whether the independent search data is what they really want to retrieve. This link helps to improve the accuracy of the search results and avoid misunderstandings or mismatched queries. After the system terminal receives the user's trust confirmation result, it will continue with subsequent processing based on the confirmation result. On the basis that the trust confirmation result is correct, The system terminal will use the confirmed independent search data to set the fuzzy range. The fuzzy range setting is to allow some flexibility when searching to avoid the failure to retrieve relevant content due to overly strict or limited keywords. The fuzzy range setting can include allowing the retrieval of content related to the search term but not completely matching it, such as related synonyms, functions, etc. It can also expand the scope of the search conditions. For example, when the user queries for performance optimization, similar keywords can be set, such as performance improvement, resource optimization, etc.; after completing the trust confirmation and fuzzy range setting, the system terminal performs the final annotation search based on the fuzzy range setting results and independent search terms; the system terminal will search for matching annotation information in the code snippet database according to the adjusted search conditions, and through fuzzy matching, it can retrieve code snippets related to user needs to ensure that the annotation results that best meet the needs are found; this process ensures that users can accurately and flexibly find annotation results that meet their needs, thereby improving the accuracy and efficiency of retrieval.
[0045] Furthermore, step S500 of the present application also includes:
[0046] Step S540: Obtain annotated search results, and establish a search enhancement factor and a search weakening factor based on the annotated search results, wherein the search weakening factor is a coefficient for weakening the user's search features; Step S550: Integrate the search enhancement factor and the search weakening factor into a search option, and perform user annotated search management based on the search option.
[0047] Specifically, after obtaining the annotated search results, the system terminal will analyze the relationship between these results and the user's preferences and establish a search enhancement factor. The role of the search enhancement factor is to strengthen the user's specific search needs or features. The search enhancement factor will increase the weight of certain keywords or features based on these preferences so that the results related to these preferences can be more prominent during the search. For example, if the user often searches for code related to performance optimization, the system terminal can enhance the priority of code snippets related to performance optimization. If the user often uses a specific programming language or tool (such as Python, JavaScript), the display frequency of code snippets related to these technology stacks will be enhanced. Similarly, a search weakening factor will be established. The role of the search weakening factor is to weaken the specific preferences or features in the user's search, which can be used to allow users to break out of their own habits. The search scope can be expanded to explore new content that is not usually searched, so as to avoid the search results being too single or repeated and broaden the search horizon. For example, the user is accustomed to using function A to handle event 1, and the search weakening factor allows the system terminal to reduce the recommendation or even block function A and code with similar functions to function A in this search, and then push other possible solutions that the user would not normally search for, prompting the user to explore new programming methods. The system terminal combines the search enhancement factor and the search weakening factor to form a comprehensive search option, and manages the user's search results according to the search option. In this way, the system terminal can flexibly adjust the search results according to the user's preferences and needs. This combination of search enhancement and weakening can make the user's search more personalized, and also help to break the user's regular search pattern and discover more potential solutions.
[0048] The embodiments of the present application solve the technical problem of low management efficiency and retrieval accuracy of code snippets due to the high complexity of code snippets in multi-dimensional feature annotation, and achieve the technical effect of improving the annotation accuracy and retrieval efficiency of code snippets by combining multi-dimensional annotation with adaptive backtracking analysis.
[0049] In the above, refer to Figure 1 A method for marking any code snippet according to an embodiment of the present application is described in detail. Figure 2 A system for retrieving arbitrary code snippets according to an embodiment of the present application is described.
[0050] According to an embodiment of the present application, a retrieval system for arbitrary code snippets is used to solve the technical problem of low management efficiency and retrieval accuracy of code snippets due to the high complexity of code snippets in multi-dimensional feature annotation, and to achieve the technical effect of improving the annotation accuracy and retrieval efficiency of code snippets by combining multi-dimensional annotation with adaptive backtracking analysis. A retrieval system for arbitrary code snippets includes: a code snippet annotation unit 1, a context backtracking analysis unit 2, a fusion analysis unit 3, a code version change annotation unit 4, and an annotation retrieval management unit 5.
[0051] A code snippet annotation unit 1, the code snippet annotation unit 1 is used to establish a code snippet database, activate a multidimensional feature annotation model, perform code snippet annotation, and establish a first annotation result, wherein the code snippet annotation includes grammatical structure feature annotation, code style feature annotation, functional feature annotation, quality feature annotation, code performance feature annotation, code language and platform feature annotation; a context backtracking analysis unit 2, the context backtracking analysis unit 2 is used to call an adaptive annotation network, use the adaptive annotation network to perform context backtracking analysis on code snippets in the code snippet database, and establish a second annotation result based on the context backtracking analysis result; a fusion analysis unit 3, the fusion analysis unit 3 is used to perform static and dynamic fusion analysis on the code snippet database, establish a joint functional similarity, and establish a third annotation result based on the joint functional similarity; a code version change annotation unit 4, the code version change annotation unit 4 is used to perform code version change annotation in the code snippet database and establish a fourth annotation result; an annotation retrieval management unit 5, the annotation retrieval management unit 5 is used to perform annotation retrieval management of codes based on the first annotation result, the second annotation result, the third annotation result and the fourth annotation result.
[0052] The specific configuration of the context backtracking analysis unit 2 will be described in detail below. As described above, the context backtracking analysis of the code snippets in the code snippet database using the adaptive annotation network can further include: obtaining the call record of the code snippet in the code snippet database, the call record including the usage context and the usage feedback; performing similar usage clustering according to the usage context in the call record, and establishing similar usage clustering results; time-marking the usage context, and using the time mark to construct a backtracking trust coefficient, performing a context backtracking trust mark of the similar usage clustering result through the backtracking trust coefficient and the usage feedback, and constructing a context backtracking analysis result using the context backtracking trust mark.
[0053] The specific configuration of the context backtracking analysis unit 2 will be described in detail below. As described above, the similar usage clustering is performed based on the usage context in the call record to establish a similar usage clustering result. The context backtracking analysis unit 2 may further include: establishing a usage scenario based on the usage context, performing scenario similarity calculation using the usage scenario, and establishing a first similarity result; performing user backtracking based on the usage context, establishing a user usage identifier, and establishing a second similarity result based on the user usage identifier; performing similar usage clustering based on the first similarity result and the second similarity result to establish a similar usage clustering result.
[0054] The specific configuration of the fusion analysis unit 3 will be described in detail below. As described above, the code snippet database is subjected to static and dynamic fusion analysis, a joint functional similarity is established, and a third annotation result is established based on the joint functional similarity. The fusion analysis unit 3 may further include: inputting the code snippet database into a joint analysis channel, calling the static analysis sub-channel of the joint analysis channel to perform code parsing, extracting function, class, and module information, and generating a first functional feature; calling the dynamic analysis sub-channel of the joint analysis channel, using the dynamic analysis sub-channel to perform dynamic execution path analysis of the code snippet database, and establishing a second functional feature; after performing feature fusion on the first functional feature and the second functional feature, a joint functional similarity is established.
[0055] The specific configuration of the code version change annotation unit 4 will be described in detail below. As described above, the code version change annotation in the execution code snippet database is established to establish the fourth annotation result. The code version change annotation unit 4 may further include: obtaining the submission history data of the code snippet in the code snippet database, and establishing a submission timing chain according to the submission history data; obtaining the merge history of the code snippet in the code snippet database, performing a merge mark of the submission timing chain based on the merge history, and establishing an update timing chain; obtaining the developer mark of the code snippet in the code snippet database, and constructing an additional mark based on the developer mark; and establishing the fourth annotation result using the submission timing chain, the update timing chain and the additional mark.
[0056] The specific configuration of the annotation retrieval management unit 5 will be described in detail below. As described above, the annotation retrieval management unit 5 may further include: when executing the annotation retrieval of the user, obtaining the user's retrieval data, and setting the independent retrieval terms in the retrieval data as independent retrieval data; sending the trust confirmation of the independent retrieval data to the user, and receiving the trust confirmation result of the user; using the trust confirmation result to set the fuzzy range of the independent retrieval data, and completing the annotation retrieval based on the fuzzy range setting result and the mapped independent retrieval data.
[0057] The specific configuration of the annotation retrieval management unit 5 will be described in detail below. As described above, the annotation retrieval management unit 5 may further include: obtaining the annotation retrieval result, and establishing a retrieval enhancement factor and a retrieval weakening factor based on the annotation retrieval result, wherein the retrieval weakening factor is a coefficient for weakening the user's retrieval feature; integrating the retrieval enhancement factor and the retrieval weakening factor into a retrieval option, and performing the user's annotation retrieval management based on the retrieval option.
[0058] A retrieval system for any code fragment provided in an embodiment of the present application can execute a marking method for any code fragment provided in any embodiment of the present application, and has functional modules and beneficial effects corresponding to the execution method.
[0059] Although the present application makes various references to certain modules in the system according to the embodiments of the present application, any number of different modules may be used and run on the user terminal and / or server, and the various units and modules included are only divided according to functional logic, but are not limited to the above division, as long as the corresponding functions can be achieved; in addition, the specific names of the functional units are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of the present application.
[0060] The above specific implementations do not constitute a limitation on the protection scope of this application. It should be understood by those skilled in the art that various modifications, combinations and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of this application should be included in the protection scope of this application.
Claims
1. A method for marking any code fragment, characterized in that: The method comprises: Establishing a code snippet database, activating a multidimensional feature annotation model, performing code snippet annotation, and establishing a first annotation result, wherein the code snippet annotation includes grammatical structure feature annotation, code style feature annotation, functional feature annotation, quality feature annotation, code performance feature annotation, and code language and platform feature annotation; Calling an adaptive annotation network, using the adaptive annotation network to perform context backtracking analysis on the code snippets in the code snippet database, and establishing a second annotation result according to the context backtracking analysis result; Performing static and dynamic fusion analysis on the code snippet database to establish joint functional similarity, and establishing a third annotation result according to the joint functional similarity; Execute code version change annotation in the code snippet database to establish a fourth annotation result; The code annotation retrieval management is performed according to the first annotation result, the second annotation result, the third annotation result and the fourth annotation result.
2. A method for marking an arbitrary code fragment as claimed in claim 1, characterized in that: The using the adaptive annotation network to perform context backtracking analysis on the code snippets in the code snippet database includes: Acquire a call record of a code snippet in the code snippet database, wherein the call record includes a usage context and a usage feedback; Performing similar usage clustering according to the usage context in the call record, and establishing similar usage clustering results; The usage context is time-marked, and a retrospective trust coefficient is constructed using the time mark. A context retrospective trust mark of the similar usage clustering result is performed using the retrospective trust coefficient and the usage feedback, and a context retrospective analysis result is constructed using the context retrospective trust mark.
3. A method for marking an arbitrary code fragment as claimed in claim 2, characterized in that: The performing similar usage clustering according to the usage context in the call record and establishing similar usage clustering results includes: Establishing a usage scenario based on the usage context, performing scenario similarity calculation using the usage scenario, and establishing a first similarity result; Perform user backtracking based on the usage context, establish a user usage identifier, and establish a second similar result according to the user usage identifier; Similar usage clustering is performed according to the first similarity result and the second similarity result to establish a similar usage clustering result.
4. A method for marking an arbitrary code fragment as claimed in claim 1, characterized in that: The performing static and dynamic fusion analysis on the code snippet database to establish joint functional similarity, and establishing a third annotation result according to the joint functional similarity, further includes: Input the code snippet database into the joint analysis channel, call the static analysis sub-channel of the joint analysis channel to perform code parsing, extract function, class, and module information, and generate a first functional feature; Calling the dynamic analysis sub-channel of the joint analysis channel, using the dynamic analysis sub-channel to perform dynamic execution path analysis of the code snippet database, and establishing a second functional feature; After the first functional feature and the second functional feature are fused, a joint functional similarity is established.
5. A method for marking an arbitrary code fragment as claimed in claim 1, characterized in that: The code version change annotation in the execution code snippet database to establish a fourth annotation result includes: Acquire submission history data of code snippets in the code snippet database, and establish a submission timing chain according to the submission history data; Acquire the merge history of the code snippets in the code snippet database, perform a merge identification of the submission timing chain based on the merge history, and establish an update timing chain; Obtaining developer tags of code snippets in the code snippet database, and constructing additional tags based on the developer tags; A fourth marking result is established using the submission timing chain, the update timing chain, and the additional mark.
6. A method for marking an arbitrary code fragment as claimed in claim 1, characterized in that: The method further comprises: When executing a user's annotated search, obtaining the user's search data, and setting the independent search terms in the search data as independent search data; Sending a trust confirmation of independent retrieval data to the user, and receiving the trust confirmation result from the user; The trust confirmation result is used to set the fuzzy range of the independent search data, and the annotated search is completed based on the fuzzy range setting result and the mapped independent search data.
7. A method for marking an arbitrary code fragment as claimed in claim 6, characterized in that: The method further comprises: Obtaining annotated search results, and establishing a search enhancement factor and a search weakening factor based on the annotated search results, wherein the search weakening factor is a coefficient for weakening a user's search feature; The retrieval enhancement factor and the retrieval weakening factor are integrated into a retrieval option, and the user's annotation retrieval management is performed based on the retrieval option.
8. A retrieval system for arbitrary code snippets, characterized in that: The system is used to implement the method for marking any code fragment according to any one of claims 1 to 7, and the system comprises: A code snippet annotation unit: establishes a code snippet database, activates a multi-dimensional feature annotation model, performs code snippet annotation, and establishes a first annotation result, wherein the code snippet annotation includes grammatical structure feature annotation, code style feature annotation, function feature annotation, quality feature annotation, code performance feature annotation, code language and platform feature annotation; A context backtracking analysis unit: calling an adaptive annotation network, using the adaptive annotation network to perform context backtracking analysis on code snippets in a code snippet database, and establishing a second annotation result according to the context backtracking analysis result; Fusion analysis unit: performing static and dynamic fusion analysis on the code snippet database, establishing joint functional similarity, and establishing a third annotation result according to the joint functional similarity; A code version change annotation unit: executes code version change annotation in the code snippet database and establishes a fourth annotation result; Annotation retrieval management unit: performs annotation retrieval management of the code according to the first annotation result, the second annotation result, the third annotation result and the fourth annotation result.
Citation Information
Patent Citations
Code snippet marking and reusing system based on body
CN104881285A
Target code generation method and device
CN114661616A
Software community warehouse mining method and device based on self-attention interactive network
CN115291888A
Code annotation generation method and device
CN116661855A
Code labeling method and device, computer equipment and storage medium
CN118151940A