Database natural language query method and device, electronic equipment and storage medium
By calculating the vector similarity between the query statement and the database table and constructing a pattern graph, the target data table is determined, which solves the problem of low efficiency in natural language queries for large databases and achieves efficient and accurate query result acquisition.
Patent Information
- Application Number
- CN202111496150.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-08
- Publication Date
- 2025-10-21
- Estimated Expiration
- 2041-12-08
AI Technical Summary
Existing database natural language query solutions cannot effectively support large databases with hundreds of data tables, and have low computational efficiency and insufficient accuracy.
By obtaining the vector of the question statement, calculating its similarity with the vector of the data table in the database, constructing a pattern graph and determining the target data table, querying the results from the target data table using a natural language escaping model, processing the question statement and data table using a pre-trained encoder, attention pooling layer and feedforward layer, and training the similarity model using Triplet Loss.
It improves the computational efficiency and accuracy of natural language queries on large databases, enabling queries on hundreds or even thousands of data tables, thus enhancing query efficiency and accuracy.
Smart Images

Figure CN114328823B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of data processing technology, and in particular to a database natural language query method and device, electronic equipment, and storage medium. Background Art
[0002] A database natural language interface is an interactive interface for a relational database. In a narrow sense, a database natural language interface refers to a database natural language query interface. Through the database natural language interface, users can directly make query requests in the natural language used in daily life to obtain information in the database.
[0003] With the development of big data technologies, the scale and complexity of database applications are also growing. A large number of applications now use large databases containing thousands of tables covering hundreds of subdomains. However, existing database natural language interface (NLI) query solutions are limited by computational efficiency and the expressiveness of existing network models, making them incapable of supporting large databases with hundreds of tables. Summary of the Invention
[0004] The embodiments of the present application provide a database natural language query method and device, electronic device, and storage medium to solve or partially solve the problem in the related art that the database natural language query solution cannot support large databases.
[0005] In order to solve the above problems, the present invention discloses a method for natural language querying a database, which includes:
[0006] Obtaining a question statement to be queried; the question statement is a natural language statement;
[0007] Determining a question vector corresponding to the question statement;
[0008] Calculating the similarity between the question vector and the table vectors corresponding to each data table in the database;
[0009] determining a target data table according to the similarity;
[0010] A natural language translation model is used to determine the query result corresponding to the question statement from the target data table.
[0011] Optionally, determining a question vector corresponding to the question statement includes:
[0012] The question sentence is processed in sequence using a pre-trained encoder, an attention pooling layer, and a feed-forward layer, and a question vector corresponding to the question sentence is output.
[0013] Optionally, before calculating the similarity between the question vector and the table vectors corresponding to each data table in the database, the method further includes:
[0014] Get each data table in the database;
[0015] For each of the data tables, a pre-trained encoder, an attention pooling layer, and a feedforward layer are used to process the data table in sequence, and a table vector corresponding to the data table is output.
[0016] Optionally, determining the target data table according to the similarity includes:
[0017] Generating a schema of the database according to each data table in the database; the schema includes a plurality of connected components, each of which includes at least one data table;
[0018] For each connected component, determining a connected component score of the connected component according to a similarity corresponding to a data table in the connected component;
[0019] Determine the connected component whose connected component score meets the specified condition as the target connected component;
[0020] A target data table is determined from the data tables included in the target connected component.
[0021] Optionally, determining the score of the connected component according to the similarity corresponding to the data table in the connected component includes:
[0022] For each of the data tables in the connected components, determining a data table score of the data table according to a similarity corresponding to the data table;
[0023] Determine a maximum data table score from the data table scores of the connected components;
[0024] The maximum data table score is determined as the connected component score of the connected component.
[0025] Optionally, determining the connected component whose connected component score meets a specified condition as a target connected component includes:
[0026] sorting the plurality of connected components according to the connected component scores of the respective connected components;
[0027] A preset number of connected components with the highest connected component scores are determined as target connected components.
[0028] Optionally, determining a target data table from a data table included in the target connected component includes:
[0029] For each of the target connected components, determining whether there is a data table with a data table score greater than a preset score threshold in the target connected component;
[0030] If so, determining the data table corresponding to the data table score greater than the preset score threshold in the target connected component as the target data table;
[0031] If not, the data table corresponding to the largest data table score in the target connected component is determined as the target data table.
[0032] The present application also discloses a database natural language query device, which includes:
[0033] A question statement acquisition module is used to acquire a question statement to be queried; the question statement is a natural language statement;
[0034] A question vector determination module, configured to determine a question vector corresponding to the question statement;
[0035] A similarity calculation module is used to calculate the similarity between the question vector and the table vectors corresponding to each data table in the database;
[0036] a target data table determining module, configured to determine a target data table according to the similarity;
[0037] A target data table query module is used to determine the query result corresponding to the question statement from the target data table by adopting a natural language escape model.
[0038] Optionally, the question vector determination module is specifically configured to sequentially process the question statement using a pre-trained encoder, an attention pooling layer, and a feedforward layer, and output a question vector corresponding to the question statement.
[0039] Optionally, the device further comprises:
[0040] The data table acquisition module is used to obtain each data table in the database;
[0041] The table vector determination module is used to process each data table in sequence using a pre-trained encoder, an attention pooling layer, and a feedforward layer, and output a table vector corresponding to the data table.
[0042] Optionally, the target data table determination module includes:
[0043] A pattern graph generating module, configured to generate a pattern graph of the database according to each data table in the database; the pattern graph includes a plurality of connected components, each of which includes at least one data table;
[0044] A connected component score determination module is used to determine the connected component score of each connected component according to the similarity corresponding to the data table in the connected component;
[0045] a target connected component determination module, configured to determine a connected component whose connected component score satisfies a specified condition as a target connected component;
[0046] The target data table determination module based on the target connected component is used to determine the target data table from the data tables included in the target connected component.
[0047] Optionally, the connected component score determination module includes:
[0048] a data table score determination module, configured to determine, for each of the data tables in the connected components, a data table score of the data table according to the similarity corresponding to the data table;
[0049] a maximum data table score determining module, configured to determine the maximum data table score from the data table scores of the data tables of the connected components;
[0050] The connected component score determination module based on the maximum data table score is configured to determine the maximum data table score as the connected component score of the connected component.
[0051] Optionally, the target connected component determination module includes:
[0052] A connected component score sorting module, configured to sort the plurality of connected components according to the connected component score of each connected component;
[0053] The module for determining target connected components based on ranking is used to determine a preset number of connected components with the highest connected component scores as target connected components.
[0054] Optionally, the module for determining a target data table based on a target connected component includes:
[0055] A data table score determination module is used to determine, for each target connected component, whether there is a data table in the target connected component with a data table score greater than a preset score threshold;
[0056] A first target data table determining module is configured to determine, in the target connected component, a data table corresponding to a data table score greater than the preset score threshold as a target data table;
[0057] The second target data table determination module is configured to, if not, determine the data table corresponding to the largest data table score in the target connected component as the target data table.
[0058] An embodiment of the present application also discloses an electronic device, including: a processor; and a memory, on which executable code is stored. When the executable code is executed, the processor executes the database natural language query method as described in one or more embodiments of the present application.
[0059] The embodiments of the present application also disclose one or more machine-readable media on which executable codes are stored. When the executable codes are executed, the processor executes the database natural language query method as described in one or more embodiments of the present application.
[0060] Compared with the prior art, the embodiments of the present application have the following advantages:
[0061] In an embodiment of the present application, during the natural language processing process, especially in the process of querying a database using natural language, the question vector corresponding to the question statement can be determined by obtaining the natural language question statement to be queried, and then the similarity between the question vector and the table vectors corresponding to each data table in the database is calculated. Then, the target data table is determined based on the similarity, and finally, the natural language escape model is used to determine the query result corresponding to the question statement from the target data table. By screening the data tables in the database and inputting the screened target data tables into the natural language escape model to obtain the query result corresponding to the question statement, the computational efficiency of the database natural language query process can be improved, so that the database natural language query interface can support large databases with hundreds or even thousands of data tables. BRIEF DESCRIPTION OF THE DRAWINGS
[0062] Figure 1 This is a flowchart of the steps of a natural language query method for a database provided in an embodiment of the present application;
[0063] Figure 2 1 is a flow chart of calculating the similarity between a problem statement and a data table provided in an embodiment of the present application;
[0064] Figure 3 It is a schematic diagram of the pattern diagram provided in the embodiments of the present application;
[0065] Figure 4 It is a structural block diagram of a database natural language query device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0066] In order to make the above-mentioned purposes, features and advantages of the present application more obvious and easy to understand, the present application is further described in detail below with reference to the accompanying drawings and specific implementation methods.
[0067] With the development of big data technologies, the scale and complexity of database applications are also growing. A large number of applications now use large databases containing thousands of tables covering hundreds of subdomains. There are two main approaches to natural language database queries: rule-based methods and neural network methods.
[0068] Among them, the rule method is to first convert the natural language question sentence input by the user into a syntax tree, and then convert the nodes in the syntax tree into components in the corresponding SQL (Structured Query Language) statement according to pre-defined rules and linguistic knowledge (such as knowledge graph). Finally, different SQL components are assembled into a complete SQL statement according to pre-defined manual rules. In the process of converting the syntax tree nodes into SQL components, it is necessary to compare the similarity between a node and all data tables and columns in the database. When the number of data tables is too large, the calculation rate of this method is very slow. In addition, since the commonly used similarity calculation method is difficult to fully represent the semantics of a word, the accuracy of this method is low and it is difficult to support a variety of questioning methods.
[0069] Existing neural network methods typically feed the natural language question sentence and the table and column names of all the data tables in the database into a single neural network model. However, due to limitations in computational efficiency and the expressive power of existing network models, this neural network approach is not suitable for large databases.
[0070] In this regard, one of the core inventive points of the database natural language query method provided in the embodiment of the present application is that before escaping the natural language question statement, tables related to the natural language question statement are selected from the database, thereby reducing the number of data tables subsequently input into the natural language escaping model, which can improve computing efficiency and realize database natural language query of large databases with hundreds or even thousands of data tables.
[0071] Reference Figure 1 , shows a flowchart of the steps of a database natural language query method provided in an embodiment of the present application, which may specifically include the following steps:
[0072] Step 101: Obtain a question statement to be queried; the question statement is a natural language statement.
[0073] In the embodiments of the present application, a query statement generally refers to a statement entered by a user in natural language to retrieve relevant information from a database. Natural language, as opposed to computer-specific languages such as programming languages, generally refers to a language that has evolved naturally with culture and is a primary tool for human communication and thinking.
[0074] That is to say, the embodiments of the present application can obtain question statements input by the user in natural language form. Specifically, it can obtain question statements input by the user through text input, or it can obtain question statements input by the user through voice input, etc. The present application does not limit the specific method of user input of question statements.
[0075] Step 102: Determine the question vector corresponding to the question statement.
[0076] In an embodiment of the present application, after obtaining a question statement in natural language form input by the user, the question statement needs to be encoded into a corresponding vector form that can be understood by the computer, that is, the question vector corresponding to the question statement needs to be determined.
[0077] In an optional embodiment of the present application, the process of determining the question vector corresponding to the question statement includes:
[0078] The question sentence is processed in sequence using a pre-trained encoder, an attention pooling layer, and a feed-forward layer, and a question vector corresponding to the question sentence is output.
[0079] This embodiment uses a pre-trained encoder, an attention pooling layer, and a feedforward layer to process the question statement in sequence to obtain a question vector corresponding to the question statement. Specifically, the question statement is input into the pre-trained encoder in a specific format, and then the output of the pre-trained encoder based on the question statement is used as the input of the attention pooling layer, and then the output of the attention pooling layer is used as the input of the feedforward layer, and finally the output result of the feedforward layer is used as the question vector corresponding to the question statement.
[0080] The pre-trained encoder can be BERT (Bidirectional Encoder Representations from Transformers, a method based on large-scale corpus pre-training to represent words in sentences or articles as vectors), or other pre-trained word vector models, such as the word2vec model, the Glove model, the ELMo model, the RoBERTa model, etc.
[0081] Taking BERT as an example, Figure 2 As shown, first, each word of the question sentence is sequentially connected and input into BERT. The input sequence can be expressed as:
[0082] [CLS], q_1, q_2,…, q_D, [SEP]
[0083] Where D represents the length of the question sentence, i.e., q_1 represents the first word in the question sentence, q_2 represents the second word in the question sentence, and q_D represents the last word in the question sentence. [CLS] and [SEP] are special symbols used by BERT to indicate the beginning and end of the question sentence input to BERT. i.e., [CLS] is placed at the beginning of the question sentence to indicate that the question sentence is about to be input, and [SEP] is placed at the end of the question sentence to indicate that the question sentence input is complete.
[0084] For example, when the question statement is "Who is the athlete with the highest average score per game...", the corresponding input sequence is expressed as:
[0085] [CLS], who is the athlete with the highest average score per game? [SEP]
[0086] After the question statement is input into BERT, the corresponding word vector can be output. The word vector can be expressed as:
[0087] h_([CLS]), h_(q_1), h_(q_2),…, h_(q_D), h_([SEP])
[0088] Among them, h_() represents the vector corresponding to each word in the input BERT.
[0089] After obtaining the BERT output, the BERT output (i.e., h_([CLS]), h_(q_1), h_(q_2), …, h_(q_D), h_([SEP])) is used as the input to the attention pooling layer. The attention mechanism in the attention pooling layer learns the most important words in the question sentence and uses the pooling mechanism to represent the entire sentence with a single vector. In other words, the attention pooling layer outputs a vector that represents the semantic information of the entire question sentence. The output vector of the attention pooling layer is denoted as h_Q.
[0090] After obtaining the output of the attention pooling layer, the output of the attention pooling layer (i.e., h_Q) is used as the input of the feedforward layer. Feature extraction is performed through the feedforward layer to output the final vector corresponding to the question statement, i.e., the question vector, which can be expressed as E_Q.
[0091] Step 103: Calculate the similarity between the question vector and the table vectors corresponding to each data table in the database.
[0092] In an embodiment of the present application, the process of determining the question vector corresponding to the question statement and the process of determining the table vector corresponding to each data table in the database can be independent of each other. Therefore, the table vector corresponding to each data table in the database can be calculated in advance. For example, the table vector corresponding to each data table in the database can be calculated before obtaining the question statement. By decoupling the calculation process of the question vector and the table vector, the embodiment of the present application can improve the efficiency of the query.
[0093] The process of determining the table vector corresponding to the data table in the embodiment of the present application may specifically include:
[0094] Get each data table in the database;
[0095] For each of the data tables, a pre-trained encoder, an attention pooling layer, and a feedforward layer are used to process the data table in sequence, and a table vector corresponding to the data table is output.
[0096] In this embodiment, each data table in the database can be first obtained, including the table name and the column names of all columns in the data table; each data table is processed sequentially using a pre-trained encoder, an attention pooling layer, and a feed-forward layer to obtain a table vector corresponding to the data table. Specifically, the data table can be input into the pre-trained encoder according to a specific format, and the output of the pre-trained encoder based on the data table is then used as the input of the attention pooling layer. The output of the attention pooling layer is then used as the input of the feed-forward layer. Finally, the output of the feed-forward layer is used as the table vector corresponding to the data table.
[0097] Among them, the pre-trained encoder, attention pooling layer and feed-forward layer can be the same as the pre-trained encoder, attention pooling layer and feed-forward layer used in the process of determining the question vector corresponding to the question sentence in the previous article.
[0098] Taking BERT as an example, Figure 2 As shown, first input the table name and column name of the data table into BERT in order. The input sequence can be expressed as:
[0099] [CLS],t_1,t_2,…,t_i,…,t_(K_t),[SEP],c_1^1,c_2^1,…,c_j^1…,c_(K_1)^ 1,[SEP],…,c_1^i,…,c_(K_i)^i,[SEP],…,c_1^S,c_2^S,…,c_(K_S)^S,[SEP]
[0100] Where t_i represents the i-th word in the table name, K_t represents the length of the table name, c_j^i represents the j-th word in the name of the i-th column in the data table, K_i represents the length of the i-th column name, K_S represents the number of words in the S-th column name (that is, the serial number subscript i that generally represents K_i is replaced by S), and [CLS] and [SEP] are special symbols used by BERT.
[0101] For example, when the data table is named "Player Skills" and the column names are "Player Name", "Player Age", ..., "Player Nationality", the corresponding input sequence is expressed as:
[0102] [CLS], player, skills, [SEP], player, name, [SEP], player, age, [SEP]… player, nationality, [SEP]
[0103] After the data table is input into BERT, the corresponding word vector can be output. The word vector can be expressed as:
[0104] h_([CLS]), h_(t_1), h_(t_2),…, h_(t_i),…, h_(t_(K_t)), h_([SEP]), h_(c_1^1), h_(c_2^1),…, h_(c_j^1)…, h_(c_(K_ 1)^1),h_([SEP]),…,h_(c_1^i),…,h_(c_(K_i)^i),h_([SEP]),…,h_(c_1^S),h_(c_2^S),…,h_(c_(K_S)^S),h_([SEP])
[0105] Among them, h_() represents the vector corresponding to each word in the input BERT.
[0106] After obtaining the BERT output, it is used as the input to the attention pooling layer. The attention mechanism in the attention pooling layer learns the most important words in the question sentence and uses the pooling mechanism to represent the entire sentence with a single vector. In other words, the attention pooling layer outputs a vector that represents the semantic information of the entire question sentence. The output vector of the attention pooling layer is denoted as h_T.
[0107] After obtaining the output of the attention pooling layer, the output of the attention pooling layer (i.e., h_T) is used as the input of the feedforward layer. Feature extraction is performed through the feedforward layer, and the final vector corresponding to the output data table, i.e., the table vector, can be expressed as E_T.
[0108] After determining the question vector corresponding to the question statement and the table vectors corresponding to each data table in the database, the similarity between the question vector and each table vector can be calculated. This similarity can be cosine similarity or other similarity metrics such as Pearson similarity, dynamic time warping, Hamming distance, Euclidean distance, etc.
[0109] In this embodiment, a trained similarity model is used to calculate the similarity between the question vector and each table vector. During the training process of the similarity model, Triplet Loss (a metric learning method for representation learning) can be used as the learning training target of the similarity model. The corresponding triplet is constructed as follows:
[0110] Anchor point: Since the question sentence input by the user is used as the query in the use stage of the cosine similarity model, the current reference sample instance when constructing Triplet Loss is the question vector, i.e. E_Q;
[0111] Positive example: a sample in the database that corresponds to the anchor point or is of the same category, that is, the table vector E_T of the data table;
[0112] Negative examples: Sample examples in the database that are irrelevant to the anchor point, that is, irrelevant table vectors, denoted as E_T^*.
[0113] Taking cosine similarity as an example, Triplet Loss is constructed as: relu(cos(E_Q, E_T^*)-cos〖(E_Q, E_T)+m〗), where relu() represents the linear rectification function and cos() represents the cosine similarity function.
[0114] In this embodiment, any convex optimization algorithm can be used to achieve the optimization of Triplet Loss, and this application does not limit this. Through the above training method, the similarity model can better analyze the intrinsic connection between the question and the data table to support a variety of questioning methods and improve accuracy.
[0115] Step 104: Determine the target data table according to the similarity.
[0116] In an embodiment of the present application, after obtaining the similarity between the question vector and each table vector, the target data table for question statement query can be determined based on the similarity to reduce the number of data tables to be queried and improve the accuracy of the query.
[0117] In an optional embodiment of the present application, the process of determining the target data table based on similarity may specifically include:
[0118] Generate a schema of the database according to each data table in the database; the schema includes a plurality of connected components, each of which includes at least one data table;
[0119] For each connected component, determining a connected component score of the connected component according to a similarity corresponding to a data table in the connected component;
[0120] Determine the connected component whose connected component score meets the specified condition as the target connected component;
[0121] A target data table is determined from the data tables included in the target connected component.
[0122] In this embodiment, a schema diagram of the database is first constructed, and the schema diagram is a directed graph. The specific construction process can be to determine the nodes of the schema diagram based on the table names of the various data tables in the database and the column names contained in each data table, that is, the table name of a data table corresponds to a node, and each column name contained in each data table corresponds to a node. It can be understood that the nodes in the schema diagram include two types, one is the node corresponding to the table name of the data table, and the other is the node corresponding to the column name of the data table. For each data table, the column names contained in the data table can be determined. Therefore, a directed line segment can be generated from the node corresponding to the table name of the data table to the node corresponding to each column name contained therein, as the edge of the schema diagram. If two data tables contain a common keyword, that is, the table name of one data table is the column name of the other data table, then the two data tables are considered to contain a common keyword; the primary and foreign key relationship between the two data tables can be determined, that is, the data table with the common keyword as the table name is determined to be the primary key table, and the data table with the column name containing the common keyword as the foreign key table, and a directed line segment is generated from the node corresponding to the foreign key table to the node corresponding to the primary key table, which also serves as the edge of the pattern graph. It can be understood that the edges in the pattern graph include two types: one is a directed line segment from the node corresponding to the table name to the node corresponding to the column name contained in the data table corresponding to the table name, and the other is a directed line segment from the node corresponding to the foreign key table to the node corresponding to the primary key table.
[0123] In order to make those skilled in the art better understand the above database schema diagram, Figure 3 The examples shown are explained and illustrated:
[0124] Suppose there are six data tables in the database. The first data table is named Skill Scores, and its column names are passing, dribbling, finishing, and player_id; the second data table is named SoccerPlayer, and its column names are Skill Scores, age, team_id, player_id, player_name, id, and SoccerTeam; the third data table is named Soccer Team, and its column names are team_id, team_name, and country; the fourth data table is named Baseball Team, and its column names are team_id and team_name; the fifth data table is named Baseball Player, and its column names are Baseball Team, team_id, player_name, birthday, height, player_id, and Batting; the sixth data table is named Batting, and its column names are id, player_id, and points_won.
[0125] The nodes of the pattern graph can be determined based on the table names of each data table and the column names contained in each data table. At the same time, directed line segments are determined from the nodes corresponding to the table names of each data table to the nodes corresponding to the column names of the data table. Based on the primary-foreign key relationship between the two data tables, directed line segments are generated from the nodes corresponding to the foreign key table to the nodes corresponding to the primary key table. For example, the table name "Skill Scores" in the first data table is also one of the column names in the second data table "Soccer Player". Therefore, the node corresponding to the column name "Skill Scores" in the second data table "Soccer Player" can be merged with the node corresponding to the table name "Skill Scores" in the first data table. At the same time, it is determined that there is a primary-foreign key relationship between the first data table and the second data table, that is, the first data table is the primary key table and the second data table is the foreign key table. A directed line segment is generated from the node corresponding to the table name of the second data table to the node corresponding to the table name of the first data table.
[0126] The constructed pattern graph contains multiple connected components, each of which contains at least one data table; it can be understood that the data tables in the same connected component have an association relationship. Figure 3The pattern diagram shown contains two connected components, one of which includes three data tables named Skill Scores, Soccer Player, and Soccer Team; the other includes three data tables named Baseball Team, Baseball Player, and Batting. It can be seen that there is no connection between nodes belonging to different connected components. However, any node in the same connected component has at least one other node connected to it. Since the node corresponding to the table name in the pattern diagram and the node corresponding to each column name in the data table corresponding to the table name must have a connection relationship, all nodes in the pattern diagram corresponding to the same data table must belong to the same connected component. Therefore, it can be determined that a connected component contains at least one data table.
[0127] In the process of determining the target data table, the target connected component where the target data table is located can be first determined. When determining the target connected component, the target connected component can be determined based on the connected component scores of each connected component in the pattern graph. That is, in this embodiment, after the pattern graph of the database is constructed, the connected components included in the pattern graph and the data tables included in each connected component can be determined. Then, based on the similarity corresponding to the data tables included in the connected components, the connected component scores of each connected component can be determined. Then, the connected components whose connected component scores meet the specified conditions are determined as target connected components. Finally, the target data table is determined from the data tables included in the target connected components.
[0128] The process of determining the score of the connected component based on the similarity corresponding to the data table in the connected component may specifically include:
[0129] For each of the data tables in the connected components, determining a data table score of the data table according to a similarity corresponding to the data table;
[0130] Determining a maximum data table score from the data table scores of the connected components;
[0131] The maximum data table score is determined as the connected component score of the connected component.
[0132] In this embodiment, the similarity between the problem vector and each table vector calculated as above can be used to determine the similarity corresponding to each data table, and then the similarity corresponding to the data table can be determined as the data table score of the data table; for each connected component, the data table scores of each data table in the connected component can be determined, and then the maximum data table score is determined therefrom, and the maximum data table score is used as the connected component score of the connected component.
[0133] For example, assuming that a connected component contains three data tables, the corresponding similarities of these three data tables are 70%, 80%, and 90%, respectively. It can be determined that the data table scores of these three data tables are 70, 80, and 90, respectively; among them, the largest data table score is 90, so the connected component score of the connected component is 90.
[0134] After determining the connected component scores of each connected component, a target connected component can be determined based on the connected component scores. Optionally, the process of determining the connected component whose connected component scores meet a specified condition as the target connected component may specifically include:
[0135] sorting the plurality of connected components according to the connected component scores of the respective connected components;
[0136] A preset number of connected components with the highest connected component scores are determined as target connected components.
[0137] In this embodiment, after determining the connected component scores of each connected component, the connected component scores can be sorted in ascending order or descending order. Then, starting with the connected component with the highest connected component score, a preset number of connected components are selected as target connected components. The preset number can be set according to actual conditions, for example, the preset number can be 5.
[0138] It should be noted that the method of determining the target connected component in the present application is not limited to the method described in the above example. For example, a connected component whose connected component score is greater than a connected component score threshold can also be determined as a target connected component, where the connected component score threshold can be pre-set according to actual conditions; all of these fall within the scope of protection of the present application.
[0139] After determining the target connected component, in one example, the data table included in the target connected component can be directly determined as the target data table. In order to improve query efficiency, the embodiment of the present application can further filter out the target data table from the data table included in the target connected component to further reduce the number of data tables subsequently input into the natural language escape model. Specifically, it may include:
[0140] For each of the target connected components, determining whether there is a data table with a data table score greater than a preset score threshold in the target connected component;
[0141] If so, determining the data table corresponding to the data table score greater than the preset score threshold in the target connected component as the target data table;
[0142] If not, the data table corresponding to the largest data table score in the target connected component is determined as the target data table.
[0143] In this embodiment, for each determined target connected component, it is first determined whether the target connected component includes a data table whose data table score is greater than a preset score threshold. If so, the data table corresponding to the data table score greater than the preset score threshold is determined as the target data table; if not, the data table corresponding to the largest data table score in the target connected component is determined as the target data table.
[0144] Continuing with the above example, assume that a target connected component contains three data tables, and the data table scores of these three data tables are 70, 80, and 90 respectively; when the preset score threshold is 85, it can be determined that there is a data table in the target connected component with a data table score greater than the preset score threshold, that is, a data table with a data table score of 90. Therefore, the data table with a data table score of 90 is determined as the target data table. When the preset score threshold is 95, it can be determined that there is no data table in the target connected component with a data table score greater than the preset score threshold. Therefore, the data table with the largest data table score in the target connected component is determined as the target data table, that is, the data table with the largest data table score is 90, that is, the data table with a data table score of 90 is determined as the target data table.
[0145] It should be noted that the method for filtering out the target data table from the data table of the target connected component is not limited to the method described in the above example. For example, the total number of data tables contained in the target connected component can also be determined first. However, when the total number of data tables exceeds a set threshold, the data tables are sorted according to their data table scores, and the data tables with the highest data table scores and the number of data tables corresponding to the threshold are selected as the target data tables. Alternatively, the data table with the highest data table score in each target connected component can be directly determined as the target data table, and so on; all of these methods fall within the scope of protection of this application.
[0146] Step 105: Using a natural language translation model, determine the query result corresponding to the question statement from the target data table.
[0147] In this embodiment, after determining the target data table, the target data table and the question statement can be input into a natural language translation model. This natural language translation model can be a model used by neural network methods in existing database natural language query methods. The model is used to convert natural language into SQL and then search for query results corresponding to the input question statement from the input target data table. Because the target data tables are a relatively small number of data tables selected from a large database and are most relevant to the question statement, the natural language translation model can improve query efficiency while ensuring accuracy when searching for query results corresponding to the question statement, thereby enabling natural language queries of large databases with hundreds or even thousands of data tables.
[0148] Optionally, the query results may be fed back to the user.
[0149] For example, when a user wants to query a database about ball sports, the database contains hundreds of tables covering dozens of sub-fields, such as football, basketball, tennis, etc. Each sub-field contains dozens of data tables, such as players, teams, venues, events, coaches, etc. If the user is mainly concerned about information about football, he asks the question: "Who is the player with the highest average dribbling success rate per game?"; using the database natural language query method provided in the embodiment of the present application, according to the question input by the user, football-related data tables can be selected from the ball sports database and input into the natural language escape model to query the results corresponding to the user's question from the selected data tables, thereby realizing natural language query of large databases and improving query efficiency and accuracy.
[0150] In practical applications, the database natural language query method provided by the embodiments of the present application can select the correct table in an 80-table database (i.e., a database with 80 data tables) with an accuracy rate of over 93%. In a 1000-table database (i.e., a database with 1000 data tables), the correct table can be found with an accuracy rate of over 90%. It can support table selection for dozens of queries per second, with high efficiency and accuracy.
[0151] By decoupling the calculation of question vectors and table vectors, the embodiment of the present application can calculate the table vectors corresponding to each data table in the database before obtaining the question statement to be queried, thereby improving the computational efficiency of the query process. By using Triplet Loss as the learning and training target of the similarity model and combining it with the method of constructing a database schema diagram to obtain the target data table, the intrinsic connection between the question and the data table can be better analyzed to support a variety of questioning methods and improve the accuracy of the query. By screening the data tables in the database and inputting the screened target data tables into the natural language escape model to obtain the query results corresponding to the question statement, the computational efficiency of the database natural language query process can be improved, so that the database natural language query interface can support large databases with hundreds or even thousands of data tables.
[0152] It should be noted that for the method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the embodiments of the present application are not limited by the order of the actions described, because according to the embodiments of the present application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily required by the embodiments of the present application.
[0153] Reference Figure 4, shows a structural block diagram of an embodiment of a database natural language query device of the present application. The device corresponds to the above-mentioned database natural language query method embodiment and may specifically include the following modules:
[0154] The question statement acquisition module 401 is used to acquire the question statement to be queried; the question statement is a natural language statement;
[0155] A question vector determination module 402 is configured to determine a question vector corresponding to the question statement;
[0156] A similarity calculation module 403 is used to calculate the similarity between the question vector and the table vectors corresponding to each data table in the database;
[0157] A target data table determination module 404 is configured to determine a target data table according to the similarity;
[0158] The target data table query module 405 is configured to determine a query result corresponding to the question statement from the target data table by using a natural language translation model.
[0159] In an optional embodiment, the question vector determination module 402 is specifically configured to sequentially process the question statement using a pre-trained encoder, an attention pooling layer, and a feed-forward layer, and output a question vector corresponding to the question statement.
[0160] In an optional embodiment, the device further includes:
[0161] The data table acquisition module is used to obtain each data table in the database;
[0162] The table vector determination module is used to process each data table in sequence using a pre-trained encoder, an attention pooling layer, and a feedforward layer, and output a table vector corresponding to the data table.
[0163] In an optional embodiment, the target data table determination module 404 includes:
[0164] A pattern graph generating module, configured to generate a pattern graph of the database according to each data table in the database; the pattern graph includes a plurality of connected components, each of which includes at least one data table;
[0165] A connected component score determination module is used to determine the connected component score of each connected component according to the similarity corresponding to the data table in the connected component;
[0166] a target connected component determination module, configured to determine a connected component whose connected component score satisfies a specified condition as a target connected component;
[0167] The target data table determination module based on the target connected component is used to determine the target data table from the data tables included in the target connected component.
[0168] In an optional embodiment, the connected component score determination module includes:
[0169] a data table score determination module, configured to determine, for each of the data tables in the connected components, a data table score of the data table according to the similarity corresponding to the data table;
[0170] a maximum data table score determining module, configured to determine the maximum data table score from the data table scores of the data tables of the connected components;
[0171] The connected component score determination module based on the maximum data table score is configured to determine the maximum data table score as the connected component score of the connected component.
[0172] In an optional embodiment, the target connected component determination module includes:
[0173] A connected component score sorting module, configured to sort the plurality of connected components according to the connected component score of each connected component;
[0174] The module for determining target connected components based on ranking is used to determine a preset number of connected components with the highest connected component scores as target connected components.
[0175] In an optional embodiment, the module for determining a target data table based on a target connected component includes:
[0176] A data table score determination module is used to determine, for each target connected component, whether there is a data table in the target connected component with a data table score greater than a preset score threshold;
[0177] A first target data table determining module is configured to determine, in the target connected component, a data table corresponding to a data table score greater than the preset score threshold as a target data table;
[0178] The second target data table determination module is configured to, if not, determine the data table corresponding to the largest data table score in the target connected component as the target data table.
[0179] As for the device embodiment, since it 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.
[0180] An embodiment of the present application also discloses an electronic device, including a processor, a memory, and a computer program stored in the memory and capable of running on the processor. When the computer program is executed by the processor, the steps of the database natural language query method described above are implemented.
[0181] The embodiment of the present application further discloses a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the database natural language query method described above are implemented.
[0182] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.
[0183] Those skilled in the art will appreciate that the embodiments of the present application can be provided as methods, devices, or computer program products. Therefore, the present application can adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present application can adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to magnetic disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0184] The embodiments of the present application are described with reference to the flowcharts and / or block diagrams of the methods, terminal devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing terminal device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing terminal device generate instructions for implementing the steps in the process. Figure 1 a process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0185] These computer program instructions may also be stored in a computer readable memory that can direct a computer or other programmable data processing terminal device to operate in a specific manner, so that the instructions stored in the computer readable memory produce a manufactured product including an instruction device, which implements the process Figure 1 a process or multiple processes and / or boxes Figure 1 The function specified in one or more boxes.
[0186] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal device so that a series of operating steps are executed on the computer or other programmable terminal device to produce a computer-implemented process, thereby providing instructions for executing on the computer or other programmable terminal device to implement the process. Figure 1 a process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.
[0187] Although preferred embodiments of the present invention have been described, those skilled in the art may make additional changes and modifications to these embodiments once they become aware of the basic inventive concepts. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the embodiments of the present invention.
[0188] Finally, it should be noted that, in this document, relational terms such as first and second, etc., are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the terms "comprises," "comprising," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or terminal device that includes a series of elements includes not only those elements, but also other elements not explicitly listed, or elements inherent to such process, method, article, or terminal device. In the absence of further limitations, an element defined by the phrase "comprising a ..." does not exclude the presence of additional identical elements in the process, method, article, or terminal device that includes the element.
[0189] The above is a detailed introduction to the database natural language query method and device, equipment and storage medium provided by the present application. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea; at the same time, for general technical personnel in this field, based on the ideas of the present application, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.
Claims
1. A database natural language query method, characterized in that: The method comprises: Obtaining a question statement to be queried; the question statement is a natural language statement; Determining a question vector corresponding to the question statement; Calculating the similarity between the question vector and the table vectors corresponding to each data table in the database; Generating a schema of the database according to each data table in the database; the schema includes a plurality of connected components, each of which includes at least one data table; For each connected component, determining a connected component score of the connected component according to a similarity corresponding to a data table in the connected component; Determine the connected component whose connected component score meets the specified condition as the target connected component; Determine a target data table from the data tables included in the target connected component; A natural language translation model is used to determine the query result corresponding to the question statement from the target data table.
2. The method according to claim 1, characterized in that Determining the question vector corresponding to the question statement includes: The question sentence is processed in sequence using a pre-trained encoder, an attention pooling layer, and a feed-forward layer, and a question vector corresponding to the question sentence is output.
3. The method according to claim 2, characterized in that Before calculating the similarity between the question vector and the table vectors corresponding to each data table in the database, the method further includes: Get each data table in the database; For each of the data tables, a pre-trained encoder, an attention pooling layer, and a feedforward layer are used to process the data table in sequence, and a table vector corresponding to the data table is output.
4. The method according to claim 1, wherein Determining the score of the connected component according to the similarity corresponding to the data table in the connected component includes: For each of the data tables in the connected components, determining a data table score of the data table according to a similarity corresponding to the data table; Determine a maximum data table score from the data table scores of the connected components; The maximum data table score is determined as the connected component score of the connected component.
5. The method according to claim 4, characterized in that The step of determining the connected component whose connected component score satisfies a specified condition as a target connected component includes: sorting the plurality of connected components according to the connected component scores of the respective connected components; A preset number of connected components with the highest connected component scores are determined as target connected components.
6. The method according to claim 5, characterized in that The determining of the target data table from the data table included in the target connected component includes: For each of the target connected components, determining whether there is a data table with a data table score greater than a preset score threshold in the target connected component; If so, determining the data table corresponding to the data table score greater than the preset score threshold in the target connected component as the target data table; If not, the data table corresponding to the largest data table score in the target connected component is determined as the target data table.
7. A database natural language query device, characterized in that: The device comprises: A question statement acquisition module is used to acquire a question statement to be queried; the question statement is a natural language statement; A question vector determination module, configured to determine a question vector corresponding to the question statement; A similarity calculation module is used to calculate the similarity between the question vector and the table vectors corresponding to each data table in the database; A pattern graph generating module, configured to generate a pattern graph of the database according to each data table in the database; the pattern graph includes a plurality of connected components, each of which includes at least one data table; A connected component score determination module is used to determine the connected component score of each connected component according to the similarity corresponding to the data table in the connected component; a target connected component determination module, configured to determine a connected component whose connected component score satisfies a specified condition as a target connected component; A target data table determination module based on a target connected component, configured to determine a target data table from the data tables included in the target connected component; A target data table query module is used to determine the query result corresponding to the question statement from the target data table by adopting a natural language escape model.
8. An electronic device, characterized in that: The method comprises a processor, a memory, and a computer program stored in the memory and capable of running on the processor, wherein when the computer program is executed by the processor, the steps of the database natural language query method according to any one of claims 1 to 6 are implemented.
9. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, which, when executed by a processor, implements the steps of the database natural language query method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Method and device for generating SQL statements according to natural languages
CN110888897A