Software clustering method and device, electronic equipment and storage medium

By constructing code architecture diagrams and combining multi-clustering methods that annotate semantic features and code features, the problems of unclear architecture and insufficient modularity in existing software clusters are solved, and higher-quality software clustering results are achieved.

CN119782855APending Publication Date: 2025-04-08CHINA AUTOMOTIVE INNOVATION CORP
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411867864.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-18
Publication Date
2025-04-08

AI Technical Summary

Technical Problem

The existing software clustering methods have problems such as unclear architecture and insufficient modularity, which leads to difficulties in software maintenance and understanding.

Method used

By building a code architecture diagram, obtaining the annotated semantic features and code features of the code components, combining the node structure organization, using a multi-clustering algorithm to generate multiple clustering results, and gradually improving the stability and accuracy of clustering results through the target loss optimization process.

Benefits of technology

It improves the quality and accuracy of software clustering, enhances the modular structure of the software, and improves the maintainability and understanding of the system.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119782855A_ABST
    Figure CN119782855A_ABST
Patent Text Reader

Abstract

The invention relates to a software clustering method and device, electronic equipment and a storage medium. The method comprises the steps of obtaining a code architecture diagram of an original code; based on the code architecture diagram, acquiring annotation semantic features and code features; generating a node structure organization; based on the node structure organization, the annotation semantic features and the code features, the nodes are clustered, and a first clustering result, a second clustering result and a third clustering result are obtained; determining target loss based on the first clustering result, the second clustering result and the third clustering result; generating a current clustering result based on the target loss; and updating the first clustering result, the second clustering result and the third clustering result as the current clustering result, and skipping to the step of determining the target loss based on the first clustering result, the second clustering result and the third clustering result until the current target loss meets a preset convergence condition, and outputting the corresponding current clustering result. According to the embodiment of the invention, the stability and accuracy of software clustering can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of computer technology, and in particular to a software clustering method, device, electronic device and storage medium. Background Art

[0002] Maintenance of existing software requires a lot of time to understand the source code. However, without up-to-date documentation, maintainers may not be clear about the architecture of the software. Software clustering is often used as a technique for re-modularization and architecture recovery to help recover the semantic representation of the software design.

[0003] By automatically clustering the classes, methods or other software components in the software system into highly cohesive and low-coupling modules, the modular structure of the software is improved, and the maintainability and understandability of the system are enhanced. Currently, the commonly used clustering algorithms include the Bunch clustering algorithm and the community detection algorithm. The existing Bunch algorithm and community detection only perform clustering based on dependency relationships, which leads to problems such as unclear architecture and insufficient modularity in the existing software clustering results. Summary of the invention

[0004] The present disclosure provides a software clustering method, device, electronic device and storage medium to at least solve the problems of unclear architecture and insufficient modularization of software clustering results in the related art.

[0005] According to a first aspect of an embodiment of the present disclosure, a software clustering method is provided, comprising:

[0006] Obtain a code architecture diagram of the original code, wherein the code architecture diagram is a structure diagram with code components as nodes and dependencies between the code components as edges;

[0007] Based on the code architecture diagram, obtaining annotation semantic features corresponding to each node in the code architecture diagram and code features corresponding to each node;

[0008] Generate a node structure organization corresponding to the code architecture diagram, where the node structure organization is used to indicate a connection relationship between any two nodes in the code architecture diagram;

[0009] Based on the node structure organization, the annotation semantic features and the code features, clustering the nodes in the code architecture diagram respectively to obtain a first clustering result, a second clustering result and a third clustering result;

[0010] Determining a target loss based on the first clustering result, the second clustering result, and the third clustering result;

[0011] Based on the target loss, generate a current clustering result corresponding to the node in the code architecture diagram;

[0012] The first clustering result, the second clustering result and the third clustering result are all updated as the current clustering result, and the step of determining the target loss based on the first clustering result, the second clustering result and the third clustering result is jumped to, until the current target loss meets the preset convergence condition, and the corresponding current clustering result is output.

[0013] According to a second aspect of an embodiment of the present disclosure, a software clustering device is provided, including:

[0014] A code architecture diagram acquisition module is used to acquire a code architecture diagram of the original code, wherein the code architecture diagram is a structure diagram with code components as nodes and dependencies between the code components as edges;

[0015] A feature acquisition module, used to acquire, based on the code architecture diagram, an annotation semantic feature corresponding to each node in the code architecture diagram and a code feature corresponding to each node;

[0016] A node structure organization acquisition module, used to generate a node structure organization corresponding to the code architecture diagram, wherein the node structure organization is used to indicate a connection relationship between any two nodes in the code architecture diagram;

[0017] A clustering result acquisition module, used to cluster the nodes in the code architecture diagram based on the node structure organization, the annotation semantic features and the code features, to obtain a first clustering result, a second clustering result and a third clustering result;

[0018] a target loss acquisition module, configured to determine a target loss based on the first clustering result, the second clustering result, and the third clustering result;

[0019] A current clustering result acquisition module, used to generate a current clustering result corresponding to a node in the code architecture diagram based on the target loss;

[0020] The current clustering result output module is used to update the first clustering result, the second clustering result and the third clustering result to the current clustering result, and jump to the step of determining the target loss based on the first clustering result, the second clustering result and the third clustering result, until the current target loss meets the preset convergence condition, and output the corresponding current clustering result.

[0021] According to a third aspect of an embodiment of the present disclosure, an electronic device is provided, comprising: a processor; and a memory for storing instructions executable by the processor; wherein the processor is used to execute the instructions to implement a method as described in any one of the above-mentioned first aspects.

[0022] According to a fourth aspect of an embodiment of the present disclosure, a computer-readable storage medium is provided. When instructions in the storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute any method described in the first aspect of the embodiment of the present disclosure.

[0023] According to a fifth aspect of an embodiment of the present disclosure, there is provided a computer program product comprising instructions, which, when executed on a computer, enables the computer to execute any one of the methods described in the first aspect of the embodiment of the present disclosure.

[0024] The technical solution provided by the embodiments of the present disclosure brings at least the following beneficial effects:

[0025] By constructing a code architecture diagram with code components as nodes and dependencies as edges, the dependencies between code components are intuitively presented, which helps to understand the original code from a macro perspective and provide basic data for subsequent analysis and optimization of clustering results. Obtain the annotation semantic features and code features of the node. The annotation semantic features can reflect the developer's intentions, while the code features provide the implementation details of the code components. The combination of the two can more accurately characterize the node characteristics and provide a comprehensive basis for subsequent clustering. Clustering is performed based on node structure organization, annotation semantic features, and code features, and multiple clustering results are obtained. Among them, different features reflect the characteristics of different aspects of the code. Clustering from the perspectives of connection relationships, annotation semantic understanding, and code implementation can discover code structure patterns and similarities in different dimensions. Determine the target loss based on multiple clustering results, comprehensively consider the differences and advantages and disadvantages of different clustering methods, comprehensively evaluate the clustering quality, avoid the one-sidedness of a single clustering, and make the target loss more accurately reflect the degree of deviation between the clustering and the ideal result. By continuously updating the clustering results and recalculating the target loss until the convergence conditions are met, the clustering results can be gradually optimized, which can effectively improve the stability and accuracy of clustering, thereby effectively improving the quality of software clustering.

[0026] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS

[0027] The drawings herein are incorporated into and constitute a part of the specification, illustrate embodiments consistent with the present disclosure, and together with the description are used to explain the principles of the present disclosure, and do not constitute improper limitations on the present disclosure.

[0028] Figure 1 is a flowchart of a software clustering method according to an exemplary embodiment;

[0029] Figure 2 is a schematic diagram of a process of determining target loss according to an exemplary embodiment;

[0030] Figure 3 is a schematic diagram of a process for determining a semantic loss weight and a code expression loss weight according to an exemplary embodiment;

[0031] Figure 4 is a schematic diagram of a process of determining a semantic loss weight according to an exemplary embodiment;

[0032] Figure 5 is a schematic diagram of a process of determining a modularity score, a semantic loss, and a code expression loss according to an exemplary embodiment;

[0033] Figure 6 is a schematic diagram of a process of determining semantic loss according to an exemplary embodiment;

[0034] Figure 7 is a block diagram of a software clustering device according to an exemplary embodiment;

[0035] Figure 8 It is a block diagram of an electronic device for software clustering according to an exemplary embodiment. DETAILED DESCRIPTION

[0036] In order to enable ordinary persons in the art to better understand the technical solutions of the present disclosure, the technical solutions in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the accompanying drawings.

[0037] It should be noted that the terms "first", "second", etc. in the specification and claims of the present disclosure and the above-mentioned drawings are used to distinguish similar different contents, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present disclosure described here can be implemented in an order other than those illustrated or described here. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. On the contrary, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the attached claims.

[0038] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data for display, data for analysis, etc.) involved in this disclosure are all information and data authorized by the user or fully authorized by all parties.

[0039] Figure 1 is a flowchart of a software clustering method according to an exemplary embodiment. Figure 1 As shown, the software clustering method is used in electronic devices such as servers, and includes the following steps.

[0040] In step S101, a code architecture diagram of the original code is obtained.

[0041] In a specific embodiment, the above-mentioned code architecture diagram is a structural diagram with code components as nodes and dependencies between code components as edges. Specifically, code components are elements such as functions and classes in the original code. The dependencies between code components are the calling relationships between elements such as functions and classes. Exemplarily, the code architecture diagram can be a directed acyclic graph. Accordingly, a preset parsing tool can be used to extract elements such as functions and classes in the original code, and components such as functions and classes are used as nodes in a directed acyclic graph. The preset parsing tool can be used to parse the calling relationships between components such as functions and classes in the original code, and the calling relationships are used as directed edges in a directed acyclic graph.

[0042] In step S103, based on the code architecture diagram, the annotation semantic features corresponding to each node in the code architecture diagram and the code features corresponding to each node are obtained.

[0043] In a specific embodiment, based on the code architecture diagram, obtaining the annotation semantic features corresponding to each node in the code architecture diagram and the code features corresponding to each node includes:

[0044] Input the text annotation information corresponding to each node into a preset feature extraction model to generate the annotation semantic features corresponding to each node;

[0045] The code information corresponding to each node is input into the preset feature extraction model to generate the code features corresponding to each node.

[0046] In a specific embodiment, the text annotation information is used to explain the function of the code, the type of input parameters, the meaning of the return value, etc. The text annotation information may include at least one of the following information: description of the function function, explanation of the algorithm idea, and explanation of special case handling.

[0047] For example, for a function node that calculates the sum of two numbers, its text annotation information may be " / / This function is used to calculate the sum of two input integers and return the calculation result." For a sorting function node, its text annotation information may be " / / Use the quick sort algorithm to sort the input array in ascending order, recursively select the base element and divide the array until the subarray length is less than or equal to 1."

[0048] Specifically, text comment information is usually written in the code in a specific format. The text comment information can be in the form of line comments (such as starting with "#" in Python and " / / " in C / C++) and block comments (such as the part enclosed by " / ** / " in C / C++). The text comment information can be placed before the function definition, next to the key code section inside the function, or above the class definition.

[0049] Exemplarily, taking a function node as an example, its code information may include function name, parameter list, code statements in the function body, etc. The code statements in the function body may include variable definition, control flow structure, function call, etc.

[0050] In a specific embodiment, the annotation semantic feature may be a semantic vector, and the code feature may be a code vector.

[0051] In a specific embodiment, the above-mentioned inputting of the text annotation information corresponding to each node into the preset feature extraction model to generate the annotation semantic features corresponding to each node may include: first, performing necessary preprocessing operations on the text annotation information corresponding to each node. Exemplarily, the preprocessing operation may include removing punctuation marks and special characters in the annotations and converting the text annotations into lowercase or uppercase forms according to specific needs and model characteristics. Then, the preprocessed text annotation information is segmented using a preset word segmentation tool or algorithm to split the continuous text sequence into individual words or tags. Then, the text annotation tag sequence after word segmentation is passed as input to the preset feature extraction model. The preset feature extraction model converts the input text annotation tag sequence into a vector representation, that is, the annotation semantic vector corresponding to each node through a series of calculations based on its internal parameters and algorithms.

[0052] In a specific embodiment, the code information corresponding to each node is input into the preset feature extraction model, and the code features corresponding to each node are generated, which may include: first, performing necessary preprocessing operations on the acquired code information of each node. Exemplarily, the preprocessing operation may include formatting the code, and performing at least one of some basic syntax checks. Then, according to the requirements of the preset feature extraction model, appropriate code features are selected for extraction and encoding. Including but not limited to the structural features of the code (such as the number of lines of code, nesting depth, type and number of control flow structures, etc.), grammatical features (such as the programming language keywords used, the types and frequencies of operators, etc.) and data dependency features (such as the definition and usage relationship of variables, function call relationship, etc.). For each selected feature, it is converted into a format suitable for model input and passed to the preset feature extraction model. The preset feature extraction model abstracts and converts the code features according to its internal algorithms and parameters to generate a code feature vector corresponding to each node.

[0053] In the above embodiment, by converting text annotation information into annotation semantic features, the readability and maintainability of the code can be significantly improved, which helps to understand the relationship and functional logic between code components from a higher-level semantic perspective. At the same time, converting code information into code features provides information about nodes from the specific details of code implementation, helping to quickly identify problems in the code, thereby improving development efficiency. Combining these two different dimensions of features can more comprehensively and deeply characterize the characteristics of each node in the code architecture. The multi-dimensional feature representation method provides important basic data for subsequent clustering analysis, code understanding, and architecture optimization tasks, so that the analysis of the code architecture is not limited to a single level, but integrates the relationship between the code structure itself and the semantic level, so as to more accurately grasp the overall logic and function of the code, and further improve the quality and effect of software clustering.

[0054] In step S105, a node structure organization corresponding to the code architecture diagram is generated.

[0055] In a specific embodiment, the node structure organization is used to indicate the connection relationship between any two nodes in the code architecture diagram. Exemplarily, the node structure organization can be an adjacency matrix or an adjacency list.

[0056] Exemplarily, taking the node structure organized as an adjacency matrix as an example, the rows and columns of the adjacency matrix correspond to the nodes in the code architecture diagram. If there is an edge connecting node i to node j, the element in the i-th row and j-th column of the adjacency matrix is ​​1, otherwise it is 0.

[0057] Exemplarily, taking the node structure organized as an adjacency list as an example, for each node in the code architecture diagram, the adjacency list stores a list of nodes pointed to by its outgoing edges.

[0058] In step S107, based on the node structure organization, annotation semantic features and code features, the nodes in the code architecture diagram are clustered to obtain a first clustering result, a second clustering result and a third clustering result.

[0059] In a specific embodiment, the first clustering result includes multiple first node sets, the second clustering result includes multiple second node sets, and the third clustering result includes multiple third node sets.

[0060] In a specific embodiment, the above-mentioned clustering of nodes in the code architecture diagram based on node structure organization, annotation semantic features and code features, respectively, to obtain the first clustering result, the second clustering result and the third clustering result may include: clustering the nodes in the code architecture diagram based on node structure organization to obtain the first clustering result; clustering the nodes in the code architecture diagram based on annotation semantic features to obtain the second clustering result; clustering the nodes in the code architecture diagram based on code features to obtain the third clustering result.

[0061] Specifically, clustering the nodes in the code architecture diagram based on the node structure organization to obtain the first clustering result may include: clustering the nodes in the code architecture diagram based on a first preset clustering algorithm and the node structure organization to obtain the first clustering result.

[0062] Exemplarily, the first preset clustering algorithm is the Girvan-Newman algorithm in the community detection algorithm as an example. Based on the first preset clustering algorithm and the node structure organization, clustering the nodes in the code architecture diagram, and obtaining the first clustering result may include: creating a first data structure for each edge in the code architecture diagram to store its betweenness centrality value, and initially setting the betweenness centrality of all edges to 0. Exemplarily, a dictionary can be used, with the key being the identifier of the edge and the value being the betweenness centrality value. For each node group (node ​​1, node 2), where node 1 ≠ node 2, all shortest paths from node 1 to node 2 are calculated using a breadth-first search algorithm or a depth-first search algorithm. For each shortest path, determine the edges on the path, and add 1 to the betweenness centrality values ​​of these edges. Exemplarily, if the shortest path from node 1 to node 2 is (node ​​1→node 3→node 2), the betweenness centrality values ​​of the edges (node ​​1, node 3) and (node ​​3, node 2) are each added by 1. After calculating the shortest paths between all node groups, the betweenness centrality value of each edge is divided by the number of combinations of node groups to obtain a standardized betweenness centrality value. After calculating the betweenness centrality values ​​of all edges, find the edge with the highest betweenness centrality value. Exemplarily, the dictionary storing the betweenness centrality values ​​of the edges can be traversed to compare each value to determine the edge corresponding to the maximum value. The edge corresponding to the highest betweenness centrality value found is removed from the node structure organization. After removing the edge, a connectivity detection algorithm is used to identify the first node set in the node structure organization. Determine the first node set to which each node belongs. Exemplarily, the node structure organization is traversed from one node through a depth-first search, and all nodes that can reach each other constitute a first node set. Record the number or identifier of the first node set to which each node belongs to form a preliminary first clustering result. Create a second data structure to store information related to the first node set, including but not limited to the number of first node sets, all nodes contained in each first node set, and other information. Exemplarily, a list can be used, in which each element is a first node set, and the elements in the set are nodes in the set. Repeat the above steps of calculating the betweenness centrality value of the edge to determine the new first node set until the preset stopping condition is met. Exemplarily, the preset stopping condition may be that the betweenness centrality values ​​of the remaining edges in the code architecture diagram are all 0, that is, the edges that may affect the structure of the first node set have been removed. The preset stopping condition may be that the first node set no longer changes when it is iterated for a first preset number of times, that is, the first node set obtained after continuously removing the edges is the same. When the preset stopping condition is met, the final first clustering result is obtained. The first clustering result includes but is not limited to information such as the number or identifier of the first node set to which each node belongs, the number of the first node sets, and all the nodes contained in each first node set.

[0063] Specifically, clustering the nodes in the code architecture diagram based on the annotation semantic features to obtain the second clustering result may include: clustering the nodes in the code architecture diagram based on a second preset clustering algorithm and the annotation semantic features to obtain the second clustering result.

[0064] Exemplarily, the second preset clustering algorithm is taken as the semantic K-means clustering algorithm. A preset number of annotation semantic features are randomly selected from all annotation semantic features as the initial clustering center, wherein the preset number is the number of pre-set second node sets. Exemplarily, if the number of second node sets is set to 3, 3 different annotation semantic features are randomly selected from the annotation semantic features corresponding to all nodes, and used as the initial clustering centers (center 1, center 2, center 3) of the three second node sets respectively. For each node, the distance between its corresponding annotation semantic feature and each initial clustering center is calculated, and the node is assigned to the second node set corresponding to the nearest initial clustering center. The initial second node set of each node is recorded to form an initial second clustering result. For each second node set, the mean feature of the annotation semantic features of all nodes in the second node set is calculated and used as the new clustering center. That is, the annotation semantic features of all nodes in each second node set are added, and then divided by the number of all nodes corresponding to the second node set to obtain a new clustering center. Recalculate the distance between the annotation semantic feature of each node and the new cluster center, and reallocate the node to the second node set that is closest to it. If the node's affiliation has changed, mark the second node set structure as changed. Repeat the above steps of recalculating the cluster center and updating the node's affiliation to the second node set until a preset convergence condition is met. Exemplarily, the preset convergence condition may be that the moving distance of the cluster center in a second preset number of consecutive iterations is less than a preset threshold. The preset convergence condition may be that the node's affiliation no longer changes. When the preset convergence condition is met, the final second clustering result is obtained, which includes but is not limited to the number or identifier of the second node set to which each node belongs, as well as information such as the number of second node sets and all nodes contained in each second node set.

[0065] Specifically, clustering the nodes in the code architecture diagram based on code features to obtain a third clustering result may include: clustering the nodes in the code architecture diagram based on a third preset clustering algorithm and code features to obtain a third clustering result. Exemplarily, the third preset clustering algorithm may be a code vector K-means clustering algorithm. Clustering the nodes in the code architecture diagram based on the third preset clustering algorithm and code features to obtain the specific details of the third clustering result can be found in the above-mentioned clustering of the nodes in the code architecture diagram based on the second preset clustering algorithm and annotation semantic features to obtain the second clustering result, which will not be repeated here.

[0066] In step S109 , a target loss is determined based on the first clustering result, the second clustering result, and the third clustering result.

[0067] In a specific embodiment, Figure 2 As shown, the above-mentioned determination of the target loss based on the first clustering result, the second clustering result and the third clustering result includes:

[0068] In step S201, a semantic loss weight and a code expression loss weight are determined based on the first clustering result, the second clustering result and the third clustering result.

[0069] In a specific embodiment, Figure 3 As shown, the above-mentioned determination of the semantic loss weight and the code expression loss weight based on the first clustering result, the second clustering result and the third clustering result includes:

[0070] In step S301 , a semantic loss weight is determined based on the first clustering result and the second clustering result.

[0071] In a specific embodiment, Figure 4 As shown, the above-mentioned determination of the semantic loss weight based on the first clustering result and the second clustering result includes:

[0072] In step S401, the number of first nodes in the code architecture diagram, the number of second nodes corresponding to each of the multiple first node sets, the number of third nodes corresponding to each of the multiple second node sets, and the number of node intersections of any first node set and any second node set are obtained.

[0073] In a specific embodiment, the first number of nodes is the number of all nodes in the code architecture diagram. The second number of nodes is the number of all nodes included in each first node set. The third number of nodes is the number of all nodes included in each second node set. The node intersection number is the number of nodes in any first node set and any second node set at the same time.

[0074] In an optional embodiment, a depth-first search algorithm or a breadth-first search algorithm may be used to traverse the code architecture graph to obtain the first node number in the code architecture graph.

[0075] In an optional embodiment, each node in the first node set may be traversed, and for each node, it is checked whether it is also in the second node set. If it is, the node intersection number counter is incremented by 1, thereby obtaining the node intersection number.

[0076] In step S403, a semantic loss weight is determined based on the first node number, the second node number, the third node number, and the node intersection number.

[0077] In a specific embodiment, determining the semantic loss weight based on the first number of nodes, the second number of nodes, the third number of nodes, and the number of node intersections may include: based on the formula Determine the semantic loss weight, where ARI represents the semantic loss weight and n represents the total number of nodes, i.e. the number of first nodes; a i represents the number of nodes in the i-th first node set in the first clustering result, that is, the number of second nodes; b j Indicates the number of nodes in the jth second node set in the second clustering result, that is, the number of third nodes; n ij It represents the number of nodes that belong to both the i-th first node set in the first clustering result and the j-th second node set in the second clustering result, that is, the number of node intersections.

[0078] In step S303, based on the first clustering result and the third clustering result, the code expression loss weight is determined.

[0079] In a specific embodiment, the above-mentioned determination of the code expression loss weight based on the first clustering result and the third clustering result can refer to the above-mentioned determination of the semantic loss weight based on the first clustering result and the second clustering result, which will not be repeated here.

[0080] In the above embodiment, the first clustering result focuses on the dependency relationship between nodes and can more objectively reveal the interaction between nodes. The second clustering result focuses on text annotations, and the third clustering result focuses on the code itself. The semantic loss weight is determined by the first clustering result and the second clustering result, and the code expression loss weight is determined based on the first clustering result and the third clustering result. The code structure information reflected by the two different clustering results is combined to make the determination of the semantic loss weight and the code expression loss weight more representative and reliable, so that the importance of semantic loss and code expression loss in the target loss can be accurately measured, and the impact of semantics and code on the final clustering effect can be more accurately evaluated, so that the target loss subsequently calculated based on this weight can more accurately reflect the degree of retention or deviation of semantics and code in the clustering process, thereby providing a more valuable reference for optimizing the clustering results.

[0081] In step S203, the code architecture diagram is traversed to obtain the node interaction status and architecture complexity.

[0082] In an optional embodiment, a depth-first search algorithm or a breadth-first search algorithm may be used to traverse the code architecture graph.

[0083] In step S205, the modularity score, semantic loss and code expression loss are determined based on the node structure organization, node interaction status, architecture complexity, annotation semantic features, code features, the first clustering result, the second clustering result and the third clustering result.

[0084] In a specific embodiment, Figure 5 As shown, the above-mentioned determination of modularity score, semantic loss and code expression loss based on node structure organization, node interaction status, architecture complexity, annotation semantic features, code features, first clustering results, second clustering results and third clustering results includes:

[0085] In step S501, a modularity score is determined based on the node structure organization, the node interaction status, the architecture complexity and the first clustering result.

[0086] In a specific embodiment, the node interaction status includes a first edge association quantity corresponding to the node group and a second edge association quantity corresponding to the node group.

[0087] In a specific embodiment, the determining of the modularity score based on the node structure organization, the node interaction status, the architecture complexity and the first clustering result includes:

[0088] A modularity score is determined based on the node structural organization, the number of first edge associations, the number of second edge associations, the architectural complexity, and the first clustering results.

[0089] In a specific embodiment, the above-mentioned node group includes any first node in the code architecture diagram and any second node in the code architecture diagram, the first edge association number is used to indicate the number of edges connected to the first node, the second edge association number is used to indicate the number of edges connected to the second node, and the architecture complexity is used to indicate the number of edges in the code architecture diagram.

[0090] In a specific embodiment, determining the modularity score based on the node structure organization, the first edge association number, the second edge association number, the architecture complexity and the first clustering result may include: based on the formula Determine the modularity score, where Q represents the modularity score and A ij Represents the connection relationship between node i and node j. If there is an edge between node i and node j, then A ij =1; if there is no edge between node i and node j, then A ij =0; k i is the number of edges directly connected to node i, k j represents the number of edges directly connected to node j, m represents the total number of edges in the code architecture graph, δ(C i ,C j ) is an indicator function. If i and j belong to the same first node set in the first clustering result, then δ(c i ,c j )=1, otherwise it is 0. k represents the first node set number.

[0091] In step S503, the semantic loss is determined based on the annotation semantic features and the second clustering result.

[0092] In a specific embodiment, Figure 6 As shown, based on the annotation semantic features and the second clustering results, the semantic loss is determined as follows:

[0093] In step S601, the number of third nodes corresponding to each of the plurality of second node sets is obtained.

[0094] In step S603, based on the annotation semantic features and the number of third nodes, the mean features corresponding to each of the plurality of second node sets are determined.

[0095] In a specific embodiment, the above-mentioned determination of the mean features corresponding to each of the multiple second node sets based on the annotation semantic features and the number of third nodes may include: adding the annotation semantic features of all nodes in each second node set, and then dividing by the number of third nodes corresponding to the second node set to determine the mean features corresponding to the second node set.

[0096] In step S605 , the semantic loss is determined based on the annotation semantic features and the mean features.

[0097] In a specific embodiment, the above-mentioned determination of semantic loss based on annotation semantic features and mean features may include: based on the formula Determine the semantic loss, where l represents the semantic loss, x represents the annotation semantic feature, and μ i represents the mean feature corresponding to each second node set after being divided into k second node sets, and k represents the number of the second node set.

[0098] In step S505, the code expression loss is determined based on the code features and the third clustering result.

[0099] In a specific embodiment, the above-mentioned determination of code expression loss based on code features and the third clustering result can refer to the above-mentioned determination of semantic loss based on annotation semantic features and the second clustering result, which will not be repeated here.

[0100] In step S207, the target loss is determined based on the semantic loss weight, the code expression loss weight, the preset dependency weight, the modularity score, the semantic loss and the code expression loss.

[0101] In a specific embodiment, the above-mentioned preset dependency weight can be preset to a value of 1.

[0102] In a specific embodiment, the above-mentioned determination of the target loss based on the semantic loss weight, the code expression loss weight, the preset dependency weight, the modularity score, the semantic loss and the code expression loss may include: determining the target loss based on the formula y=λ1l-λ2Q+λ3l′, wherein y represents the target loss, λ1 represents the semantic loss weight, λ2 represents the preset dependency weight, λ3 represents the code expression loss weight, l represents the semantic loss, Q represents the modularity score, and l ′ Indicates the loss of code expression.

[0103] In the above embodiment, based on the first clustering result, the second clustering result, and the third clustering result, the semantic loss weight and the code expression loss weight are determined, and the information of multiple clustering results is integrated to fully consider the code structure characteristics reflected by different clustering methods, so that the weight determination is more scientific and reasonable, avoiding the dominant weight distribution of a single factor, and more in line with the actual characteristics of the code. In addition, different clustering results have their own advantages and disadvantages in reflecting the code structure. By determining the weight through comprehensive analysis, the role of high-quality clustering results in the overall evaluation can be highlighted, so that the subsequent loss calculation can more accurately reflect the impact of various aspects of the code on the clustering effect, and provide a more reliable basis for optimizing clustering. Based on the node structure organization, node interaction status, architecture complexity, annotation semantic features, code features, the first clustering result, the second clustering result, and the third clustering result, the modularity score, semantic loss, and code expression loss are determined, so that the loss calculation is more comprehensive and accurate, not only considering the characteristics of the node itself, but also taking into account the overall architecture impact. By integrating the semantic loss weight, code expression loss weight, preset dependency weight, modularity score, semantic loss, and code expression loss, the target loss is calculated. The modularity score determined based on multiple factors can effectively measure the degree of fit between the clustering results and the ideal modular structure. The semantic loss and code expression loss accurately measure the degree of deviation of clustering from the original information from the perspective of semantic understanding and code implementation. The loss value obtained by combining the three can more comprehensively and accurately evaluate the clustering effect and provide a clear direction for subsequent optimization.

[0104] In step S111, based on the target loss, the current clustering results corresponding to the nodes in the code architecture diagram are generated.

[0105] In step S113, the first clustering result, the second clustering result and the third clustering result are all updated to the current clustering result, and the process jumps to the step of determining the target loss based on the first clustering result, the second clustering result and the third clustering result, until the current target loss meets the preset convergence condition, and the corresponding current clustering result is output.

[0106] In a specific embodiment, the above-mentioned step of updating the first clustering result, the second clustering result and the third clustering result as the current clustering result, and jumping to the step of determining the target loss based on the first clustering result, the second clustering result and the third clustering result includes:

[0107] The first clustering result, the second clustering result and the third clustering result are all updated as the current clustering result, and jump to the step of determining the modularity score, semantic loss and code expression loss based on the node structure organization, node interaction status, architecture complexity, annotation semantic features, code features, the first clustering result, the second clustering result and the third clustering result.

[0108] In a specific embodiment, the above-mentioned target loss is based on generating the current clustering result corresponding to the node in the code architecture diagram, and the first clustering result, the second clustering result and the third clustering result are all updated as the current clustering result, and jump to the step of determining the target loss based on the first clustering result, the second clustering result and the third clustering result, until the current target loss meets the preset convergence condition, and the output of the corresponding current clustering result may include: using a genetic algorithm to randomly generate an initial clustering result, which is used as the initial population of the genetic algorithm. Each clustering result uses an integer code to represent the cluster number to which the node belongs. The coding length is the total number of nodes, and each number is randomly selected between 1 and the preset number of clusters k. The fitness of each clustering result is calculated according to the target loss function minλ1l-λ2Q+λ3l′ Using methods such as roulette selection, the selection probability is calculated according to the fitness of the clustering results, and the clustering results with high fitness are selected as the parents. For the selected parents, a crossover operation is performed with a certain probability. The intersection point is randomly selected, and the clustering number parts of the two parents after the intersection point are exchanged to generate new offspring. Each clustering result is mutated with a small probability, and a node is randomly selected to change its clustering number to a random value between 1 and k. A new generation of population is generated through selection, crossover and mutation operations. Repeat the above fitness evaluation, selection, crossover and mutation steps to continuously optimize the clustering results in the population. Determine whether to stop iteration according to the preset convergence condition. Exemplarily, the preset convergence condition can be any one of the convergence conditions such as reaching the maximum number of iterations, the average fitness of the population has no obvious change for several consecutive generations, and the target loss meets the accuracy requirements. If the preset convergence condition is met, the current optimal clustering result is output; otherwise, the iteration continues until the condition is met.

[0109] Figure 7 FIG. 1 is a block diagram of a software clustering device according to an exemplary embodiment. Figure 7 , the device comprises:

[0110] A code architecture diagram acquisition module 710 is used to acquire a code architecture diagram of the original code;

[0111] A feature acquisition module 720 is used to acquire, based on the code architecture diagram, the annotation semantic features corresponding to each node in the code architecture diagram and the code features corresponding to each node;

[0112] The node structure organization acquisition module 730 is used to generate the node structure organization corresponding to the code architecture diagram;

[0113] The clustering result acquisition module 740 is used to cluster the nodes in the code architecture diagram based on the node structure organization, annotation semantic features and code features, and obtain a first clustering result, a second clustering result and a third clustering result;

[0114] A target loss acquisition module 750, configured to determine a target loss based on the first clustering result, the second clustering result, and the third clustering result;

[0115] The current clustering result acquisition module 760 is used to generate the current clustering result corresponding to the node in the code architecture diagram based on the target loss;

[0116] The current clustering result output module 770 is used to update the first clustering result, the second clustering result and the third clustering result to the current clustering result, and jump to the target loss acquisition module 750 until the current target loss meets the preset convergence condition, and output the corresponding current clustering result.

[0117] In a specific embodiment, the target loss acquisition module 750 includes:

[0118] A loss weight acquisition unit, used to determine a semantic loss weight and a code expression loss weight based on the first clustering result, the second clustering result and the third clustering result;

[0119] A code architecture diagram information acquisition unit is used to traverse the code architecture diagram to obtain node interaction status and architecture complexity;

[0120] A data acquisition unit, for determining a modularity score, a semantic loss, and a code expression loss based on node structure organization, node interaction status, architecture complexity, annotation semantic features, code features, a first clustering result, a second clustering result, and a third clustering result;

[0121] A target loss acquisition unit, used for determining a target loss based on a semantic loss weight, a code expression loss weight, a preset dependency weight, a modularity score, a semantic loss, and a code expression loss;

[0122] The above-mentioned current clustering result output module 770 includes:

[0123] The jump unit is used to update the first clustering result, the second clustering result and the third clustering result to the current clustering result, and jump to the step of the data acquisition unit.

[0124] In a specific embodiment, the loss weight acquisition unit includes:

[0125] A semantic loss weight acquisition first subunit is used to determine a semantic loss weight based on the first clustering result and the second clustering result;

[0126] The code expression loss weight acquisition first subunit is used to determine the code expression loss weight based on the first clustering result and the third clustering result.

[0127] In a specific embodiment, the semantic loss weight acquisition first subunit includes:

[0128] A node information acquisition subunit, used to acquire the number of first nodes in the code architecture diagram, the number of second nodes corresponding to each of the plurality of first node sets, the number of third nodes corresponding to each of the plurality of second node sets, and the number of node intersections between any first node set and any second node set;

[0129] The semantic loss weight acquisition second subunit is used to determine the semantic loss weight based on the first node number, the second node number, the third node number and the node intersection number.

[0130] In a specific embodiment, the data acquisition unit includes:

[0131] A modularity score acquisition first subunit is used to determine a modularity score based on the node structure organization, the node interaction status, the architecture complexity and the first clustering result;

[0132] A semantic loss acquisition first subunit is used to determine a semantic loss based on the annotated semantic features and the second clustering result;

[0133] The code expression loss acquisition subunit is used to determine the code expression loss based on the code features and the third clustering result.

[0134] In a specific embodiment, the modularity score obtaining first subunit includes:

[0135] The modularity score acquisition second subunit is used to determine the modularity score based on the node structure organization, the first edge association number, the second edge association number, the architecture complexity and the first clustering result.

[0136] In a specific embodiment, the semantic loss acquisition first subunit includes:

[0137] The third node quantity obtaining subunit is used to obtain the third node quantity corresponding to each of the plurality of second node sets.

[0138] A mean feature acquisition subunit, used to determine the mean features corresponding to each of the plurality of second node sets based on the annotation semantic features and the number of third nodes;

[0139] Semantic Loss The third subunit is used to determine the semantic loss based on the annotation semantic features and the mean features.

[0140] In a specific embodiment, the feature acquisition module 720 includes:

[0141] An annotation semantic feature acquisition unit, used to input the text annotation information corresponding to each node into a preset feature extraction model to generate an annotation semantic feature corresponding to each node;

[0142] The code feature acquisition unit is used to input the code information corresponding to each node into a preset feature extraction model to generate the code feature corresponding to each node.

[0143] Regarding the device in the above embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and will not be elaborated here.

[0144] Figure 8 is a block diagram of an electronic device for software clustering according to an exemplary embodiment. The electronic device may be a server, and its internal structure diagram may be as shown in FIG. Figure 8 As shown. The electronic device includes a processor, a memory, a network interface, a display screen and an input device connected via a system bus. Among them, the processor of the electronic device is used to provide computing and control capabilities. The memory of the electronic device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The network interface of the electronic device is used to communicate with an external terminal via a network connection. When the computer program is executed by the processor, a software clustering method is implemented. The display screen of the electronic device can be a liquid crystal display screen or an electronic ink display screen, and the input device of the electronic device can be a touch layer covering the display screen, or a key, trackball or touchpad provided on the housing of the electronic device, or an external keyboard, touchpad or mouse, etc.

[0145] Those skilled in the art will understand that Figure 8 The structure shown in the figure is merely a block diagram of a partial structure related to the scheme of the present disclosure, and does not constitute a limitation on the electronic device to which the scheme of the present disclosure is applied. The specific electronic device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0146] In an exemplary embodiment, an electronic device is also provided, including: a processor; and a memory for storing instructions executable by the processor; wherein the processor is used to execute the instructions to implement the software clustering method in the embodiment of the present disclosure.

[0147] In an exemplary embodiment, a computer-readable storage medium is further provided. When instructions in the storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the software clustering method in the embodiment of the present disclosure.

[0148] Those skilled in the art will readily appreciate other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of the present disclosure that follow the general principles of the present disclosure and include common knowledge or customary techniques in the art that are not disclosed in the present disclosure. The specification and examples are intended to be exemplary only, and the true scope and spirit of the present disclosure are indicated by the following claims.

[0149] It should be understood that the present disclosure is not limited to the exact structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.

Claims

1. A software clustering method, characterized in that: include: Obtain a code architecture diagram of the original code, wherein the code architecture diagram is a structure diagram with code components as nodes and dependencies between the code components as edges; Based on the code architecture diagram, obtaining annotation semantic features corresponding to each node in the code architecture diagram and code features corresponding to each node; Generate a node structure organization corresponding to the code architecture diagram, where the node structure organization is used to indicate a connection relationship between any two nodes in the code architecture diagram; Based on the node structure organization, the annotation semantic features and the code features, clustering the nodes in the code architecture diagram respectively to obtain a first clustering result, a second clustering result and a third clustering result; Determining a target loss based on the first clustering result, the second clustering result, and the third clustering result; Based on the target loss, generate a current clustering result corresponding to the node in the code architecture diagram; The first clustering result, the second clustering result and the third clustering result are all updated as the current clustering result, and the step of determining the target loss based on the first clustering result, the second clustering result and the third clustering result is jumped to, until the current target loss meets the preset convergence condition, and the corresponding current clustering result is output.

2. The method according to claim 1, characterized in that: The determining the target loss based on the first clustering result, the second clustering result, and the third clustering result includes: Determine a semantic loss weight and a code expression loss weight based on the first clustering result, the second clustering result, and the third clustering result; Traversing the code architecture diagram to obtain node interaction status and architecture complexity; Determine a modularity score, a semantic loss, and a code expression loss based on the node structure organization, the node interaction status, the architecture complexity, the annotation semantic features, the code features, the first clustering result, the second clustering result, and the third clustering result; Determining the target loss based on the semantic loss weight, the code expression loss weight, the preset dependency weight, the modularity score, the semantic loss, and the code expression loss; The step of updating the first clustering result, the second clustering result, and the third clustering result as the current clustering result, and jumping to the step of determining the target loss based on the first clustering result, the second clustering result, and the third clustering result includes: The first clustering result, the second clustering result and the third clustering result are all updated as the current clustering result, and the process jumps to the step of determining the modularity score, semantic loss and code expression loss based on the node structure organization, the node interaction status, the architecture complexity, the annotation semantic features, the code features, the first clustering result, the second clustering result and the third clustering result.

3. The method according to claim 2, characterized in that: The determining of the semantic loss weight and the code expression loss weight based on the first clustering result, the second clustering result and the third clustering result includes: Determining the semantic loss weight based on the first clustering result and the second clustering result; Based on the first clustering result and the third clustering result, the code expression loss weight is determined.

4. The method according to claim 3, characterized in that: The first clustering result includes a plurality of first node sets, the second clustering result includes a plurality of second node sets, and the determining the semantic loss weight based on the first clustering result and the second clustering result includes: Obtain the number of first nodes in the code architecture diagram, the number of second nodes corresponding to each of the multiple first node sets, the number of third nodes corresponding to each of the multiple second node sets, and the number of node intersections of any first node set and any second node set; The semantic loss weight is determined based on the first number of nodes, the second number of nodes, the third number of nodes, and the number of node intersections.

5. The method according to claim 2, characterized in that: The determining of the modularity score, the semantic loss, and the code expression loss based on the node structure organization, the node interaction status, the architecture complexity, the annotation semantic features, the code features, the first clustering result, the second clustering result, and the third clustering result includes: Determining the modularity score based on the node structure organization, the node interaction status, the architecture complexity and the first clustering result; Determining the semantic loss based on the annotation semantic feature and the second clustering result; Based on the code features and the third clustering result, the code expression loss is determined.

6. The method according to claim 5, characterized in that: The node interaction status includes a first edge association number corresponding to a node group and a second edge association number corresponding to the node group, and the determining the modularity score based on the node structure organization, the node interaction status, the architecture complexity and the first clustering result includes: The modularity score is determined based on the node structure organization, the first edge association number, the second edge association number, the architecture complexity and the first clustering result, the node group includes any first node in the code architecture diagram and any second node in the code architecture diagram, the first edge association number is used to indicate the number of edges connected to the first node, the second edge association number is used to indicate the number of edges connected to the second node, and the architecture complexity is used to indicate the number of edges in the code architecture diagram.

7. The method according to claim 5, characterized in that: The second clustering result includes a plurality of second node sets, and the determining the semantic loss based on the annotation semantic feature and the second clustering result includes: Obtain the number of third nodes corresponding to each of the plurality of second node sets; Based on the annotation semantic feature and the number of the third nodes, determining the mean feature corresponding to each of the plurality of second node sets; The semantic loss is determined based on the annotation semantic feature and the mean feature.

8. The method according to claim 1, characterized in that: The acquiring, based on the code architecture diagram, the annotation semantic features corresponding to each node in the code architecture diagram and the code features corresponding to each node in the code architecture diagram comprises: Inputting the text annotation information corresponding to each node into a preset feature extraction model to generate annotation semantic features corresponding to each node; The code information corresponding to each node is input into the preset feature extraction model to generate the code feature corresponding to each node.

9. A software clustering device, characterized in that: include: A code architecture diagram acquisition module is used to acquire a code architecture diagram of the original code, wherein the code architecture diagram is a structure diagram with code components as nodes and dependencies between the code components as edges; A feature acquisition module, used to acquire, based on the code architecture diagram, an annotation semantic feature corresponding to each node in the code architecture diagram and a code feature corresponding to each node; A node structure organization acquisition module, used to generate a node structure organization corresponding to the code architecture diagram, wherein the node structure organization is used to indicate a connection relationship between any two nodes in the code architecture diagram; A clustering result acquisition module, used to cluster the nodes in the code architecture diagram based on the node structure organization, the annotation semantic features and the code features, to obtain a first clustering result, a second clustering result and a third clustering result; a target loss acquisition module, configured to determine a target loss based on the first clustering result, the second clustering result, and the third clustering result; A current clustering result acquisition module, used to generate a current clustering result corresponding to a node in the code architecture diagram based on the target loss; The current clustering result output module is used to update the first clustering result, the second clustering result and the third clustering result to the current clustering result, and jump to the step of determining the target loss based on the first clustering result, the second clustering result and the third clustering result, until the current target loss meets the preset convergence condition, and output the corresponding current clustering result.

10. An electronic device, characterized in that: include: processor; a memory for storing instructions executable by the processor; The processor is configured to execute the instructions to implement the software clustering method according to any one of claims 1 to 8.