Method, device and equipment for querying database through natural language and medium
By using preset query feature sets and conversion templates in the database query system, natural language query is converted into SQL query, which solves the problems of high training costs, poor migration capabilities and low accuracy in the existing technology, and achieves efficient and accurate database query.
Patent Information
- Application Number
- CN202411959510.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-27
- Publication Date
- 2025-04-18
AI Technical Summary
When converting natural language queries into SQL queries, the prior art has problems such as high training costs, poor migration capabilities and low accuracy in complex SQL generation.
The natural language query statement is split through the preset query feature set, and the split query content is converted into a database query statement using the conversion template. This method combines the analytical capabilities of natural language models with the accuracy of manual coding.
It improves the accuracy of database query statement generation, reduces hardware configuration requirements, and realizes efficient migration capabilities in different business fields, and is lower than that of solutions that rely entirely on large language models.
Smart Images

Figure CN119848077A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of database technology, and in particular to a method and device for performing database query through natural language, an electronic device, and a computer-readable storage medium. Background Art
[0002] The technology of converting natural language queries into Structured Query Language (SQL) queries allows users to ask questions in natural language, and then the system automatically converts these questions into SQL query statements that the database can understand.
[0003] As an important part of Business Intelligence (BI) information systems, the technology of converting natural language queries into SQL queries can significantly improve the usability and user experience of BI information systems, because it allows users to query databases through natural language without the need for SQL programming knowledge. This capability greatly reduces the threshold for non-technical personnel to use BI information systems, allowing more people to easily gain insights from data.
[0004] At present, the common implementation scheme is to use a large number of annotated natural language and SQL data sets to train large language models (LLMs), and use the seq2seq (sequence to sequence) capability of large language models to complete the conversion from natural language to SQL. However, this scheme has many limitations:
[0005] First, large language models require more data for training, which requires high computing power and leads to high implementation costs. Second, since the above-mentioned data sets are usually limited to specific fields, this limits the migration capabilities of large language models trained based on the above data sets in different business fields. In addition, experience has shown that the accuracy of existing solutions is low for the generation of complex SQL. Summary of the invention
[0006] The purpose of this application is to provide a method and device for performing database query through natural language, an electronic device and a computer-readable storage medium.
[0007] The first aspect of the present application provides a method for performing a database query through a natural language, comprising:
[0008] Receiving a natural language query statement for a database;
[0009] According to a preset query feature set for the database, split the natural language query statement into query contents corresponding to each query feature in the query feature set;
[0010] According to the preset conversion template, the query content corresponding to each query feature is converted into a database query statement fragment and then reorganized into a database query statement;
[0011] The database is queried according to the database query statement, and the query result is output.
[0012] In a possible implementation, the query feature set includes: query time, query condition, query target and grouping condition;
[0013] The step of splitting the natural language query statement into query contents corresponding to each query feature in the query feature set includes:
[0014] The natural language query statement is split into a time screening sub-condition corresponding to the query time, a data screening sub-condition corresponding to the query condition, a data grouping sub-condition corresponding to the grouping condition, and a data target sub-condition corresponding to the query target.
[0015] In a possible implementation, splitting the natural language query statement into query content corresponding to each query feature in the query feature set further includes:
[0016] Output the query content corresponding to each query feature according to a preset format;
[0017] If the query content corresponding to a certain query feature includes multiple sub-conditions, the multiple sub-conditions are separated by using a first preset symbol;
[0018] Among them, the preset format is that each sub-condition in the query content is represented by a condition name and condition content; if a sub-condition does not have a condition name in the natural language query statement, the condition name is replaced by a second preset symbol.
[0019] In a possible implementation, the conversion template is constructed in the following manner:
[0020] Establish a dictionary of English characters and Chinese characters for enumeration of database table names, field names, and field content in the database;
[0021] The Chinese characters in the reference dictionary are expanded with synonyms and near-synonyms to form a conversion template.
[0022] In a possible implementation, the query content corresponding to each query feature is converted into a database query statement fragment according to a preset conversion template, and then reorganized into a database query statement, including:
[0023] For each sub-condition, the condition name is matched with the conversion template for similarity, and the database table name or field name with the greatest similarity is found;
[0024] Perform similarity matching between the condition content and the conversion template to find the field content with the greatest similarity;
[0025] Constructing a database query statement fragment corresponding to the sub-condition according to the database table name or field name with the greatest similarity and the field content with the greatest similarity;
[0026] Reorganize the database query statement fragments corresponding to all sub-conditions into a database query statement.
[0027] In a possible implementation, the splitting of the natural language query statement into query contents corresponding to each query feature in the query feature set according to a preset query feature set for the database includes:
[0028] By utilizing a natural language model, according to a preset query feature set for the database, the natural language query statement is split into query contents corresponding to each query feature in the query feature set.
[0029] In a possible implementation, the natural language model adopts a small sample learning model or a fine-tuned large language model.
[0030] A second aspect of the present application provides a device for performing database query by natural language, comprising:
[0031] A receiving module, used for receiving a natural language query statement for a database;
[0032] A splitting module, configured to split the natural language query statement into query contents corresponding to each query feature in the query feature set according to a preset query feature set for the database;
[0033] A conversion module, used to convert the query content corresponding to each query feature into a database query statement fragment according to a preset conversion template and then reorganize it into a database query statement;
[0034] The query module is used to query the database according to the database query statement and output the query result.
[0035] In a possible implementation, the query feature set includes: query time, query condition, query target and grouping condition;
[0036] The splitting module is specifically used for:
[0037] The natural language query statement is split into a time screening sub-condition corresponding to the query time, a data screening sub-condition corresponding to the query condition, a data grouping sub-condition corresponding to the grouping condition, and a data target sub-condition corresponding to the query target.
[0038] In a possible implementation, the splitting module is further used to:
[0039] Output the query content corresponding to each query feature according to a preset format;
[0040] If the query content corresponding to a certain query feature includes multiple sub-conditions, the multiple sub-conditions are separated by using a first preset symbol;
[0041] Among them, the preset format is that each sub-condition in the query content is represented by a condition name and condition content; if a sub-condition does not have a condition name in the natural language query statement, the condition name is replaced by a second preset symbol.
[0042] In a possible implementation, the conversion module is used to construct a conversion template in the following manner:
[0043] Establish a dictionary of English characters and Chinese characters for enumeration of database table names, field names, and field content in the database;
[0044] The Chinese characters in the reference dictionary are expanded with synonyms and near-synonyms to form a conversion template.
[0045] In a possible implementation, the conversion module is specifically configured to:
[0046] For each sub-condition, the condition name is matched with the conversion template for similarity, and the database table name or field name with the greatest similarity is found;
[0047] Perform similarity matching between the condition content and the conversion template to find the field content with the greatest similarity;
[0048] Constructing a database query statement fragment corresponding to the sub-condition according to the database table name or field name with the greatest similarity and the field content with the greatest similarity;
[0049] Reorganize the database query statement fragments corresponding to all sub-conditions into a database query statement.
[0050] In a possible implementation, the splitting module is specifically used to:
[0051] By utilizing a natural language model, according to a preset query feature set for the database, the natural language query statement is split into query contents corresponding to each query feature in the query feature set.
[0052] In a possible implementation, the natural language model adopts a small sample learning model or a fine-tuned large language model.
[0053] The third aspect of the present application provides an electronic device, 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 for performing database query through natural language as described in the first aspect of the present application.
[0054] The fourth aspect of the present application provides a computer-readable storage medium having computer-readable instructions stored thereon, and the computer-readable instructions can be executed by a processor to implement the method for performing database query through natural language described in the first aspect of the present application.
[0055] The method, device, electronic device and storage medium for performing database query through natural language provided by the present application receive natural language query statements for a database; according to a preset query feature set for the database, split the natural language query statement into query contents corresponding to each query feature in the query feature set; according to a preset conversion template, convert the query contents corresponding to each query feature into database query statement fragments and then reorganize them into database query statements; query the database according to the database query statement and output the query results. It can be seen that the present application constructs a specific query feature set for a specific database, can accurately split the natural language query statement, and then convert the split parts into accurate database query statements based on the preset conversion template, thereby ensuring the accuracy of the generation of the database query statement, and having low requirements for hardware configuration, which is economical and practical. Therefore, the present application saves more computing power than the currently common solution that is completely based on a large language model, and has a higher accuracy rate. BRIEF DESCRIPTION OF THE DRAWINGS
[0056] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the detailed description of the preferred embodiments below. The accompanying drawings are only for the purpose of illustrating the preferred embodiments and are not to be considered as limiting the present application. Also, the same reference symbols are used throughout the accompanying drawings to represent the same components. In the accompanying drawings:
[0057] Figure 1 A schematic diagram of a process for performing a database query using a natural language provided by the present application is shown;
[0058] Figure 2 A schematic diagram of splitting a natural language query statement provided by the present application is shown;
[0059] Figure 3 An example of SQL statements generated by splitting and converting natural language query statements provided in the present application is shown;
[0060] Figure 4A schematic diagram of the structure of a device for performing database query through natural language provided by the present application is shown;
[0061] Figure 5 A schematic structural diagram of an electronic device provided by the present application is shown. DETAILED DESCRIPTION
[0062] The exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments described herein. On the contrary, these embodiments are provided in order to enable a more thorough understanding of the present disclosure and to fully convey the scope of the present disclosure to those skilled in the art.
[0063] It should be noted that, unless otherwise specified, the technical terms or scientific terms used in this application should have the common meanings understood by technicians in the field to which this application belongs.
[0064] In addition, the terms "first" and "second" etc. are used to distinguish different objects, rather than to describe a specific order. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not limited to the listed steps or units, but may optionally include steps or units that are not listed, or may optionally include other steps or units that are inherent to these processes, methods, products or devices.
[0065] The embodiments of the present application provide a method and device for performing database query through natural language, an electronic device, and a computer-readable storage medium, which are described below in conjunction with the accompanying drawings.
[0066] Figure 1 The flowchart of a method for performing database query by natural language provided by an embodiment of the present application is shown. The execution subject of the present embodiment may be a BI information system (hereinafter referred to as: system), which may be implemented based on software and / or hardware, and the present application does not limit this. Figure 1 As shown, the method specifically comprises the following steps:
[0067] S101, receiving a natural language query statement for a database;
[0068] For example, the above-mentioned database can be a vehicle information database, and the natural language query statement can be "the sales ranking of tractors with National V emission standards in each province from November 18, 2023 to December 5, 2024" or "the proportion of tractor sales in Shijiazhuang City in Hebei Province in 2024".
[0069] S102, according to a preset query feature set for the database, splitting the natural language query statement into query contents corresponding to each query feature in the query feature set;
[0070] The above query feature set is constructed based on the query statement features of a specific database, and can also be understood as a natural language query grammar protocol that defines the database. For example, for a vehicle sales database, its query feature set includes: query time, query condition, query target, and grouping condition.
[0071] In the above step S102, the natural language query statement is split into query contents corresponding to each query feature in the query feature set, specifically including: splitting the time filtering sub-condition corresponding to the query time, the data filtering sub-condition corresponding to the query condition, the data grouping sub-condition corresponding to the grouping condition, and the data target sub-condition corresponding to the query target from the natural language query statement.
[0072] like Figure 2 As shown in the figure, the natural language query statement "sales ranking of tractors in each province with emission standard National V from November 18, 2023 to December 5, 2024" can be split into four parts according to the query feature set:
[0073] Query time (time): November 18, 2023 to December 5, 2024; the time filter sub-condition corresponding to the query time is after November 18, 2023 and before December 5, 2024;
[0074] Query conditions (filter): Emission standard National V, tractor; the data filtering sub-conditions corresponding to the query conditions are emission standard National V, vehicle type tractor;
[0075] Grouping condition (group): each province; the data grouping sub-condition corresponding to the grouping condition is grouping range is province;
[0076] Query target (title): sales ranking; the data target sub-condition corresponding to the query target is the sales ranking of the car.
[0077] After the sub-conditions corresponding to each query feature are separated in the above step S102, the following steps are further included: outputting the query content corresponding to each query feature according to a preset format; if the query content corresponding to a query feature includes multiple sub-conditions, the multiple sub-conditions are separated by using a first preset symbol; for example, the first preset symbol may be a pound sign “#”;
[0078] Among them, the preset format is that each sub-condition in the query content is represented by a condition name and condition content; if a sub-condition does not have a condition name in the natural language query statement, the condition name is replaced by a second preset symbol; for example, the second preset symbol can be a question mark "?".
[0079] Specifically, the above preset format is as follows:
[0080] The time field format is: [comparison operator][yyyy-MM-dd], and multiple sub-conditions are separated by the pound sign "#";
[0081] The filter field format is: [condition name] = [condition content], unspecified condition names are replaced by question marks "?", and multiple sub-conditions are separated by pound signs "#";
[0082] The title field format is: [target], multiple sub-conditions are separated by the pound sign "#";
[0083] The group field format is: [group], and multiple sub-conditions are separated by the pound sign “#”.
[0084] For example,
[0085] Enter a natural language query:
[0086] "Sales ranking of tractors with National V emission standards in each province from November 18, 2023 to December 5, 2024"
[0087] Output split results:
[0088] {
[0089] "time":">=2023-11-18#<=2024-12-05",
[0090] "filter":"Emission standard = National Five#? = Tractor",
[0091] "title":"Sales Ranking",
[0092] "group":"Province"
[0093] }
[0094] In some embodiments of the present application, the above step S102 can be specifically implemented as follows: using a natural language model, according to a preset query feature set for the database, splitting the natural language query statement into query content corresponding to each query feature in the query feature set.
[0095] Specifically, the above-mentioned natural language model can adopt a small sample learning model or a fine-tuned large language model according to the specific business scenario. Few-shot learning aims to enable the model to complete learning tasks, such as classification and regression, well with only a small number of labeled samples (usually far less than the amount of data required for traditional training). Fine-tuning a large language model refers to the process of further training the model with a small-scale, labeled data set for a specific task or field based on an existing large-scale pre-trained language model, so that the model can better adapt to and accurately complete the specific task.
[0096] This application uses a small sample learning model or fine-tuned large language model to enable the model to have the ability to migrate to different business fields, thereby enabling this application to have the ability to accurately query databases in different business fields.
[0097] S103, according to a preset conversion template, converting the query content corresponding to each query feature into a database query statement fragment and then reorganizing it into a database query statement;
[0098] The conversion template can be understood as a kind of customized manual coding. Specifically, the conversion template is constructed in the following way:
[0099] A comparison dictionary of English characters and Chinese characters for enumeration of database table names, field names, and field content in the database is established; synonyms and near-synonyms are expanded for the Chinese characters in the comparison dictionary to form a conversion template.
[0100] For example, if the database stores "China's First Automobile Works", it may be simplified to "FAW" in spoken expression. In this case, "FAW" is a synonym expansion of "China's First Automobile Works", which is beneficial for subsequent matching.
[0101] Specifically, the above step S103 can be implemented as follows: for each sub-condition, the condition name is matched with the conversion template for similarity to find the database table name or field name with the greatest similarity; the condition content is matched with the conversion template for similarity to find the field content with the greatest similarity; the database query statement fragment corresponding to the sub-condition is constructed based on the database table name or field name with the greatest similarity and the field content with the greatest similarity; the database query statement fragments corresponding to all sub-conditions are reorganized into a database query statement.
[0102] In this step, the similarity algorithm is used to match the words in the sub-conditions with the database tables and fields to be queried according to the conversion template. Specifically, for the query condition (filter), the condition name in its sub-condition is calculated for similarity with the previously defined database field dictionary to find the best match. At the same time, the condition content is calculated for similarity with the field content enumeration to find the best match, and finally an SQL fragment is generated, such as: discharge_standard = 'National V' AND vehicle_type = 'Tractor'; other query features are converted into SQL fragments using the same method: `date`> = '2023-11-18' AND `date` < =
[0103] '2024-12-05', FROM tb_vehicle_newcar, group by province. Finally, the SQL fragments are reorganized into SQL statements according to the query target. Complex queries will generate multiple simple statements, such as year-on-year growth and percentage. Figure 3 Shown is an example of the generated SQL statement.
[0104] S104: Query the database according to the database query statement and output the query result.
[0105] According to the database query statement, the data that meets the query conditions in each database table are queried from the database. For complex searches, the data queried from the database table are associated through relational operation algorithms such as year-on-year and percentage to generate the final data query result data.
[0106] It can be seen that compared with the existing problems of high training cost and low accuracy of SQL generation based on large language models, this application combines the advantages of large language models in natural language understanding and analysis with the high accuracy of SQL generation by manual coding. First, a general large language model is used to analyze and process natural language according to a custom natural language query grammar protocol to generate formatted output, and then the formatted output is translated into SQL through custom encoding, and the query data is output.
[0107] For ease of understanding, the above method of the present application is further introduced below with a specific embodiment.
[0108] Suppose the query is: "What is the proportion of tractor sales in Shijiazhuang City in Hebei Province in 2024?"
[0109] Step 1: First, based on the query feature set, use the natural language model to decompose the question into:
[0110] "time":">=2024-01-01#<=2024-12-31",
[0111] "filter":"?=Shijiazhuang City#?=Hebei Province#?=Tractor",
[0112] "title":"Sales volume #%",
[0113] "group":"".
[0114] Step 2: Convert based on conversion template and similarity algorithm:
[0115] Convert the time condition to: `date`>='2024-01-01'and`date`<='2024-12-31';
[0116] Convert the filter condition to: city = 'Shijiazhuang City' and province = 'Hebei Province' and vehicle_type = 'tractor';
[0117] Determine the table name based on "sales volume": from tb_vehicle_newcar;
[0118] The group field is empty, so no group statement is generated.
[0119] Step 3: Generate a SQL statement for the "sales volume" in the title:
[0120] select sum(total)sum_total from tb_vehicle_newcar where `date`>='2024-01-01'and `date`<='2024-12-31'and city='Shijiazhuang'and province='Hebei'and vehicle_type='tractor';
[0121] Generate two SQL statements for "proportion", one of which is to query the sales volume of "Shijiazhuang City": select sum(total)um_total_occupancy_F from tb_vehicle_newcar where `date`>='2024-01-01'and `date`<='2024-12-31'and city='Shijiazhuang City'and province='Hebei Province'and vehicle_type='Tractor';
[0122] Another one is to query the sales volume of "Hebei Province": select sum(total)sum_total_occupancy_Pfromtb_vehicle_newcar where `date`>='2024-01-01'and`date`<='2024-12-31'
[0123] and province = 'Hebei Province' and vehicle_type = 'Tractor'.
[0124] Step 4: Execute the three SQL queries generated above, calculate the percentage value according to the "percentage" algorithm, and return the final result.
[0125] Experiments show that the method of combining the natural language understanding analysis that the large language model is good at with manual coding not only solves the problems of high training cost and poor transferability caused by relying entirely on the large language, but also improves the accuracy of complex SQL. The accuracy of SQL queries generated by this application is over 95%, and with the same accuracy and performance, the cost is over 80% lower than the existing method of generating SQL through a large model. It can be seen that the method of this application has high accuracy and economy.
[0126] The method for performing database query through natural language provided in the embodiment of the present application receives a natural language query statement for a database; according to a preset query feature set for the database, the natural language query statement is split into query contents corresponding to each query feature in the query feature set; according to a preset conversion template, the query contents corresponding to each query feature are converted into database query statement fragments and then reorganized into database query statements; the database is queried according to the database query statement, and the query results are output. It can be seen that the present application constructs a specific query feature set for a specific database, and can accurately split the natural language query statement, and then convert the split parts into accurate database query statements based on the preset conversion template, thereby ensuring the accuracy of the database query statement generation, and having low requirements for hardware configuration, and being economical and practical. Therefore, the present application saves more computing power than the currently common solution that is completely based on a large language model, and has a higher accuracy rate.
[0127] In the above-mentioned embodiment, a method for performing database query through natural language is provided. Correspondingly, the present application also provides a device for performing database query through natural language. The device for performing database query through natural language provided in the embodiment of the present application can implement the above-mentioned method for performing database query through natural language. The device for performing database query through natural language can be implemented by software, hardware, or a combination of software and hardware. For example, the device for performing database query through natural language can include integrated or separate functional modules or units to perform the corresponding steps in the above-mentioned methods. Please refer to Figure 4 , which shows a schematic diagram of a device for performing database query through natural language provided by an embodiment of the present application. Since the device embodiment is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment. The device embodiment described below is only illustrative.
[0128] like Figure 4 As shown, the device 10 for performing database query by natural language may include:
[0129] A receiving module 101 is used to receive a natural language query statement for a database;
[0130] A splitting module 102 is used to split the natural language query statement into query contents corresponding to each query feature in the query feature set according to a preset query feature set for the database;
[0131] The conversion module 103 is used to convert the query content corresponding to each query feature into a database query statement fragment and then reorganize it into a database query statement according to a preset conversion template;
[0132] The query module 104 is used to query the database according to the database query statement and output the query result.
[0133] In a possible implementation, the query feature set includes: query time, query condition, query target and grouping condition;
[0134] The splitting module 102 is specifically used for:
[0135] The natural language query statement is split into a time screening sub-condition corresponding to the query time, a data screening sub-condition corresponding to the query condition, a data grouping sub-condition corresponding to the grouping condition, and a data target sub-condition corresponding to the query target.
[0136] In a possible implementation, the splitting module 102 is further configured to:
[0137] Output the query content corresponding to each query feature according to a preset format;
[0138] If the query content corresponding to a certain query feature includes multiple sub-conditions, the multiple sub-conditions are separated by using a first preset symbol;
[0139] Among them, the preset format is that each sub-condition in the query content is represented by a condition name and condition content; if a sub-condition does not have a condition name in the natural language query statement, the condition name is replaced by a second preset symbol.
[0140] In a possible implementation, the conversion module 103 is used to construct a conversion template in the following manner:
[0141] Establish a dictionary of English characters and Chinese characters for enumeration of database table names, field names, and field content in the database;
[0142] The Chinese characters in the reference dictionary are expanded with synonyms and near-synonyms to form a conversion template.
[0143] In a possible implementation, the conversion module 103 is specifically configured to:
[0144] For each sub-condition, the condition name is matched with the conversion template for similarity, and the database table name or field name with the greatest similarity is found;
[0145] Perform similarity matching between the condition content and the conversion template to find the field content with the greatest similarity;
[0146] Constructing a database query statement fragment corresponding to the sub-condition according to the database table name or field name with the greatest similarity and the field content with the greatest similarity;
[0147] Reorganize the database query statement fragments corresponding to all sub-conditions into a database query statement.
[0148] In a possible implementation, the splitting module 102 is specifically configured to:
[0149] By utilizing a natural language model, according to a preset query feature set for the database, the natural language query statement is split into query contents corresponding to each query feature in the query feature set.
[0150] In a possible implementation, the natural language model adopts a small sample learning model or a fine-tuned large language model.
[0151] The device for performing database query through natural language provided in the embodiment of the present application and the method for performing database query through natural language provided in the embodiment of the present application are based on the same inventive concept and have the same beneficial effects as the methods adopted, operated or implemented therein.
[0152] An embodiment of the present application also provides an electronic device corresponding to the method for performing database query through natural language provided in the aforementioned embodiment. The electronic device may be a mobile phone, a laptop computer, a tablet computer, a desktop computer, etc., to execute the aforementioned method for performing database query through natural language.
[0153] Please refer to Figure 5 , which shows a schematic diagram of an electronic device provided by some embodiments of the present application. Figure 5 As shown, the electronic device 20 includes: a processor 200, a memory 201, a bus 202 and a communication interface 203, and the processor 200, the communication interface 203 and the memory 201 are connected via the bus 202; the memory 201 stores a computer program that can be run on the processor 200, and when the processor 200 runs the computer program, it executes the method for performing database query through natural language provided in any of the aforementioned embodiments of the present application.
[0154] The memory 201 may include a high-speed random access memory (RAM), and may also include a non-volatile memory, such as at least one disk memory. The communication connection between the system network element and at least one other network element is realized through at least one communication interface 203 (which may be wired or wireless), and the Internet, wide area network, local area network, metropolitan area network, etc. may be used.
[0155] The bus 202 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 201 is used to store programs, and the processor 200 executes the programs after receiving the execution instruction. The method for performing database query by natural language disclosed in any implementation of the embodiment of the present application may be applied to the processor 200, or implemented by the processor 200.
[0156] The processor 200 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 or software instructions in the processor 200. The above processor 200 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 readily available 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 application can be implemented or executed. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor, etc. The steps of the method disclosed in the embodiments of the present application 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 201, and the processor 200 reads the information in the memory 201 and completes the steps of the above method in combination with its hardware.
[0157] The electronic device provided in the embodiment of the present application and the method for performing database query through natural language provided in the embodiment of the present application are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented therein.
[0158] An embodiment of the present application also provides a computer-readable storage medium corresponding to the method for performing database query through natural language provided in the aforementioned embodiment. The computer-readable storage medium may be a CD on which a computer program (i.e., a program product) is stored. When the computer program is run by a processor, it executes the method for performing database query through natural language provided in any of the aforementioned embodiments.
[0159] 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.
[0160] The computer-readable storage medium provided by the above-mentioned embodiments of the present application and the method for performing database query through natural language provided by the embodiments of the present application 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.
[0161] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein by equivalents. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present application, and they should all be included in the scope of the claims and specification of the present application.
Claims
1. A method for performing database query by natural language, characterized in that: include: Receiving a natural language query statement for a database; According to a preset query feature set for the database, split the natural language query statement into query contents corresponding to each query feature in the query feature set; According to the preset conversion template, the query content corresponding to each query feature is converted into a database query statement fragment and then reorganized into a database query statement; The database is queried according to the database query statement, and the query result is output.
2. The method according to claim 1, characterized in that The query feature set includes: query time, query conditions, query target and grouping conditions; The step of splitting the natural language query statement into query contents corresponding to each query feature in the query feature set includes: The natural language query statement is split into a time screening sub-condition corresponding to the query time, a data screening sub-condition corresponding to the query condition, a data grouping sub-condition corresponding to the grouping condition, and a data target sub-condition corresponding to the query target.
3. The method according to claim 2, characterized in that The step of splitting the natural language query statement into query contents corresponding to each query feature in the query feature set further includes: Output the query content corresponding to each query feature according to a preset format; If the query content corresponding to a certain query feature includes multiple sub-conditions, the multiple sub-conditions are separated by using a first preset symbol; Among them, the preset format is that each sub-condition in the query content is represented by a condition name and condition content; if a sub-condition does not have a condition name in the natural language query statement, the condition name is replaced by a second preset symbol.
4. The method according to claim 3, characterized in that The transformation template is constructed in the following way: Establish a dictionary of English characters and Chinese characters for enumeration of database table names, field names, and field content in the database; The Chinese characters in the reference dictionary are expanded with synonyms and near-synonyms to form a conversion template.
5. The method according to claim 4, characterized in that The step of converting the query content corresponding to each query feature into a database query statement fragment according to a preset conversion template and then reorganizing the fragment into a database query statement includes: For each sub-condition, the condition name is matched with the conversion template for similarity, and the database table name or field name with the greatest similarity is found; Perform similarity matching between the condition content and the conversion template to find the field content with the greatest similarity; Constructing a database query statement fragment corresponding to the sub-condition according to the database table name or field name with the greatest similarity and the field content with the greatest similarity; Reorganize the database query statement fragments corresponding to all sub-conditions into a database query statement.
6. The method according to claim 1 or 2, characterized in that: The step of splitting the natural language query statement into query contents corresponding to each query feature in the query feature set according to the preset query feature set for the database includes: By utilizing a natural language model, according to a preset query feature set for the database, the natural language query statement is split into query contents corresponding to each query feature in the query feature set.
7. The method according to claim 6, characterized in that The natural language model adopts a small sample learning model or a fine-tuned large language model.
8. A device for performing database query via natural language, characterized in that: include: A receiving module, used for receiving a natural language query statement for a database; A splitting module, configured to split the natural language query statement into query contents corresponding to each query feature in the query feature set according to a preset query feature set for the database; A conversion module, used to convert the query content corresponding to each query feature into a database query statement fragment according to a preset conversion template and then reorganize it into a database query statement; The query module is used to query the database according to the database query statement and output the query result.
9. An electronic device, comprising: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the method according to any one of claims 1 to 7 when executing the computer program.
10. A computer-readable storage medium, characterized in that: Computer-readable instructions are stored thereon, and the computer-readable instructions can be executed by a processor to implement the method according to any one of claims 1 to 7.