Code retrieval methods, apparatus and equipment

By using pre-trained Chinese and English language representation models for semantic vector space mapping, the problem of difficulty in retrieving synonyms and uncommented code in code retrieval is solved, thereby improving the accuracy and efficiency of code retrieval.

CN116521955BActive Publication Date: 2026-03-13CHINA CONSTRUCTION BANK +1
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-05-05
Publication Date
2026-03-13

AI Technical Summary

Technical Problem

In existing technologies, code retrieval cannot accurately retrieve synonyms or near-synonyms of the keywords actually appearing in the code description, and code lacking comments or documentation is difficult to retrieve, resulting in significant limitations, low efficiency, and poor accuracy in retrieval.

Method used

It employs pre-trained language representation models based on Chinese (e.g., Chinese BERT) and English (e.g., English BERT), calculates the similarity between user request statements and code semantic vectors through word segmentation and semantic vector space mapping, generates a list of similar codes, and obtains the target code based on user instructions.

Benefits of technology

It improves the accuracy and flexibility of code retrieval, and enhances the accuracy and efficiency of code retrieval through deep retrieval.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116521955B_ABST
    Figure CN116521955B_ABST
Patent Text Reader

Abstract

This application provides a code retrieval method, apparatus, and device, relating to the field of computer technology. The method includes: reading a user request statement and determining a target semantic vector corresponding to the user request statement; calculating the similarity of various code semantic vectors stored in a preset database based on the target semantic vector, determining a list of similar vectors corresponding to the target semantic vector, and determining a list of similar codes corresponding to the list of similar vectors; responding to a user instruction, determining a target code block in the list of similar codes, and obtaining the target code corresponding to the target code block. This method, based on spatial semantic vectors, achieves deep code retrieval, improving the accuracy and flexibility of code retrieval.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and in particular to a code retrieval method, apparatus, and device. Background Technology

[0002] Open-source code platforms provide developers with an environment to share and exchange code. More and more code is being shared on open-source code platforms, which improves code reusability and enables the effective use of code.

[0003] In existing technologies, keywords are used to retrieve code from a code platform.

[0004] However, in existing technologies, when the search keywords are synonyms or near-synonyms of the keywords actually appearing in the code description, such code cannot be found; when the code lacks comments or documentation, it is difficult to retrieve. Therefore, code retrieval has significant limitations and makes accurate code retrieval difficult to achieve. Summary of the Invention

[0005] This application provides a code retrieval method, apparatus, and device to solve the problem of difficulty in accurately retrieving codes.

[0006] Firstly, this application provides a code retrieval method, the method comprising:

[0007] Read the user request statement and determine the target semantic vector corresponding to the user request statement; wherein, the user request statement is used to represent the user's retrieval needs for the target code;

[0008] Based on the target semantic vector, similarity is calculated for each code semantic vector stored in a preset database to determine a list of similar vectors corresponding to the target semantic vector, and a list of similar codes corresponding to the list of similar vectors is determined; wherein, the list of similar vectors includes a preset number of similar semantic vectors, and each similar semantic vector in the list of similar vectors is arranged in descending order of similarity to the target semantic vector, and each code block in the list of similar codes corresponds to each similar semantic vector in the list of similar vectors;

[0009] In response to a user instruction, a target code block in the list of similar codes is determined, and the target code corresponding to the target code block is obtained; wherein, the user instruction is used to indicate the target code block in the list of similar codes.

[0010] In one optional implementation, determining the target semantic vector corresponding to the user request statement includes:

[0011] Based on the word segmentation model in the first preset model, the user request statement is segmented to determine the character vector corresponding to each character in the user request statement, and the statement feature matrix corresponding to the user request statement is determined; wherein, the statement feature matrix is ​​used to characterize the semantic feature information of the user request statement.

[0012] Based on the Chinese pre-training parameters in the first preset model, the semantic vector space is mapped to the sentence feature matrix to determine the target semantic vector corresponding to the user demand sentence, wherein the first preset model is a Chinese pre-trained language representation model.

[0013] In one optional implementation, the method further includes:

[0014] Obtain the first training set; wherein, the first training set includes multiple training documents, and the training documents are explanatory documents corresponding to financial business codes;

[0015] Based on the Chinese pre-trained language representation model, semantic vector mapping is performed on each training document in the first training set, and the mapping loss corresponding to each training document is determined; wherein, the mapping loss is used to characterize the error of semantic vector mapping.

[0016] Based on the mapping loss of the Chinese pre-trained language representation model for each training document in the first training set, the Chinese pre-training parameters in the Chinese pre-trained language representation model are optimized to determine the first preset model.

[0017] In one optional implementation, the method further includes:

[0018] Based on the word segmentation model in the second preset model, each code in the code library is segmented, the character vector corresponding to each character in each code is determined, and the code feature matrix corresponding to each code is determined; wherein, the code feature matrix is ​​used to characterize the semantic feature information of the corresponding code;

[0019] Based on the English pre-training parameters in the second preset model, the semantic vector space mapping is performed on the code feature matrix corresponding to each code in the code library to determine the code semantic vector corresponding to each code in the code library; wherein, the second preset model is an English pre-trained language representation model;

[0020] The code path information corresponding to each code semantic vector is stored in the preset database.

[0021] In one optional implementation, the method further includes:

[0022] Obtain the second training set; wherein, the second training set includes multiple training codes and code description documents corresponding to each code;

[0023] Based on the second training set and the first preset model, the second preset model is trained; wherein, the first preset model is a Chinese pre-trained language representation model.

[0024] In one optional implementation, model training is performed on the second preset model based on the second training set and the first preset model, including:

[0025] Based on the first preset model, a first vector of the code description document corresponding to the training code is determined; wherein, the first vector is the semantic vector corresponding to the code description document;

[0026] Based on the second preset model, a second vector corresponding to the training code is determined; wherein, the second vector is a semantic vector corresponding to the training code;

[0027] Calculate the spatial distance between the first vector and the second vector; wherein the spatial distance is used to characterize the vector loss value;

[0028] Based on the spatial distance, the pre-training parameters in the second preset model are corrected.

[0029] In one optional implementation, in response to a user instruction, determining the target code block in the similar code list and obtaining the target code corresponding to the target code block includes:

[0030] In response to a user instruction, the target code block in the list of similar codes is determined;

[0031] The target code is scheduled and displayed according to the code storage path corresponding to the target code block stored in the preset database; wherein, the preset database stores the code storage path corresponding to each code block.

[0032] Secondly, this application provides a code retrieval device, the device comprising:

[0033] A determining unit is used to read user request statements and determine the target semantic vector corresponding to the user request statements; wherein, the user request statements are used to characterize the user's retrieval requirements for target code;

[0034] The first processing unit is configured to calculate the similarity of each code semantic vector stored in a preset database based on the target semantic vector, determine the similar vector list corresponding to the target semantic vector, and determine the similar code list corresponding to the similar vector list; wherein, the similar vector list includes a preset number of similar semantic vectors, and each similar semantic vector in the similar vector list is arranged in descending order of similarity to the target semantic vector, and each code block in the similar code list corresponds to each similar semantic vector in the similar vector list;

[0035] The second processing unit is configured to, in response to a user instruction, determine a target code block in the similar code list and obtain the target code corresponding to the target code block; wherein the user instruction is used to indicate the target code block in the similar code list.

[0036] Thirdly, this application provides an electronic device, which includes a memory and a processor;

[0037] The memory is used to store computer programs;

[0038] The processor is configured to read the computer program stored in the memory and execute the code retrieval method as described in the first aspect based on the computer program in the memory.

[0039] Fourthly, this application provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the code retrieval method as described in the first aspect.

[0040] Fifthly, this application provides a computer program product, including a computer program that, when executed by a processor, implements the code retrieval method as described in the first aspect.

[0041] The code retrieval method, apparatus, and device provided in this application, through the following steps: reading a user request statement and determining the target semantic vector corresponding to the user request statement; calculating the similarity of various code semantic vectors stored in a preset database based on the target semantic vector, determining a list of similar vectors corresponding to the target semantic vector, and determining a list of similar codes corresponding to the list of similar vectors; responding to a user instruction, determining the target code block in the list of similar codes, and obtaining the target code corresponding to the target code block. This method, based on spatial semantic vectors, achieves deep code retrieval, improving the accuracy and flexibility of code retrieval. Attached Figure Description

[0042] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.

[0043] Figure 1 A flowchart illustrating a code retrieval method provided in this application embodiment;

[0044] Figure 2 A flowchart illustrating another code retrieval method provided in this application embodiment;

[0045] Figure 3 A flowchart illustrating a model training method provided in this application embodiment;

[0046] Figure 4 This is a schematic diagram of the structure of a code retrieval device provided in an embodiment of this application;

[0047] Figure 5 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application;

[0048] Figure 6 This is a block diagram of an electronic device provided in an embodiment of this application.

[0049] The accompanying drawings illustrate specific embodiments of this application, which will be described in more detail below. These drawings and descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concept of this application to those skilled in the art through reference to particular embodiments. Detailed Implementation

[0050] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.

[0051] The collection, storage, use, processing, transmission, provision, and disclosure of financial data or user data involved in the technical solution of this application all comply with the provisions of relevant laws and regulations and do not violate public order and good morals.

[0052] Open-source code platforms provide researchers with an environment for sharing and exchanging code. More and more code is being shared on these platforms, improving code reusability and enabling effective use of the code.

[0053] Currently, major code platforms, such as open-source code repositories (GitHub), still use keyword search. When the searched keywords are synonyms or near-synonyms of the keywords actually appearing in the code description—that is, when the meaning is the same but the characters are different—such code cannot be found. Or, when the code has no comments or documentation at all, such code is even more difficult to find. Furthermore, keyword-based code search often ignores the user's code search intent during matching, only retrieving code that is superficially similar. Therefore, current keyword-based code search has significant limitations, low search efficiency, and poor accuracy.

[0054] In one example, the field of Natural Language Processing (NLP) has gone through three stages: 1) Statistical machine learning stage: This stage assigns the probability of a natural language text forming a sentence, uses frequency to represent word importance, and uses probability to represent the document formation process; 2) Word vector model (word2vec) stage: Word2vec is a relevant model used to generate word vectors, mapping words to a vector space. Common models include the Continuous Bag-of-Words (CBOW) model and the skip-gram model, which predict the head word from the context and the context from the head word, respectively. Its advantage is that it changes the traditional approach of simply numbering words, making similar words closer in high-dimensional space and transforming the sparse matrix representing words into a denser matrix. Its disadvantage is that after training, words and vectors are in a one-to-one correspondence, making it impossible to obtain word vectors appropriate for the context and thus unable to achieve polysemy; 3) Bidirectional Encoder Representation Model based on the transformer model. The Transformers (BERT) stage: Proposed by Google, Transformers offer an attention-based architecture for sequence modeling. Its model structure primarily consists of an encoder-decoder architecture with an attention mechanism, which addresses issues like long connections and contextual information. BERT, on the other hand, is an end-to-end model that uses only the encoder module of the Transformer architecture and employs an attention mechanism. Therefore, BERT is currently considered the best model in NLP, achieving top performance in most natural language tasks.

[0055] In one example, the open-source machine learning library PyTorch, used for programs such as natural language processing, offers the following features: 1) It supports accelerated computation using chips with graphics processing units (GPUs) capable of accelerating deep learning calculations; 2) It provides various tensor operations and can automatically calculate gradients through automatic differentiation; 3) Compared to another framework, the symbolic mathematics system TensorFlow, it is more concise, more readable, and more in line with human thinking; 4) It is tightly integrated with the Python programming language and can seamlessly connect with other Python packages, such as the open-source scientific computing library NumPy; 6) Installation is simple. Therefore, developing with PyTorch as the framework has many advantages, effectively improving development efficiency and enhancing the development experience.

[0056] In one example, the application framework (Flask) is a lightweight World Wide Web (web) application framework written in Python. Compared with other similar web frameworks, it is more flexible, lightweight, and easier to learn. Its code is concise and extensible, with good documentation, rich plugins, and high security.

[0057] Therefore, this application provides a code retrieval method that uses the PyTorch framework, based on the BERT model, to perform semantic space mapping of user request statements and code, as well as the calculation of similarity between semantic vectors. It also uses the Flask framework to build a server that provides a retrieval interface. When a user request is received, the method receives the user request statement, calculates the code that is closest to the user request, and returns the corresponding result, thus solving the above technical problems.

[0058] The technical solution of this application and how the technical solution of this application solves the above-mentioned technical problems are described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments. The embodiments of this application will now be described with reference to the accompanying drawings.

[0059] Figure 1 A flowchart of a code retrieval method provided in this application embodiment is shown below. Figure 1 As shown, the method includes:

[0060] 101. Read the user request statement and determine the target semantic vector corresponding to the user request statement; wherein, the user request statement is used to represent the user's retrieval requirements for the target code.

[0061] For example, the executing entity of this embodiment can be an electronic device, a mobile terminal, a cloud server, a terminal device, or other apparatus or device capable of executing this embodiment, and there is no limitation thereto. This embodiment is described with an electronic device as the executing entity.

[0062] For example, an electronic device obtains user request statements based on user interface, interface, etc., and determines the target semantic vector corresponding to the user request statements in the semantic vector space based on natural language processing technology. The user request statements are used to represent the user's retrieval needs for target code, such as the target code, as well as parameters and information such as the number of similar code blocks corresponding to the user's needs included in the code retrieval results.

[0063] 102. Based on the target semantic vector, perform similarity calculations on the semantic vectors of each code stored in the preset database, determine the list of similar vectors corresponding to the target semantic vector, and determine the list of similar codes corresponding to the list of similar vectors; wherein, the list of similar vectors includes a preset number of similar semantic vectors, and each similar semantic vector in the list of similar vectors is arranged in descending order of similarity to the target semantic vector, and each code block in the list of similar codes corresponds to each similar semantic vector in the list of similar vectors.

[0064] For example, the similarity between each code semantic vector in the preset database and the target semantic vector is calculated, such as vector cosine similarity, to determine the similarity between each code semantic vector and the target semantic vector. Then, based on the similarity between each code semantic vector in the preset database and the target semantic vector, a list of similar vectors corresponding to the target semantic vector is determined. This list includes a preset number of similar semantic vectors, which are code semantic vectors in the preset database that have a similarity relationship to the target semantic vector. The similar semantic vectors in the list are arranged in descending order of their similarity to the target semantic vector, with the first similar semantic vector being the vector in the preset database with the highest similarity to the target semantic vector. Since each code semantic vector in the preset database corresponds to its own represented code block, the list of similar codes corresponding to the target semantic vector can be determined through the list of similar vectors. Similarly, in the list of similar codes, a preset number of code blocks are arranged in descending order of their similarity to the target semantic vector. The preset database stores multiple code blocks and the corresponding code semantic vectors for each code block. A code block can be a storage path for code or a part of code.

[0065] 103. In response to a user instruction, determine the target code block in the similar code list and obtain the target code corresponding to the target code block; wherein, the user instruction is used to indicate the target code block in the similar code list.

[0066] For example, after determining the code similarity list corresponding to the target semantic vector, the target code block that meets the user's needs can be determined based on user instructions such as user selection, and the target code corresponding to the target code block can be obtained.

[0067] In summary, the code retrieval method provided in this embodiment involves the following steps: reading the user's request statement and determining the target semantic vector corresponding to the user's request statement; calculating the similarity of each code semantic vector stored in a preset database based on the target semantic vector, determining a list of similar vectors corresponding to the target semantic vector, and determining a list of similar codes corresponding to the list of similar vectors; responding to the user's instruction, determining the target code block in the list of similar codes, and obtaining the target code corresponding to the target code block. This method, based on spatial semantic vector mapping, achieves deep code retrieval, improving the accuracy and flexibility of code retrieval.

[0068] Figure 2 A flowchart of another code retrieval method provided in the embodiments of this application is shown below. Figure 2 As shown, the method includes:

[0069] 201. Read the user's request statement, perform word segmentation on the user's request statement based on the word segmentation model in the first preset model, determine the character vector corresponding to each character in the user's request statement, and determine the statement feature matrix corresponding to the user's request statement; wherein, the statement feature matrix is ​​used to represent the semantic feature information of the user's request statement, and the first preset model is a Chinese pre-trained language representation model.

[0070] For example, the electronic device obtains the user's request statement for code retrieval, and performs word segmentation on the user's request statement based on the word segmentation model in the Chinese pre-trained language model, determines the character vector corresponding to each character in the user's request statement, and then determines the statement feature matrix corresponding to the user's request statement from each character vector.

[0071] In one example, a server can be built based on the Flask framework to provide a retrieval interface. Based on the interface, the user's request statement is obtained. When a user makes a request, the user's request parameters and statements are received. Table 1 is an interface request parameter format table provided in an embodiment of this application. As shown in Table 1, the user defines the required and optional parameters in the request interface according to the interface format requirements.

[0072] Table 1 Interface Request Parameter Format Table

[0073]

[0074]

[0075] 202. Based on the Chinese pre-training parameters in the first preset model, perform semantic vector space mapping on the sentence feature matrix to determine the target semantic vector corresponding to the user's request sentence, wherein the first preset model is a Chinese pre-trained language representation model.

[0076] For example, based on the Chinese pre-training parameters in the first preset model, the semantic vector space is mapped to the sentence feature matrix to determine the target semantic vector corresponding to the user's request sentence.

[0077] In one example, the pre-trained Chinese language model is Chinese BERT. The pre-trained parameters in Chinese BERT are Chinese parameters, which can better perform word segmentation and semantic space vector mapping on the user's input statement.

[0078] 203. Based on the target semantic vector, perform similarity calculations on each code semantic vector stored in the preset database to determine the list of similar vectors corresponding to the target semantic vector, and determine the list of similar codes corresponding to the list of similar vectors; wherein, the list of similar vectors includes a preset number of similar semantic vectors, and each similar semantic vector in the list of similar vectors is arranged in descending order of similarity to the target semantic vector, and each code block in the list of similar codes corresponds to each similar semantic vector in the list of similar vectors.

[0079] For example, the similarity between each code semantic vector in the pre-defined database and the target semantic vector can be calculated using functions in the software machine learning library (sklearn) or the specialized scientific computing toolkit (scipy) within the PyTorch framework. For instance, vector cosine similarity can be used to determine the similarity between each code semantic vector and the target semantic vector. Then, based on the similarity between each code semantic vector in the pre-defined database and the target semantic vector, a pre-defined number of code semantic vectors with the highest to lowest similarity are selected to form a list of similar vectors to the target semantic vector. Since each code semantic vector in the pre-defined database corresponds to a code block, the list of similar codes corresponding to the target semantic vector can be determined from the list of similar vectors. Similarly, in the list of similar codes, the pre-defined number of code blocks are arranged in descending order of their similarity to the target semantic vector. The pre-defined database stores multiple code blocks and the corresponding code semantic vectors for each code block. A code block can be a storage path for code or a portion of code.

[0080] In one example, a server can be built based on the Flask framework to construct an interface and return results. Table 2 is a table of interface return parameter formats provided in an embodiment of this application. The interface return parameter formats are as follows:

[0081] Table 2. Interface Return Parameter Format Table

[0082]

[0083]

[0084] 204. In response to a user instruction, determine the target code block in the similar code list; wherein the user instruction is used to indicate the target code block in the similar code list.

[0085] For example, a predetermined list of similar code is displayed in a preset interface for the user to select from, and the target code block selected by the user is determined in response to the user's instruction.

[0086] 205. Based on the code storage path corresponding to the target code block stored in the preset database, schedule and display the target code; wherein, the preset database stores the code storage path corresponding to each code block.

[0087] For example, a preset database stores the code storage path corresponding to each code block. After determining the target code block selected by the user, the target code is scheduled and displayed according to the code storage path corresponding to the target code block stored in the preset database.

[0088] In summary, the code retrieval method provided in this embodiment, based on Chinese BERT, performs semantic vector space mapping on user requirement statements, which improves the ability to extract language representations, enhances the accuracy of semantic vector mapping, improves model parallelism and running speed, and improves the accuracy and speed of code search.

[0089] One or more embodiments provided in this application may further include:

[0090] Obtain the first training set; the first training set includes multiple training documents, which are explanatory documents corresponding to financial business codes.

[0091] Based on the Chinese pre-trained language representation model, semantic vector mapping is performed on each training document in the first training set, and the mapping loss corresponding to each training document is determined; wherein, the mapping loss is used to characterize the error of semantic vector mapping.

[0092] Based on the mapping loss of the Chinese pre-trained language representation model for each training document in the first training set, the Chinese pre-training parameters in the Chinese pre-trained language representation model are optimized to determine the first preset model.

[0093] For example, a first preset model is pre-trained, for instance, by using documentation of frequently used business code in the field or by users, or by using business-specific documentation in common code repositories as training materials to generate a first training set. Semantic vector mapping is performed on each training document in the first training set based on Chinese BERT, and the mapping loss corresponding to each training document, i.e., the error of semantic vector mapping, is determined. Based on the mapping loss of the Chinese pre-trained language representation model for each training document in the first training set, the Chinese pre-training parameters in the Chinese pre-trained language representation model are optimized to determine the first preset model.

[0094] In summary, by using documentation of frequently used business code in this field or by users as training material, or by using business-specific documentation from commonly used code repositories, the Chinese BERT model is fine-tuned to give it business-specific characteristics. This allows for more accurate semantic vector space mapping of user requirement statements, thereby improving the accuracy of code retrieval.

[0095] Figure 3 A flowchart of a model training method provided in an embodiment of this application is shown below. Figure 3 As shown, the method includes:

[0096] 301. Obtain the second training set; the second training set includes multiple training codes and a code description document for each code.

[0097] For example, a second training set for English BERT is generated based on business code with business characteristics and its corresponding code documentation.

[0098] 302. Based on the second training set and the first preset model, train the second preset model; wherein the first preset model is a Chinese pre-trained language representation model.

[0099] In one example, step 302 includes the following steps:

[0100] Based on the first preset model, a first vector of the code description document corresponding to the training code is determined; wherein, the first vector is the semantic vector corresponding to the code description document.

[0101] Based on the second preset model, a second vector corresponding to the training code is determined; wherein, the second vector is the semantic vector corresponding to the training code.

[0102] Calculate the spatial distance between the first vector and the second vector; where the spatial distance is used to characterize the vector loss value.

[0103] Based on the spatial distance, the pre-trained parameters in the second preset model are corrected.

[0104] For example, based on a first preset model, a first vector of code documentation corresponding to the training code in the second training set is determined; wherein the first vector is a semantic vector corresponding to the code documentation; based on a second preset model, a second vector of training code is determined; wherein the second vector is a semantic vector corresponding to the training code; the spatial distance between the first vector and the second vector is calculated; wherein the spatial distance is used to characterize the vector loss value; and the pre-training parameters in the second preset model are corrected according to the spatial distance. A second preset model for code semantic mapping is generated.

[0105] In summary, when mapping the semantic vector of the code, the semantic vector of the corresponding document is directly used as the target. The more accurate fine-tuned Chinese BERT is used to help train the less accurate English character-level BERT for the code, making the code retrieval results more accurate and direct.

[0106] One or more embodiments provided in this application may further include:

[0107] Based on the word segmentation model in the second preset model, each code in the code library is segmented to determine the character vector corresponding to each character in each code, and the code feature matrix corresponding to each code is determined; wherein, the code feature matrix is ​​used to represent the semantic feature information of the corresponding code.

[0108] Based on the English pre-training parameters in the second preset model, the semantic vector space mapping is performed on the code feature matrix corresponding to each code in the code library to determine the code semantic vector corresponding to each code in the code library; wherein, the second preset model is an English pre-trained language representation model.

[0109] Store the code path information corresponding to each code semantic vector in a preset database.

[0110] For example, since the code primarily consists of letters and symbols, and contains numerous non-word variable names, character-level English BERT is used as the model. For existing code in the codebase, each piece of code is segmented based on the word segmentation model in the second preset model, determining the character vector corresponding to each character in each piece of code, and determining the code feature matrix corresponding to each piece of code. Then, based on the English pre-trained parameters in the second preset model, semantic vector space mapping is performed on the code feature matrix corresponding to each piece of code in the codebase to determine the code semantic vector corresponding to each piece of code in the codebase; wherein, the second preset model is an English pre-trained language representation model; the code path information corresponding to each code semantic vector is stored in a preset database. For newly added code in the codebase, a scheduled task is set using a scheduling system to perform the above semantic vector space mapping on the newly added code at a specified time each day, and then store it in the preset database.

[0111] In one example, the Bert tokenizer, Bert model framework, and corresponding Chinese and English pre-training parameters are imported through the transformers package. Data loading, batching, model building, loss calculation, and optimizer optimization can all be performed using functions from the PyTorch library.

[0112] In summary, using pre-trained English BERT to map the semantic vector space of the code improves the ability to extract the semantics of the code, thereby improving the accuracy of code retrieval.

[0113] Figure 4 This is a schematic diagram of the structure of a code retrieval device provided in an embodiment of this application, as shown below. Figure 4 As shown, the device includes:

[0114] The determining unit 31 is used to read the user requirement statement and determine the target semantic vector corresponding to the user requirement statement; wherein, the user requirement statement is used to represent the user's retrieval requirements for the target code.

[0115] The first processing unit 32 is used to calculate the similarity of each code semantic vector stored in the preset database according to the target semantic vector, determine the similar vector list corresponding to the target semantic vector, and determine the similar code list corresponding to the similar vector list; wherein, the similar vector list includes a preset number of similar semantic vectors, and each similar semantic vector in the similar vector list is arranged in descending order of similarity with the target semantic vector, and each code block in the similar code list corresponds to each similar semantic vector in the similar vector list.

[0116] The second processing unit 33 is used to respond to a user instruction to determine the target code block in the similar code list and obtain the target code corresponding to the target code block; wherein the user instruction is used to indicate the target code block in the similar code list.

[0117] In one example, determining unit 31 includes:

[0118] The first processing subunit is used to perform word segmentation on the user request statement based on the word segmentation model in the first preset model, determine the character vector corresponding to each character in the user request statement, and determine the statement feature matrix corresponding to the user request statement; the statement feature matrix is ​​used to characterize the semantic feature information of the user request statement.

[0119] The second processing subunit is used to perform semantic vector space mapping on the sentence feature matrix based on the Chinese pre-training parameters in the first preset model, and determine the target semantic vector corresponding to the user's request sentence. The first preset model is a Chinese pre-trained language representation model.

[0120] In one example, the device also includes:

[0121] The first acquisition unit is used to acquire the first training set; wherein, the first training set includes multiple training documents, and the training documents are explanatory documents corresponding to financial business codes.

[0122] The first training unit is used to perform semantic vector mapping on each training document in the first training set based on the Chinese pre-trained language representation model, and to determine the mapping loss corresponding to each training document; wherein, the mapping loss is used to characterize the error of semantic vector mapping.

[0123] The second training unit is used to optimize the Chinese pre-training parameters in the Chinese pre-training language representation model based on the mapping loss of the Chinese pre-training language representation model for each training document in the first training set, and to determine the first preset model.

[0124] In one example, the device also includes:

[0125] The first preprocessing unit is used to perform word segmentation on each code in the code library based on the word segmentation model in the second preset model, determine the character vector corresponding to each character in each code, and determine the code feature matrix corresponding to each code; wherein, the code feature matrix is ​​used to represent the semantic feature information of the corresponding code.

[0126] The second preprocessing unit is used to perform semantic vector space mapping on the code feature matrix corresponding to each code in the code library based on the English pre-training parameters in the second preset model, and to determine the code semantic vector corresponding to each code in the code library; wherein, the second preset model is an English pre-trained language representation model.

[0127] The storage unit is used to store the code path information corresponding to each code semantic vector in a preset database.

[0128] In one example, the device also includes:

[0129] The second acquisition unit is used to acquire the second training set; wherein, the second training set includes multiple training codes and code description documents corresponding to each code.

[0130] The third training unit is used to train the second preset model based on the second training set and the first preset model; wherein the first preset model is a Chinese pre-trained language representation model.

[0131] In one example, the third training unit includes:

[0132] The third processing subunit is used to determine the first vector of the code description document corresponding to the training code based on the first preset model; wherein the first vector is the semantic vector corresponding to the code description document.

[0133] The fourth processing subunit is used to determine the second vector corresponding to the training code based on the second preset model; wherein the second vector is the semantic vector corresponding to the training code.

[0134] The computational subunit is used to calculate the spatial distance between the first vector and the second vector; where the spatial distance is used to characterize the vector loss value.

[0135] The fifth processing subunit is used to correct the pre-trained parameters in the second preset model based on the spatial distance.

[0136] In one example, processing unit 33 includes:

[0137] Determine sub-units to identify target code blocks in a list of similar codes in response to user instructions.

[0138] The scheduling subunit is used to schedule and display target code according to the code storage path corresponding to the target code block stored in the preset database; wherein, the preset database stores the code storage path corresponding to each code block.

[0139] Figure 5 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application, such as... Figure 5 As shown, the electronic device includes: a memory 41 and a processor 42.

[0140] Memory 41 is used to store computer programs.

[0141] Processor 42 is configured to read a computer program stored in memory and execute the method of any of the above embodiments according to the computer program in memory.

[0142] Figure 6 This is a block diagram of an electronic device provided in an embodiment of this application. The device may be a mobile phone, computer, digital broadcasting terminal, messaging device, game console, tablet device, medical device, fitness equipment, personal digital assistant, etc.

[0143] The device 800 may include one or more of the following components: a processing component 802, a memory 804, a power supply component 806, a multimedia component 808, an audio component 810, an input / output (I / O) interface 812, a sensor component 814, and a communication component 816.

[0144] Processing component 802 typically controls the overall operation of device 800, such as operations associated with display, telephone calls, data communication, camera operation, and recording. Processing component 802 may include one or more processors 820 to execute instructions to perform all or part of the steps of the methods described above. Furthermore, processing component 802 may include one or more modules to facilitate interaction between processing component 802 and other components. For example, processing component 802 may include a multimedia module to facilitate interaction between multimedia component 808 and processing component 802.

[0145] Memory 804 is configured to store various types of data to support the operation of device 800. Examples of such data include instructions for any application or method operating on device 800, contact data, phonebook data, messages, pictures, videos, etc. Memory 804 can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk.

[0146] Power supply component 806 provides power to various components of device 800. Power supply component 806 may include a power management system, one or more power sources, and other components associated with generating, managing, and distributing power to device 800.

[0147] Multimedia component 808 includes a screen that provides an output interface between device 800 and the user. In some embodiments, the screen may include a liquid crystal display (LCD) and a touch panel (TP). If the screen includes a touch panel, the screen may be implemented as a touchscreen to receive input signals from the user. The touch panel includes one or more touch sensors to sense touches, swipes, and gestures on the touch panel. The touch sensors may sense not only the boundaries of touch or swipe actions but also the duration and pressure associated with the touch or swipe operation. In some embodiments, multimedia component 808 includes a front-facing camera and / or a rear-facing camera. When device 800 is in an operating mode, such as a shooting mode or a video mode, the front-facing camera and / or rear-facing camera may receive external multimedia data. Each front-facing camera and rear-facing camera may be a fixed optical lens system or have focal length and optical zoom capabilities.

[0148] Audio component 810 is configured to output and / or input audio signals. For example, audio component 810 includes a microphone (MIC) configured to receive external audio signals when device 800 is in an operating mode, such as call mode, recording mode, and voice recognition mode. The received audio signals may be further stored in memory 804 or transmitted via communication component 816. In some embodiments, audio component 810 also includes a speaker for outputting audio signals.

[0149] I / O interface 812 provides an interface between processing component 802 and peripheral interface modules, such as keyboards, click wheels, buttons, etc. These buttons may include, but are not limited to, home buttons, volume buttons, power buttons, and lock buttons.

[0150] Sensor assembly 814 includes one or more sensors for providing state assessments of various aspects of device 800. For example, sensor assembly 814 may detect the on / off state of device 800, the relative positioning of components such as the display and keypad of device 800, changes in the position of device 800 or a component of device 800, the presence or absence of user contact with device 800, the orientation or acceleration / deceleration of device 800, and temperature changes of device 800. Sensor assembly 814 may include a proximity sensor configured to detect the presence of nearby objects without any physical contact. Sensor assembly 814 may also include a light sensor, such as a CMOS or CCD image sensor, for use in imaging applications. In some embodiments, sensor assembly 814 may also include an accelerometer, a gyroscope, a magnetometer, a pressure sensor, or a temperature sensor.

[0151] Communication component 816 is configured to facilitate wired or wireless communication between device 800 and other devices. Device 800 can access wireless networks based on communication standards, such as WiFi, 2G, or 3G, or combinations thereof. In one exemplary embodiment, communication component 816 receives broadcast signals or broadcast-related information from an external broadcast management system via a broadcast channel. In one exemplary embodiment, communication component 816 also includes a near-field communication (NFC) module to facilitate short-range communication. For example, the NFC module may be implemented based on radio frequency identification (RFID) technology, Infrared Data Association (IrDA) technology, ultra-wideband (UWB) technology, Bluetooth (BT) technology, and other technologies.

[0152] In an exemplary embodiment, the apparatus 800 may be implemented by one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field-programmable gate arrays (FPGAs), controllers, microcontrollers, microprocessors, or other electronic components to perform the methods described above.

[0153] In an exemplary embodiment, a non-transitory computer-readable storage medium including instructions is also provided, such as a memory 804 including instructions, which can be executed by a processor 820 of the device 800 to perform the above-described method. For example, the non-transitory computer-readable storage medium may be a ROM, random access memory (RAM), CD-ROM, magnetic tape, floppy disk, and optical data storage device, etc.

[0154] This application also provides a non-transitory computer-readable storage medium, which, when the instructions in the storage medium are executed by the processor of an electronic device, enables the electronic device to perform the methods provided in the above embodiments.

[0155] This application also provides a computer program product, which includes: a computer program stored in a readable storage medium, at least one processor of an electronic device can read the computer program from the readable storage medium, and the at least one processor executes the computer program to cause the electronic device to perform the solution provided in any of the above embodiments.

[0156] Other embodiments of this application will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of this application that follow the general principles of this application and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this application are indicated by the following claims.

[0157] It should be understood that this application is not limited to the precise structure described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this application is limited only by the appended claims.

Claims

1. A code retrieval method characterized by, The method comprises: reading a user demand sentence and determining a target semantic vector corresponding to the user demand sentence; wherein the user demand sentence is used to represent the user's retrieval demand for a target code; performing similarity calculation on each code semantic vector stored in a preset database according to the target semantic vector, determining a similar vector list corresponding to the target semantic vector, and determining a similar code list corresponding to the similar vector list; wherein the similar vector list includes a preset number of similar semantic vectors, each similar semantic vector in the similar vector list is arranged in descending order of similarity to the target semantic vector, and each code block in the similar code list corresponds to each similar semantic vector in the similar vector list; in response to a user instruction, determining a target code block in the similar code list and obtaining a target code corresponding to the target code block; wherein the user instruction is used to indicate the target code block in the similar code list; performing word segmentation processing on each code in the code library based on a word segmentation model in a second preset model, determining a character vector corresponding to each character in each code, and determining a code feature matrix corresponding to each code; wherein the code feature matrix is used to represent the semantic feature information of the corresponding code; performing semantic vector space mapping on the code feature matrix corresponding to each code in the code library based on English pre-training parameters in the second preset model, and determining a code semantic vector corresponding to each code in the code library; wherein the second preset model is an English pre-training language representation model; storing code path information corresponding to each code semantic vector in the preset database; obtaining a second training set; wherein the second training set includes a plurality of training codes and a code specification document corresponding to each code; determining a first vector of the code specification document corresponding to the training code based on a first preset model; wherein the first vector is a semantic vector corresponding to the code specification document; determining a second vector corresponding to the training code based on the second preset model; wherein the second vector is a semantic vector corresponding to the training code; calculating the spatial distance between the first vector and the second vector; wherein the spatial distance is used to represent the vector loss value; modifying the pre-training parameters in the second preset model according to the spatial distance; wherein the first preset model is a Chinese pre-training language representation model.

2. The method of claim 1, wherein, determining the target semantic vector corresponding to the user demand sentence comprises: performing word segmentation processing on the user demand sentence based on a word segmentation model in a first preset model, determining a character vector corresponding to each character in the user demand sentence, and determining a sentence feature matrix corresponding to the user demand sentence; wherein the sentence feature matrix is used to represent the semantic feature information of the user demand sentence; determining the target semantic vector corresponding to the user demand sentence based on Chinese pre-training parameters in the first preset model, wherein the first preset model is a Chinese pre-training language representation model.

3. The method of claim 2, wherein, The method further comprises: obtaining a first training set; wherein the first training set comprises a plurality of training documents, and the training documents are explanation documents corresponding to financial business codes; based on the Chinese pre-training language representation model, performing semantic vector mapping processing on each training document in the first training set, and determining a mapping loss corresponding to each training document; wherein the mapping loss is used to represent the error of semantic vector mapping; According to the mapping loss of each training document in the first training set with respect to the Chinese pre-training language representation model, the Chinese pre-training parameters in the Chinese pre-training language representation model are optimized to determine the first pre-set model.

4. The method according to any one of claims 1 to 3, characterized in that, In response to a user instruction, determining a target code block in the similar code list, and obtaining a target code corresponding to the target code block, comprising: In response to a user instruction, determine the target code block in the similar code list; According to the code storage path corresponding to the target code block stored in the preset database, the target code is scheduled and displayed; wherein the preset database stores the code storage path corresponding to each code block.

5. A code retrieval apparatus characterized by comprising: The device comprises: determining unit, for reading user demand sentence, and determining the target semantic vector corresponding to the user demand sentence; wherein the user demand sentence is used to represent the retrieval demand of the user for the target code; The first processing unit is used for calculating the similarity of each code semantic vector stored in the preset database according to the target semantic vector, determining the similar vector list corresponding to the target semantic vector, and determining the similar code list corresponding to the similar vector list; wherein the similar vector list includes a preset number of similar semantic vectors, each similar semantic vector in the similar vector list is arranged in order from large to small according to the similarity with the target semantic vector, and each code block in the similar code list corresponds to each similar semantic vector in the similar vector list; The second processing unit is used for determining a target code block in the similar code list and obtaining a target code corresponding to the target code block in response to a user instruction; wherein the user instruction is used to indicate the target code block in the similar code list; The first preprocessing unit is used for performing word segmentation processing on each code in the code library based on the word segmentation model in the second preset model, determining the character vector corresponding to each character in each code, and determining the code feature matrix corresponding to each code; wherein the code feature matrix is used to represent the semantic feature information of the corresponding code. The second preprocessing unit is used for performing semantic vector space mapping on the code feature matrix corresponding to each code in the code library based on the English pre-training parameters in the second preset model, and determining the code semantic vector corresponding to each code in the code library; wherein the second preset model is an English pre-training language representation model; The storage unit is used for storing the code path information corresponding to each code semantic vector in the preset database; The second obtaining unit is configured to obtain a second training set; the second training set includes a plurality of training codes and a code description document corresponding to each code; The third training unit is configured to perform model training on a second preset model based on the second training set and a first preset model; the first preset model is a Chinese pre-training language representation model; The third processing subunit is configured to determine a first vector of the code description document corresponding to the training code based on the first preset model; the first vector is a semantic vector corresponding to the code description document; The fourth processing subunit is configured to determine a second vector corresponding to the training code based on the second preset model; the second vector is a semantic vector corresponding to the training code; The computing subunit is configured to calculate a spatial distance between the first vector and the second vector; the spatial distance is used to represent a vector loss value; The fifth processing subunit is configured to correct a pre-training parameter in the second preset model according to the spatial distance.

6. An electronic device, comprising: The electronic device includes a memory and a processor; The memory is configured to store a computer program; The processor is configured to read the computer program stored in the memory, and execute the code retrieval method according to any one of claims 1-4.

7. A computer readable storage medium characterized by, The computer readable storage medium stores computer execution instructions; when the processor executes the computer execution instructions, the code retrieval method according to any one of claims 1-4 is implemented.

8. A computer program product comprising a computer program, characterized in that, The computer program is executed by the processor to implement the code retrieval method according to any one of claims 1-4.

Citation Information

Patent Citations

  • Code file query method and device

    CN104809139A

  • Method and device for searching code blocks

    CN115470311A