A marking method for arbitrary code fragments and its retrieval system

By combining the multidimensional feature annotation model and the adaptive annotation network, the problem of low management and retrieval efficiency caused by the complexity of multidimensional feature annotation of code snippets is solved, and the accuracy of code snippet annotation and retrieval efficiency are improved.

CN119576388BActive Publication Date: 2025-09-19XIAN QIKE HOUDE INFORMATION TECHNOLOGY CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510035182.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-01-09
Publication Date
2025-09-19
Estimated Expiration
2045-01-09

AI Technical Summary

Technical Problem

Due to the high complexity of code snippets in multidimensional feature annotation, the management efficiency and retrieval accuracy of code snippets are low.

Method used

By providing a method for annotating arbitrary code snippets and a retrieval system thereof, a multi-dimensional feature annotation model combined with an adaptive annotation network is adopted to perform multi-dimensional annotation and adaptive backtracking analysis of code snippets, thereby improving annotation accuracy and retrieval efficiency.

Benefits of technology

The annotation accuracy and retrieval efficiency of code snippets have been improved, and the efficiency and accuracy of code management and retrieval have been improved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119576388B_ABST
    Figure CN119576388B_ABST
Patent Text Reader

Abstract

The present application discloses a method for annotating arbitrary code snippets and a retrieval system thereof, which relates to the field of machine learning. The method comprises: establishing a code snippet database, activating a multidimensional feature annotation model, performing code snippet annotation, and establishing a first annotation result; calling an adaptive annotation network, utilizing the adaptive annotation network to perform contextual backtracking analysis on the code snippets in the database, and establishing a second annotation result; performing static and dynamic fusion analysis on the database, establishing joint functional similarity, and establishing a third annotation result; performing code version change annotation, and establishing a fourth annotation result; and performing code annotation retrieval management based on the four annotation results. The method solves the technical problem of low management efficiency and retrieval accuracy of code snippets due to the high complexity of code snippets in multidimensional feature annotation, and achieves the technical effect of improving the annotation accuracy and retrieval efficiency of code snippets by combining multidimensional annotation with adaptive backtracking analysis.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of machine learning, and in particular to a method for annotating 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 from open source software in their programming. To manage the risks brought by open source software, software composition analysis (SCA) is required. 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, identifying and managing these open source code snippets in the code is becoming increasingly difficult.

[0003] On the other hand, modern software systems often contain a large number of code snippets that are reused across different functional modules. This makes code management, updating, optimization, and retrieval a critical issue in software engineering. This is especially true in large-scale distributed systems and multi-platform environments, where the need for code snippet annotation, classification, version management, and performance optimization is particularly pressing.

[0004] To address these issues, we researched and proposed a method for annotating arbitrary code snippets and its associated retrieval system. This addresses the problem that traditional methods lack deep mining and intelligent analysis of the multi-dimensional features of code snippets, leading to low efficiency in code recognition, reuse, error detection, and function optimization. Summary of the Invention

[0005] This application solves 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 by providing a method for annotating arbitrary code snippets and a retrieval system thereof. It achieves the technical effect of improving the annotation accuracy and retrieval efficiency of code snippets by combining multi-dimensional annotation 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, utilizing 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 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, and establishing 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: 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, code language and platform feature annotation; a context backtracking analysis unit: calling an adaptive annotation network, utilizing 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; a fusion analysis unit: 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; a code version change annotation unit: executing code version change annotation in the code snippet database, and establishing a fourth annotation result; an annotation retrieval management unit: 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.

[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. It solves 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 achieves 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] To more clearly illustrate the technical solutions of the embodiments of the present application, the accompanying drawings of the embodiments of the present application will be briefly introduced below. Flowcharts are used in this application to illustrate the operations performed by the system according to the embodiments of the present application. It should be understood that the preceding or following operations are not necessarily performed in precise order. On the contrary, various steps may be processed in reverse order or simultaneously as needed. Furthermore, other operations may be added to these processes, or one or more operations may 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] Description of the accompanying symbols: code fragment 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 this application clearer, the application will be further described in detail below with reference to the accompanying drawings. The described embodiments should not be regarded as limiting this application. All other embodiments obtained by ordinary technicians in this 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 describes a subset of all possible embodiments, but it will be 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 used to distinguish similar objects and do not represent a specific ordering of the 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 that are clearly listed, but may include other steps or modules that are not clearly listed or that are inherent to these processes, methods, products, or devices. Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by those skilled in the art to which this application belongs. The terms used herein are for the purpose of describing the embodiments of this application only.

[0017] The embodiment of the present application provides a method for marking arbitrary code snippets, which is applied to a retrieval system for arbitrary code snippets, 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 aim of providing comprehensive basic data for subsequent annotation and retrieval; when annotating code snippets in the code snippet database, the system terminal will activate a pre-built multi-dimensional feature annotation model. This model can identify multiple features of code snippets and classify and annotate them through multiple internal channels including 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. 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 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 the code according to its function and business logic, 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, indicating 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 the code according to the programming language (such as Python, Java, C++, etc.) and platform (such as Web, mobile, embedded systems, etc.) used, 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 assist in subsequent retrieval, optimization and management.

[0020] Specifically, for the multidimensional feature annotation model, the system terminal constructs a grammatical structure feature annotation channel, a code style feature annotation channel, a functional feature annotation channel, a quality feature annotation channel, a code performance feature annotation channel, a code language and platform feature annotation channel, and integrates these channels in parallel to form this multidimensional feature annotation model. These channels are constructed in the same way and can be based on deep neural networks, recurrent neural networks, BERT, CodeBERT, graph neural networks, etc.; taking CodeBERT as an example, the system terminal first obtains a sample code snippet data group and a sample grammatical structure feature data group 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. It 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 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 longer code snippets and pad 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 focuses on 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 context representation of each token, which is passed through the 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 of each token; for this purpose, the system terminal will use the cross-entropy loss function to measure the gap between the predicted value and the true 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 will use code snippets and their corresponding annotated data for supervised learning; during training, the model generates context-related representations based on the input code snippets, 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 prediction result and the real label; after that, the gradient of the loss value is calculated through backpropagation and the model parameters are updated; through multiple rounds of training, the model gradually learns How to accurately annotate the grammatical structure of code snippets according to the context; after training, the model can generate accurate annotation results, annotating the grammatical features of each token, such as function definitions, local variables, conditional statements, etc.; in the model evaluation phase, 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 rate, and is especially suitable for cases of class 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 a code style feature annotation channel, a functional feature annotation channel, a quality feature annotation channel, a code performance feature annotation channel, and a code language and platform feature annotation channel; then, all the 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 will analyze and annotate independently, and finally generate 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, utilizing 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 results 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 the 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 code snippet being used, but also the usage context and usage feedback; among them, the usage context refers to the running environment and background information of the code snippet in different scenarios, such as the functional module in which 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 usage feedback), etc. Usage feedback refers to the feedback information on the running effect and performance of the code snippet, such as performance data (such as CPU usage, code latency, network bandwidth occupancy, disk I / O operations, transaction processing rate, etc.), error reporting, etc. These feedbacks can help us understand the performance of code snippets in actual applications. After obtaining the context information in the call record, the system terminal will cluster the code snippets according to their usage patterns 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 snippets. Subsequently, based on similar usage scenarios, the system terminal will output similar usage clustering results, identifying which usage scenarios of code snippets are highly relevant, which will help us further understand the application characteristics of code snippets in different scenarios. Afterwards, the system terminal will time-tag 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 and the number of negative feedbacks to get 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 timed executions from the performance data, and uses the difference between each timed execution time and the expected execution time to calculate the ratio of the expected execution time. All the calculation results are then added together and averaged to obtain a performance penalty factor. The number of error reports is counted, and the number of error reports is compared with the total number of runs to obtain an error penalty factor. The performance penalty factor is averaged with the number of error reports to obtain a usage feedback penalty factor. The contextual backtracking trust identification is then obtained by subtracting the product of the backtracking trust factor and the usage feedback penalty factor from the backtracking trust coefficient.The system then stores the context backtracking trust identifier in the context backtracking analysis results. This result indicates the backtracking status of the code snippet in different contexts and its reliability in these contexts, 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 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 snippet. The code similarity is obtained by training. The training method is similar to the above, and is carried out through steps such as forward propagation, loss calculation, backpropagation, and parameter optimization. The second similarity comparison channel in the first similarity comparison model is also constructed in the same way. After obtaining the usage scenario similarity, 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 code snippets in different time periods or contexts 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 a discount price during a promotion, using a product discount code to calculate a member discount price, and using a product discount code to calculate a bulk discount price. By backtracking and analyzing the user's behavior, the system terminal can obtain an identifier that represents the user's usage pattern in different usage scenarios, which helps to further understand the user's needs and the applicable scope of the code snippet. For example, the user usage identifier of a 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.The system terminal then performs similar usage clustering based on the first and second similar results. Specifically, the similarities at the same locations in the first and second similar results are grouped together. When the similarities in this group are both greater than the corresponding similarity threshold, it indicates that the code snippets corresponding to this group have a high degree of similarity. In this case, the two code snippets are clustered together. This process is repeated until the similarities in both the first and second similar results are compared, 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 user usage patterns, 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 based on 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 obtained from the two analyses, evaluates the functional similarity of each two code fragments, and calculates their joint functional similarity; finally, based on the joint functional similarity, the system terminal will generate a third annotation result, that is, annotate the code fragments to indicate their functional similarity; these annotations help the system terminal better understand and manage code fragments 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 structural 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 generated by aggregating these key information, which is a set of information describing the functional structure of the code fragment. For example, a code segment 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 segment enters the dynamic analysis sub-channel, the system terminal will Generate virtual input data and input the virtual input data and code snippets into the dynamic analysis sub-channel for dynamic execution path analysis. The dynamic analysis sub-channel 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 sub-channel 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); then, 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, making the analysis results more comprehensive; then, 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 above construction method), and calculates the joint functional similarity of each two code fragments to quantify their functional similarity.

[0035] Step S400: Execute 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 snippet 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 snippet 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 mark of the code snippet in the code snippet database, and construct an additional mark based on the developer mark; Step S440: Use the submission timing chain, the update timing chain and the additional mark to establish a fourth annotation result.

[0039] Specifically, the system terminal will first extract the commit history of each code snippet from the code snippet database. This commit history describes all changes of the code snippet in the version library over time. These records include the timestamp, commit content, committer and other information of each commit; for example, commit 1 fixes a calculation error, commit 2 optimizes performance and reduces memory usage, and commit 3 adds a new data processing function; based on the commit history data extracted from the code snippet database, the system terminal will establish a commit sequence chain according to the chronological order of each commit, that is, a sequence of commit records arranged by commit time. The role of the commit sequence chain is to string all commit records in chronological order to form an ordered historical trajectory, which is convenient for understanding the evolution of the code over time; then, 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 developer A's function The branch is merged into the main branch. Merge 2 merges developer B's fix branch into the main branch. Using these merge histories, the system terminal identifies the commit sequence chain, that is, it identifies the corresponding position of the merge history in the commit sequence chain and constructs an update sequence chain. This chain records the version update sequence of the code snippet in different merge operations. Afterwards, the system terminal also obtains the developer tags of each code snippet from the code snippet database. These tags are usually comments or labels added by developers when submitting code, which are used to indicate the purpose or description of a specific code change. Developer tags can help understand the specific context of each commit and merge. For example, developers may indicate that they have fixed a performance bottleneck or added a new feature in their commit. These tags help identify the specific functionality and important changes of the code snippet. The system terminal uses the developer tags to construct additional tags. These additional tags provide more contextual information about the code snippet changes, which facilitates subsequent annotation and classification. Then, the system terminal summarizes all the above information (commit sequence chain, update sequence chain, and 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 information such as the specific purpose and functional changes of each commit and merge.

[0040] Step S500: performing code annotation retrieval management 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 a different perspective. The first annotation result includes the structural characteristics of the code snippet, the second annotation result involves the usage pattern 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, providing 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 code snippets related to performance bottlenecks by matching the first annotation result (functional structure) and the third annotation result (functional similarity), helping developers to find suitable solutions directly from the annotated code snippets when encountering similar problems, thereby improving code reuse rate.

[0042] Furthermore, step S500 of the present application also includes:

[0043] Step S510: When executing the user's annotation 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 annotation 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. These search data can 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 extract separate, independently meaningful search terms 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 retrieval; then, 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, indicating 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 continues to process according to 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. 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 the search.

[0045] Furthermore, step S500 of the present application also includes:

[0046] Step S540: Obtain the annotated search results, and establish a search enhancement factor and a search weakening factor based on the annotated search results, where the search weakening factor is a coefficient that weakens 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 also 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, which can 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 recommendations or even block function A and codes with similar functions 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 an arbitrary code fragment 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 system for retrieving arbitrary code snippets is used to solve the technical problem of low code snippet management efficiency and retrieval accuracy due to the high complexity of code snippets in multi-dimensional feature annotation. This system achieves the technical effect of improving the annotation accuracy and retrieval efficiency of code snippets by combining multi-dimensional annotation with adaptive backtracking analysis. The system for retrieving 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 is configured 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, and code language and platform feature annotation; a context backtracking analysis unit 2 is configured to call an adaptive annotation network, utilize 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 is configured 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 is configured to perform code version change annotation in the code snippet database and establish a fourth annotation result; an annotation retrieval management unit 5 is configured to 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.

[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 call records of the code snippets in the code snippet database, wherein the call records include usage context and usage feedback; performing similar usage clustering based on the usage context in the call records, and establishing similar usage clustering results; time-stamping the usage context, and using the time-stamp to construct a backtracking trust coefficient, performing a context backtracking trust identifier for the similar usage clustering result using the backtracking trust coefficient and the usage feedback, and using the context backtracking trust identifier to construct a context backtracking analysis result.

[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 static and dynamic fusion analysis of the code snippet database is performed to establish a joint functional similarity, 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; and establishing a joint functional similarity after fusing the first and second functional features.

[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 unit 4 may further include: obtaining the submission history data of the code snippets in the code snippet database, and establishing a submission timing chain based on the submission history data; obtaining the merge history of the code snippets in the code snippet database, performing a merge mark on the submission timing chain based on the merge history, and establishing an update timing chain; obtaining the developer tags of the code snippets in the code snippet database, and constructing additional tags based on the developer tags; and establishing the fourth annotation result using the submission timing chain, the update timing chain, and the additional tags.

[0056] The specific configuration of the annotation search management unit 5 will be described in detail below. As described above, the annotation search management unit 5 may further include: when executing a user's annotation search, obtaining the user's search data and setting the independent search terms within the search data as independent search data; sending a trust confirmation of the independent search data to the user and receiving the user's trust confirmation result; using the trust confirmation result to set a fuzzy range for the independent search data, and completing the annotation search based on the fuzzy range setting result and the mapped independent search 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 annotation retrieval results, and establishing a retrieval enhancement factor and a retrieval weakening factor based on the annotation retrieval results, wherein the retrieval weakening factor is a coefficient that weakens the user's retrieval characteristics; integrating the retrieval enhancement factor and the retrieval weakening factor into retrieval options, and performing user annotation retrieval management based on the retrieval options.

[0058] The retrieval system for any code fragment provided in the embodiment of the present application can execute the annotation method for any code fragment provided in any embodiment of the present application, and has the corresponding functional modules and beneficial effects of 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 embodiments do not constitute a limitation on the scope of protection of this application. Those skilled in the art should understand that various modifications, combinations, and substitutions may be made based on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this application shall be included within the scope of protection of this application.

Claims

1. A method for marking arbitrary code fragments, 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; Invoking an adaptive annotation network, performing context backtracking analysis on the code snippets in the code snippet database using the adaptive annotation network, and establishing a second annotation result based on 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 based on the joint functional similarity; Execute code version change annotation in the code snippet database to create a fourth annotation result; Perform code annotation retrieval management according to the first annotation result, the second annotation result, the third annotation result, and the fourth annotation result; The method of performing contextual backtracking analysis on code snippets in a code snippet database by using the adaptive annotation network includes: Obtaining a call record of a code snippet in the code snippet database, wherein the call record includes a usage context and usage feedback; Performing similar usage clustering based on the usage context in the call record, and establishing similar usage clustering results; The usage context is time-stamped, and a retrospective trust coefficient is constructed using the time stamp. A context retrospective trust identifier 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 identifier.

2. A method for marking arbitrary code fragments according to claim 1, 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 a 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 similarity result based on the user usage identifier; Similar usage clustering is performed based on the first similarity result and the second similarity result to establish a similar usage clustering result.

3. A method for marking arbitrary code fragments according to 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 based on the joint functional similarity, further includes: Inputting the code snippet database into a joint analysis channel, calling a 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; Invoking 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 feature fusion of the first functional feature and the second functional feature, a joint functional similarity is established.

4. A method for marking arbitrary code fragments according to claim 1, characterized in that: The code version change annotation in the execution code snippet database to establish a fourth annotation result includes: Obtaining submission history data of code snippets in the code snippet database, and establishing a submission time sequence chain based on the submission history data; Acquire the merge history of the code snippets in the code snippet database, generate a merge identifier for 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 annotation result is established using the submission timing chain, the update timing chain, and the additional mark.

5. The method for marking an arbitrary code fragment according to claim 1, wherein: The method further comprises: When executing a user's annotation search, obtaining the user's search data, and setting the independent search terms in the search data as independent search data; Sending trust confirmation of independent retrieval data to the user and receiving the user's trust confirmation result; The trust confirmation result is used to set a fuzzy range for the independent search data, and the annotation search is completed based on the fuzzy range setting result and the mapped independent search data.

6. A method for marking arbitrary code fragments according to claim 5, 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 the 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.

7. 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 6, and the system includes: 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 syntax structure feature annotation, code style feature annotation, function feature annotation, quality feature annotation, code performance feature annotation, and code language and platform feature annotation; A context backtracking analysis unit: calling an adaptive annotation network, performing context backtracking analysis on code snippets in a code snippet database using the adaptive annotation network, and establishing a second annotation result based on 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 based on the joint functional similarity; A code version change annotation unit: executes code version change annotation in the code snippet database and creates 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 annotation generation method and device

    CN116661855A

  • Code labeling method and device, computer equipment and storage medium

    CN118151940A