Language escape method, device, equipment and storage medium
By splitting and correcting the target statements, executable machine language is generated, which solves the problem of syntax semantic errors in the natural language to database query language conversion, lowers the technical threshold and improves interaction efficiency.
Patent Information
- Application Number
- CN202210873471.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-07-22
- Publication Date
- 2025-08-29
- Estimated Expiration
- 2042-07-22
AI Technical Summary
In the prior art, there are syntax errors and semantic errors in the conversion process of natural language to database query language, which leads to the inability to execute the database effectively, requiring professionals to make adjustments, increasing technical thresholds and labor costs.
By splitting the target statement, determining the keyword type and semantic constraint rules, correcting the keyword clauses, and combining to generate an executable machine language, using the encoder and decoder for encoding and decoding, generating semantic feature vectors, and matching and correction in combination with the database data table.
It improves the accuracy of natural language escaping, reduces the technical capability requirements for staff using database query, improves work efficiency, and achieves barrier-free user interaction with database.
Smart Images

Figure CN115292448B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a natural language translation method, apparatus, device, and storage medium. Background Art
[0002] As the core foundational technology for many data services, databases provide support for a variety of data service needs. The management and use of databases often require the involvement of professional personnel in database usage and interaction.
[0003] Although some existing technologies have enabled data queries in databases using natural language, such as user input of natural language into a neural network, which then outputs corresponding query statements (e.g., Structured Query Language (SQL)), these queries have been less effective. Summary of the Invention
[0004] To solve or improve the problems existing in the prior art, the embodiments of the present application provide a language escape method, apparatus, device and storage medium.
[0005] In one aspect, in one embodiment of the present application, a language escape method is provided. The method includes:
[0006] Split the acquired target sentence to obtain at least one target sub-sentence;
[0007] Determining the keyword type and keyword clause contained in the target sub-sentence;
[0008] Modifying the keyword clause in the target sub-sentence according to the semantic constraint rules corresponding to the keyword type contained in the target sub-sentence;
[0009] The corrected target sub-statements are combined to generate executable machine language.
[0010] In a second aspect, in one embodiment of the present application, an electronic device is provided, including a memory and a processor; wherein,
[0011] The memory is used to store programs;
[0012] The processor is coupled to the memory and is configured to execute the program stored in the memory to implement the language escape method described in the first aspect.
[0013] In a third aspect, in one embodiment of the present application, a non-transitory machine-readable storage medium is provided, on which executable code is stored. When the executable code is executed by a processor of an electronic device, the processor executes a language escape method as described in the first aspect.
[0014] In a fourth aspect, in one embodiment of the present application, a database system is provided, the system comprising:
[0015] Encoder, used to encode the acquired natural language to obtain a semantic feature vector;
[0016] A decoder, configured to decode the semantic feature vector output by the encoder to generate a target sentence;
[0017] A splitting module, configured to split the acquired target sentence to obtain at least one target sub-sentence;
[0018] a correction module, configured to correct the keyword clause in the target sub-sentence according to the semantic constraint rules corresponding to the keyword type contained in the target sub-sentence;
[0019] A combination module, used for combining the corrected target sub-statements to generate executable machine language;
[0020] A database is used to execute data query tasks based on the executable machine language.
[0021] In the technical solution provided by the embodiment of the present application, after the user provides the natural language for query, the encoder can encode the natural language to obtain the semantic feature vector of the natural language. The language feature vector is further processed by the decoder to obtain the target sentence corresponding to the natural language that can be recognized by the database. The target sentence is split and processed to obtain a sequence containing at least one target sub-sentence. Each target sub-sentence contains keywords. When the target sub-sentence is corrected, the target sub-sentence is corrected based on the keyword type contained in the target sub-sentence and the corresponding semantic constraint rules, and the corrected target sub-sentences are combined to obtain a machine language that can be recognized by the database. Through the above method, the accuracy of natural language escape and the work efficiency of users using database queries can be effectively improved, while reducing the technical ability requirements for staff using database queries. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, a brief introduction will be given below to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0023] Figure 1 A flow chart of the language translation method provided in an embodiment of the present application;
[0024] Figure 2 A schematic diagram of the structure of the language escape system provided in an embodiment of the present application;
[0025] Figure 3 A flowchart of a method for splitting a target sentence provided in an embodiment of the present application;
[0026] Figure 4 A schematic diagram of a data table structure for illustrating an embodiment of the present application;
[0027] Figure 5 A schematic diagram of a directed graph illustrating an embodiment of the present application;
[0028] Figure 6 A schematic diagram of the structure of a language escape device provided in an embodiment of the present application;
[0029] Figure 7 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application;
[0030] Figure 8 A schematic diagram of a database system provided in an embodiment of the present application. DETAILED DESCRIPTION
[0031] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application.
[0032] In some processes described in the specification, claims and the above-mentioned figures of this application, multiple operations appearing in a specific order are included. These operations may not be executed in the order in which they appear in this document or may be executed in parallel. The serial numbers of the operations, such as 101, 102, etc., are only used to distinguish between different operations, and the serial numbers themselves do not represent any order of execution. In addition, these processes may include more or fewer operations, and these operations may be executed in sequence or in parallel. It should be noted that the descriptions of "first", "second", etc. in this document are used to distinguish different messages, devices, modules, etc., and do not represent a sequence, nor do they limit "first" and "second" to different types. In addition, the embodiments described below are only some of the embodiments of this application, not all of the embodiments. Based on the embodiments in this application, all other embodiments obtained by those skilled in the art without making creative work are within the scope of protection of this application.
[0033] Databases are the core infrastructure for most data services in the information industry, requiring constant support throughout the data processing process. Currently, end users and database operators primarily interact with databases using machine languages (such as SQL and Gremlin). This requires strong database expertise, operational experience, and a deep understanding of relevant database instances. Consequently, database management, usage, and maintenance require a significant number of specialized personnel involved in database interaction, resulting in high technical barriers and labor costs. With the advancement of artificial intelligence (AI), machine learning techniques can be used to convert natural language input from human operators into machine language, enabling computers to understand the operator's true intent and execute the corresponding tasks. However, in practical applications, the machine language generated by translating natural language into database query language (NL2SQL) using natural language processing and machine learning algorithms often contains syntactical and semantic errors, making it difficult for the database to properly execute the translated machine language. This requires technically qualified personnel to adjust the resulting machine language before it can be successfully executed. Therefore, this application proposes optimizing and adjusting the translated machine language to enable seamless interaction between staff and the database using natural language. The translated machine language is more accurate and more easily executed. This can effectively reduce the technical requirements for staff, lower labor costs, and improve work efficiency.
[0034] In the technical solution of this application, the specific working process will be described in the following embodiments.
[0035] like Figure 1The flow chart of the language escape method provided in the embodiment of the present application. The execution subject of the language escape method can be a local computer, a cloud service device, etc. Figure 1 As shown, the language escape method includes the following steps:
[0036] Step 101: Split the acquired target sentence to obtain at least one target sub-sentence.
[0037] Step 102: Determine the keyword type and keyword clause contained in the target sub-sentence;
[0038] Step 103: Modify the keyword clause in the target sub-sentence according to the semantic constraint rules corresponding to the keyword type contained in the target sub-sentence.
[0039] Step 104: Combine the modified target sub-statements to generate executable machine language.
[0040] The target statement mentioned here can be understood as the machine language (eg, SQL) obtained after encoding and decoding using an encoder and a decoder. Generally speaking, the target statement obtained may contain multiple target sub-statements with nested relationships.
[0041] When modifying a target sentence, it is necessary to split the target sentence so that none of the target sub-sentences obtained after the splitting have any nesting relationships. In other words, if two target sub-sentences are obtained after one splitting, and one of the target sub-sentences still contains a nesting relationship, it is necessary to further split the target sub-sentence until it no longer contains any nesting relationships. The specific splitting process and splitting scheme will be illustrated in the following embodiments.
[0042] Keyword types include SELECT, WHERE, FROM, HAVING, ORDER BY, GROUP BY, and LIMIT. Keyword clauses are defined as sentences derived from the keyword and its context. A single target clause may contain multiple keyword types and corresponding keyword clauses. Keyword types can be defined and adjusted based on user needs. For example, you can add or remove a keyword type.
[0043] It should be noted that different keyword types correspond to different semantic constraint rules. The semantic constraint rules mentioned here can be understood as the semantic constraint rules of the grammar used in different computer languages. For example, what types of operators can a certain type of value support, what nested relationships are supported by words or sentences, the position of a certain operator in a sentence, etc. In actual applications, in order to obtain a more accurate correction effect, the keyword clause is used as the minimum correction unit for correction according to the semantic constraint rules. When correcting, the correction can be made according to the logical relationship between the keywords in the target sub-sentence, or it can be made in order of sequence.
[0044] After modifying the keyword clauses in each target sub-statement, a modified target sub-statement is obtained, and then multiple modified target sub-statements are combined. When combining, they are combined according to the nested relationship of the split to obtain a machine language (such as SQL).
[0045] In one or more embodiments of the present application, the acquired target sentence is split to obtain at least one target sub-sentence, including: using a decoder to decode the semantic feature vector obtained based on the natural language encoding provided by the user to obtain a target sentence containing keywords, operation content and predicted values; splitting the target sentence according to the nested relationship to generate the at least one target sub-sentence without nested relationship.
[0046] like Figure 2 This is a structural diagram of the language escape system provided in the embodiment of the present application. Figure 2 The overall architecture of the technical solution of the present application is described. In the technical solution of the present application, two models are included, an encoder and a decoder. Among them, the encoder model is usually composed of a long short-term memory network (LSTM), a Transformer and other network structures, which converts the input natural language into a series of abstract semantic feature vectors. The decoder is usually composed of another independent LSTM or Tree-LSTM. It should be noted that the encoders and decoders mentioned here are various pre-trained neural network models.
[0047] When a user or staff member provides natural language through oral or text input (the natural language here usually refers to a language that naturally evolves with culture. For example, Chinese, English, Japanese, etc.), the natural language is further input into the encoder in text form to output a semantic feature vector.
[0048] After the encoder generates a semantic feature vector, it is fed into the decoder, which decodes it to produce the target sentence. The target sentence contains at least one keyword, operators (e.g., =, !=, ==), and a predicted value (predicted by the machine learning model).
[0049] As mentioned above, after obtaining the target sentence, it will be further split. The ultimate goal of the split is to eliminate nested relationships in each target sub-sentence. Of course, if the target sentence does not contain any nested relationships, it can be left unsplit and treated as a sub-sentence. The keyword clauses will be modified according to the semantic constraints corresponding to the keyword types contained in it.
[0050] For example, a user enters a natural language question like, "Tell me what is the average life expectancy for residents of countries where English is not the official language?" The target SQL statement generated directly using a decoder (e.g., a neural network model) is: SELECT avg(life_expectancy) FROM country WHERE country_name != (SELECT isofficial FROM country_language WHERE language != "English"). In this target statement, the keyword types are: SELECT type, FROM type, and WHERE type. The keyword clauses obtained in the outer target substatement are: SELECT avg(life_expectancy), FROM country, WHERE country_name !=. The operator in the WHERE clause is !=, and the predicted value is country_name.
[0051] After splitting the above target statement, the two target sub-statements without nested relationships are: ①SELECT avg(life_expectancy) FROM country WHERE country_name!= and ②SELECT isofficial FROM country_language WHERE language!= "English".
[0052] In one or more embodiments of the present application, Figure 3 This is a flow chart of a method for splitting a target sentence provided in an embodiment of the present application. Figure 3As can be seen in FIG, the target sentence is split according to the nesting relationship to generate at least one target sub-sentence without nesting relationship, including the following steps:
[0053] Step 301: Determine, based on a nesting identifier included in the target sentence, a first target sub-sentence and a second target sub-sentence that are in a nesting relationship; wherein the second target sub-sentence is nested in the first target sub-sentence.
[0054] Step 302: Add a position marker to the nested position where the nested marker is located in the first target sub-statement.
[0055] Step 303: Generate a sequence consisting of the first target sub-statement and the second target sub-statement in a hierarchical order in a nested relationship.
[0056] In actual applications, when splitting the target statement, it is split based on nested identifiers. The nested identifiers mentioned here can be specific symbols, such as brackets () and {}, etc. They can also be set operators, such as INTERSECT, UNION, EXCEPT, etc.
[0057] Since the second target sub-statement is nested within the first target sub-statement, if the second target sub-statement is found to contain other sub-statements, the second target sub-statement needs to be further split. Conversely, if the second target sub-statement no longer contains any nested content, the second target sub-statement does not need to be further split.
[0058] Continuing to assume that the second target sub-statement no longer contains any nesting relationships, it is necessary to add a position marker at the position of the nesting marker in the first target sub-statement so that the first target sub-statement and the second target sub-statement can be combined later. When combining, the modified first target sub-statement needs to be restored to its previous nesting relationship. Therefore, it is necessary to combine the modified first target sub-statement and the second target sub-statement according to the position marker.
[0059] After splitting, a first target sub-sentence and a second target sub-sentence can be obtained, which can be further combined based on the hierarchical order in the nested relationship between the first target sub-sentence and the second target sub-sentence to obtain a sub-sentence sequence.
[0060] For example, a user enters a natural language question like, "Tell me what is the average life expectancy for residents of countries where English is not the official language?" The target SQL statement generated directly using a decoder (e.g., a neural network model) is: SELECT avg(life_expectancy) FROM country WHERE country_name!=(SELECT isofficial FROM country_language WHERE language!="English"). In this target statement, we can see the keyword types: SELECT type, FROM type, and WHERE type.
[0061] After splitting the target statement, we obtain two target sub-statements that are not nested: ① SELECT avg(life_expectancy) FROM country WHERE country_name!= and ② SELECT isofficial FROM country_language WHERE language!= "English". Because the second target sub-statement is embedded within the first, the resulting sequence is [SELECT avg(life_expectancy) FROM country WHERE country_name!= NESTEDVALUE, SELECT isofficial FROM country_language WHERE language!= "English"]. NESTEDVALUE can be used as a positional identifier.
[0062] However, this SQL statement cannot be executed because it contains multiple syntax and semantic errors. The correct SQL statement is: SELECT avg(life_expectancy) FROM country WHERE country_name not in(SELECT country_name FROM country_language WHERE language!="English" and isofficial=1). The correction process will be specifically described in the following embodiment.
[0063] In one or more embodiments of the present application, after obtaining at least one target sub-sentence, the method further includes: determining the corresponding semantic constraint rule according to the keyword type included in the target sub-sentence.
[0064] Different keyword types use different grammatical constraint rules for modifying keyword clauses. For example: the predicted value in the WHERE clause is modified, and the similarity between the predicted value and the value in the data table stored in the database is calculated to determine whether the predicted value needs to be modified. In the HAVING clause, when it is determined that the aggregate function matches the column name, different correction methods are used according to the different types of aggregate functions. For example, if the aggregate function is a function that keeps the numeric type unchanged, such as max or min, the processing method can be to calculate the similarity between the predicted value and the value in the data table stored in the database to determine whether the predicted value needs to be modified; if the aggregate function is count, avg, etc., and the aggregation result is a calculated value, the predicted value is replaced with a constant, for example, the predicted value single is replaced with 1. If reasonable escape is not possible, the HAVING clause can be discarded.
[0065] It should be noted that, when making corrections, it is necessary to use a database to make corrections. In an optional embodiment, a data table can be established in the database. Figure 4 The following is a schematic diagram of the data table structure for illustrating an embodiment of the present application. Figure 4 As you can see, Data Table 1 stores column names and values. Column names can be set as needed. For example, a column named "country_name" would correspond to values like "China," "American," and "Japan." Data Table 2 also stores column names and values. For example, a column named "city_name," "country_name," ... would correspond to values in the first row like "Beijing, China," ...; values in the second row like "Shanghai, China," ...; and values in the third row like "New York, USA," ..., etc.
[0066] In one or more embodiments of the present application, when the keyword type is a SELECT type, the decoder is used to decode the semantic feature vector obtained based on the natural language encoding provided by the user to obtain a target statement containing keywords, operation content and predicted values, including: obtaining a data table provided by the database; performing a matching analysis on the operation content associated with the SELECT keyword obtained by decoding the semantic feature vector and the data column in the data table; determining a target data column that matches the operation content from the data table according to the matching degree; and generating a target statement containing the SELECT keyword, the operation content and the predicted value based on the data in the target data column.
[0067] To ensure more accurate decoding results, the decoder can analyze the matching degree between the decoded aggregate function and the column names in the data table. For example, if the column name "varchar" does not match the aggregate function "max" or "min", the probability value of the matching degree at that position can be set to zero. If the aggregate function "max" or "min" matches a column name, the corresponding probability value is set based on the matching degree. Then, the column with the highest probability value is selected from multiple columns. A SELECT clause is generated based on the values and / or operation contents contained in the selected column.
[0068] In one or more embodiments of the present application, when the keyword type is a WHERE type, the method of decoding the semantic feature vector obtained based on the natural language encoding provided by the user using a decoder to obtain a target sentence containing keywords, operation content, and predicted values includes: obtaining a data table provided by a database; filtering out operation content from the data table based on the predicted value associated with the WHERE keyword obtained by decoding the semantic feature information; and determining the filtered operation content that matches the predicted value based on the degree of matching between the predicted value and the filtered operation content, so as to obtain a target sentence containing the WHERE keyword, the predicted value, and the filtered operation content.
[0069] During the decoding process, if the decoding results in a WHERE clause containing a predicted value, the corresponding data table is selected based on the predicted value associated with the WHERE keyword, and then the matching degree between the predicted value and the operator in the data table is calculated. If there is no match, the probability value of the data column position in the corresponding data table is set to zero. Then, the matching degree between the predicted value and the operators in other data columns is calculated, so that the data column with a high probability value is selected to obtain the required WHERE clause. In actual applications, when calculating the matching degree between the predicted value and the operator, the operator can be further restricted according to the type of the predicted value. For example, if the predicted value is the string abc, the limited operator range is {=, <>, like, not like}, and the position probability value of other types of operators is set to 0.
[0070] The keyword clause modification process corresponding to various keyword types will be described below with reference to specific embodiments.
[0071] When the keyword type is a WHERE type, if the predicted value is a non-nested value, a first similarity value is calculated between the first eigenvector of the predicted value and the second eigenvector of any value in the first data column of the data table; if the first similarity value is greater than a first threshold, the predicted value is replaced with the arbitrary value to obtain a revised target sub-statement. If the similarity value is less than the first threshold, a second similarity value is calculated between the third eigenvector of any value in any column of the data table other than the first data column and the first eigenvector; if the second similarity value is greater than a second threshold, the predicted value is replaced with any value in the arbitrary column to obtain a revised target sub-statement.
[0072] For example, when the predicted value in a WHERE clause is a non-nested value, such as a string, its corresponding word2vec vector is obtained and its cosine similarity is calculated with the word2vec vectors of all values in the corresponding data column in the data table, and the value with the largest cosine similarity is found. If the largest cosine similarity is greater than the first threshold M1, the predicted value is replaced with the value in the data table; if the cosine similarity between all values in the corresponding data column in the data table and the predicted value is less than the first threshold M1, the values of other data columns in the data table are compared. If there is a data column whose cosine similarity with the predicted value is greater than the second threshold M2, the predicted value is replaced with the value in the data column, and the column name in the WHERE clause is modified to the column name of the data stream. If the cosine similarity between all values and the predicted value is less than M2, the original statement remains unchanged.
[0073] When the keyword type is HAVING, if the result of executing the operation is a numeric constant, the predicted value in the HAVING clause is replaced with the constant. For example, if the operation is an aggregate function such as count or avg, and the result of executing the aggregate function is a numeric constant, the predicted value that has a contextual relationship with the operation (aggregate function) can be replaced with a constant (e.g., 1) during the modification.
[0074] When the execution of the operation content does not change the predicted value, if the predicted value is a non-nested value, a first similarity value is calculated between the first eigenvector of the predicted value and the second eigenvector of any value in the first data column of the data table; if the first similarity value is greater than a first threshold, the predicted value is replaced with the arbitrary value to obtain a revised target sub-statement. If the similarity value is less than the first threshold, a second similarity value is calculated between the third eigenvector of any value in any column of the data table other than the first data column and the first eigenvector; if the second similarity value is greater than a second threshold, the predicted value is replaced with any value in the arbitrary column to obtain a revised target sub-statement.
[0075] For example, if the aggregation function in the HAVING clause is a function that maintains the same type, such as max or min, the word2vec vector of the predicted value (for example, the string abc) is calculated, and the cosine similarity is calculated with the word2vec vector of all values in the corresponding data column in the data table, and the value with the largest cosine similarity value is found. If the maximum cosine similarity is greater than the first threshold M1, the predicted value is replaced with the value in the data table; if the cosine similarity between all values in the corresponding data column in the data table and the predicted value is less than the first threshold M1, the values of other data columns in the data table are compared. If there is a data column whose cosine similarity with the predicted value is greater than the second threshold M2, the predicted value is replaced with the value in the data column, and the column name in the WHERE clause is modified to the column name of the data stream. If the cosine similarity between all values and the predicted value is less than M2, the original statement remains unchanged.
[0076] In one or more embodiments of the present application, each keyword clause in the target substatement is modified according to the semantic constraint rules corresponding to the keyword type contained in the target substatement, including: when the keyword type is an ORDER BY type, if other clauses in the target substatement except the ORDER BY clause contain aggregate functions, the ORDER BY clause is converted into a nested query statement.
[0077] ORDER BY is used to sort query results. When modifying an ORDER BY clause, check whether other keyword clauses in the target sub-statement contain aggregate functions, and whether the ORDER BY clause itself contains aggregate functions. If other keyword clauses in the target sub-statement do not contain aggregate functions, or if both other keyword clauses in the target sub-statement and the ORDER BY clause contain aggregate functions and a GROUP BY clause exists, no modification is required to the ORDER BY clause.
[0078] If all keyword clauses in the target sub-statement, except the ORDER BY clause, contain aggregate functions, corrections are required. For example, if any other clause contains aggregate functions, the query is converted into a nested query. The conversion rule is: add a clause to the WHERE clause in the form WHERE c = (SELECT c ORDER BY c), where c is the column name in the original ORDER BY statement. If the ORDER BY clause contains aggregate functions and there is no GROUP BY clause, the ORDER BY clause is deleted.
[0079] In one or more embodiments of the present application, each keyword clause in the target substatement is modified according to the semantic constraint rules corresponding to the keyword type contained in the target substatement, including: when the keyword type is a GROUP BY type, if the SELECT clause does not contain an aggregate function and the column name of the data column corresponding to the GROUP BY clause, the column name of the GROUP BY is replaced with the primary key; if the primary key is not found, the data column corresponding to the SELECT clause is copied to the data column corresponding to the GROUP BY.
[0080] As mentioned above, in order to improve the speed and efficiency of natural language translation, a data table is pre-created, in which data columns are stored, including column names and multiple values. Some of these values can be used as foreign keys and as primary keys of another data table, thus strengthening the link between the two data tables and facilitating data search and management. Figure 4 As shown in Table 1, country_name is the primary key of Table 1, and country_name is the foreign key of Table 2. The primary key and foreign key are essentially the same column here (in a relational database). This establishes an association between Table 1 and Table 2.
[0081] For example, if the SELECT clause contains a column name that does not contain an aggregate function and does not appear in the GROUP BY clause, the GROUP BY column name is replaced with a unique key (such as a primary key). If there is no unique key, the columns in the SELECT clause are copied to the GROUP BY clause.
[0082] In one or more embodiments of the present application, when the keyword type is the FROM type, a directed graph is generated based on the data table corresponding to the modified target substatement and the data columns therein; based on the data table and the data columns in the directed graph, a minimum spanning tree is constructed with the data column or foreign key as the root node.
[0083] For example, Figure 5 Schematic diagram of a directed graph for illustrating an embodiment of the present application. Figure 5 As you can see, we first construct a relational schema graph. Each node in the relational schema graph is a table or a column. The standard relational schema is a directed graph, and the edges have four forms:
[0084] a. The starting node is a column and the ending node is the table to which the column belongs
[0085] b. The starting node is the foreign key and the ending node is the corresponding primary key
[0086] c. The side opposite to a
[0087] d. The side opposite to b
[0088] A minimum spanning tree is constructed using the table names in the FROM clause predicted by the decoder and all tables involved in the keyword clauses as nodes. To avoid ambiguity, the tree uses only two edges: a and b. After the tree is obtained, it needs to be pruned, repeatedly removing all tables with an out-degree of 0 until the out-degree of all tables in the tree is greater than 0.
[0089] In one or more embodiments of the present application, the combining of the modified target sub-statements includes: if the SELECT clause and the WHERE clause in the modified target sub-statement have the same column name or primary-foreign key relationship, then the modified target sub-statements are combined; or, if the SELECT clause and the WHERE clause in the modified target sub-statement do not have the same column name or primary-foreign key relationship, then the column name corresponding to the WHERE clause is copied to the SELECT clause, and the modified target sub-statements are combined.
[0090] For example, after completing the correction operation on each keyword clause in the target statement, these corrected keyword statements are further recombined to obtain the corrected target statement. For the nested query in the WHERE clause, the inner SQL needs to ensure that the column name of the SELECT clause is the same as the column name of the outer condition (WHERE clause) or there is a primary key-foreign key relationship. If the above conditions are not met, the outer column name is copied to the inner SELECT clause. In particular, if the column in the inner SELECT clause is a boolean attribute, add the condition in the inner WHERE clause: WHERE c=1, where c is the column name of the boolean attribute. When combining, the combination can be performed according to the position identifier marked when splitting, which can effectively improve the combination efficiency.
[0091] For ease of understanding, this solution will be illustrated below through specific embodiments.
[0092] First, the SQL directly predicted by the pre-trained neural network model in the encoder and decoder: SELECT avg (life_expectancy) FROM country WHERE country_name! = (SELECT isofficial FROM country_language WHERE language! = "English") will be split into sequences: [
[0094] SELECT avg(life_expectancy)FROM country WHERE country_name! =NESTEDVALUE,
[0095] SELECT isofficial FROM country_language WHERE language! ="English" ]
[0097] Each keyword clause in the first target clause in the sequence is processed, where the != in country_name != NESTEDVALUE in the WHERE clause is re-predicted as not in because the operator does not match the value type.
[0098] Each keyword clause in the second target clause in the sequence is processed, where the value "English" in the WHERE clause "language!="English" is replaced with "English" through word vector similarity calculation.
[0099] Concatenate the two revised keyword clauses. The isoficiale in the inner SELECT clause is different from the column name country_name in the outer condition and does not have a primary key or foreign key relationship. Therefore, country_name is copied to the inner SELECT clause. Since isoficiale is a Boolean type, the condition isoficiale = 1 is added to the inner WHERE clause.
[0100] The final result is the corrected target statement, which is machine language executable by a computer: SELECT avg(life_expectancy) FROM country WHERE country_name not in(SELECT country_name FROM country_language WHERE language!="English" and isofficial=1). Through the above method, the SQL statement obtained by encoding and decoding is split, and then the keyword clauses obtained are corrected. Because each keyword has its own unique execution semantic constraint rules, these rules are determined by the characteristics of machine language. These rules do not exist in natural language and cannot be directly obtained through natural statement translation. In addition, since the rules are not very regular and there are many types of keywords, they cannot be learned by neural network models. Therefore, to ensure that the SQL statement can be executed correctly, the values in each keyword clause are corrected one by one, making the correction effect more accurate.
[0101] Based on the same idea, the embodiment of the present application also provides a language escape device. Figure 6 This is a schematic diagram of the structure of a language escape device provided in an embodiment of the present application. The language escape device includes:
[0102] The splitting module 61 is used to split the acquired target sentence to obtain at least one target sub-sentence.
[0103] The determination module 62 is configured to determine the keyword type and keyword clause contained in the target sub-sentence.
[0104] The correction module 63 is configured to correct the keyword clause in the target sub-sentence according to the semantic constraint rules corresponding to the keyword type contained in the target sub-sentence.
[0105] The combining module 64 is used to combine the modified target sub-statements to generate executable machine language.
[0106] Optionally, a splitting module 61 is configured to decode the semantic feature vector obtained by encoding the natural language provided by the user using a decoder to obtain a target sentence containing keywords, operation content, and predicted values;
[0107] The target sentence is split according to the nesting relationship to generate the at least one target sub-sentence without the nesting relationship.
[0108] Optionally, the splitting module 61 is configured to determine, based on a nesting identifier included in the target sentence, a first target sub-sentence and a second target sub-sentence that are in a nested relationship; wherein the second target sub-sentence is nested in the first target sub-sentence;
[0109] Adding a position marker to the nested position where the nested marker is located in the first target sub-statement;
[0110] A sequence consisting of the first target sub-statement and the second target sub-statement in a hierarchical order in a nested relationship is generated.
[0111] Optionally, the determination module 62 is further configured to determine the corresponding semantic constraint rule according to the keyword type contained in the target sub-sentence.
[0112] Optionally, a decoding module 65 is further included. When the keyword type is SELECT type, the decoding module 65 is used to obtain a data table provided by the database;
[0113] Performing a matching analysis between the operation content associated with the SELECT keyword obtained by decoding the semantic feature vector and the data column in the data table;
[0114] Determining a target data column that matches the operation content from the data table according to the matching degree;
[0115] Based on the data in the target data column, a target statement including a SELECT keyword, the operation content and the predicted value is generated.
[0116] Optionally, when the keyword type is a WHERE type;
[0117] The decoding module 65 is used to obtain the data table provided by the database;
[0118] Filtering operation content from the data table based on the predicted value associated with the WHERE keyword obtained by decoding the semantic feature information;
[0119] According to the matching degree between the predicted value and the filtered operation content, the filtered operation content that matches the predicted value is determined to obtain a target statement including the WHERE keyword, the predicted value and the filtered operation content.
[0120] The correction module 63 is configured to replace the predicted value in the HAVING clause with a constant if the execution result of executing the operation content is a numerical constant when the keyword type is the HAVING type.
[0121] a correction module 63, configured to calculate a first similarity value between a first eigenvector of the predicted value and a second eigenvector of any value in the first data column of the data table if the predicted value is a non-nested value;
[0122] If the first similarity value is greater than a first threshold, the predicted value is replaced by the arbitrary value to obtain a modified target sub-sentence.
[0123] a correction module 63, configured to calculate a second similarity value between a third eigenvector of any value in any column other than the first data column in the data table and the first eigenvector if the similarity value is less than a first threshold;
[0124] If the second similarity value is greater than a second threshold, any value in any column is replaced with the predicted value to obtain a modified target sub-sentence.
[0125] The correction module 63 is configured to convert the ORDER BY clause into a nested query statement when the keyword type is the ORDER BY type and if other clauses in the target sub-statement except the ORDER BY clause contain aggregate functions.
[0126] Correction module 63, configured to, when the keyword type is GROUP BY type, replace the GROUP BY column name with the primary key if the SELECT clause does not contain the aggregate function and the column name of the data column corresponding to the GROUP BY clause;
[0127] If the primary key is not found, the data column corresponding to the SELECT clause is copied to the data column corresponding to the GROUP BY clause.
[0128] Optionally, it further includes a generating module 66 for generating a directed graph based on the data table corresponding to the modified target sub-statement and the data columns therein when the keyword type is a FROM type;
[0129] Based on the data table and the data columns in the directed graph, a minimum spanning tree is constructed with the data columns or foreign keys as root nodes.
[0130] Optionally, the combination module 64 is used to combine the modified target substatements if the SELECT clause and the WHERE clause in the modified target substatement have the same column name or primary-foreign key relationship; or, if the SELECT clause and the WHERE clause in the modified target substatement do not have the same column name or primary-foreign key relationship, copy the column name corresponding to the WHERE clause to the SELECT clause and combine the modified target substatements.
[0131] An embodiment of the present application also provides an electronic device. Figure 7 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. The electronic device includes a memory 701, a processor 702 and a communication component 703; wherein,
[0132] The memory 701 is used to store programs;
[0133] The processor 702 is coupled to the memory and configured to execute the program stored in the memory to:
[0134] Split the acquired target sentence to obtain at least one target sub-sentence;
[0135] Determining the keyword type and keyword clause contained in the target sub-sentence;
[0136] Modifying the keyword clause in the target sub-sentence according to the semantic constraint rules corresponding to the keyword type contained in the target sub-sentence;
[0137] The corrected target sub-statements are combined to generate executable machine language.
[0138] Optionally, the processor 702 is configured to use a decoder to decode the semantic feature vector obtained based on the natural language encoding provided by the user to obtain a target sentence including keywords, operation content, and predicted value;
[0139] The target sentence is split according to the nesting relationship to generate the at least one target sub-sentence without the nesting relationship.
[0140] Optionally, the processor 702 is configured to determine, based on a nesting identifier included in the target sentence, a first target sub-sentence and a second target sub-sentence that are in a nested relationship; wherein the second target sub-sentence is nested in the first target sub-sentence;
[0141] Adding a position marker to the nested position where the nested marker is located in the first target sub-statement;
[0142] A sequence consisting of the first target sub-statement and the second target sub-statement in a hierarchical order in a nested relationship is generated.
[0143] Optionally, the processor 702 is configured to determine the corresponding semantic constraint rule according to the keyword type included in the target sub-sentence.
[0144] Optionally, when the keyword type is SELECT type, the processor 702 is configured to obtain a data table provided by a database;
[0145] Performing a matching analysis between the operation content associated with the SELECT keyword obtained by decoding the semantic feature vector and the data column in the data table;
[0146] Determining a target data column that matches the operation content from the data table according to the matching degree;
[0147] Based on the data in the target data column, a target statement including a SELECT keyword, the operation content and the predicted value is generated.
[0148] Optionally, when the keyword type is a WHERE type;
[0149] The processor 702 is configured to obtain a data table provided by a database;
[0150] Filtering operation content from the data table based on the predicted value associated with the WHERE keyword obtained by decoding the semantic feature information;
[0151] According to the matching degree between the predicted value and the filtered operation content, the filtered operation content that matches the predicted value is determined to obtain a target statement including the WHERE keyword, the predicted value and the filtered operation content.
[0152] The processor 702 is configured to, when the keyword type is the HAVING type, replace the predicted value in the HAVING clause with a constant if the execution result of executing the operation content is a numerical constant.
[0153] The processor 702 is configured to calculate a first similarity value between a first eigenvector of the predicted value and a second eigenvector of any value in a first data column of the data table if the predicted value is a non-nested value;
[0154] If the first similarity value is greater than a first threshold, the predicted value is replaced by the arbitrary value to obtain a modified target sub-sentence.
[0155] The processor 702 is configured to calculate a second similarity value between a third eigenvector of any value in any column other than the first data column in the data table and the first eigenvector if the similarity value is less than a first threshold;
[0156] If the second similarity value is greater than a second threshold, any value in any column is replaced with the predicted value to obtain a modified target sub-sentence.
[0157] The processor 702 is configured to, when the keyword type is an ORDER BY type, convert the ORDER BY clause into a nested query statement if other clauses in the target sub-statement except the ORDER BY clause contain aggregate functions.
[0158] The processor 702 is configured to, when the keyword type is a GROUP BY type, replace the GROUP BY column name with the primary key if the SELECT clause does not contain an aggregate function and the column name of the data column corresponding to the GROUP BY clause;
[0159] If the primary key is not found, the data column corresponding to the SELECT clause is copied to the data column corresponding to the GROUP BY clause.
[0160] The processor 702 is configured to generate a directed graph based on the data table corresponding to the modified target substatement and the data columns therein when the keyword type is a FROM type;
[0161] Based on the data table and the data columns in the directed graph, a minimum spanning tree is constructed with the data columns or foreign keys as root nodes.
[0162] The processor 702 is configured to combine the modified target sub-statements if the SELECT clause and the WHERE clause in the modified target sub-statements have the same column name or primary and foreign key relationship; or
[0163] If the modified SELECT clause and the WHERE clause in the target substatement do not have the same column name or primary-foreign key relationship, the column name corresponding to the WHERE clause is copied to the SELECT clause, and the modified target substatement is combined.
[0164] The memory 701 can be configured to store various other data to support operations on the electronic device. Examples of such data include instructions for any application or method operating on the electronic device. The memory can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk.
[0165] Furthermore, the processor 702 in this embodiment may specifically be: a programmable switching processing chip, which is configured with a data replication engine and can replicate received data.
[0166] When the processor 702 executes the program in the memory, in addition to the above functions, it can also realize other functions, for details, please refer to the description of the above embodiments. Figure 7 As shown, the electronic device further includes: a power supply component 704 and other components.
[0167] The embodiment of the present application further provides a non-transitory machine-readable storage medium, wherein the non-transitory machine-readable storage medium stores executable code, and when the executable code is executed by a processor of an electronic device, the processor executes Figure 1 The method described in the corresponding embodiment.
[0168] The present application also provides a computer program product, including a computer program / instruction, which, when executed by a processor, enables the processor to implement Figure 1 The method described in the corresponding embodiment.
[0169] like Figure 8 A schematic diagram of a database system provided in an embodiment of the present application. Figure 8 As can be seen in FIG, the system includes:
[0170] Encoder 81, used to encode the acquired natural language to obtain a semantic feature vector;
[0171] A decoder 82 is configured to decode the semantic feature vector output by the encoder to generate a target sentence;
[0172] A splitting module 83 is configured to split the acquired target sentence to obtain at least one target sub-sentence;
[0173] A correction module 84, configured to correct the keyword clause in the target sub-sentence according to the semantic constraint rules corresponding to the keyword type contained in the target sub-sentence;
[0174] a combining module 85 for combining the modified target sub-statements to generate executable machine language;
[0175] The database 86 is used to execute data query tasks based on the executable machine language.
[0176] In actual applications, the encoder 81 obtains a semantic feature vector obtained by encoding the natural language provided by the user; the decoder 82 is used to decode the semantic feature vector provided by the encoder 81 to obtain an unexecutable target sentence. Then, the splitting module 83 is used to split the target sentence to obtain at least one target sub-sentence. The at least one target sub-sentence is sent to the correction module 84, and the target sub-sentence is corrected according to the semantic constraint rules corresponding to the keyword type contained in the target sub-sentence sequence. The combination module 85 combines the corrected target sub-sentences to generate the machine language corresponding to the natural language. After receiving the executable machine language, the database 86 executes the corresponding data query task and feeds back the query results to the user. Of course, in actual applications, the encoder, decoder, splitting module, correction module and combination module can be integrated into one working node, and the database works as another separate node; of course, all modules and databases can also be integrated into the same local server or cloud server.
[0177] Based on the above embodiment, after the user provides natural language for query, the encoder can encode the natural language to obtain the semantic feature vector of the natural language. The language feature vector is further processed by the decoder to obtain the target sentence corresponding to the natural language that can be recognized by the database. The target sentence is split and processed to obtain a sequence containing at least one target sub-sentence. Each target sub-sentence contains keywords. When the target sub-sentence is corrected, the target sub-sentence is corrected based on the keyword type contained in the target sub-sentence and the corresponding semantic constraint rules, and the corrected target sub-sentences are combined to obtain machine language that can be recognized by the database. Through the above method, the accuracy of natural language escape and the work efficiency of users using database queries can be effectively improved, while reducing the technical ability requirements for staff using database queries.
[0178] Because each keyword has its own unique execution semantic constraints, these rules are determined by the characteristics of machine language and are not inherent in natural language, nor can they be directly derived through natural statement translation. Furthermore, since the rules lack strong regularity and the variety of keywords is vast, neural network models are unable to learn them. Therefore, to ensure the correct execution of SQL statements, we specifically modify the values in each keyword clause, ensuring more accurate corrections.
[0179] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, i.e., they may be located in one location or distributed across multiple network units. Some or all of the modules may be selected based on actual needs to achieve the objectives of the present embodiment. Persons of ordinary skill in the art will be able to understand and implement the present invention without inventive effort.
[0180] Through the description of the above embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus a necessary general hardware platform, or of course, by hardware. Based on this understanding, the essence of the above technical solution or the part that contributes to the existing technology can be embodied in the form of a software product. The computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a magnetic disk, an optical disk, etc., and includes a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or certain parts of the embodiments.
[0181] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A language escape method, comprising: Split the acquired target sentence to obtain at least one target sub-sentence; Determining the keyword type and keyword clause contained in the target sub-sentence; Modifying the keyword clause in the target sub-sentence according to the semantic constraint rules corresponding to the keyword type contained in the target sub-sentence; Combining the corrected target sub-statements to generate executable machine language; The step of splitting the acquired target sentence to obtain at least one target sub-sentence includes: using a decoder to decode a semantic feature vector obtained by encoding a natural language provided by a user to obtain a target sentence including keywords, operation content, and a predicted value; and splitting the target sentence according to a nested relationship to generate the at least one target sub-sentence without a nested relationship. The modifying of the keyword clause in the target sub-sentence according to the semantic constraint rules corresponding to the keyword type contained in the target sub-sentence includes: If the predicted value is a non-nested value, calculating a first similarity value between a first eigenvector of the predicted value and a second eigenvector of any value in a first data column in a data table provided by the database; If the first similarity value is greater than a first threshold, replacing the predicted value with the arbitrary value to obtain a modified target sub-sentence; If the similarity value is less than a first threshold, calculating a second similarity value between a third eigenvector of any value in any column other than the first data column in the data table and the first eigenvector; If the second similarity value is greater than a second threshold, any value in any column is replaced with the predicted value to obtain a modified target sub-sentence.
2. The method according to claim 1, wherein splitting the target sentence according to the nesting relationship to generate the at least one target sub-sentence without the nesting relationship comprises: Determining, based on a nesting identifier included in the target sentence, a first target sub-sentence and a second target sub-sentence that are in a nested relationship; wherein the second target sub-sentence is nested in the first target sub-sentence; Adding a position marker to the nested position where the nested marker is located in the first target sub-statement; A sequence consisting of the first target sub-statement and the second target sub-statement in a hierarchical order in a nested relationship is generated.
3. The method according to claim 1, after obtaining at least one target sub-statement, further comprising: The corresponding semantic constraint rule is determined according to the keyword type included in the target sub-sentence.
4. The method according to claim 1, when the keyword type is SELECT type, The decoder is used to decode the semantic feature vector obtained by encoding the natural language provided by the user to obtain a target sentence containing keywords, operation content and predicted value, including: Get the data table provided by the database; Performing a matching analysis between the operation content associated with the SELECT keyword obtained by decoding the semantic feature vector and the data column in the data table; Determining a target data column that matches the operation content from the data table according to the matching degree; Based on the data in the target data column, a target statement including a SELECT keyword, the operation content and the predicted value is generated.
5. The method according to claim 1, when the keyword type is a WHERE type; The decoder is used to decode the semantic feature vector obtained by encoding the natural language provided by the user to obtain a target sentence containing keywords, operation content and predicted value, including: Get the data table provided by the database; Filtering operation content from the data table according to the predicted value associated with the WHERE keyword obtained by decoding the semantic feature vector; According to the matching degree between the predicted value and the filtered operation content, the filtered operation content that matches the predicted value is determined to obtain a target statement including the WHERE keyword, the predicted value and the filtered operation content.
6. The method according to claim 3, wherein the modifying of the keyword clause in the target sub-sentence according to the semantic constraint rules corresponding to the keyword type contained in the target sub-sentence comprises: When the keyword type is the HAVING type, if the execution result when executing the operation content is a numerical constant, the predicted value in the HAVING clause is replaced with a constant.
7. The method according to claim 4 or 5, wherein the modifying each keyword clause in the target sub-sentence according to the semantic constraint rules corresponding to the keyword type contained in the target sub-sentence comprises: When the keyword type is an ORDER BY type, if other clauses in the target sub-statement except the ORDER BY clause contain aggregate functions, the ORDER BY clause is converted into a nested query statement.
8. The method according to claim 4 or 5, wherein the modifying each keyword clause in the target sub-sentence according to the semantic constraint rules corresponding to the keyword type contained in the target sub-sentence comprises: When the keyword type is GROUP BY type, if the SELECT clause does not contain the aggregate function and the column name of the data column corresponding to the GROUP BY clause, the GROUP BY column name is replaced with the primary key; If the primary key is not found, the data column corresponding to the SELECT clause is copied to the data column corresponding to the GROUP BY clause.
9. The method according to claim 4 or 5, further comprising: When the keyword type is FROM type, generating a directed graph based on the data table corresponding to the modified target sub-statement and the data columns therein; Based on the data table and the data columns in the directed graph, a minimum spanning tree is constructed with the data columns or foreign keys as root nodes.
10. The method according to claim 4 or 5, wherein combining the modified target sub-sentences comprises: If the SELECT clause and the WHERE clause in the modified target sub-statement have the same column name or primary and foreign key relationship, the modified target sub-statement is combined; or, If the modified SELECT clause and the WHERE clause in the target substatement do not have the same column name or primary-foreign key relationship, the column name corresponding to the WHERE clause is copied to the SELECT clause, and the modified target substatement is combined.
11. An electronic device comprising a memory and a processor; wherein: The memory is used to store programs; The processor is coupled to the memory, and is configured to execute the program stored in the memory to implement the method according to any one of claims 1 to 10.
12. A non-transitory machine-readable storage medium having executable code stored thereon, wherein when the executable code is executed by a processor of an electronic device, the processor is caused to execute the method according to any one of claims 1 to 10.
13. A database system, comprising: Encoder, used to encode the acquired natural language to obtain a semantic feature vector; A decoder, configured to decode the semantic feature vector output by the encoder to generate a target sentence; A splitting module, configured to split the acquired target sentence to obtain at least one target sub-sentence; a correction module, configured to correct the keyword clause in the target sub-sentence according to the semantic constraint rules corresponding to the keyword type contained in the target sub-sentence; A combination module, used for combining the corrected target sub-statements to generate executable machine language; A database, configured to execute data query tasks based on the executable machine language; The splitting module is specifically configured to: use a decoder to decode a semantic feature vector obtained by encoding a natural language provided by a user to obtain a target sentence containing keywords, operation content, and predicted values; split the target sentence according to a nested relationship to generate at least one target sub-sentence without a nested relationship; The correction module is specifically configured to: if the predicted value is a non-nested value, calculate a first similarity value between a first eigenvector of the predicted value and a second eigenvector of any value in a first data column in a data table provided by the database; If the first similarity value is greater than a first threshold, the predicted value is replaced by the arbitrary value to obtain a modified target sub-sentence; if the similarity value is less than the first threshold, a second similarity value between the third eigenvector of any value in any column of the data table other than the first data column and the first eigenvector is calculated; If the second similarity value is greater than a second threshold, any value in any column is replaced with the predicted value to obtain a modified target sub-sentence.
Citation Information
Patent Citations
Structured query language conversion method based on natural language, and related equipment thereof
CN111177184A
Method and equipment for escaping meaning from natural language to SQL (Structured Query Language) and storage medium
CN114265858A
Cited By
Language escape method and apparatus, and device and storage medium
WO2024017386A1