Sentence generation method, device, electronic device and medium

By preprocessing and modeling the natural language description information, and combining the string matching algorithm to generate target SQL query statements, the problem of inaccurate dependence on predefined templates and information in the existing NL2SQL methods is solved, and a higher accuracy of SQL query statement generation is achieved.

CN115576983BActive Publication Date: 2025-09-02STATE GRID INFORMATION & TELECOMM BRANCH
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211358221.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-11-01
Publication Date
2025-09-02
Estimated Expiration
2042-11-01

AI Technical Summary

Technical Problem

The existing NL2SQL methods rely on predefined templates, limiting the generation application of complex SQL queries, and the information asked in natural language is limited and inaccurate, resulting in low accuracy of generated SQL query statements.

Method used

By preprocessing the natural language description information, a word embedding sequence is generated, and the initial SQL query statement is obtained using the setting model and network processing. Combined with the string matching algorithm, the target condition information is found from the database table, and the accurate target SQL query statement is finally generated.

Benefits of technology

It improves the accuracy of SQL query statement generation, can effectively deal with complex queries and spelling errors, and ensures that the generated SQL query statement can be executed accurately.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115576983B_ABST
    Figure CN115576983B_ABST
Patent Text Reader

Abstract

The embodiments of the present invention disclose a statement generation method, device, electronic device, and medium. The method includes: preprocessing input natural language description information to obtain a word embedding sequence; inputting the word embedding sequence into a set model to obtain a target sequence; inputting the target sequence into a set network to obtain an initial SQL query statement; searching for target condition information corresponding to the natural language description information from a set database table through a string matching algorithm; and generating a target SQL query statement based on the initial SQL query statement and the target condition information. The method improves the accuracy of SQL query statement generation by processing the word embedding sequence corresponding to the natural language description information through a set model and a set network to obtain an initial SQL query statement, and then obtaining the target condition information through a string matching algorithm to generate a corresponding target SQL query statement based on the initial SQL query statement and the target condition information.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] Embodiments of the present invention relate to the field of database technology, and in particular to a statement generation method, device, electronic device, and medium. Background Art

[0002] Natural Language to SQL (NL2SQL) is a technology that converts user natural language queries into executable Structured Query Language (SQL) queries to retrieve query results from a database. With the development of society, the conversion of natural language queries into accurate and executable SQL queries has received considerable attention and has been applied in many fields.

[0003] Currently, existing NL2SQL methods typically use natural language questions and predefined templates to generate SQL logical forms (parse trees) for easy conversion into executable queries. However, the use of predefined templates limits the application of these methods to complex SQL query generation. Furthermore, the limited and inaccurate information in natural language questions makes predefined templates unusable, thus reducing the accuracy of natural language-to-SQL conversion. Summary of the Invention

[0004] Embodiments of the present invention provide a statement generation method, device, electronic device, and medium to improve the accuracy of SQL query statement generation.

[0005] According to one aspect of an embodiment of the present invention, a sentence generation method is provided, comprising:

[0006] Preprocessing the input natural language description information to obtain a word embedding sequence, wherein the word embedding sequence includes at least one word vector;

[0007] Inputting the word embedding sequence into a set model to obtain a target sequence, where the target sequence is a sequence of vectors representing the initial SQL query statement corresponding to the natural language description information;

[0008] Input the target sequence into the set network to obtain an initial SQL query statement;

[0009] By using a string matching algorithm, target condition information corresponding to the natural language description information is searched from a set database table; and a target SQL query statement is generated based on the initial SQL query statement and the target condition information.

[0010] According to another aspect of an embodiment of the present invention, there is provided a sentence generating apparatus, comprising:

[0011] A preprocessing module, configured to preprocess the input natural language description information to obtain a word embedding sequence, wherein the word embedding sequence includes at least one word vector;

[0012] A first input module is configured to input the word embedding sequence into a set model to obtain a target sequence, where the target sequence is a sequence of vectors representing the initial SQL query statement corresponding to the natural language description information;

[0013] A second input module is used to input the target sequence into a set network to obtain an initial SQL query statement;

[0014] A search module, configured to search for target condition information corresponding to the natural language description information from a set database table using a string matching algorithm;

[0015] A generating module is used to generate a target SQL query statement based on the initial SQL query statement and target condition information.

[0016] According to another aspect of an embodiment of the present invention, an electronic device is provided, the electronic device comprising:

[0017] at least one processor; and

[0018] a memory communicatively connected to the at least one processor; wherein,

[0019] The memory stores a computer program that can be executed by the at least one processor. The computer program is executed by the at least one processor so that the at least one processor can execute the sentence generation method described in any embodiment of the present invention.

[0020] According to another aspect of an embodiment of the present invention, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement the statement generation method described in any embodiment of the present invention when executed.

[0021] The technical solution of the embodiment of the present invention first pre-processes the input natural language description information to obtain a word embedding sequence, which includes at least one word vector; secondly, the word embedding sequence is input into a set model to obtain a target sequence, which is a sequence of vectors representing the natural language description information corresponding to the initial SQL query statement; then the target sequence is input into a set network to obtain an initial SQL query statement; then, through a string matching algorithm, the target condition information corresponding to the natural language description information is searched from a set database table; finally, based on the initial SQL query statement and the target condition information, a target SQL query statement is generated. This method improves the accuracy of SQL query statement generation by processing the word embedding sequence corresponding to the natural language description information through a set model and a set network to obtain an initial SQL query statement, and then obtaining the target condition information through a string matching algorithm to generate a corresponding target SQL query statement based on the initial SQL query statement and the target condition information.

[0022] It should be understood that the content described in this section is not intended to identify the key or important features of the embodiments of the present invention, nor is it intended to limit the scope of the present invention. Other features of the present invention will become readily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS

[0023] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0024] Figure 1 A flowchart of a sentence generation method provided in Example 1 of the present invention;

[0025] Figure 2 A flowchart of a sentence generation method provided in the second embodiment of the present invention;

[0026] Figure 3 A schematic diagram of the structure of a sentence generation device provided in Embodiment 3 of the present invention;

[0027] Figure 4 This is a structural diagram of an electronic device provided in Example 4 of the present invention. DETAILED DESCRIPTION

[0028] In order to enable those skilled in the art to better understand the solutions of the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the embodiments described are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts should fall within the scope of protection of the present invention.

[0029] It should be noted that the terms "first", "second", etc. in the description and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that the numbers used in this way can be interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0030] In the power sector, with the development of grid data center analysis, efficient access to the terabytes of data generated during grid operation is crucial for promoting the development of power system services and improving management. This embodiment proposes an efficient and accurate NL2SQL algorithm that can accurately query the required data information and accurately convert natural language questions into correct and executable SQL query statements, which plays a vital role in accurately querying the required power data content.

[0031] At present, the generation of accurate and executable SQL query statements from natural language questions has received a lot of attention and has been applied in many fields. In this embodiment, the algorithms for generating SQL query statements from various natural language questions can be divided into two categories. The first category is the semantic analysis method or the time-slot filling method. These models use the semantic and grammatical information in natural language questions and table patterns to generate SQL logical forms (parse trees), which can be easily converted into corresponding executable SQL query statements. The second category is the model method of language generation. These models use the function of language generation model and can directly generate SQL query statements without building predefined SQL templates. Therefore, no matter how many tables and columns are involved, they can be used to generate complex SQL queries. However, the above methods have the following defects:

[0032] (1) Existing NL2SQL methods strongly rely on predefined templates, which limits their application in generating complex SQL queries.

[0033] (2) Some NL2SQL models leverage the power of language generation models to directly generate SQL query statements without the need to build predefined SQL templates. Therefore, they can be easily applied to generate complex SQL query statements regardless of the number of tables and columns involved. However, due to the limited and inaccurate information in some natural language questions (e.g., random questions with spelling errors and missing keywords), the predicted SQL query statements may not be executable. In addition, there is also the disadvantage that it is difficult to interpret the language generation model and its output.

[0034] (3) Even if some NL2SQL model algorithms can generate high-quality SQL structures and columns, the generation of conditional values ​​may still be a bottleneck in generating correct and executable SQL queries.

[0035] Example 1

[0036] In order to solve the above technical problems, Figure 1 This is a flow chart of a statement generation method provided in Example 1 of the present invention. This method can be applied to situations where natural language description information is converted into SQL query statements to improve the accuracy of statement generation. The method can be executed by a statement generation device, where the device can be implemented by software and / or hardware and is generally integrated on an electronic device. In this embodiment, the electronic device includes but is not limited to: desktop computers, laptops, servers and other devices.

[0037] like Figure 1 As shown, a sentence generation method provided in the first embodiment of the present invention includes the following steps:

[0038] S110: Preprocess the input natural language description information to obtain a word embedding sequence, where the word embedding sequence includes at least one word vector.

[0039] In this embodiment, natural language description information can be understood as descriptive information in natural language related to data query. Natural language generally refers to a language that evolves naturally with culture. For example, the natural language description information can be a natural language question, specifically a sentence such as "Query data C in Table A and Table B."

[0040] A word embedding sequence can be understood as a sequence of word vectors corresponding to natural language description information. A word embedding sequence can include at least one word vector. A word vector can be understood as the corresponding vector obtained by mapping the words contained in the natural language description information from a one-dimensional word space to a continuous vector space with a lower dimension. Natural language description information can include at least one word, such as "table", "A", and "in", which can all be considered as one word; each word can be mapped to a corresponding word vector.

[0041] Preprocessing can be understood as pre-processing. Here, there is no specific limitation on how to preprocess the input natural language description information to obtain a word embedding sequence. For example, the words contained in the natural language description information can be mapped to vectors to obtain a word vector corresponding to each word. Each word vector can then form a corresponding word embedding sequence.

[0042] S120: Input the word embedding sequence into a set model to obtain a target sequence, where the target sequence is a sequence of vectors representing the initial SQL query statement corresponding to the natural language description information.

[0043] In this embodiment, the set model can be understood as a pre-set, trained model. The set model is not specifically limited here; for example, the set model can be a sequence-to-sequence (Seq2Seq) model. The target sequence can be understood as a sequence of vectors representing the initial SQL query statement corresponding to the natural language description information. The initial SQL query statement can be understood as an SQL query statement initially formed from the natural language description information.

[0044] The word embedding sequence is input into the set model to obtain the target sequence. Here, there is no specific limitation on how to obtain the target sequence through the set model. For example, the word embedding sequence can be input into the set model as input data, and the set model can finally obtain output data, i.e., the target sequence, after performing corresponding processing based on the word embedding model.

[0045] S130: Input the target sequence into a set network to obtain an initial SQL query statement.

[0046] In this embodiment, the setting network can be understood as a pre-set and trained network model; the setting network is not specifically limited here. For example, the setting network can be a pointer generation network, and the pointer generation network can be considered as a network model that combines the pointer network and the generation network.

[0047] The target sequence is input into the set network to obtain an initial SQL query statement. The method for obtaining the initial SQL query statement through the set network is not specifically limited herein. For example, the target sequence can be input into the set network as input data, and the set network performs corresponding processing based on this input data to obtain corresponding output data, i.e., the initial SQL statement.

[0048] S140 , searching for target condition information corresponding to the natural language description information from a set database table through a string matching algorithm.

[0049] In this embodiment, the string matching algorithm can be understood as an algorithm for matching strings with each other. The string matching algorithm is not specifically limited here and can be flexibly set according to actual needs.

[0050] The set database table can be understood as at least one pre-set database table. There is no specific limitation on the set database table here, and it may include all database tables in the queried database, or may include some database tables in the queried database.

[0051] Target condition information can be understood as information indicating the query conditions in the target SQL query statement. For example, a SQL query statement typically consists of a select list, a FROM clause, and a WHERE clause. The select list (select_list) indicates the columns to be queried and can be a list of column names, asterisks, expressions, and variables (including local and global variables). The FROM clause specifies the SELECT statement query and the tables or views associated with the query. The WHERE clause sets the query conditions and filters out unnecessary data rows. The FROM and WHERE clauses contain information related to the query conditions.

[0052] For example, a SQL query statement is "Select id, title FROM table A WHERE id < 5," where "table A" and "id < 5" represent the query-related information contained in the FROM and WHERE clauses. id represents an identity number.

[0053] The method for searching the target condition information corresponding to the natural language description information from the set database table using the string matching algorithm is not specifically limited herein. For example, information related to the query condition contained in the natural language description information can be first determined, and then the determined information can be treated as strings. The string matching algorithm can be used to search the set database table for relevant information matching these strings to serve as the target condition information.

[0054] In one embodiment, it is assumed that the information related to the query condition contained in the natural language description information is "id < 5", and the set database table contains condition information such as "id < five", "id < five", and "id < 5". These condition information are all information associated with "id < 5" in the natural language description information. Therefore, through the string matching algorithm, based on "id < 5", the condition information such as "id < five", "id < five", and "id < 5" contained in the set database table can be matched, and these condition information are determined as the target condition information.

[0055] S150. Generate a target SQL query statement based on the initial SQL query statement and the target condition information.

[0056] In this embodiment, the target SQL query statement can be understood as the accurate executable SQL query statement corresponding to the generated natural language description information.

[0057] Based on the initial SQL query statement and the target condition information, a corresponding target SQL query statement can be generated. Here, there is no specific limitation on how to generate the target SQL query statement. For example, the target condition information can be added to the condition area in the initial SQL query statement to form a new SQL query statement, and this new SQL query statement is the target SQL query statement; among them, the condition area can be understood as the area in the SQL statement for placing information related to the query condition, such as the areas corresponding to the FROM clause and the WHERE clause.

[0058] A statement generation method provided in Embodiment 1 of the present invention first preprocesses the input natural language description information to obtain a word embedding sequence, and the word embedding sequence includes at least one word vector; secondly, inputs the word embedding sequence into a set model to obtain a target sequence, and the target sequence is a sequence of vectors representing the initial SQL query statement corresponding to the natural language description information; then inputs the target sequence into a set network to obtain the initial SQL query statement; then through the string matching algorithm, searches for the target condition information corresponding to the natural language description information from the set database table; finally, generates a target SQL query statement based on the initial SQL query statement and the target condition information. This method obtains the initial SQL query statement through the processing of the word embedding sequence corresponding to the natural language description information by the set model and the set network, and then obtains the target condition information through the string matching algorithm, so as to generate the corresponding target SQL query statement based on the initial SQL query statement and the target condition information, improving the accuracy of SQL query statement generation.

[0059] Embodiment 2

[0060] Figure 2The flowchart of a statement generation method provided in the second embodiment of the present invention is a refinement of the second embodiment based on the above embodiments. In this embodiment, the process of pre-processing the input natural language description information to obtain a word embedding sequence, the process of inputting the word embedding sequence into a set model to obtain a target sequence, the process of searching the target condition information corresponding to the natural language description information from the set database table through a string matching algorithm, and the process of generating a target SQL query statement based on the initial SQL query statement and the target condition information are specifically described. It should be noted that the technical details not fully described in this embodiment can be referred to any of the above embodiments. Figure 2 As shown, the method includes:

[0061] like Figure 2 As shown, a sentence generation method provided in the second embodiment of the present invention includes the following steps:

[0062] S210: Obtain input natural language description information.

[0063] In this embodiment, the input natural language description information is not specifically limited, and can be natural language description information input by a user or natural language description information transmitted by other devices.

[0064] S220: Perform word segmentation processing on the natural language description information to obtain at least one word.

[0065] In this embodiment, word segmentation can be understood as a text processing method in natural language processing, that is, classifying text content at the word level. Taking natural language description information as text content, word segmentation processing can be performed on the natural language description information to obtain at least one word.

[0066] S230: Perform word embedding processing on the at least one word to obtain a corresponding word embedding sequence.

[0067] In this embodiment, word embedding can be understood as embedding a high-dimensional space equal to the number of words into a lower-dimensional continuous vector space, where each word or phrase is a vector mapped to the real field. By performing word embedding on at least one word, each word can be mapped to a vector, i.e., a word vector. These word vectors can then form a corresponding word embedding sequence.

[0068] S240: Input the word embedding sequence into the set model to obtain an encoder hidden state sequence and a decoder hidden state sequence.

[0069] In this embodiment, the model may include an encoder and a decoder. The encoder can be understood as a module that encodes input data. Correspondingly, the decoder can be understood as a module that decodes input data. The encoding and decoding methods of the encoder and decoder are not specifically defined herein.

[0070] The encoder hidden state sequence can be understood as the sequence obtained by encoding the input data (such as the word embedding sequence). The decoder hidden state sequence can be understood as the sequence obtained by decoding the input data (such as the word embedding sequence and the encoder hidden state sequence).

[0071] In this embodiment, the word embedding sequence is input into the set model to obtain the encoder hidden state sequence and the decoder hidden state sequence. How to obtain the encoder hidden state sequence and the decoder hidden state sequence by setting the model is not specifically limited here. For example, the word embedding sequence can be first input into the encoder of the set model to obtain the corresponding encoder hidden state sequence; on this basis, the encoder hidden state sequence and the word embedding sequence can be input as input data into the decoder of the set model to obtain the corresponding decoder hidden state sequence.

[0072] Optionally, set the model to include an encoder and a decoder;

[0073] Inputting the word embedding sequence into the set model to obtain an encoder hidden state sequence and a decoder hidden state sequence, including: inputting the word embedding sequence into the encoder of the set model to obtain an encoder hidden state sequence, wherein the encoder hidden state sequence includes an encoder hidden state vector corresponding to each word vector; inputting the encoder hidden state sequence and the word embedding sequence into the decoder of the set model to obtain a decoder hidden state sequence, wherein the decoder hidden state sequence includes a decoder hidden state vector corresponding to each encoder hidden state vector.

[0074] In this embodiment, the encoder hidden state vector can be understood as a hidden state feature vector obtained by encoding the word vector, and the decoder hidden state vector can be understood as a hidden state feature vector obtained by decoding the encoder hidden state vector based on the word vector.

[0075] In this embodiment, a word embedding sequence can be input into an encoder of a set model to obtain an encoder hidden state sequence, and the encoder hidden state sequence can include an encoder hidden state vector corresponding to each word vector. There is no specific limitation on how to obtain the encoder hidden state sequence. For example, a word embedding sequence can be input into an encoder of a set model, and each word vector of the word embedding sequence can be encoded by the encoder to obtain an encoder hidden state vector corresponding to each word vector. The obtained encoder hidden state vectors can constitute a corresponding encoder hidden state sequence.

[0076] In this embodiment, the encoder hidden state sequence and the word embedding sequence can be input into the decoder of the set model to obtain a decoder hidden state sequence. The decoder hidden state sequence can include decoder hidden state vectors corresponding to each encoder hidden state vector. There is no specific limitation on how to obtain the decoder hidden state sequence. For example, the word embedding sequence and the encoder hidden state sequence can be input into the decoder of the set model. The decoder can decode each encoder hidden state vector of the encoder hidden state sequence based on each word vector to obtain a decoder hidden state vector corresponding to each encoder hidden state vector. The obtained decoder hidden state vectors can constitute a corresponding decoder hidden state sequence.

[0077] S250: Determine a context vector corresponding to the natural language description information according to the encoder hidden state sequence and the decoder hidden state sequence.

[0078] In this embodiment, the context vector can be understood as a vector corresponding to content associated with the context of the natural language description information.

[0079] There are no specific restrictions on how to determine the context vector corresponding to the natural language description information based on the encoder hidden state sequence and the decoder hidden state sequence. For example, an alignment score function between the encoder hidden state sequence and the decoder hidden state sequence can be predefined. Then, based on this alignment score function and the encoder hidden state sequence and the decoder hidden state sequence, corresponding calculations can be performed to obtain an alignment score between the encoder hidden state sequence and the decoder hidden state sequence. This alignment score and the word embedding sequence are input into a pre-defined attention mechanism model, and corresponding processing is performed to obtain the context vector corresponding to the natural language description information.

[0080] The attention mechanism can be considered a special structure embedded in machine learning models that automatically learns and calculates the contribution of input data to output data. An attention mechanism model can be understood as a network model based on the attention mechanism. The alignment score function can be understood as a predefined function used to calculate the alignment score; the definition of the alignment score function is not specifically defined here. The alignment score can be understood as a sequence that represents the probability of alignment between the encoder hidden state sequence and the decoder hidden state sequence. The alignment probability can be understood as the probability of the encoder hidden state vector and the corresponding decoder hidden state vector accurately corresponding to each other.

[0081] Optionally, a context vector corresponding to the natural language description information is determined based on the encoder hidden state sequence and the decoder hidden state sequence, including: determining the alignment score between the encoder hidden state sequence and the decoder hidden state sequence based on a set alignment score function, the encoder hidden state sequence and the decoder hidden state sequence; and inputting the alignment score and the word embedding sequence into a set attention mechanism model to obtain a context vector.

[0082] In this embodiment, setting the alignment score function can be understood as a pre-set alignment score function. Setting the attention mechanism model can be understood as a pre-set network model based on the attention mechanism.

[0083] Here, there is no specific limitation on how to determine the alignment score between the encoder hidden state sequence and the decoder hidden state sequence based on the alignment score function, the encoder hidden state sequence, and the decoder hidden state sequence. For example, the alignment score function can be set and the encoder hidden state sequence and the decoder hidden state sequence can be used as known quantities to perform corresponding calculations to obtain the unknown quantity of the alignment score function, i.e., the alignment score.

[0084] In this embodiment, the alignment score and word embedding sequence are input into the attention mechanism model to obtain output data, namely the context vector. How to obtain the context vector by setting the attention mechanism model is not specifically limited here.

[0085] S260: Input the context vector, the encoder hidden state sequence, and the word embedding sequence into the decoder of the set model to obtain a target sequence.

[0086] In this embodiment, the context vector, the encoder hidden state sequence, and the word embedding sequence are input as input data to the decoder of the set model to obtain a corresponding target sequence. How to obtain the target sequence is not specifically limited here. For example, the context vector, the encoder hidden state sequence, and the word embedding sequence can be input as input data to the decoder of the set model. The context vector, the encoder hidden state sequence, and the word embedding sequence are decoded by the decoder of the set model respectively, and the decoded results are fused based on the correlation between the decoded vectors to obtain the target sequence.

[0087] S270. Determine at least one initial condition information based on the natural language description information.

[0088] In this embodiment, the initial condition information can be understood as information indicating the query condition in the natural language description information. For example, if the natural language description information is "search for data with id < 5", the initial condition information can be "id < 5". There can be one or more initial condition information.

[0089] There is no specific limitation here on how to determine at least one initial condition information based on the natural language description information. For example, information related to the query condition can be parsed from the natural language description information through methods such as grammatical analysis in natural language processing to serve as the initial condition information.

[0090] S280 . For each initial condition information, search the setting database table for at least one target condition information that matches the initial condition information by using a string matching algorithm.

[0091] In this embodiment, for each initial condition information, a string matching algorithm may be used to search a setting database table for at least one string that matches the string corresponding to the initial condition information, to serve as the target condition information.

[0092] S290: Add the target condition information to the condition area of ​​the initial SQL query statement to form a corresponding target SQL query statement.

[0093] In this embodiment, after the target condition information is determined, the target condition information may be added to the condition area of ​​the initial SQL query statement to form a corresponding target SQL query statement.

[0094] A second embodiment of the present invention provides a method that specifically implements a process of preprocessing input natural language description information to obtain a word embedding sequence, a process of inputting the word embedding sequence into a set model to obtain a target sequence, a process of searching a set database table for target condition information corresponding to the natural language description information through a string matching algorithm, and a process of generating a target SQL query statement based on the initial SQL query statement and the target condition information. Utilizing this method, a word embedding sequence is first obtained through preprocessing, a context vector is determined based on the word embedding sequence and the set model, and then the context vector is used to determine a target sequence capable of generating the initial SQL query statement, thereby preliminarily improving the accuracy of the initial SQL query statement. On this basis, the target condition information is determined through a string matching algorithm and added to the initial SQL query statement to obtain the target SQL query statement, thereby further improving the accuracy of the SQL query statement generation.

[0095] The present invention is described below by way of example.

[0096] This paper proposes a language generation model based on convertible editing. It first converts natural language questions into corresponding SQL queries, and then mines the conditional values ​​(i.e., target condition information) based on the content of the natural language questions and the content of the database tables. This editable NL2SQL algorithm makes the proposed model more powerful in handling random natural language questions with insufficient information in the tables and spelling errors, and makes it feasible to effectively retrieve and recover conditional values.

[0097] A language generation method is adopted in the model because natural language questions may be related to one or more tables, and due to the involvement of professional terminology, the keywords in natural language questions may be inaccurate. In order to solve these problems, the present invention proposes a conversion editing model that uses a language generation model to generate a draft of the query and edits it according to the table schema. In the proposed method, it is assumed that the table schema information is implicitly included in the input natural language question as semantic information. Therefore, in the translation process, it is only necessary to deal with how to infer y from x. However, since accurate table schema information has not yet appeared at this stage, the generated SQL query statement can only roughly capture this information. In the second stage, the draft of the SQL query statement (i.e., the initial SQL query statement) is edited according to the table schema information, and tables with content keyword content are searched to restore accurate information. This two-stage strategy can easily adapt the model to other general tasks. It mainly includes the following three parts:

[0098] (1) Use a Seq2Seq-based model to convert the input natural language question into an SQL query statement;

[0099] (2) Use the attention replication mechanism (i.e., set the attention mechanism) to edit the generated query SQL statement;

[0100] (3) Use a task-specific lookup table (i.e., a set database table) to further edit the generated query SQL statement.

[0101] (1) Sequence-to-Sequence Framework

[0102] In the task of generating SQL query statements from natural language questions, an end-to-end (Seq2Seq) framework of recurrent neural network (RNN) is adopted. The proposed Seq2Seq framework consists of a question encoder (single-layer bidirectional long short-term memory network (LSTM)) and a SQL decoder (single-layer unidirectional LSTM). The encoder reads the word embedding sequence of the input token and converts it into a sequence of encoder hidden states (features). In each decoding step t, the decoder takes the encoder hidden state and the embedding word of the previous token (i.e., the word embedding sequence) as input and generates a decoder hidden state. The word embedding sequences in both encoder and decoder come from the same matrix W emb A token can be understood as each word vector in a word embedding sequence, and a word vector can be considered as an abstract token.

[0103] (2) Query-based temporal attention framework

[0104] At each decoding step t, the decoder not only takes its internal hidden state and previously generated tokens as input, but also selectively focuses on parts of the query that are relevant to the current one. First, an alignment score function is defined between the current decoder hidden state sequence and each encoder hidden state sequence. A new alignment score with time dependency is then obtained. Finally, a context vector for the natural language question is obtained by employing a temporal attention mechanism.

[0105] (3) Dynamic and temporal attention models on SQL queries

[0106] In the SQL query generation problem task, different parts of the query may not have strict sequential dependencies. For example, switching two conditions in a query will produce the same query. However, when generating the conditional value (i.e., target condition information), the decoder may need to consider not only the previously generated tokens, its own hidden state, and the encoder context vector, but also pay more attention to the table name and title (i.e., field) generated by the previously generated tokens. Therefore, a dynamic attention mechanism is introduced in the decoder, which allows the decoder to dynamically pay attention to the previously generated tokens, and the alignment score on the previously generated tokens can be calculated in the same way as the encoder's alignment score.

[0107] (4) Use table contents to find conditional values

[0108] This method uses a string matching metric (string matching algorithm) to find the most similar condition value (i.e., target condition information) for each initial condition information from a lookup table (i.e., a predefined database table) and then replace it. In implementation, this is achieved by calculating word-level and character-level similarity between two sequences.

[0109] (5) Limited attention generation and reproduction

[0110] Combine the generative network and the pointer network to generate the initial SQL query statement. Pointer networks have been widely used in language modeling and generation tasks such as abstract text summarization and question counting.

[0111] Unlike existing methods, the proposed method can generate relatively accurate SQL query statements even without applying recovery techniques. This method improves efficiency in predicting correct conditional values ​​without compromising the performance of other components of the SQL query. By also considering a temporal attention mechanism for natural language questions, dynamic attention to SQL, and controlled statement generation and replication techniques, the proposed recovery techniques for conditional values ​​can also significantly improve performance on template questions and natural language questions.

[0112] Example 3

[0113] Figure 3 This is a structural diagram of a sentence generation device provided by the third embodiment of the present invention, which can be implemented by software and / or hardware. Figure 3 As shown, the device includes:

[0114] A preprocessing module 310 is configured to preprocess the input natural language description information to obtain a word embedding sequence, wherein the word embedding sequence includes at least one word vector;

[0115] A first input module 320 is configured to input the word embedding sequence into a set model to obtain a target sequence, where the target sequence is a sequence of vectors representing the initial SQL query statement corresponding to the natural language description information;

[0116] The second input module 330 is used to input the target sequence into the set network to obtain an initial SQL query statement;

[0117] A search module 340 is configured to search for target condition information corresponding to the natural language description information from a set database table using a string matching algorithm;

[0118] The generating module 350 is configured to generate a target SQL query statement based on the initial SQL query statement and the target condition information.

[0119] In this embodiment, the device first preprocesses the input natural language description information through a preprocessing module 310 to obtain a word embedding sequence, which includes at least one word vector; secondly, through a first input module 320, the word embedding sequence is input into a set model to obtain a target sequence, which is a sequence of vectors representing the natural language description information corresponding to the initial SQL query statement; then, through a second input module 330, the target sequence is input into a set network to obtain an initial SQL query statement; then, through a search module 340, the target condition information corresponding to the natural language description information is searched from a set database table through a string matching algorithm; finally, through a generation module 350, a target SQL query statement is generated based on the initial SQL query statement and the target condition information. The device obtains an initial SQL query statement by processing the word embedding sequence corresponding to the natural language description information through a set model and a set network, and then obtains the target condition information through a string matching algorithm, so as to generate a corresponding target SQL query statement based on the initial SQL query statement and the target condition information, thereby improving the accuracy of SQL query statement generation.

[0120] Optionally, the pre-processing module 310 includes:

[0121] An acquisition unit, used to acquire input natural language description information;

[0122] A word segmentation unit, configured to perform word segmentation processing on the natural language description information to obtain at least one word;

[0123] The embedding processing unit is used to perform word embedding processing on the at least one word to obtain a corresponding word embedding sequence.

[0124] Optionally, the first input module 320 includes:

[0125] An input unit, configured to input the word embedding sequence into the set model to obtain an encoder hidden state sequence and a decoder hidden state sequence;

[0126] a vector determination unit, configured to determine a context vector corresponding to the natural language description information based on an encoder hidden state sequence and a decoder hidden state sequence;

[0127] A sequence determination unit is used to input the context vector, the encoder hidden state sequence and the word embedding sequence into the decoder of the set model to obtain a target sequence.

[0128] Optionally, the setting model includes an encoder and a decoder;

[0129] Input unit, including:

[0130] A first input subunit is configured to input the word embedding sequence into an encoder of the set model to obtain an encoder hidden state sequence, wherein the encoder hidden state sequence includes an encoder hidden state vector corresponding to each word vector;

[0131] A second input subunit is used to input the encoder hidden state sequence and the word embedding sequence into the decoder of the set model to obtain a decoder hidden state sequence, where the decoder hidden state sequence includes a decoder hidden state vector corresponding to each encoder hidden state vector.

[0132] Optionally, the vector determination unit includes:

[0133] a score determination subunit, configured to determine an alignment score between the encoder hidden state sequence and the decoder hidden state sequence according to a set alignment score function, the encoder hidden state sequence, and the decoder hidden state sequence;

[0134] The vector determination subunit is used to input the alignment score and the word embedding sequence into a set attention mechanism model to obtain the context vector.

[0135] Optionally, the search module 340 includes:

[0136] an information determining unit, configured to determine at least one piece of initial condition information based on the natural language description information;

[0137] The matching unit is configured to search, for each piece of initial condition information, the setting database table for at least one target condition information that matches the initial condition information by using a string matching algorithm.

[0138] Optionally, the generating module 350 includes:

[0139] The statement construction unit is used to add the target condition information to the condition area of ​​the initial SQL query statement to construct a corresponding target SQL query statement.

[0140] The sentence generation device provided in the embodiment of the present invention can execute the sentence generation method provided in any embodiment of the present invention, and has the corresponding functional modules and beneficial effects of the execution method.

[0141] Example 4

[0142] Figure 4A schematic diagram of the structure of an electronic device provided for embodiment four of the present invention. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device may also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices (such as helmets, glasses, watches, etc.) and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present invention described and / or required herein.

[0143] like Figure 4 As shown, the electronic device 10 includes at least one processor 11 and a memory, such as a read-only memory (ROM) 12, a random access memory (RAM) 13, etc., which is communicatively connected to the at least one processor 11. The memory stores a computer program that can be executed by the at least one processor. The processor 11 can perform various appropriate actions and processes according to the computer program stored in the read-only memory (ROM) 12 or the computer program loaded from the storage unit 18 into the random access memory (RAM) 13. Various programs and data required for the operation of the electronic device 10 can also be stored in the RAM 13. The processor 11, ROM 12, and RAM 13 are connected to each other via a bus 14. An input / output (I / O) interface 15 is also connected to the bus 14.

[0144] Multiple components in the electronic device 10 are connected to the I / O interface 15, including an input unit 16, such as a keyboard, a mouse, etc.; an output unit 17, such as various types of displays, speakers, etc.; a storage unit 18, such as a magnetic disk, an optical disk, etc.; and a communication unit 19, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 19 allows the electronic device 10 to exchange information / data with other devices via a computer network such as the Internet and / or various telecommunication networks.

[0145] The processor 11 can be any general-purpose and / or specialized processing component with processing and computing capabilities. Some examples of the processor 11 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various specialized artificial intelligence (AI) computing chips, various processors running machine learning model algorithms, a digital signal processor (DSP), and any appropriate processor, controller, microcontroller, etc. The processor 11 executes the various methods and processes described above, such as the statement generation method.

[0146] In some embodiments, the statement generation method may be implemented as a computer program that is tangibly contained in a computer-readable storage medium, such as the storage unit 18. In some embodiments, part or all of the computer program may be loaded and / or installed on the electronic device 10 via the ROM 12 and / or the communication unit 19. When the computer program is loaded into the RAM 13 and executed by the processor 11, one or more steps of the statement generation method described above may be performed. Alternatively, in other embodiments, the processor 11 may be configured to perform the statement generation method in any other appropriate manner (e.g., by means of firmware).

[0147] Various embodiments of the systems and techniques described herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), system-on-chip systems (SOCs), programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments can include being implemented in one or more computer programs that are executable and / or interpreted on a programmable system that includes at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.

[0148] Computer programs for implementing the methods of the present invention may be written in any combination of one or more programming languages. These computer programs may be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, such that when the computer program is executed by the processor, the functions / operations specified in the flowcharts and / or block diagrams are implemented. The computer program may be executed entirely on the machine, partially on the machine, as a stand-alone software package, partially on the machine and partially on a remote machine, or entirely on a remote machine or server.

[0149] In the context of the present invention, computer-readable storage media can be tangible media that can contain or store a computer program for use with an instruction execution system, device or equipment or used in combination with an instruction execution system, device or equipment. Computer-readable storage media can include but are not limited to electronic, magnetic, optical, electromagnetic, infrared or semiconductor systems, devices or equipment, or any suitable combination of the foregoing. Alternatively, computer-readable storage media can be machine-readable signal media. More specific examples of machine-readable storage media can include electrical connections based on one or more lines, portable computer disks, hard disks, random access memories (RAM), read-only memories (ROM), erasable programmable read-only memories (EPROM or flash memory), optical fibers, portable compact disk read-only memories (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.

[0150] To provide interaction with a user, the systems and techniques described herein can be implemented on an electronic device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user can provide input to the electronic device. Other types of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).

[0151] The systems and techniques described herein can be implemented in a computing system that includes back-end components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes front-end components (e.g., a user computer with a graphical user interface or web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such back-end components, middleware components, or front-end components. The components of the system can be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), a blockchain network, and the Internet.

[0152] A computing system may include clients and servers. The clients and servers are typically remote from each other and typically interact via a communication network. This client-server relationship arises through computer programs running on the respective computers, creating a client-server relationship. The server may be a cloud server, also known as a cloud computing server or cloud host. This server is a hosting product within the cloud computing service ecosystem that addresses the management difficulties and limited scalability of traditional physical hosting and VPS services.

[0153] It should be understood that the various forms of the processes shown above can be used to reorder, add, or delete steps. For example, the steps described in the present invention can be performed in parallel, sequentially, or in a different order, as long as the desired results of the technical solution of the present invention can be achieved. This is not limited herein.

[0154] The above specific embodiments do not limit the scope of protection of the present invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions may be made based on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention are intended to be included within the scope of protection of the present invention.

Claims

1. A sentence generation method, characterized in that: The method comprises: Preprocessing the input natural language description information to obtain a word embedding sequence, wherein the word embedding sequence includes at least one word vector; Inputting the word embedding sequence into a set model to obtain a target sequence, including: inputting the word embedding sequence into the set model to obtain an encoder hidden state sequence and a decoder hidden state sequence; determining a context vector corresponding to the natural language description information based on the encoder hidden state sequence and the decoder hidden state sequence; inputting the context vector, the encoder hidden state sequence, and the word embedding sequence into the decoder of the set model to obtain the target sequence; wherein the set model is a pre-set and trained model, and the set model includes a sequence-to-sequence model; the target sequence is a sequence of vectors representing an initial structured query language SQL query statement corresponding to the natural language description information; Input the target sequence into the set network to obtain an initial SQL query statement; Using a string matching algorithm, searching for target condition information corresponding to the natural language description information from a set database table; A target SQL query statement is generated based on the initial SQL query statement and the target condition information.

2. The method according to claim 1, characterized in that Preprocess the input natural language description information to obtain a word embedding sequence, including: Get the input natural language description information; Performing word segmentation processing on the natural language description information to obtain at least one word; Perform word embedding processing on the at least one word to obtain a corresponding word embedding sequence.

3. The method according to claim 1, characterized in that The setting model includes an encoder and a decoder; Inputting the word embedding sequence into the set model to obtain an encoder hidden state sequence and a decoder hidden state sequence, including: Inputting the word embedding sequence into the encoder of the set model to obtain an encoder hidden state sequence, wherein the encoder hidden state sequence includes an encoder hidden state vector corresponding to each word vector; The encoder hidden state sequence and the word embedding sequence are input into a decoder of the set model to obtain a decoder hidden state sequence, wherein the decoder hidden state sequence includes a decoder hidden state vector corresponding to each encoder hidden state vector.

4. The method according to claim 1, wherein Determining a context vector corresponding to the natural language description information according to the encoder hidden state sequence and the decoder hidden state sequence includes: Determine an alignment score between the encoder hidden state sequence and the decoder hidden state sequence according to a set alignment score function, the encoder hidden state sequence, and the decoder hidden state sequence; The alignment score and the word embedding sequence are input into a set attention mechanism model to obtain the context vector.

5. The method according to claim 1, wherein Using a string matching algorithm, searching for target condition information corresponding to the natural language description information from a set database table includes: determining at least one initial condition information based on the natural language description information; For each initial condition information, at least one target condition information matching the initial condition information is searched from the setting database table by a string matching algorithm.

6. The method according to claim 1, wherein Generating a target SQL query statement based on the initial SQL query statement and the target condition information includes: The target condition information is added to the condition area of ​​the initial SQL query statement to form a corresponding target SQL query statement.

7. A sentence generating device, characterized in that: include: A preprocessing module, configured to preprocess the input natural language description information to obtain a word embedding sequence, wherein the word embedding sequence includes at least one word vector; A first input module is used to input the word embedding sequence into a set model to obtain a target sequence; The first input module includes: an input unit, a vector determination unit and a sequence determination unit; The input unit is used to input the word embedding sequence into the set model to obtain an encoder hidden state sequence and a decoder hidden state sequence; The vector determination unit is configured to determine a context vector corresponding to the natural language description information based on the encoder hidden state sequence and the decoder hidden state sequence; The sequence determination unit is configured to input the context vector, the encoder hidden state sequence, and the word embedding sequence into a decoder of the set model to obtain the target sequence; wherein the set model is a pre-set, trained model including a sequence-to-sequence model; and the target sequence is a sequence of vectors representing the initial SQL query statement corresponding to the natural language description information; A second input module is used to input the target sequence into a set network to obtain an initial SQL query statement; A search module, configured to search for target condition information corresponding to the natural language description information from a set database table using a string matching algorithm; A generating module is used to generate a target SQL query statement based on the initial SQL query statement and the target condition information.

8. An electronic device, characterized in that: The electronic device comprises: at least one processor; and a memory communicatively connected to the at least one processor; wherein, The memory stores a computer program that can be executed by the at least one processor. The computer program is executed by the at least one processor to enable the at least one processor to execute the sentence generation method according to any one of claims 1 to 6.

9. A computer-readable storage medium, characterized in that The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement the sentence generation method according to any one of claims 1 to 6 when executed.

Citation Information

Patent Citations

  • Neural network based translation of natural language queries to database queries

    CN110945495A

  • Database mode matching method and device and SQL query statement generation method

    CN113868252A