Text-to-sql processing method and device, and storage medium
By processing text-to-SQL statements and utilizing semantic analysis and dynamic linking adjustments, the linking relationship between database list names and text is optimized, solving the problem of inaccuracy in text-to-SQL statements and achieving higher intelligence and accuracy.
Patent Information
- Application Number
- CN202211304043.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-10-24
- Publication Date
- 2026-01-27
- Estimated Expiration
- 2042-10-24
AI Technical Summary
Existing technologies for converting text to SQL statements suffer from a significant discrepancy between the target database list name and the actual database list name mentioned in the text to be converted, resulting in inaccurate generated SQL statements.
By acquiring the text to be converted, semantic analysis is performed to establish initial link relationships. These relationships are then optimized through dynamic adjustment and cosine similarity calculation. Finally, a pre-trained model and a relational graph neural network are used for encoding to generate more accurate SQL statements.
It improves the intelligence and accuracy of text-to-SQL conversion, expands application scenarios, and can identify database list names with the same semantics but different names, generating more accurate SQL statements.
Smart Images

Figure CN115563121B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of natural language processing technology, and in particular to a method, apparatus and storage medium for converting text to SQL statements. Background Technology
[0002] Structured Query Language (SQL) is a programming language used for database querying and program design, for accessing, querying, updating, and managing relational database systems. Text-to-SQL statements primarily help users without SQL syntax knowledge interact with databases; that is, text-to-SQL statements need to perform semantic analysis on the user's natural language text and generate appropriate SQL statements based on database information.
[0003] In text-to-SQL conversion, a pre-trained machine learning model is typically used to obtain a link between the text to be converted (the natural language text) and a list of database names (including table names and / or column names). This link is then used to determine the target database list names mentioned in the text, leading to the corresponding SQL statement. The machine learning model is trained using the relationships between the natural language text and database list names constructed in the pre-trained model as labeled data. However, this method often results in target database list names that differ significantly from the actual database list names mentioned in the text, leading to inaccurate SQL statements. Summary of the Invention
[0004] This application provides a method, apparatus, and storage medium for processing text to SQL statements, in order to solve the problem that SQL statements generated when establishing link relationships based on semantics are not accurate enough.
[0005] In a first aspect, this application provides a method for converting text to SQL statements, comprising: obtaining text to be converted, wherein the text to be converted is natural language text containing information related to a database list name; performing semantic analysis on the text to be converted based on the database list name to obtain a first link relationship between the text to be converted and the database list name, wherein the first link relationship contains semantic information; adjusting the first link relationship based on the text to be converted and the database list name to obtain a second link relationship between the text to be converted and the database list name; and determining the SQL statement corresponding to the text to be converted based on the second link relationship.
[0006] Optionally, the first link relationship is adjusted based on the text to be converted and the database list name to obtain a second link relationship between the text to be converted and the database list name. This includes: inputting the text to be converted into a pre-trained model for encoding processing to obtain the text encoding of the text to be converted output by the pre-trained model; inputting the database list name into the pre-trained model for encoding processing to obtain the database list name encoding of the database list name output by the pre-trained model; calculating the cosine similarity between the text encoding and the database list name encoding to obtain a similarity matrix; in the similarity matrix, determining the text encoding item with the highest similarity to each database list name encoding item in the database list name encoding, wherein the text encoding contains the text encoding item; and adjusting the first link relationship based on the text encoding item with the highest similarity and the database list name encoding item to obtain the second link relationship.
[0007] Optionally, based on the second link relationship, the SQL statement corresponding to the text to be converted is determined, including: performing attention-based encoding on the second link relationship to obtain the first encoding of the second link relationship; and performing decoding on the first encoding to obtain the SQL statement corresponding to the text to be converted.
[0008] Optionally, the second link relationship is encoded using an attention mechanism to obtain a first encoding of the second link relationship, including: inputting the second link relationship into a relational graph neural network for encoding using an attention mechanism to obtain a first encoding of the second link relationship output by the relational graph neural network.
[0009] Optionally, based on the database list name, semantic analysis is performed on the text to be converted to obtain the first link relationship between the text to be converted and the database list name. This includes: merging the text to be converted and the database list name to obtain the first input; inputting the first input into a pre-trained model for encoding to obtain the second encoding of the first input output by the pre-trained model; traversing the words in the text to be converted and replacing the currently traversed word with a mask to obtain the target text; merging the target text and the database list name to obtain the second input; inputting the second input into a pre-trained model for encoding to obtain the third encoding of the second input output by the pre-trained model; determining the database list name with the largest encoding difference between the second and third encodings; and establishing a link between the database list name with the largest encoding difference and its corresponding word to obtain the first link relationship.
[0010] Optionally, a link is established between the database list name with the largest encoding difference and its corresponding word, including: determining whether the largest encoding difference is greater than a set threshold; if so, a link is established between the database list name with the largest encoding difference and its corresponding word; if not, no link is established between the database list name with the largest encoding difference and its corresponding word.
[0011] Secondly, this application provides a data query method, comprising: in response to detecting text to be converted, executing the processing method provided in any of the first aspects above to obtain an SQL statement corresponding to the text to be converted, wherein the text to be converted is natural language text containing information related to a database list name; and querying data from a database containing a database list name according to the SQL statement to obtain data corresponding to the SQL statement.
[0012] Thirdly, this application provides a text-to-SQL statement processing apparatus, comprising: an acquisition module for acquiring text to be converted, wherein the text to be converted is natural language text containing information related to a database list name; an analysis module for performing semantic analysis on the text to be converted based on the database list name to obtain a first link relationship between the text to be converted and the database list name, wherein the first link relationship contains semantic information; an adjustment module for adjusting the first link relationship based on the text to be converted and the database list name to obtain a second link relationship between the text to be converted and the database list name; and a determination module for determining the SQL statement corresponding to the text to be converted based on the second link relationship.
[0013] Fourthly, this application provides an electronic device, including: a memory and a processor; the memory for storing program instructions; and the processor for calling the program instructions to perform a processing method as provided in the first aspect above or a data querying method as provided in the second aspect above.
[0014] Fifthly, this application provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, are used to implement a processing method as provided in the first aspect above or a data querying method as provided in the second aspect above.
[0015] Sixthly, this application provides a computer program product, including a computer program; when the computer program is executed, it implements the processing method provided in the first aspect above or the data query method provided in the second aspect above.
[0016] The text-to-SQL processing method, apparatus, and storage medium provided in this application acquire the text to be converted, which is natural language text containing information related to database list names; perform semantic analysis on the text to be converted based on the database list names to obtain a first link relationship between the text to be converted and the database list names, the first link relationship containing semantic information; adjust the first link relationship based on the text to be converted and the database list names to obtain a second link relationship between the text to be converted and the database list names; and determine the SQL statement corresponding to the text to be converted based on the second link relationship. This application enables the establishment of link relationships between text and database list names based on the same semantic information, and by dynamically adjusting the link relationships, outputs more accurate SQL statements. By establishing and adjusting semantically based links between text and database names, it is possible to identify different names with the same semantics (including names with the same, similar, or different expressions), improving the intelligence and accuracy of text-to-SQL conversion and expanding the application scenarios of text-to-SQL conversion. Attached Figure Description
[0017] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.
[0018] Figure 1 This is a schematic diagram illustrating an application scenario provided in the embodiments of this application;
[0019] Figure 2 A flowchart illustrating the text-to-SQL statement processing method provided in this application embodiment. Figure 1 ;
[0020] Figure 3 A flowchart illustrating the text-to-SQL statement processing method provided in this application embodiment. Figure 2 ;
[0021] Figure 4 A flowchart illustrating the text-to-SQL statement processing method provided in this application embodiment. Figure 3 ;
[0022] Figure 5 A flowchart illustrating the text-to-SQL statement processing method provided in this application embodiment. Figure 4 ;
[0023] Figure 6 A schematic diagram of the structure of the text-to-SQL statement processing device provided in the embodiments of this application;
[0024] Figure 7 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application.
[0025] The accompanying drawings illustrate specific embodiments of this application, which will be described in more detail below. These drawings and descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concept of this application to those skilled in the art through reference to particular embodiments. Detailed Implementation
[0026] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.
[0027] Figure 1 This is a schematic diagram illustrating an application scenario provided in an embodiment of this application. For example... Figure 1 As shown, this application scenario is a database query system, involving questioner 101, Text-to-SQL function module 102, and database 103.
[0028] Database 103 is where data is stored and used to retrieve target data. Database 103 includes at least one table, each with its own name; each table includes at least one column, each with its own name. The table below lists a table in the database named "CFLDraft", which includes multiple columns named "NO", "CFL_Team", "Player", ..., "College", and lists the data corresponding to each column.
[0029] NO. CFL_Team Player … College 27 A e … Wilfrid 28 B f … York 29 C g … California 30 D h … York … … … … …
[0030] Text-to-SQL module 102 is used to convert text into machine language (i.e., SQL statements) containing the table and column names of the database (hereinafter referred to as database list names), thereby enabling the retrieval of the required data from database 103. For example, the questioner 101 poses the text question "How many CFL teams are from YorkCollege?" to the database query system. Text-to-SQL module 102 extracts mentions of database list names from this text question, such as "CFL", "CFL teams", and "York College". Based on these mentions, it converts the text question into the SQL statement "SELECT COUNT(CFL_Team) FROM(CFLDraft) WHERE College = York", thus mapping it to columns (CFL_Team) and (College) in the table (CFLDraft) in the database. It then iterates through the column (College) to count the number of items named "York". As shown in the table above, the answer to this text question is "2", and the database query system returns the answer to the questioner 101.
[0031] It is evident that finding the database list name mentioned in the text is crucial for the Text-to-SQL module 102 when converting text to SQL statements. However, existing methods require the database list name mentioned in the text to be completely consistent with the list name contained in the database in order to match the corresponding target data location. Even in the case of synonyms with the same semantics, the database list name cannot be extracted.
[0032] In summary, this application aims to address the problem of inaccurate matching of text containing database list names with the same semantics when converting them to SQL statements. It seeks to find the optimal correspondence between text and database for SQL generation tasks and proposes a semantic-based text-database name alignment method. By utilizing the semantic information of current large-scale pre-trained models, the method finds the semantic alignment information between the input text and the database name, thereby further improving the performance of text-to-SQL statement technology.
[0033] The text-to-SQL statement processing method proposed in this application will be explained in detail below with reference to specific embodiments.
[0034] Figure 2 A flowchart illustrating the text-to-SQL statement processing method provided in this application embodiment. Figure 1 .like Figure 2 As shown, the processing method includes:
[0035] S201: Obtain the text to be converted, which is natural language text containing information related to the database list name.
[0036] The text to be converted refers to the text question input into the database query system. This text question can be a complete sentence or one or more keywords, and is natural language text containing information related to the database list name. Information related to the database list name can be understood as including phrases, words, or characters that have the same semantic meaning as the database list name.
[0037] S202: Based on the database list name, perform semantic analysis on the text to be converted to obtain the first link relationship between the text to be converted and the database list name. The first link relationship contains semantic information.
[0038] The database list names can be obtained by traversing the database of the database query system. After determining the database list names, semantic analysis is performed on the text to be converted to extract names with the same semantic meaning as the database list names, and a first semantic link relationship is established between these names and the database list names.
[0039] S203: Adjust the first link relationship based on the text to be converted and the database list name to obtain the second link relationship between the text to be converted and the database list name.
[0040] The initial generated first link relationship is based on general semantic information and has not been specially optimized for text-to-SQL generation, so it may still contain a lot of noise that affects the final model performance.
[0041] To address this issue, this application employs a dynamic link adjustment strategy. In each iteration of the training process, a set of dynamic links is generated. Specifically, the first link relationship generated initially is dynamically adjusted to obtain a second link relationship. The second link relationship can be directly optimized by the target of the text-to-SQL task. Furthermore, by using dynamic links to adjust the initial links, a link relationship that is both highly interpretable and highly helpful for the text-to-SQL task is obtained.
[0042] S204: Based on the second link relationship, determine the SQL statement corresponding to the text to be converted.
[0043] After obtaining the second link relationship, it can be encoded to obtain the encoding information between each item in the text to be converted and each item in the database list name. The encoded information can be connected to a standard SQL statement decoder to obtain the SQL statement corresponding to the text to be converted.
[0044] In this embodiment, the text to be converted (natural language text containing information related to database list names) is acquired. Semantic analysis is performed on the text to be converted based on the database list names to obtain a first link relationship between the text to be converted and the database list names. This first link relationship contains semantic information. The first link relationship is then adjusted based on the text to be converted and the database list names to obtain a second link relationship between the text to be converted and the database list names. Based on the second link relationship, the corresponding SQL statement for the text to be converted is determined. This application enables the establishment of links between text and database list names based on the same semantic information. By dynamically adjusting the initial link relationship, more accurate SQL statements can be output. By establishing and adjusting semantically based links between text and database names, different names with the same semantics (including names with the same, similar, or different expressions) can be identified, improving the intelligence and accuracy of text-to-SQL conversion and expanding its application scenarios.
[0045] Figure 3 A flowchart illustrating the text-to-SQL statement processing method provided in this application embodiment. Figure 2 . Reference Figure 3 Optionally, based on the above embodiments, the first link relationship is adjusted according to the text to be converted and the database list name to obtain a second link relationship between the text to be converted and the database list name, including:
[0046] The text to be converted is input into a pre-trained model for encoding, resulting in the text encoding of the text output by the pre-trained model. For example, the text Q in the illustration represents the text to be converted. Inputting text Q into the pre-trained model for encoding yields the text encoding Q, as shown in the illustration. Text Q contains one or more names, and the text encoding Q contains one or more name encodings q corresponding to those names. i For example, as shown in the diagram (q1, q2, ..., q n ).
[0047] The database list name is input into the pre-trained model for encoding, resulting in the database list name encoding output by the pre-trained model. For example, the data name S in the illustration represents a database list name. Inputting the data name S into the pre-trained model for encoding yields the database list name encoding, i.e., the encoding S shown in the illustration. The database list name S contains one or more names, and the database list name encoding contains one or more name encodings s corresponding to those names. i For example, as shown in the diagram (s1, s2, ..., s n ).
[0048] Cosine similarity is calculated between the text encoding and the database list name encoding to obtain a similarity matrix. This is illustrated in the diagram for each item (q1, q2, ..., q...) in Q.n ) and each term (s1, s2, ..., s) in S n ), calculate the cosine similarity, and obtain the similarity matrix A.
[0049] In the similarity matrix, the text code item with the highest similarity to each database list name code item in the database list name code is identified. The text code contains text code items. The similarity matrix A includes the similarity between each text code item and each database list name code item. Among these similarity relationships, the set of correspondences with the highest similarity is determined, that is, finding the pair in matrix A that corresponds to each s... i The closest q i .
[0050] Based on the text encoding item with the highest similarity and the database list name encoding item, the first link relationship is adjusted to obtain the second link relationship. Optionally, based on the first link relationship, find the link relationship in matrix A that corresponds to each s. i The closest q i And then establish a new link between the two, thereby forming a second link relationship after adjusting the first link relationship.
[0051] Since a database list name is typically linked to only one text word, we select the closest text word from the similarity matrix for each database list name to establish a link. This results in a dynamic link, which can be continuously optimized by the training objective, ultimately adjusting the initial link relationships.
[0052] Optionally, based on the above embodiments, determining the SQL statement corresponding to the text to be converted according to the second link relationship includes: performing attention-based encoding processing on the second link relationship to obtain a first encoding of the second link relationship; and decoding the first encoding to obtain the SQL statement corresponding to the text to be converted. The attention mechanism (AM) in neural networks is a resource allocation scheme that allocates computational resources to more important tasks when computational power is limited, while simultaneously solving the problem of information overload. Attention-based encoding assigns weights to link relationships with different degrees of similarity.
[0053] Furthermore, the second link relationship is encoded using an attention-based mechanism to obtain the first encoding of the second link relationship. This includes: inputting the second link relationship into a relational graph neural network for attention-based encoding to obtain the first encoding of the second link relationship output by the relational graph neural network. A relational graph neural network is a type of graph neural network that can effectively encode topological graphs containing links of different types / similarities. In our example, the link is the edge established between the text node and the database list name node, as well as the edges that originally existed between nodes within the text and nodes within the database, thus forming a graph containing different types of edges (a heterogeneous graph).
[0054] The detailed process of encoding is illustrated below. First, the text Q and the database list name S are merged into input X, and the attention value between every two nodes in input X is obtained using the following formula (Formula 1):
[0055] a ji =(x i W q (x) j W k +M ji [F(E ji )]) T , Formula 1
[0056] Where, α ji This represents the attention value between node j and node i in the input X, x i x j W represents the encoded item in input X. q W k M ji E represents the encoding matrix containing parameters. ji Let F() represent the link matrix formed by the second connection relationship, F() represent the function that converts the edges (links) into codes, and T represent the transpose operation of the vector.
[0057] After obtaining the attention value between every two nodes, we can use this attention value to obtain the updated encoding for each node. Specifically, the encoding can be obtained according to the following formula:
[0058] x i =∑α ji (x j W v +M ji [ψ(E ji )]), Formula 2
[0059] Where, x i x j Indicates the updated node code, α jiW represents the attention value between node j and node i in input X. v M ji E represents the encoding matrix containing parameters. ji This represents the link matrix formed by the second connection relationship, and Ψ() represents the operation of converting the specific link relationship into a vector encoding.
[0060] After performing this update operation on each node, one update of the relational graph neural network is completed. This achieves the updating of the encoding information of the text and the database using the link information between the text and the database. Subsequently, this encoding information can be used to access a standard SQL decoder to obtain the generated SQL statement.
[0061] Figure 4 A flowchart illustrating the text-to-SQL statement processing method provided in this application embodiment. Figure 3 . Reference Figure 4 Optionally, based on the above embodiments, semantic analysis is performed on the text to be converted according to the database list name to obtain the first link relationship between the text to be converted and the database list name, including:
[0062] The text to be converted and the database list names are merged to obtain the first input. For example, such as... Figure 4 As shown, the text Q and the list of data names S are merged into the first input I (i.e., input I).
[0063] The first input is input into the pre-trained model and encoded to obtain the second encoding of the first input from the output of the pre-trained model. That is, as shown in the figure, the input I is input into the pre-trained model to obtain the encoding I.
[0064] The process iterates through the words in the text to be converted, replacing the currently traversed word with a mask to obtain the target text. The target text and the database list names are then merged to obtain the second input. This second input is then fed into a pre-trained model for encoding, resulting in the third encoding of the second input output by the pre-trained model. The text to be converted, Q, includes one or more names / words q. i Iterate through the q in the text Q i , in turn q i Replace it with a mask, and then merge the target text containing the mask with the database list name again and input it into the pre-trained model to obtain a new encoding I1 (i.e., the third encoding).
[0065] Identify the database list name with the largest encoding difference between the second and third codes. Compare the change f of the database list name encoding in the new code I1 with that in code I, and find the database list name corresponding to the database list name item with the largest change f / largest difference.
[0066] Establish links between the database list names with the largest encoding differences and their corresponding words to obtain the first link relationship.
[0067] Optionally, a link is established between the database list name with the largest encoding difference and its corresponding word, including: determining whether the largest encoding difference exceeds a set threshold; if so, establishing a link between the database list name with the largest encoding difference and its corresponding word; if not, not establishing a link between the database list name with the largest encoding difference and its corresponding word. Figure 4 As shown, after finding the database list name corresponding to the database list name with the largest change f / largest gap, this gap can be compared with a set threshold. Only when the gap is greater than the set threshold is a link established between the database list name and the corresponding word, which further improves the accuracy of the link relationship.
[0068] Based on the above method embodiments, this application embodiment also provides a data query method, including: in response to detecting text to be converted, executing any of the processing methods provided in the above method embodiments to obtain an SQL statement corresponding to the text to be converted, wherein the text to be converted is natural language text containing information related to a database list name; and querying data from a database containing a database list name according to the SQL statement to obtain data corresponding to the SQL statement.
[0069] Figure 5 A flowchart illustrating the text-to-SQL statement processing method provided in this application embodiment. Figure 4 .like Figure 5 As shown, in order to find the optimal correspondence between text and database for the SQL generation task, this application embodiment mainly uses three functional components to achieve the goal of link generation: an initial link generation component, a dynamic link adjustment component, and a relational graph neural network encoding component.
[0070] The initial link generation component, also known as the first link relationship generation component, generates a link relationship between text containing semantic information and database list names using a pre-trained model before model training. This method solves the problem of direct name string matching failing in the case of synonyms. (See reference...) Figure 5 For example, solid circles represent text items, such as "How many French singers"; hollow circles represent database list name items, such as "singer, country, singer.id"; a pre-trained model is used to generate a link relationship (initial link) between text and database list names containing semantic information, and dashed lines represent the establishment of the link relationship.
[0071] The dynamic link adjustment component, also known as the second link generation component, dynamically generates link relationships between text and database list names during training. It adjusts and corrects the initial links generated in the first stage, and these adjusted links serve as input for the downstream text-to-SQL model. (See reference...) Figure 5 A pre-trained model is used to convert text and database list names into corresponding encoded items, and a similarity matrix is obtained through similarity learning. This matrix shows the similarity between each text encoding and each database list name encoding.
[0072] The dynamic link adjustment component adds the link relationships generated by the similarity matrix to the initial link relationship graph, thereby correcting and adjusting the link relationships between the text and the database list names.
[0073] The relational graph neural network encoding component uses a graph neural network containing relational encoding to encode the adjusted links using an attention-based mechanism. The updated text and database representation are then fed into a standard text-to-SQL decoding network to generate SQL statements. (See reference...) Figure 5 The SQL statement generated in this example is "SELECT count(*) FROM singerwhere country=French".
[0074] The following are embodiments of the apparatus of this application, which can be used to execute the embodiments of the method of this application. For details not disclosed in the embodiments of the apparatus of this application, please refer to the embodiments of the method of this application.
[0075] Figure 6 This is a schematic diagram of the structure of the text-to-SQL statement processing device provided in an embodiment of this application. Figure 6 As shown, the processing device 600 includes:
[0076] The acquisition module 601 is used to acquire the text to be converted, which is natural language text containing information related to the database list name;
[0077] Analysis module 602 is used to perform semantic analysis on the text to be converted based on the database list name, and obtain the first link relationship between the text to be converted and the database list name. The first link relationship contains semantic information.
[0078] The adjustment module 603 is used to adjust the first link relationship based on the text to be converted and the database list name to obtain the second link relationship between the text to be converted and the database list name;
[0079] The determination module 604 is used to determine the SQL statement corresponding to the text to be converted based on the second link relationship.
[0080] Optionally, the adjustment module 603 can be specifically used to input the text to be converted into a pre-trained model for encoding processing to obtain the text encoding of the text to be converted output by the pre-trained model; input the database list name into the pre-trained model for encoding processing to obtain the database list name encoding of the database list name output by the pre-trained model; calculate the cosine similarity between the text encoding and the database list name encoding to obtain a similarity matrix; in the similarity matrix, determine the text encoding item with the highest similarity to each database list name encoding item in the database list name encoding, wherein the text encoding contains the text encoding item; and adjust the first link relationship according to the text encoding item with the highest similarity and the database list name encoding item to obtain the second link relationship.
[0081] Optionally, the determining module 604 can be specifically used to perform attention-based encoding processing on the second link relationship to obtain the first encoding of the second link relationship; and to decode the first encoding to obtain the SQL statement corresponding to the text to be converted.
[0082] Optionally, the determining module 604 can also be specifically used to input the second link relationship into the relation graph neural network for encoding processing based on the attention mechanism, so as to obtain the first encoding of the second link relationship output by the relation graph neural network.
[0083] Optionally, the analysis module 602 can be specifically used to merge the text to be converted and the database list name to obtain a first input; input the first input into a pre-trained model for encoding to obtain a second encoding of the first input output by the pre-trained model; traverse the words in the text to be converted, and replace the currently traversed word with a mask to obtain the target text; merge the target text and the database list name to obtain a second input; input the second input into a pre-trained model for encoding to obtain a third encoding of the second input output by the pre-trained model; determine the database list name with the largest encoding difference between the second and third encodings; and establish a link between the database list name with the largest encoding difference and its corresponding word to obtain a first link relationship.
[0084] Optionally, the analysis module 602 can also be specifically used to determine whether the largest encoding difference is greater than a set threshold; if so, a link is established between the database list name with the largest encoding difference and the corresponding word; if not, no link is established between the database list name with the largest encoding difference and the corresponding word.
[0085] The apparatus provided in this application embodiment can be used to execute the control method described above. Its implementation and technical effects are similar, and will not be repeated here.
[0086] Figure 7 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Figure 7 As shown, the electronic device 700 includes:
[0087] Processor 701, memory 702, communication interface 703 and system bus 704.
[0088] The memory 702 and the communication interface 703 are connected to the processor 701 via the system bus 704 and communicate with each other. The memory 702 is used to store computer execution instructions, the communication interface 703 is used to communicate with other devices, and the processor 701 is used to execute any of the processing methods or data query methods provided in the above embodiments.
[0089] Specifically, processor 701 may include one or more processing units. For example, processor 701 may be a CPU, a Digital Signal Processing (DSP), an Application Specific Integrated Circuit (ASIC), etc. A general-purpose processor may be a microprocessor or any conventional processor. The steps of the method disclosed in the application can be directly manifested as being executed by a hardware processor, or executed by a combination of hardware and software modules within the processor.
[0090] The memory 702 can be used to store program instructions. The memory 702 may include a program storage area and a data storage area. The program storage area may store the operating system, at least one application program required for a function (such as sound playback), etc. The data storage area may store data created during the use of the electronic device 700 (such as audio data), etc. Furthermore, the memory 702 may include high-speed random access memory, and may also include non-volatile memory, such as at least one disk storage device, flash memory device, Universal Flash Storage (UFS), etc. The processor 701 executes various functional applications and data processing of the electronic device 700 by running the program instructions stored in the memory 702.
[0091] Communication interface 703 can provide solutions for wireless communication, including 2G / 3G / 4G / 17G, applied to electronic device 700. Communication interface 703 can receive electromagnetic waves via an antenna, and perform filtering, amplification, and other processing on the received electromagnetic waves before transmitting them to a modem processor for demodulation. Communication interface 703 can also amplify the signal modulated by the modem processor and radiate it as electromagnetic waves via the antenna. In some embodiments, at least some functional modules of communication interface 703 can be housed in processor 701. In some embodiments, at least some functional modules of communication interface 703 and at least some modules of processor 701 can be housed in the same device.
[0092] The system bus 704 can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This system bus 704 can be divided into address bus, data bus, control bus, etc. For ease of illustration, only one thick line is used to represent it in the diagram, but this does not indicate that there is only one bus or one type of bus.
[0093] It should be noted that the number of memory 702 and processor 701 is not limited in this embodiment; there can be one or more of them. Figure 7 The diagram illustrates an example; the memory 702 and processor 701 can be connected via wired or wireless means, such as a bus connection. In practical applications, the electronic device 700 can be various forms of computers or mobile terminals. Computers include, for example, laptops, desktop computers, workbenches, servers, blade servers, mainframe computers, etc.; mobile terminals include, for example, personal digital processors, cellular phones, smartphones, wearable devices, and other similar computing devices.
[0094] The electronic device in this embodiment can be used to execute the technical solutions in the above method embodiments. Its implementation principle and technical effect are similar, and will not be repeated here.
[0095] This application also provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, are used to implement the processing method or data query method in the above-described method embodiments.
[0096] This application also provides a computer program product, including a computer program; when the computer program is executed, it implements the processing method or data query method as described in the above method embodiments.
[0097] Other embodiments of this application will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of this application that follow the general principles of this application and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this application are indicated by the following claims.
[0098] It should be understood that this application is not limited to the precise structure described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this application is limited only by the appended claims.
Claims
1. A method for converting text to SQL statements, characterized in that, include: Obtain the text to be converted, which is natural language text containing information related to the database list name; Based on the database list name, semantic analysis is performed on the text to be converted to obtain a first link relationship between the text to be converted and the database list name, and the first link relationship contains semantic information; The first link relationship is adjusted based on the text to be converted and the database list name to obtain a second link relationship between the text to be converted and the database list name; Based on the second link relationship, determine the SQL statement corresponding to the text to be converted; The step of adjusting the first link relationship based on the text to be converted and the database list name to obtain a second link relationship between the text to be converted and the database list name includes: The text to be converted is input into a pre-trained model for encoding processing to obtain the text encoding of the text to be converted output by the pre-trained model. The database list name is input into the pre-trained model for encoding processing to obtain the database list name encoding of the database list name output by the pre-trained model; A cosine similarity matrix is obtained by calculating the cosine similarity between the text encoding and the database list name encoding; In the similarity matrix, the text encoding item with the highest similarity to each database list name encoding item in the database list name encoding is determined, and the text encoding includes the text encoding item; The first link relationship is adjusted based on the text encoding item with the highest similarity and the database list name encoding item to obtain the second link relationship.
2. The processing method according to claim 1, characterized in that, The step of determining the SQL statement corresponding to the text to be converted based on the second link relationship includes: The second link relationship is encoded using an attention mechanism to obtain the first encoding of the second link relationship; The first encoding is decoded to obtain the SQL statement corresponding to the text to be converted.
3. The processing method according to claim 2, characterized in that, The step of performing attention-based encoding on the second link relationship to obtain the first encoding of the second link relationship includes: The second link relationship is input into the graph neural network and encoded using an attention mechanism to obtain the first encoding of the second link relationship output by the graph neural network.
4. The processing method according to claim 1, characterized in that, The step of performing semantic analysis on the text to be converted based on the database list name to obtain the first link relationship between the text to be converted and the database list name includes: The text to be converted and the database list name are merged to obtain the first input; The first input is input into the pre-trained model for encoding processing to obtain the second encoding of the first input output by the pre-trained model; The words in the text to be converted are traversed, and the currently traversed word is replaced with a mask to obtain the target text; the target text and the database list name are merged to obtain the second input; the second input is input into the pre-trained model for encoding to obtain the third encoding of the second input output by the pre-trained model; Determine the list of databases with the largest encoding difference between the second and third encodings; Establish a link between the database list name with the largest encoding difference and its corresponding word to obtain the first link relationship.
5. The processing method according to claim 4, characterized in that, The process of establishing a link between the database list names with the largest encoding differences and their corresponding words includes: Determine if the maximum encoding difference is greater than a set threshold; If so, then establish a link between the database list name with the largest encoding difference and the corresponding word; If not, then no link will be established between the database list name with the largest encoding difference and the corresponding word.
6. A data query method, characterized in that, include: In response to detecting text to be converted, the processing method as described in any one of claims 1 to 5 is executed to obtain an SQL statement corresponding to the text to be converted, wherein the text to be converted is natural language text containing information related to the database list name; Based on the SQL statement, a data query is performed on the database containing the database list names to obtain the data corresponding to the SQL statement.
7. A text-to-SQL statement processing device, characterized in that, The text-to-SQL statement processing device is used to implement the text-to-SQL statement processing method according to any one of claims 1-5, the device comprising: The acquisition module is used to acquire the text to be converted, which is natural language text containing information related to the database list name; The analysis module is used to perform semantic analysis on the text to be converted based on the database list name to obtain a first link relationship between the text to be converted and the database list name, wherein the first link relationship contains semantic information; An adjustment module is used to adjust the first link relationship based on the text to be converted and the database list name to obtain a second link relationship between the text to be converted and the database list name; The determining module is used to determine the SQL statement corresponding to the text to be converted based on the second link relationship.
8. An electronic device, characterized in that, include: Memory, processor; The memory is used to store program instructions; The processor is configured to invoke the program instructions to execute the processing method as described in any one of claims 1 to 5 or the data query method as described in claim 6.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed by a processor, are used to implement the processing method as described in any one of claims 1 to 5 or the data query method as described in claim 6.
Citation Information
Patent Citations
SQL (Structured Query Language) conversion method and system based on language model coding and multi-task decoding
CN112988785A
Automatic SQL generation method and device
CN113434130A