Code searching method and device, electronic equipment and storage medium

By searching natural language annotations in the natural language annotation collection of natural language annotations in the code base, the problem of low code search efficiency in the existing technology is solved, and fast and efficient code search is achieved, which is in line with people's search habits.

CN119149665APending Publication Date: 2024-12-17BEIJING GUIXIN TECHNOLOGY CO LTD

Patent Information

Application Number
CN202411042853.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-31
Publication Date
2024-12-17

AI Technical Summary

Technical Problem

The existing code search method is inefficient and requires traversing the search in all the original code text of the code base, which makes it take a long time.

Method used

Search using natural language, and by searching for natural language annotations corresponding to the search statement in the natural language annotation set of natural language annotations in the code base, determining its corresponding function code snippets, thereby narrowing the search scope and improving efficiency.

Benefits of technology

There is no need to use code as a search term, and use functions to search, which improves search efficiency, conforms to people's search habits, and can quickly find the required functional code snippets.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119149665A_ABST
    Figure CN119149665A_ABST
Patent Text Reader

Abstract

The invention provides a code searching method and device, electronic equipment and a storage medium, and relates to the technical field of retrieval. The method comprises the steps that in response to a received retrieval statement of a natural language, a natural language annotation corresponding to the retrieval statement is searched in a natural language annotation set of a code library, and the natural language annotation is used for indicating functions of functions in function code snippets in the code library; and if the natural language annotation is found, determining a function code snippet corresponding to the natural language annotation in the code library. According to the embodiment, codes do not need to be adopted as search terms, natural languages can be adopted for searching, searching personnel are more friendly, the searching habits of the personnel are better met, searching can be conducted through the functions, and the needed functions can be searched easily.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

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

[0002] Code search can help software developers retrieve reusable code from the code base, thereby improving software development efficiency.

[0003] In the related art, in the code search, it is necessary to use the code as the search term and directly traverse the code text in the code library. This search method requires traversing the code text. The traversal process takes a long time, so the search efficiency of this search method is low, and an efficient search method is urgently needed. Summary of the invention

[0004] In view of this, the purpose of the present disclosure is to propose a code search method, device, electronic device and storage medium, which can solve the existing problems in a targeted manner.

[0005] Based on the above-mentioned purpose, in a first aspect, the present disclosure proposes a code search method, comprising: in response to receiving a natural language retrieval statement, searching for a natural language annotation corresponding to the retrieval statement in a natural language annotation set of a code base, wherein the natural language annotation is used to indicate the function of a function in a function code snippet in the code base; if the natural language annotation is found, determining the function code snippet to which the natural language annotation corresponds in the code base.

[0006] According to a second aspect, a code search device is provided, which is characterized in that it includes: a search unit, configured to, in response to receiving a natural language search statement, search for a natural language annotation corresponding to the search statement in a natural language annotation set of a code base, wherein the natural language annotation is used to indicate the function of a function in a function code snippet in the code base; and a determination unit, configured to determine the function code snippet to which the natural language annotation corresponds in the code base if the natural language annotation is found.

[0007] According to a third aspect, an electronic device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the method described in the first aspect.

[0008] According to a fourth aspect, a computer-readable storage medium is provided, on which a computer program is stored, and the computer program is executed by a processor to implement any method described in the first aspect.

[0009] In a fifth aspect, a computer program product is also provided, including a computer program, wherein the computer program is executed by a processor to implement any method described in the first aspect.

[0010] In general, the present disclosure has at least the following beneficial effects: instead of using code as a search term, natural language can be used for search, which is more friendly to search personnel and more in line with the search habits of personnel, and can be searched by function, which helps to search for the required function. In addition, instead of searching for function code snippets in all the original code texts, the search is performed in the natural language annotation set for the function function, which narrows the search scope, helps to shorten the search time, and improves the code search efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0011] In the accompanying drawings, unless otherwise specified, the same reference numerals throughout the multiple drawings represent the same or similar parts or elements. These drawings are not necessarily drawn to scale. It should be understood that these drawings only depict some embodiments disclosed in accordance with the present disclosure and should not be regarded as limiting the scope of the present disclosure.

[0012] Figure 1 A flowchart of a code search method according to an embodiment of the present disclosure is shown;

[0013] Figure 2 Another flow chart of a code search method according to an embodiment of the present disclosure is shown;

[0014] Figure 3 A schematic diagram of a search interface according to an embodiment of the present disclosure is shown;

[0015] Figure 4 A schematic diagram showing a code search device according to an embodiment of the present disclosure;

[0016] Figure 5 A schematic diagram of the structure of an electronic device provided by an embodiment of the present disclosure is shown;

[0017] Figure 6 A schematic diagram of a storage medium provided by an embodiment of the present disclosure is shown. DETAILED DESCRIPTION

[0018] The present disclosure is further described in detail below in conjunction with the accompanying drawings and embodiments. It is understood that the specific embodiments described herein are only used to explain the relevant invention, rather than to limit the invention. It is also necessary to explain that, for ease of description, only the parts related to the relevant invention are shown in the accompanying drawings.

[0019] It should be noted that, in the absence of conflict, the embodiments and features in the embodiments of the present disclosure may be combined with each other. The present disclosure will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.

[0020] Figure 1 The code search method of the present disclosure is shown. In an embodiment of the present disclosure, the method includes:

[0021] Step S101, in response to receiving a natural language search statement, searching for a natural language annotation corresponding to the search statement in a natural language annotation set of a code base, wherein the natural language annotation is used to indicate a function of a function in a function code snippet in the code base.

[0022] In this embodiment, when the execution subject of the code search method receives a natural language search statement, it searches for a natural language annotation corresponding to the search statement in a natural language annotation set.

[0023] The natural language annotation set is a set of natural language annotations corresponding to the code base. The natural language annotations included in the natural language annotation set are annotations on the functions of the functions in the function code snippet. The annotations are used to indicate the functions of the functions in the function code snippet. For example, the function of the function can be file reading and writing.

[0024] The above-mentioned execution subject may be various electronic devices, such as a terminal or a server.

[0025] Step S102: If the natural language annotation is found, determine the function code snippet corresponding to the natural language annotation in the code library.

[0026] In this embodiment, when the execution subject finds the natural language annotation, the function code snippet corresponding to the natural language annotation in the code library can be determined. Afterwards, the execution subject can also output the function code snippet. Specifically, the object output by the execution subject can be various electronic devices such as terminal devices or display devices.

[0027] In this embodiment, the code does not need to be used as a search term, but natural language can be used for search, which is more friendly to search personnel and more in line with their search habits. It can also search by function, which helps to search for the required function. In addition, instead of searching for function code snippets in all the original code texts, the search is performed in the natural language annotation set for the function function, which narrows the search scope, helps to shorten the search time, and improves the code search efficiency.

[0028] Figure 2 The code search method according to the embodiment of the present disclosure is shown. Figure 2 As shown, the code search method includes:

[0029] Step S201, extracting function code snippets from the code library.

[0030] In this embodiment, in these optional implementations, the above-mentioned execution entity can extract function code snippets from the code library, which helps to smoothly generate new natural language annotations for the extracted function code snippets.

[0031] The execution subject may parse the codes of each circuit in the code base to determine the codes corresponding to the functions in the circuit, that is, the function code snippets, and then extract the function code snippets.

[0032] Step S202: Generate a new natural language annotation for the extracted function code snippet, and obtain the natural language annotation including the new natural language annotation.

[0033] In this embodiment, before searching, the execution subject may first generate natural language annotations for the function code snippets in the code library to obtain the natural language annotations including the new natural language annotations.

[0034] Step S203, in response to receiving a natural language search statement, searching for a natural language annotation corresponding to the search statement in a natural language annotation set of the code base, wherein the natural language annotation is used to indicate the function of a function in a function code snippet in the code base.

[0035] Step S204: if the natural language annotation is found, determine the function code snippet corresponding to the natural language annotation in the code library.

[0036] This embodiment can first generate natural language annotations for function code snippets in the code library, which helps to smoothly find the natural language annotations, and output the function code snippets corresponding to the natural language annotations to achieve efficient code search.

[0037] In some optional implementations of any embodiment of the present disclosure, the natural language annotations include the original annotations in the code base and the new natural language annotations; the method also includes: establishing an index between the natural language annotations and the function code snippets, and adding the index to the natural language annotation set.

[0038] In these optional implementations, the execution subject may establish an index between the natural language annotation and the function code snippet, and then add the index to the natural language annotation set. The index may indicate the relationship between the natural language annotation and the function code snippet. Therefore, the function code snippet may be found in the code base through the natural language annotation.

[0039] These implementations can establish an index between the natural language annotation and the function code snippet, and add the index to the natural language annotation set, so that the corresponding function code snippet can be found through the index.

[0040] Optionally, searching for the natural language annotation corresponding to the search statement in the natural language annotation set of the code base includes: searching for the natural language annotation matching the search statement in the index to obtain a target natural language annotation; determining the function code snippet corresponding to the natural language annotation in the code base includes: determining the function code snippet corresponding to the target natural language annotation in the index.

[0041] In these optional implementations, the execution subject may find a natural language annotation matching the search statement in the index, and use the natural language annotation as the target natural language annotation. Afterwards, the execution subject may determine the function code snippet corresponding to the target natural language annotation in the index, thereby obtaining the function code snippet to be found.

[0042] These implementations can quickly and accurately find the corresponding function code snippets through indexing.

[0043] Optionally, the method also includes: establishing a code search engine system including the index; searching for the natural language annotation corresponding to the search statement in the natural language annotation set of the code library, including: searching for the natural language annotation corresponding to the search statement in the natural language annotation set of the code library through the code search engine system; determining the function code snippet corresponding to the natural language annotation in the code library, including: searching for the function code snippet corresponding to the natural language annotation in the code library through the code search engine system.

[0044] The execution subject may establish a code search engine system including an index, so that the execution subject may search for the natural language annotation corresponding to the search statement in the natural language annotation set of the code library through the code search engine system.

[0045] Through the code search engine system, the speed of finding natural language comments and function code snippets can be accelerated, thereby improving the search efficiency of the code.

[0046] Optionally, generating a new natural language annotation for the extracted function code snippet includes: calling an annotation generation model to process the extracted function code snippet to generate a new natural language annotation.

[0047] In these implementations, the execution entity may call the annotation generation model to process the extracted function code snippet, thereby generating a new natural language annotation.

[0048] The comment generation model here can be any model that can generate comments for function code snippets, such as a deep neural network.

[0049] These implementations can process the extracted function code snippets through the annotation generation model to improve the accuracy and stability of generating new natural language annotations.

[0050] In some optional implementations of any embodiment of the present disclosure, extracting function code snippets from the code library includes: splitting the code in the code library based on function functions to obtain function code snippets, wherein the function code snippets have complete function functions and are the smallest function function units.

[0051] In these optional implementations, the execution subject can split the code in the code base based on the function of the function, and the result of the splitting is the function code fragment, so as to realize the extraction of the function code fragment.

[0052] The function code snippet has complete function functions and is the smallest function unit. This ensures that the function is searched with precision.

[0053] In some optional implementations of any embodiment of the present disclosure, in response to receiving a natural language search statement, searching for a natural language annotation corresponding to the search statement in a natural language annotation collection of a code base, including: in a search interface, if the currently selected option in the search function bar is a natural language search option, in response to receiving a natural language search statement, searching for a natural language annotation corresponding to the search statement in a natural language annotation collection of a code base.

[0054] In these optional implementations, the execution subject may, in the search interface, search for a natural language annotation corresponding to the search statement in the natural language annotation set of the code base if the currently selected option in the search function bar is a natural language search option and if a natural language search statement is received.

[0055] There may be multiple options (labs) in the search interface. Among these options, there is a natural language search option, which is used to indicate that a code search is performed by inputting a natural language.

[0056] like Figure 3 As shown in the figure, the display effect in the search interface when the natural language search option is selected is shown. The received search statement is file reading and writing. Function code snippets can be displayed in the search interface.

[0057] In some optional implementations of any embodiment of the present disclosure, the method further includes: outputting to a display the function code snippet corresponding to the natural language annotation in the code library, so that the display displays the function code snippet.

[0058] In these optional implementations, the execution subject may output the function code snippet to the display after determining the function code snippet corresponding to the natural language annotation in the code library, so that the display displays the function code snippet. In this way, the display can display the function code snippet to the person searching for the code.

[0059] These implementations can realize visualization of the function code snippet through a display, specifically displaying the function code snippet to a person searching for the code.

[0060] The present disclosure provides a code search device, which is used to execute the code search method described in the above embodiment. Figure 4 As shown, the device includes: a search unit 401, which is configured to, in response to receiving a natural language search statement, search for a natural language annotation corresponding to the search statement in a natural language annotation set of a code base, wherein the natural language annotation is used to indicate the function of a function in a function code snippet in the code base; and a determination unit 402, which is configured to determine the function code snippet corresponding to the natural language annotation in the code base if the natural language annotation is found.

[0061] The code search device provided in the above-mentioned embodiment of the present disclosure and the code search method provided in the embodiment of the present disclosure are based on the same inventive concept, and have the same beneficial effects as the method adopted, run or implemented by the application program stored therein.

[0062] The disclosed embodiment also provides an electronic device corresponding to the code search method provided in the above embodiment, so as to execute the above code search method. The disclosed embodiment is not limited.

[0063] Please refer to Figure 5 , which shows a schematic diagram of an electronic device provided by some embodiments of the present disclosure. Figure 5 As shown, the electronic device 50 includes: a processor 500, a memory 501, a bus 502 and a communication interface 503, and the processor 500, the communication interface 503 and the memory 501 are connected via the bus 502; the memory 501 stores a computer program that can be run on the processor 500, and the processor 500 executes the method provided in any of the aforementioned embodiments of the present disclosure when running the computer program.

[0064] The memory 501 may include a high-speed random access memory (RAM), and may also include a non-volatile memory, such as at least one disk storage. The communication connection between the system network element and at least one other network element is realized through at least one communication interface 503 (which may be wired or wireless), and the Internet, wide area network, local area network, metropolitan area network, etc. may be used.

[0065] The bus 502 may be an ISA bus, a PCI bus, or an EISA bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. The memory 501 is used to store programs, and the processor 500 executes the programs after receiving execution instructions. The code search method disclosed in any implementation of the aforementioned embodiment of the present disclosure may be applied to the processor 500, or implemented by the processor 500.

[0066] The processor 500 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by the hardware integrated logic circuit in the processor 500 or the instruction in the form of software. The above processor 500 can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The methods, steps and logic block diagrams disclosed in the embodiments of the present disclosure can be implemented or executed. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor. The steps of the method disclosed in conjunction with the embodiments of the present disclosure can be directly embodied as a hardware decoding processor to be executed, or the hardware and software modules in the decoding processor can be executed. The software module can be located in a mature storage medium in the field such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory 501, and the processor 500 reads the information in the memory 501 and completes the steps of the above method in combination with its hardware.

[0067] The electronic device provided by the embodiment of the present disclosure and the code search method provided by the embodiment of the present disclosure are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented by them.

[0068] The present disclosure also provides a computer-readable storage medium corresponding to the code search method provided in the above embodiment. Figure 6 The computer-readable storage medium shown is a CD 60 on which a computer program (ie, a program product) is stored. When the computer program is run by a processor, the code search method provided in any of the aforementioned embodiments is executed.

[0069] It should be noted that examples of the computer-readable storage medium may also include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other optical or magnetic storage media, which are not listed here one by one.

[0070] The computer-readable storage medium provided by the above-mentioned embodiments of the present disclosure and the code search method provided by the embodiments of the present disclosure are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented by the application programs stored therein.

[0071] It should be noted that:

[0072] In the above text, the term "comprises", "comprising" or any other variant thereof is intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprising a ..." does not exclude the presence of other identical elements in the process, method, article or device including the element. In addition, it should be noted that the scope of the method and device in the embodiments of the present disclosure is not limited to performing functions in the order shown or discussed, and may also include performing functions in a substantially simultaneous manner or in reverse order according to the functions involved, for example, the described method may be performed in an order different from that described, and various steps may also be added, omitted, or combined. In addition, the features described with reference to certain examples may be combined in other examples.

[0073] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present disclosure, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, a magnetic disk, or an optical disk), and includes a number of instructions for enabling a terminal (which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) to execute the methods described in each embodiment of the present disclosure.

[0074] The embodiments of the present disclosure are described above in conjunction with the accompanying drawings, which are only specific implementation modes of the present disclosure. However, the present disclosure is not limited to the above-mentioned specific implementation modes. The above-mentioned specific implementation modes are merely illustrative and not restrictive. Under the guidance of the present disclosure, ordinary technicians in this field can also make many forms without departing from the purpose of the present disclosure and the scope of protection of the claims, all of which are within the protection of the present disclosure.

Claims

1. A code search method, characterized in that: include: In response to receiving a natural language search statement, searching a natural language annotation set of a code base for a natural language annotation corresponding to the search statement, wherein the natural language annotation is used to indicate a function of a function in a function code snippet in the code base; If the natural language annotation is found, the function code snippet corresponding to the natural language annotation in the code library is determined.

2. The method according to claim 1, characterized in that: The method further comprises: Extracting function code snippets from the code base; A new natural language annotation is generated for the extracted function code snippet, and the natural language annotation including the new natural language annotation is obtained.

3. The method according to claim 1, characterized in that The natural language annotations include the original annotations in the code base and the new natural language annotations; The method further comprises: An index is established between the natural language annotation and the function code snippet, and the index is added to the natural language annotation set.

4. The method according to claim 3, characterized in that The searching, in the natural language annotation set of the code base, for the natural language annotation corresponding to the search statement comprises: searching, in the index, for the natural language annotation matching the search statement to obtain the target natural language annotation; The determining of the function code snippet corresponding to the natural language annotation in the code library includes: determining, in the index, the function code snippet corresponding to the target natural language annotation.

5. The method according to claim 3 or 4, characterized in that: The method further comprises: establishing a code search engine system including the index; The searching, in the natural language annotation set of the code base, for the natural language annotation corresponding to the search statement comprises: searching, by the code search engine system, in the natural language annotation set of the code base, for the natural language annotation corresponding to the search statement; The determining the function code snippet corresponding to the natural language annotation in the code library includes: searching the function code snippet corresponding to the natural language annotation in the code library through the code search engine system.

6. The method according to claim 2, characterized in that The step of generating new natural language annotations for the extracted function code snippets includes: The annotation generation model is called to process the extracted function code snippets to generate new natural language annotations.

7. The method according to claim 1, characterized in that Extracting function code snippets from the code library includes: The codes in the code base are split based on the function of the function to obtain function code fragments, which have complete function functions and are the minimum function units.

8. The method according to claim 1, characterized in that In response to receiving a search statement in a natural language, searching a natural language annotation set in a code base for a natural language annotation corresponding to the search statement includes: In the search interface, if the currently selected option in the search function bar is a natural language search option, in response to receiving a natural language search statement, a natural language annotation corresponding to the search statement is searched in a natural language annotation set in a code library.

9. The method according to claim 1, characterized in that: The method further comprises: Outputting the function code snippet corresponding to the natural language annotation in the code library to a display, so that the display displays the function code snippet.

10. A code search device, characterized in that: include: A search unit is configured to, in response to receiving a natural language search statement, search for a natural language annotation corresponding to the search statement in a natural language annotation set of a code base, wherein the natural language annotation is used to indicate a function of a function in a function code snippet in the code base; The determination unit is configured to determine the function code snippet corresponding to the natural language annotation in the code library if the natural language annotation is found.

Citation Information

Patent Citations

  • Code segment recommendation method based on deep semantic mining

    CN108717423A

  • Code searching method based on functional similarity matching

    CN110716749A

  • Code retrieval method, device and equipment

    CN116521955A

  • Code abstract enhanced code search method based on contrast learning technology

    CN118332082A

  • Source code retrieval

    US20220138240A1

Cited By

  • Smart contract test method and device, electronic equipment and storage medium

    CN119848851A

  • A smart contract testing method and device, an electronic device, and a storage medium

    CN119848851B