A database query statement generation method, device, equipment and storage medium
By generating SQL query statements through nested named entity recognition and semantic matching, the problem of low matching degree between natural language descriptions and databases is solved, achieving more efficient SQL generation and join queries, and reducing the difficulty for users to use the database.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-05-12
- Publication Date
- 2026-03-31
AI Technical Summary
Existing technologies struggle to effectively generate SQL query statements that meet the needs of real-world scenarios. In particular, when the natural language description does not match the database well and column names are omitted, the SQL retrieval results are unsatisfactory, and it is difficult to handle join queries.
A nested named entity recognition model is used to identify natural language query statements. The dataset is trained by nested annotation rules, named entity combination and semantic matching are performed, and structured query statements are generated by combining database and SQL keywords.
It improves the recall rate of SQL generation, reduces incompleteness issues caused by missing components, is suitable for single-table and join queries in real-world scenarios, and lowers the barrier to entry for non-professional users to use the database.
Smart Images

Figure CN116737758B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of natural language processing technology, specifically to a method, apparatus, device, and storage medium for generating database query statements. Background Technology
[0002] Querying relational databases requires proficiency in specialized programming languages, making it difficult for ordinary users to interact effectively with the database. With the rapid development of Natural Language Processing (NLP), generating Structured Query Language (SQL) from Natural Language Query (NLQ) has become a crucial research area in semantic parsing. Numerous SQL-related datasets and deep learning models have emerged, leading to significant advancements in SQL generation. While substantial research has been achieved in NLQ-based SQL generation, challenges remain.
[0003] Due to differences in terminology, natural language often uses multiple expressions to describe the same entity, leading to a low degree of matching between the entity described in natural language and the standard description in the database. This results in unsatisfactory SQL retrieval performance. Furthermore, column names required for SQL generation are sometimes unintentionally omitted in natural language expressions, preventing the generation of effectively executable SQL. In addition, most existing technologies focus on single-table queries, while use cases often require table joins. Therefore, SQL generated by existing methods is difficult to execute effectively and cannot meet the query needs of real-world scenarios. Summary of the Invention
[0004] The purpose of this application is to provide a method and apparatus for generating database query statements, an electronic device, and a computer-readable storage medium.
[0005] The first aspect of this application provides a method for generating database query statements, including:
[0006] Named entities in natural language query statements are identified using a named entity recognition model to obtain nested recognition results; wherein, the named entity recognition model is trained on a dataset with named entities annotated using nested annotation rules;
[0007] The named entities in the nested recognition results are fully combined according to different nesting levels to obtain multiple named entity combinations.
[0008] For each named entity combination, semantic matching is performed on each named entity based on database and structured query language keywords to obtain the semantic matching results and confidence scores of each named entity.
[0009] The average confidence scores of the named entities in each named entity combination are calculated to obtain the named entity combination with the highest average confidence score.
[0010] The named entity combination with the highest average confidence is processed according to the syntax of the database structured query language to obtain the structured query statement corresponding to the natural language query statement.
[0011] A second aspect of this application provides an apparatus for generating database query statements, comprising:
[0012] The recognition module is used to identify named entities in natural language query statements using a named entity recognition model to obtain nested recognition results; wherein, the named entity recognition model is trained on a dataset with named entities annotated using nested annotation rules;
[0013] The full combination module is used to fully combine the named entities in the nested recognition result according to different nesting levels to obtain multiple named entity combinations.
[0014] The semantic matching module is used to perform semantic matching on each named entity combination based on database and structured query language keywords, and obtain the semantic matching results and confidence scores of each named entity.
[0015] The filtering module is used to average the confidence of the named entities in each named entity combination to obtain the named entity combination with the highest average confidence.
[0016] The generation module is used to process the named entity combination with the highest average confidence score according to the syntax of the database structured query language to obtain the structured query statement corresponding to the natural language query statement.
[0017] A third aspect of this application provides an electronic device, including: 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 of this application.
[0018] A fourth aspect of this application provides a computer-readable medium having computer-readable instructions stored thereon, the computer-readable instructions being executable by a processor to implement the method described in the first aspect of this application.
[0019] Compared to existing technologies, the database query statement generation method provided in this application utilizes a named entity recognition model to identify named entities within natural language query statements, obtaining nested recognition results. The named entity recognition model is trained on a dataset with named entities annotated using nested annotation rules. The named entities in the nested recognition results are fully combined according to different nesting levels to obtain multiple named entity combinations. For each named entity combination, semantic matching is performed on each named entity based on database and structured query language keywords to obtain the semantic matching results and their confidence levels. The confidence levels of the named entities in each named entity combination are averaged to obtain the named entity combination with the highest average confidence level. The named entity combination with the highest average confidence level is processed according to the syntax of the database structured query language to obtain the structured query statement corresponding to the natural language query statement. Compared to existing technologies, this application improves recall by annotating natural language query statements in a nested manner, reducing the problem of incomplete structured query statements generated due to missing components in natural language descriptions. It is more suitable for real-world database queries, lowers the barrier for non-professionals to use databases for large-scale data analysis, and reduces manpower consumption. Attached Figure Description
[0020] Various other advantages and benefits will become apparent to those skilled in the art upon reading the following detailed description of preferred embodiments. The accompanying drawings are for illustrative purposes only and are not intended to limit the scope of this application. Furthermore, the same reference numerals denote the same parts throughout the drawings. In the drawings:
[0021] Figure 1 A flowchart illustrating a method for generating database query statements provided in this application is shown;
[0022] Figure 2 A schematic diagram of nested named entities provided in this application is shown;
[0023] Figure 3 A schematic diagram of the named entity combination provided in this application is shown;
[0024] Figure 4 A schematic diagram of a database query statement generation device provided in this application is shown;
[0025] Figure 5 A schematic diagram of an electronic device provided in this application is shown;
[0026] Figure 6 A schematic diagram of a computer-readable storage medium provided in this application is shown. Detailed Implementation
[0027] Exemplary embodiments of the present disclosure will now be described in more detail with reference to the accompanying drawings. While exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure may be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that this disclosure will be thorough and complete, and will fully convey the scope of the disclosure to those skilled in the art.
[0028] It should be noted that, unless otherwise stated, the technical or scientific terms used in this application shall have the ordinary meaning as understood by one of ordinary skill in the art to which this application pertains.
[0029] Furthermore, the terms "first" and "second," etc., are used to distinguish different objects, not to describe a specific order. Additionally, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion. For example, a process, method, system, product, or apparatus that includes a series of steps or units is not limited to the listed steps or units, but may optionally include steps or units not listed, or may optionally include other steps or units inherent to those processes, methods, products, or apparatuses.
[0030] To address common problems in existing NLQ-generated SQL, including the difficulty in effectively executing generated SQL due to missing components in natural language queries, and inconsistencies between entities mentioned in natural language queries and entities in the database (e.g., the names of natural language query tables and columns not being identical to those in the database, leading to incorrect selection of table or column names in the generated SQL), and to meet the query requirements of real-world scenarios, this application provides a method and apparatus for generating database query statements, an electronic device, and a computer-readable storage medium, which will be described below with reference to the accompanying drawings.
[0031] Please refer to Figure 1 The diagram illustrates a flowchart of a method for generating a database query statement according to some embodiments of this application, which may include the following steps S101 to S105:
[0032] Step S101: Use the named entity recognition model to identify named entities in the natural language query statement to obtain nested recognition results.
[0033] The named entity recognition model is trained on a dataset with named entities annotated using nested annotation rules.
[0034] In this embodiment, the nested annotation rules are based on SQL syntax settings, and the specific training process of the named entity recognition model is as follows:
[0035] 1. Collect the natural language query statements used by users when querying the database in the natural language query interface, and organize them to obtain a dataset.
[0036] 2. Perform nested entity annotation on the dataset. In a feasible embodiment, the named entity categories of the annotations can be divided into at least seven categories according to SQL syntax: Table name, Attribute column name, Value, Query column name, Aggregate function, Logic of multi-condition logical relationship, and Operator.
[0037] It should be understood that the meanings of the above seven types of named entities are as follows: Table refers to the table name mentioned in NLQ, used in the SQL FROM clause; Attribute refers to the attribute column name mentioned in NLQ, Value refers to the attribute value mentioned in NLQ, and Operator refers to the operator mentioned in NLQ. All three are used in the SQL WHERE clause as sub-conditions; Query refers to the query column name mentioned in NLQ, and Function refers to the aggregate function mentioned in NLQ. Both are used in the SQL SELECT clause to select columns and perform operations; Logic refers to the logical relationship of multiple conditions in NLQ, mainly divided into two types: AND and OR, used in the SQL WHERE clause to connect two sub-conditions.
[0038] 3. To improve recall and reduce incomplete SQL generation due to missing components, the nesting annotation rules set in this embodiment are as follows: nesting of Attribute, Value, and Operator categories is allowed within the Attribute category; nesting of Query and Function categories is allowed within the Query category; and nesting is not allowed within other types of entities.
[0039] It should be understood that the benefits of this nested annotation rule are: the annotation method is based on SQL syntax, which can improve the recall rate when natural language components are missing or semantics are unclear, and can separate each sub-condition in the SQL syntax, which is beneficial for subsequent filtering and full combination.
[0040] To better understand how the nested annotation rules described above improve recall and separate sub-conditions, please refer to [link to relevant documentation]. Figure 2 Examples. Figure 2The natural language query is: [Average urinary calcium value for those with a urinary protein-to-creatinine ratio exceeding 3 or urinary urobilinogen greater than 2 μmol / L in routine urinalysis]. To improve recall, without referring to the database, it's impossible to distinguish whether the [urinary routine] table contains two columns named [urinary protein] and [urinary creatinine] or a single column named [ratio of urinary protein to creatinine]. Choosing either option arbitrarily could result in the generated SQL query failing to find results. Therefore, nested annotations are needed to preserve both scenarios and sort them by probability score during final semantic matching. Similarly, it's impossible to distinguish whether the [urinary routine] table contains a single column named [average urinary calcium value] or a single column named [urinary calcium value] before calculating the average using aggregate functions. Therefore, the query also needs nested annotations. Furthermore, to separate sub-conditions, attributes are needed to further annotate the sub-conditions in the SQL WHERE clause. For example... Figure 2 As shown, [a ratio of urine protein to urine creatinine exceeding 3] is labeled as an Attribute, and [urinary urobilinogen greater than 2 μmol / L] is also labeled as an Attribute. Figure 2 As shown, in this example, the number of named entities at the outermost level is N = 5.
[0041] 4. Label the dataset according to the above nested labeling rules and divide the labeled dataset into training set, validation set, and test set.
[0042] 5. Build a named entity recognition model. In a feasible embodiment, the named entity recognition model can be a BERT-based pre-trained model.
[0043] 6. Train the named entity recognition model based on the labeled dataset, iterating until convergence. After training, the model can be used to recognize named entities within NLQ to obtain nested recognition results, such as... Figure 2 As shown.
[0044] Step S102: Combine each named entity in the nested recognition result according to different nesting levels to obtain multiple named entity combinations.
[0045] For ease of understanding, remember Figure 2 The outermost entity is the first-level entity, the entities nested inside it are the second-level entities, and so on.
[0046] Let N be the number of entities in the first level, and M be the number of distinct nested levels within each of the N first-level entities, where M = {m1, m2, ..., m}. N},m i≥0. Consider the N first-level entities and their nested entities as groups. Then the set G of the number of elements in each group is G = {g1, g2, ..., g...}. N},g i =m i +1. Perform full combinations on the elements within N groups, selecting one element from each group at a time. The final number of combinations is C.
[0047] For ease of understanding, please refer to the appendix for the combination format. Figure 3 In this example, the set G of the number of elements in each group is G = {1, 3, 1, 2, 2}. Each time, one element from each of the five groups is selected for combination, resulting in a final number of combinations C = 1 × 3 × 1 × 2 × 2 = 12. The following shows two of the combinations after all combinations:
[0048] Combination 1:
[0049] Urinalysis Table The ratio of urine protein to urine creatinine Attribute Exceed Operator three Value or Logic urobilinogen greater than 2 μmol / L Attribute Average urinary calcium level Query
[0050] Combination 2:
[0051]
[0052]
[0053] According to some embodiments of this application, before step S102 above, the method further includes the step of: filtering entities in the nested identification result that do not conform to the nested labeling rules to obtain the filtered nested identification result.
[0054] In this embodiment, to ensure the accuracy of the nested identification results, entities in the nested identification results that do not conform to the nesting annotation rules are filtered out. The filtering method is as follows:
[0055] Traverse the nested entities within the first-level entity, with the following filtering rules:
[0056] (1) If the identified entities Table, Value, Function, Logic, and Operator contain any other types of entities, then delete all levels of entities nested within the entities Table, Value, Function, Logic, and Operator;
[0057] (2) If the identified Attribute entity contains nested entities of categories other than Attribute, Value, and Operator, or if the nested entities do not contain Attribute, then delete all levels of entities nested within that Attribute entity.
[0058] (3) If the identified Query entity does not contain both Query and Function nested within it, then delete all levels of entities nested within that Query entity.
[0059] After traversing the nested entities within the first-level entity, traverse the nested entities within the second-level entity, and so on, until the traversal is complete. This process removes categories that should not be included in the named entity, or identifies nested entities that should not be nested according to the rules, and deletes all nested entity identification results within them. After filtering, proceed to step S102 based on the filtered nested identification results.
[0060] Step S103: For each named entity combination, perform semantic matching on each named entity based on database and structured query language keywords to obtain the semantic matching results and confidence scores of each named entity.
[0061] Specifically, step S103 can be implemented as follows: standardizing the entity classes of table name Table, attribute column name Attribute, and query column name Query based on the database; normalizing the entity classes of aggregate function Function, multi-condition logical relationship Logic, and operator based on SQL keywords; and standardizing the attribute value Value based on the matched attribute column name Attribute.
[0062] In this embodiment, standardizing Table, Attribute, and Query based on the database can be considered a binary classification task. When building the dataset, non-standard natural language descriptions can be concatenated with their corresponding standard database descriptions and labeled as 1, while non-standard natural language descriptions can be concatenated with their non-corresponding standard database descriptions and labeled as 0. These are then input into the binary classification model for training. In a feasible embodiment, the dataset can be concatenated using the [SEP] tag, and the binary classification model can also be the BERT model.
[0063] In this embodiment, normalizing Function, Logic, and Operator based on SQL keywords can be considered a multi-classification task. Separate datasets are created for Function, Logic, and Operator, using non-standard natural language descriptions as feature inputs and standard keywords as outputs to train the multi-classification model. In a feasible embodiment, the loss function for the above multi-classification model can be ArcLoss.
[0064] The value is standardized based on the matched attribute. For example, if the content of the column matched by the attribute is in the YYYYMMDD time format, the value is standardized to that format. Similarly, if the content of the column matched by the attribute is numeric, the value is standardized to that type.
[0065] Step S104: Calculate the average confidence of the named entities in each named entity combination to obtain the named entity combination with the highest average confidence.
[0066] Based on the binary classification model and multi-class classification model mentioned above, the semantic matching results and their confidence scores of each entity in each named entity combination are obtained. The average confidence score of each combination is calculated, and the combination with the highest average confidence score is used as the final combination for generating SQL statements.
[0067] Step S105: Process the named entity combination with the highest average confidence score according to the syntax of the database structured query language to obtain the structured query statement corresponding to the natural language query statement.
[0068] Specifically, in this embodiment, the named entity combination with the highest average confidence is completed according to the query type to generate a structured query statement corresponding to the natural language query statement. The query type can be a single-table query or a join query.
[0069] (1) The query type is a single-table query:
[0070] ① Simply add the `from` keyword before the `Table` entity to get the `from` clause;
[0071] ② Connect the various sub-conditions using Logic, and add the WHERE keyword before each sub-condition fragment to obtain the WHERE clause;
[0072] ③ Combine Function and Query, and add the select keyword before the operation column to obtain the select clause;
[0073] ④ Finally, the three clauses are concatenated in the order of select, from, and where to generate an executable SQL.
[0074] (2) The query type is a join query:
[0075] ① Simply add the `from` keyword before two or more `Table` entities to obtain a `from` clause;
[0076] ② Before concatenating sub-conditions, first supplement the relevant sub-conditions for the join query, i.e., Table.Attribute. a1 =Table.Attributea2 Then connect the various sub-conditions using Logic, and add the WHERE keyword before the sub-condition fragments to obtain the WHERE clause;
[0077] ③ After completing the WHERE clause, add Table before Query to form Table.Query. Then concatenate Function and Table.Query, and add the SELECT keyword before the operation column to obtain the SELECT clause;
[0078] ④ Finally, the three clauses are concatenated in the order of select, from, and where to generate an executable SQL.
[0079] This application maps non-standard terms in natural language to specific databases and SQL keywords through semantic matching, solving the problem of non-standard natural language descriptions leading to unexecutable generated SQL. This application uses a nested approach to annotate NLQs to improve recall and reduce the problem of incomplete generated SQL due to missing components in natural language descriptions. It also fully considers the format and syntax rules of SQL statements and is applicable to single-table and join queries in real-world scenarios, ensuring users can interact with relational databases containing large amounts of data and lowering the barrier for users to extract useful information from massive datasets.
[0080] In the above embodiments, a method for generating database query statements is provided. Correspondingly, this application also provides an apparatus for generating database query statements. Please refer to... Figure 4 This illustration shows a schematic diagram of a database query statement generation apparatus provided by some embodiments of this application. Since the apparatus embodiments are basically similar to the method embodiments, the description is relatively simple; relevant details can be found in the description of the method embodiments. The apparatus embodiments described below are merely illustrative.
[0081] like Figure 4 As shown, the database query statement generation device 10 may include:
[0082] The recognition module 101 is used to recognize named entities in a natural language query statement using a named entity recognition model to obtain nested recognition results; wherein, the named entity recognition model is trained on a dataset with named entities annotated using nested annotation rules;
[0083] The full combination module 102 is used to fully combine each named entity in the nested recognition result according to different nesting levels to obtain multiple named entity combinations.
[0084] The semantic matching module 103 is used to perform semantic matching on each named entity combination based on database and structured query language keywords to obtain the semantic matching results and confidence of each named entity.
[0085] The filtering module 104 is used to calculate the average confidence of the named entities in each named entity combination to obtain the named entity combination with the highest average confidence.
[0086] The generation module 105 is used to process the named entity combination with the highest average confidence according to the syntax of the database structured query language to obtain the structured query statement corresponding to the natural language query statement.
[0087] According to some embodiments of this application, the fully assembled module 102 is specifically used for:
[0088] Before combining all named entities in the nested recognition result according to different nesting levels to obtain multiple named entity combinations, the entities in the nested recognition result that do not conform to the nesting annotation rules are filtered to obtain the filtered nested recognition result.
[0089] The named entities in the filtered nested recognition results are fully combined according to different nesting levels to obtain multiple named entity combinations.
[0090] According to some embodiments of this application, the nested annotation rules are as follows:
[0091] The named entity categories are divided into at least seven categories based on the syntax of the Structured Query Language: table names, attribute column names, attribute values, query column names, aggregate functions, logical relations and operators with multiple conditions mentioned in the natural language query statement;
[0092] Nesting is allowed within the attribute column name category (attribute column name, attribute value, and operator), and nesting is allowed within the query column name category (query column name and aggregate function). Nesting is not allowed within other entity types.
[0093] According to some embodiments of this application, the semantic matching module 103 is specifically used for:
[0094] Based on the database, standardize the entity types of table names, attribute column names, and query column names;
[0095] Normalization processing is performed on aggregate functions, multi-condition logical relations, and operator-type entities based on structured query language keywords;
[0096] The attribute values are standardized based on the matched attribute column names.
[0097] According to some embodiments of this application, the generation module 105 is specifically used for:
[0098] The named entity combination with the highest average confidence is completed according to the query type to generate a structured query statement corresponding to the natural language query statement.
[0099] According to some embodiments of this application, if the query type is a single-table query, the completion method is as follows:
[0100] Add the `from` keyword before the table name entity to get the `from` clause;
[0101] Attribute column names, attribute values, and operator class entities are used as sub-conditions. Each sub-condition is connected by a logical relationship class entity of multiple conditions, and the WHERE keyword is added before the sub-condition fragment to obtain the WHERE clause.
[0102] By concatenating aggregate functions and query column names (which are similar to entities), and adding the `SELECT` keyword before the columns to be operated on, a `SELECT` clause is obtained.
[0103] The three clauses are concatenated in the order of select, from, and where to generate an executable structured query statement.
[0104] According to some embodiments of this application, if the query type is a join query, the completion method is as follows:
[0105] Adding the `from` keyword before two or more table-named entities creates a `from` clause.
[0106] Attribute column names, attribute values, and operator class entities are used as sub-conditions. Before concatenating the sub-conditions, the relevant sub-conditions of the join query are added first. Then, each sub-condition is connected with a logical relationship class entity of multiple conditions, and the WHERE keyword is added before the sub-condition fragment to obtain the WHERE clause.
[0107] Add a table name to the query column name to obtain the SELECT clause. Then, concatenate the aggregate function to the query column name to obtain the table name. Finally, add the SELECT keyword before the operation column to obtain the SELECT clause.
[0108] The three clauses are concatenated in the order of select, from, and where to generate an executable structured query statement.
[0109] The database query statement generation device 10 provided in this application embodiment is based on the same inventive concept and has the same beneficial effects as the database query statement generation method provided in the foregoing embodiments of this application.
[0110] This application also provides an electronic device, such as a mobile phone, laptop computer, tablet computer, or desktop computer, corresponding to the database query statement generation method provided in the foregoing embodiments, to execute the database query statement generation method described above.
[0111] Please refer to Figure 5 This illustrates a schematic diagram of an electronic device provided by some embodiments of this application. For example... Figure 5 As shown, the electronic device 20 includes: a processor 200, a memory 201, a bus 202, and a communication interface 203. 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 run on the processor 200. When the processor 200 runs the computer program, it executes the database query statement generation method provided in any of the foregoing embodiments of this application.
[0112] The electronic device provided in this application embodiment and the database query statement generation method provided in this application embodiment are based on the same inventive concept and have the same beneficial effects as the methods they adopt, run or implement.
[0113] This application also provides a computer-readable storage medium corresponding to the database query statement generation method provided in the foregoing embodiments. Please refer to... Figure 6 The computer-readable storage medium shown is an optical disc 30, on which a computer program (i.e., a program product) is stored. When the computer program is run by a processor, it executes the database query statement generation method provided in any of the foregoing embodiments.
[0114] 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 and magnetic storage media, which will not be elaborated here.
[0115] The computer-readable storage medium provided in the above embodiments of this application and the database query statement generation method provided in the embodiments of this application are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented by the applications stored therein.
[0116] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of this application, and they should all be covered within the scope of the claims and specification of this application.
Claims
1. A method of generating a database query statement, characterized by, The method comprises the following steps: recognizing a named entity in a natural language query sentence by using a named entity recognition model to obtain a nested recognition result, wherein the named entity recognition model is trained based on a data set in which named entities are labeled by using nested labeling rules; completely combining each named entity in the nested recognition result according to different nested levels to obtain a plurality of named entity combinations; performing semantic matching on each named entity in each named entity combination based on a database and a structured query language keyword to obtain a semantic matching result and a confidence level of each named entity; averaging the confidence levels of the named entities in each named entity combination to obtain a named entity combination with the highest average confidence level; processing the named entity combination with the highest average confidence level according to the syntax of the database structured query language to obtain a structured query statement corresponding to the natural language query statement. The nested labeling rules are as follows: The labeled named entity categories are divided into at least seven categories according to the syntax of the structured query language: a table name mentioned in the natural language query statement, an attribute column name, an attribute value, a query column name, an aggregation function, a logical relationship of multiple conditions and an operator; The attribute column name category allows nested attribute column names, attribute values and operators, the query column name category allows nested query column names and aggregation functions, and other types of entities do not allow nested conditions.
2. The method of claim 1, wherein, Before the step of completely combining each named entity in the nested recognition result according to different nested levels to obtain a plurality of named entity combinations, the method further comprises the following steps: filtering entities in the nested recognition result that do not meet the nested labeling rules to obtain a filtered nested recognition result; completely combining each named entity in the filtered nested recognition result according to different nested levels to obtain a plurality of named entity combinations. The step of performing semantic matching on each named entity in each named entity combination based on a database and a structured query language keyword comprises the following steps:
3. The method of claim 1, wherein, standardizing table names, attribute column names and query column name category entities based on the database; normalizing aggregation function, logical relationship of multiple conditions and operator category entities based on the structured query language keyword; standardizing attribute values based on matched attribute column names. The step of processing the named entity combination with the highest average confidence level according to the syntax of the database structured query language to obtain a structured query statement corresponding to the natural language query statement comprises the following step:
4. The method of claim 1, wherein, completing the named entity combination with the highest average confidence level according to a query type to generate a structured query statement corresponding to the natural language query statement. If the query type is a single-table query, the completion mode is as follows:
5. The method of claim 4, wherein, a from keyword is supplemented before the table name category entity to obtain a from clause; The attribute column name, attribute value and operator class entity are taken as a sub-condition, each sub-condition is connected by a multi-condition logical relationship class entity, and a where keyword is supplemented before the sub-condition segment to obtain a where clause; The aggregation function and query column name class entity are spliced, and a select keyword is supplemented before the operation column to obtain a select clause; The three clauses are spliced in the order of select, from and where to generate an executable structured query statement.
6. The method of claim 4, wherein, If the query type is a join table query, the following completion method is used: A from keyword is supplemented before the two or more table name class entities to obtain a from clause; The attribute column name, attribute value and operator class entity are taken as a sub-condition, each sub-condition is connected by a multi-condition logical relationship class entity, and a where keyword is supplemented before the sub-condition segment to obtain a where clause; The table name class entity is supplemented before the query column name class entity, and the aggregation function class entity is spliced with the query column name class entity supplemented with the table name, and a select keyword is supplemented before the operation column to obtain a select clause; The three clauses are spliced in the order of select, from and where to generate an executable structured query statement.
7. A device for generating database query statements, characterized in that, The recognition module is configured to recognize a named entity in the natural language query statement by using a named entity recognition model to obtain a nested type recognition result; the named entity recognition model is trained based on a data set in which named entities are labeled by using a nested labeling rule; The full combination module is configured to fully combine each named entity in the nested type recognition result according to different nested levels to obtain a plurality of named entity combinations; The semantic matching module is configured to perform semantic matching on each named entity in each named entity combination based on a database and a structured query language keyword to obtain a semantic matching result and a confidence degree of each named entity; The screening module is configured to average the confidence degrees of the named entities in each named entity combination to obtain a named entity combination with the highest average confidence degree; The generation module is configured to process the named entity combination with the highest average confidence degree according to a syntax of a database structured query language to obtain a structured query statement corresponding to the natural language query statement. The nested labeling rule is as follows: The labeled named entity categories are divided into at least seven categories according to the syntax of the structured query language: a table name mentioned in the natural language query statement, an attribute column name, an attribute value, a query column name, an aggregation function, a multi-condition logical relationship and an operator; The attribute column name, attribute value and operator are allowed to be nested in the attribute column name category, and the query column name and aggregation function are allowed to be nested in the query column name category, and no nesting is allowed in other types of entities. The memory, the processor and the computer program stored on the memory and executable on the processor are characterized in that the processor executes the computer program to implement the method of any one of claims 1 to 6.
8. An electronic device comprising: 9. A computer-readable storage medium, characterized in that, having computer readable instructions stored thereon that are executable by a processor to implement the method of any one of claims 1 to 6.
Citation Information
Patent Citations
Entity identification method and device based on multilayer model, equipment and storage medium
CN111738008A
Language escape method and device, equipment and storage medium
CN115292448A