Method and device for generating SQL (Structured Query Language) based on natural language, and computer equipment

By identifying and converting logical operators in natural language and processing nested and negative logic, the problem of SQL query statements in the prior art is solved, and more flexible and accurate SQL generation is achieved, reducing the difficulty of writing. Improve work efficiency.

CN119106043APending Publication Date: 2024-12-10HAINAN DIGITAL MFG TECH CO LTD
View PDF 0 Cites 4 Cited by

Patent Information

Application Number
CN202411174610.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-26
Publication Date
2024-12-10

AI Technical Summary

Technical Problem

The existing SQL-generated method based on natural language has shortcomings in the identification and priority processing of logical operators, and it is difficult to handle complex logical operations and multi-conditional queries, resulting in the generated SQL query statements that do not match the user's intentions and cannot be deeply analyzed. The logical structure of layer nesting.

Method used

By identifying and converting logical operators in natural languages, such as AND, OR, NOT, and processing nested logic and negative logic, users can manually set the priority of logical operators, and build-in error detection and correction mechanisms to ensure the generated SQL Query statement accuracy and consistency.

Benefits of technology

Improves the flexibility and accuracy of SQL queries, enables non-professionals to easily express complex query needs, reduces the difficulty of writing SQL queries, and improves work efficiency and decision-making accuracy.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure FDA0005010370180000031
    Figure FDA0005010370180000031
  • Figure HDA0005010370210000011
    Figure HDA0005010370210000011
  • Figure HDA0005010370210000021
    Figure HDA0005010370210000021
Patent Text Reader

Abstract

The invention discloses a method and device for generating an SQL based on a natural language and computer equipment, and relates to the technical field of natural language processing, and the method comprises the following steps: S100, logic operator identification and conversion: identifying a logic operator in the natural language, converting the logic operator into a corresponding logic operator in the SQL, supporting multiple logic operators, and storing the logic operators in the SQL; comprising logic AND, logic OR and logic non-NOT, logic operators in a natural language are accurately identified and converted, and the priorities, nested logic and negative logic of the logic operators are processed, so that a user can express more complex and diversified query requirements by using the natural language, and the query efficiency is improved. According to the method, a simple logic relationship or a multi-layer nested complex logic combination can be accurately converted into the SQL query statement, so that the query flexibility and accuracy are improved, and non-professionals can easily obtain required information from a database.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of natural language processing, and in particular to a method, an apparatus, and a computer device for generating SQL based on natural language. Background Art

[0002] With the rapid development of information technology, databases have been widely used in various fields. Databases have become an important tool for enterprise management and decision support. For non-professionals, writing SQL query statements is a challenging task. Although there are many natural language-based query tools that can help non-technical users generate SQL statements, these tools are often limited to simple query scenarios and have difficulty handling complex logical operations and multi-condition query requirements.

[0003] However, the accuracy and comprehensiveness of the existing methods for generating SQL based on natural language in the recognition of logical operators need to be improved. They can only recognize common simple logical operators such as "and", "or" and "not". For some special logical expressions, that is, the logical relationship implied in the complex semantic association of "either...or...", it is often impossible to accurately recognize it. In addition, in the priority processing of logical operators, the existing technology usually adopts fixed priority rules and cannot be flexibly adjusted according to the actual needs of users. This results in the generated SQL query statements not being consistent with the user's query intention when processing complex logical combinations, making it difficult to deeply parse multi-layer nested logical structures, and unable to accurately convert complex natural language logical combinations into nested query statements in SQL.

[0004] In summary, the existing technology of generating SQL based on natural language has obvious shortcomings in processing complex logical operations, especially multi-conditional logical processing, and cannot meet the users' growing demand for flexible queries. This not only affects work efficiency, but also limits the wider application of databases in various fields. Therefore, it is of great practical significance and broad application prospects to develop a method, device, and computer equipment based on natural language generation of SQL that can efficiently and accurately process multi-conditional logical operations. Summary of the invention

[0005] The purpose of the present invention is to make up for the deficiencies of the prior art and to provide a method, device, and computer equipment for generating SQL based on natural language. The method, device, and computer equipment can accurately identify and convert complex logical operators in natural language, namely logical AND, logical OR, and logical NOT, and process complex nested logic and negation logic, so that users can use a more natural and flexible language to express complex query requirements, reduce the difficulty for non-professionals to write SQL queries, and make database queries more popular and efficient.

[0006] In order to solve the above technical problems, the present invention provides the following technical solutions: On the one hand, a method for generating SQL based on natural language, the method comprising the following steps:

[0007] S100, logical operator recognition and conversion: recognizes logical operators in natural language and converts them into corresponding logical operators in SQL, supporting multiple logical operators, including logical AND, logical OR, and logical NOT;

[0008] S200, logical operator priority processing: calculate and identify the priority of logical operators, generate SQL query statements that meet the user's query intent, and support manual setting of the priority of logical operators through brackets;

[0009] S300, nested logic processing: supports the nested use of logical operators, realizes complex logical combinations, and processes nested logic structures;

[0010] S400, negative logic processing: accurately process logical NOT operations, exclude specified conditions from query results, and support the combined use of multiple negative logics;

[0011] S500, Error Detection and Correction: Built-in error detection mechanism, identifies logical inconsistencies and invalid queries, automatically corrects them, provides error prompts, and guides users to make manual corrections.

[0012] Furthermore, the S100 logical operator recognition and conversion queries the input natural language, parses the query statement through natural language processing, identifies keywords and logical operators, performs word segmentation on the input natural language query statement, splits the sentence into individual words and phrases, and performs part-of-speech tagging. By analyzing the semantic relationship and context information between words, the logical operators present therein are identified. When the words "and" and "at the same time" are detected, they are identified as potential expressions of logical AND. For the expressions of "or" and "either...or...", they are identified as logical OR. For "is not" and "does not contain", they are identified as logical NOT. For the identified natural language logical operators, they are converted into corresponding logical operators in SQL, that is, "and" is converted into "AND", "or" is converted into "OR", and "is not" is converted into "NOT". The conversion is performed by combining multiple simple logical operators. After completing the recognition and conversion of the logical operators, for the situation where multiple logical operators exist at the same time, the priority of the logical operators is processed.

[0013] Furthermore, the specific steps of the S200 logical operator priority processing are:

[0014] S201, constructing a priority identification algorithm: based on natural language processing and logic operation rules, automatically analyzing and identifying the priority of logic operators in natural language, and determining the execution order of each logic operator in the query statement by parsing the contextual relationship between the logic operators and combining the logic operation priority rules;

[0015] S202, automatic priority determination: after the logical operator recognition and conversion in S100 are completed, the priority identification algorithm constructed in S201 is used to determine the priority of the converted SQL logical operators, including analyzing the hierarchical structure of the logical operators, so that the logical operation order in the generated SQL query statement meets the query intention of the user;

[0016] S203, bracket priority processing: supports users to manually set the priority of logical operators by entering brackets. When brackets are detected in user input, it can identify the logical operation in the brackets as the highest priority, and adjust the SQL generation strategy accordingly, so that the logical operation in the brackets is executed before the logical operation outside the brackets;

[0017] S204, priority adjustment and SQL generation: according to the results of S202 and S203, the priority of the logical operator is adjusted, and combined with the SQL logical operator converted in S100, an SQL query statement that complies with the priority rule is generated.

[0018] Furthermore, the step S201 of constructing a priority identification algorithm includes:

[0019] S201.1, Logical Operator Context Analysis: By performing context analysis on logical operators in natural language, the context position of each logical operator and the relationship between adjacent words are determined;

[0020] S201.2, Logical Operator Weight Assignment: According to the type and context position of the logical operator, a weight value is assigned to each logical operator. The weight value reflects the importance of the logical operator and thus affects its execution order in the SQL query statement, i.e., W i =α·f(Type i )+β·g(Position i ), where W i represents the weight value of the i-th logical operator. α and β are adjustment factors used to balance the importance of the type and position of the logical operator. i ) is a function of the logical operator type. Different logical operators have different scores. Position i It is a function of the position of the logical operator and the score is calculated based on the position of the logical operator.

[0021] Furthermore, the step S201 of constructing a priority identification algorithm further includes:

[0022] S201.3 defines a set of priority rules based on the execution order of weight values, which stipulates the default priority order of different logical operators, namely NOT>AND>OR, and allows users to explicitly define the priority through parentheses. For the NOT operator: f(Type i )=3, for AND operator: f(Type i )=2, for OR operator: f(Type i )=1, then the logical operator is inside the brackets: g(Position i )=3, the logical operator is outside the parentheses but adjacent to them: g(Position i )=2, logical operators are far away from brackets: g(Position i )=1, the default priority order is that the NOT operator has the highest priority, the AND operator has the second highest priority, and the OR operator has the lowest priority. The logical operators in parentheses have a higher priority than those outside the parentheses.

[0023] S201.4, Dynamic Priority Adjustment: Dynamically adjust the priority of logical operators according to the weight values ​​and priority rules of logical operators so that the generated SQL query statements meet the user's query intent. The priority adjustment formula is: P i represents the priority of the i-th logical operator, W i represents the weight value of the i-th logical operator, and n is the number of logical operators.

[0024] Furthermore, the nested logic processing of S300 divides the input natural language query statement into logical units through word segmentation and part-of-speech tagging, analyzes each logical unit, identifies the logical operators and their combinations therein, and for each layer of nested logic identified, applies the logical operator identification and conversion in S100 within the layer, converts the logical operators in the natural language into corresponding logical operators in SQL, keeps the relative relationship between the logical operators in the same layer unchanged, and combines the logical operator priority processing mechanism in S200 to prioritize the logical operators in each nested layer. For cross-layer logical operators, ensure that the priority of the outer logical operator is higher than that of the inner logical operator. Based on the converted logical operators and the sorting results, build the SQL nested query statement layer by layer, and generate it step by step from the inner layer to the outer layer, so that each layer of query can be correctly nested in the previous layer of query. For each logical unit, generate the corresponding SQL subquery statement according to its priority and nesting level.

[0025] Furthermore, the S400 negative logic processing performs word segmentation and part-of-speech tagging on the input natural language query statement, identifies negative logic vocabulary, converts the identified negative logic vocabulary into the logical negation operation in SQL, converts it into NOT, analyzes the logical relationship between multiple negative logics, and combines the results of the S300 nested logic processing to process the negative logic in the nested logic structure. For the query statement, the logic within the brackets is processed first, and then the logic outside the brackets is processed. According to the results of the S200 logical operator priority processing, the priority of the negative logic is adjusted to make the priority relationship between the negative logic NOT and the AND and OR logical operators correct.

[0026] In a second aspect, a device for generating SQL based on natural language, the components of the device include: a logical operator recognition and conversion module, a logical operator priority processing module, a nested logic processing module, a negation logic processing module, and an error detection and correction module;

[0027] The logical operator recognition and conversion module is used to recognize logical operators in natural language and convert them into corresponding logical operators in SQL, supporting multiple logical operators, including AND, OR, and NOT;

[0028] The logic operator priority processing module calculates and identifies the priority of the logic operator, generates an SQL query statement that meets the user's query intent, and supports manually setting the priority of the logic operator through brackets;

[0029] The nested logic processing module supports the nested use of logic operators, realizes complex logic combinations, and processes nested logic structures;

[0030] The negation logic processing module accurately processes logical NOT operations, excludes specified conditions in the query results, and supports the combined use of multiple negation logics;

[0031] The error detection and correction module has a built-in error detection mechanism to identify logical inconsistencies and invalid queries, and automatically correct them, provide error prompts, and guide users to make manual corrections.

[0032] Furthermore, the error detection and correction module has a built-in error detection mechanism, specifically including:

[0033] Logical consistency check: Check the logical consistency principle of the generated SQL statements to make the logical structure of the statements reasonable;

[0034] Syntax validity verification: Verify that the generated SQL statement complies with SQL syntax rules, including keyword spelling and grammatical structure;

[0035] Logical operator priority confirmation: according to the priority processing result of the logical operator priority processing module, the logical operator priority in the SQL statement is correct;

[0036] Nested logic structure verification: based on the processing results of the nested logic processing module, ensure the correctness of the nested logic structure;

[0037] Negative logic processing verification: Based on the processing results of the negative logic processing module, ensure the correctness of the negative logic in the SQL statement;

[0038] User manual correction support: Allow users to manually correct SQL statements based on error prompts to make query results more accurate.

[0039] In a third aspect, a computer device for generating SQL based on natural language is provided, wherein the computer device comprises a processor, a memory, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the method for generating SQL based on natural language of claims 1 to 7 is implemented.

[0040] Compared with the prior art, a method, device, and computer device for generating SQL based on natural language have the following beneficial effects:

[0041] 1. The present invention enables users to express more complex and diverse query requirements in natural language by accurately identifying and converting logical operators in natural language, as well as processing logical operator priorities, nested logic, and negation logic. Whether it is a simple logical relationship or a complex logical combination of multiple layers of nesting, it can be accurately converted into an SQL query statement, thereby improving the flexibility and accuracy of the query, allowing non-professionals to easily obtain the required information from the database, and avoiding query errors caused by unfamiliarity with SQL syntax.

[0042] 2. The present invention provides a function for manually setting the priority of logical operators through brackets. Users can accurately control the query logic according to their special query needs. At the same time, the combination support of multiple negative logics enables users to more carefully exclude unnecessary query results. This flexibility enables the present invention to adapt to various query scenarios, meet users' special needs for complex queries, and improve work efficiency and decision-making accuracy.

[0043] Other advantages, objectives and features of the present invention will be set forth in part in the following description and, in part, will be apparent to those skilled in the art based on an examination of the following or may be taught from the practice of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS

[0044] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the prior art descriptions are briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention, and for ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0045] Figure 1 The present invention is an operation flow chart of a method for generating SQL based on natural language;

[0046] Figure 2 The present invention is a structural diagram of a device for generating SQL based on natural language. DETAILED DESCRIPTION

[0047] The technical solutions in the embodiments of the present invention are described clearly and completely below. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0048] Embodiment 1

[0049] A method for generating SQL based on natural language. The method can handle complex logical operations, including logical AND, logical OR, and logical NOT. Users can express more flexible and diverse query requirements through natural language without having to understand the specific SQL syntax details, thereby improving the accuracy and efficiency of queries.

[0050] First, the logical operator identification and conversion stage (S100) is entered. Starting from the natural language query sentence input by the user, the natural language processing technology is used to perform word segmentation processing, the sentence is split into individual words and phrases, and part-of-speech tagging is performed. The semantic relationship and context information between the words are analyzed, and the logical operators are accurately identified, that is, "and" is converted to "AND" in SQL, "or" is converted to "OR", and "not" is converted to "NOT". For complex logical expressions, that is, "either...or...", it can also be accurately identified and converted. After the identification is completed, the situation where multiple logical operators exist at the same time is further processed to ensure the accuracy and consistency of the logical operation. For example, the user enters a natural language query sentence: "Query the employee information whose age is greater than 30 years old and whose gender is male or whose position is manager". The sentence is segmented and part-of-speech tagged, and then the semantic relationship and context information between the words are analyzed to identify the logical operators "and" (logical AND) and "or" (logical OR). At the same time, the keywords "age greater than 30 years old", "gender is male", and "position is manager" are identified, and "and" is converted to "AND" in SQL, and "or" is converted to "OR".

[0051] Then, the logic operator priority processing stage (S200) is entered. After the logic operator recognition and conversion are completed, a priority recognition algorithm is constructed. The algorithm automatically analyzes and recognizes the priority of the logic operators based on natural language processing and logic operation rules. The algorithm first analyzes the contextual relationship between the logic operators, and combines the logic operation priority rules (such as NOT>AND>OR) to determine the execution order of each logic operator. At the same time, it supports users to manually set the priority of the logic operator by entering brackets to ensure that the generated SQL query statement fully meets the user's query intention. For example, it is determined that the priority of "and" is higher than that of "or". According to the situation where there are no brackets in the user input, it is processed according to the default priority order, that is, the logic of "age is greater than 30 years old and gender is male" is processed first, and then the "or" operation is performed with "position is manager".

[0052] Subsequently, the nested logic processing stage (S300) is entered to support more complex query requirements and realize the processing of nested logic and negative logic. In the nested logic processing stage, the natural language query statement is divided into multiple logical units, and the logical operators and their combinations are analyzed layer by layer. For each layer of nested logic, logical operators are applied within the layer for identification and conversion, and the logical relationship between multiple logics is processed at the same time to ensure that the priority and logical relationship of the negative logic are correct. In this embodiment, the user's query statement does not involve nested logic, so no special processing is required. However, if there is nested logic in the query statement, such as "querying employee information (age greater than 30 years old and gender is male) or (position is manager and working years are greater than 5 years)", it is divided into logical units, each logical unit is analyzed, and after identifying the nested logic, it is processed from the innermost layer to the outside step by step to ensure that the logical operation of each layer can be correctly reflected in the generated SQL statement.

[0053] Secondly, enter the negative logic processing stage (S400). In the semantic analysis process, natural language processing is used to identify negative words or phrases in the input natural language query statement, such as "does not contain", "is not", "none", etc., as well as the entities, attributes or conditional expressions modified by them. These negative words and phrases are marked as negative logic tags. According to the preset negative logic mapping rules, the identified negative logic tags are converted into the logical negation operator (NOT) in the SQL query statement. At the same time, the scope affected by the negative logic is analyzed to ensure that the logical negation operator is correctly applied to the corresponding query condition or subquery. In the process of constructing the SQL query statement, the converted logical negation operator (NOT) is embedded in the corresponding position. For queries containing multiple negative logics, the correct execution order of the negative logic is ensured according to the logical operator priority processing mechanism. In this embodiment, there is no negative expression in the user's query statement, so this step does not need to be specially processed. However, if the user enters negative logic, such as "query does not contain employee information under the age of 25", the negative logic word "does not contain" will be identified and converted into the logical negation operation "NOT" in SQL, and the corresponding logical negation condition will be added to the generated SQL query statement.

[0054] Finally, the error detection and correction stage (S500) is entered. In order to ensure the accuracy and effectiveness of the generated SQL query statement, the method also has a built-in error detection and correction mechanism, which includes multiple aspects such as logic consistency check, syntax validity verification, logical operator priority confirmation, nested logic structure verification and negative logic processing verification. After the SQL query statement is generated, these checks will be automatically performed to identify logical inconsistencies and invalid queries. Once an error is found, an error prompt is provided and automatic correction is attempted. If the automatic correction fails, the user is guided to perform manual correction to ensure the accuracy of the query result.

[0055] In summary, this embodiment provides a method for generating SQL based on natural language, which can accurately identify logical operators, keywords and logical relationships in natural language query statements input by users, and convert natural language query statements into accurate SQL query statements through steps such as logical operator identification and conversion, logical operator priority processing, nested logic processing, negative logic processing, and error detection and correction. This method improves the flexibility and accuracy of queries, allowing non-professionals to easily obtain required information from the database through natural language, avoiding query errors caused by unfamiliarity with SQL syntax. At the same time, this method can adapt to various complex query requirements and improve work efficiency and decision-making accuracy.

[0056] Embodiment 2

[0057] This embodiment provides a device for generating SQL based on natural language. The components of this device include: a logical operator recognition and conversion module, a logical operator precedence processing module, a nested logic processing module, a negation logic processing module, and an error detection and correction module. Through natural language processing technology, the input natural language query statement is tokenized, part-of-speech tagged, and the keywords and logical operators in the query statement are parsed, and these natural language logical operators are converted into the corresponding logical operators in SQL.

[0058] In specific implementation, the input natural language query statement is tokenized, splitting the sentence into individual words or phrases, and each word or phrase is part-of-speech tagged to identify their respective parts of speech (such as nouns, verbs, conjunctions, etc.). Stop words that are not helpful for logical operation recognition, such as "de" (的) and "le" (了), are removed. A thesaurus containing common logical operators and their variants is established, such as "and", "or", "not", "either...or...". Using natural language processing technology, semantic analysis is performed on the tokenized result to identify the logical operators and their context relationships in the sentence. For complex logical relationships implicit in the sentence, they are identified through context understanding and semantic reasoning. A logical operator mapping table is constructed to map the logical operators in natural language to the corresponding operators in SQL (for example, "and" is mapped to "AND", "or" is mapped to "OR", "not" is mapped to "NOT", etc.). According to the mapping table, the identified natural language logical operators are converted into the logical operators in SQL. For complex logical expressions, they are converted into combinations of multiple SQL logical operators.

[0059] After receiving the output of the logical operator recognition and conversion module, the precedence recognition algorithm is started. First, context analysis of logical operators is performed to determine the context position of each logical operator and the relationship with adjacent words. For example, in this example, it is determined that the precedence of "and" is higher than that of "or". Weight assignment for logical operators is carried out. According to the type and context position of the logical operator, a weight value is assigned to each logical operator. The calculation formula for the weight value is: W i = α·f(Type i ) + β·g(Position i ), where W i represents the weight value of the i-th logical operator, α and β are adjustment factors used to balance the importance of the logical operator type and position, f(Type i ) is a function of the logical operator type, different logical operators have different scores, and Position iIt is a function of the position of the logical operator. The score is calculated based on the position of the logical operator. A set of priority rules are defined based on the execution order of the weight value. The default priority order of different logical operators is NOT>AND>OR. At the same time, users are allowed to explicitly define the priority through parentheses. For the NOT operator, f(Type i )=3, for AND operator: f(Type i )=2, for OR operator: f(Type i )=1, then the logical operator is inside the brackets: g(Position i )=3, the logical operator is outside the parentheses but adjacent to them: g(Position i )=2, logical operators are far away from brackets: g(Position i )=1, according to the weight value and priority rule of the logical operator, dynamically adjust the priority of the logical operator so that the generated SQL query statement meets the user's query intention. The priority adjustment formula is: P i represents the priority of the i-th logical operator, W i represents the weight value of the i-th logical operator, and n is the number of logical operators.

[0060] If the user's query statement involves nested logic, the nested logic processing module will divide the input natural language query statement into logical units through word segmentation and part-of-speech tagging, analyze each logical unit, identify the logical operators and their combinations, and for each layer of nested logic identified, apply the function of the logical operator identification and conversion module within the layer to convert the logical operators in the natural language into the corresponding logical operators in SQL, keeping the relative relationship between the logical operators in the same layer unchanged. Combined with the mechanism of the logical operator priority processing module, the logical operators in each nested layer are prioritized. For cross-layer logical operators, ensure that the priority of the outer logical operator is higher than that of the inner logical operator. Based on the converted logical operators and sorting results, the SQL nested query statement is constructed layer by layer, and is generated step by step from the inner layer to the outer layer, so that each layer of query can be correctly nested in the previous layer of query. For each logical unit, the corresponding SQL subquery statement is generated according to its priority and nesting level.

[0061] Secondly, the negative logic processing module performs word segmentation and part-of-speech tagging on the input natural language query statements, identifies negative logic vocabulary such as "does not include" and "exclude", and converts them into the logical NOT operation in SQL, analyzes the logical relationship between multiple negative logics, and combines the results of the nested logic processing module to process the negative logic in the nested logic structure. For query statements, the logic within the brackets is processed first, and then the logic outside the brackets. According to the results of the logical operator priority processing module, the priority of the negative logic is adjusted to make the priority relationship between the negative logic NOT and the AND and OR logical operators correct.

[0062] Finally, the error detection and correction module has a built-in error detection mechanism, including logical consistency check, grammatical validity verification, logical operator priority confirmation, nested logical structure verification and negative logic processing verification, etc. It checks the logical consistency principle of the generated SQL statement to make the logical structure of the statement reasonable and verifies that the generated SQL statement conforms to the SQL grammar rules, including keyword spelling and grammatical structure. It ensures that the logical operator priority in the SQL statement is correct based on the priority processing results of the logical operator priority processing module, ensures the correctness of the nested logical structure based on the processing results of the nested logic processing module, and ensures the correctness of the negative logic in the SQL statement based on the processing results of the negative logic processing module. At the same time, it allows users to manually correct SQL statements according to error prompts to make query results more accurate.

[0063] In summary, the device for generating SQL based on natural language can effectively process natural language query statements. Through the collaborative work of various modules, it can accurately identify logical operators, process priorities, process nested logic and negative logic, and perform error detection and correction, and finally generate SQL query statements that meet the user's intentions. This device improves the convenience and accuracy of users' query data and is suitable for various scenarios that need to obtain information from the database.

[0064] It will be apparent to those skilled in the art that the invention is not limited to the details of the exemplary embodiments described above and that the invention can be implemented in other specific forms without departing from the spirit or essential features of the invention. Therefore, the embodiments should be considered exemplary and non-limiting in all respects, and the scope of the invention is defined by the appended claims rather than the foregoing description, and it is intended that all variations falling within the meaning and scope of the equivalent elements of the claims be included in the invention. Any reference numeral in a claim should not be considered as limiting the claim to which it relates.

Claims

1. A method for generating SQL based on natural language, characterized in that: The method comprises the following steps: S100, logical operator recognition and conversion: recognizes logical operators in natural language and converts them into corresponding logical operators in SQL, supporting multiple logical operators, including logical AND, logical OR, and logical NOT; S200, logical operator priority processing: calculate and identify the priority of logical operators, generate SQL query statements that meet the user's query intent, and support manual setting of the priority of logical operators through brackets; S300, nested logic processing: supports the nested use of logical operators, realizes complex logical combinations, and processes nested logic structures; S400, negative logic processing: accurately process logical NOT operations, exclude specified conditions from query results, and support the combined use of multiple negative logics; S500, Error Detection and Correction: Built-in error detection mechanism, identifies logical inconsistencies and invalid queries, automatically corrects them, provides error prompts, and guides users to make manual corrections.

2. The method for generating SQL based on natural language according to claim 1, characterized in that: The S100 logical operator identification and conversion queries the input natural language, parses the query statement through natural language processing, identifies keywords and logical operators, performs word segmentation on the input natural language query statement, splits the sentence into individual words and phrases, and performs part-of-speech tagging. By analyzing the semantic relationship and context information between words, the logical operators present therein are identified. When the words "and" and "at the same time" are detected, they are identified as potential expressions of logical AND. For expressions of "or" and "either...or...", they are identified as logical OR. For "is not" and "does not contain", they are identified as logical NOT. For the identified natural language logical operators, they are converted into corresponding logical operators in SQL, that is, "and" is converted into "AND", "or" is converted into "OR", and "is not" is converted into "NOT". The conversion is performed by combining multiple simple logical operators. After completing the identification and conversion of the logical operators, for the case where multiple logical operators exist at the same time, the priority of the logical operators is processed.

3. The method for generating SQL based on natural language according to claim 1, characterized in that: The specific steps of the S200 logical operator priority processing are: S201, constructing a priority identification algorithm: based on natural language processing and logic operation rules, automatically analyzing and identifying the priority of logic operators in natural language, and determining the execution order of each logic operator in the query statement by parsing the contextual relationship between the logic operators and combining the logic operation priority rules; S202, automatic priority determination: after the logical operator recognition and conversion in S100 are completed, the priority identification algorithm constructed in S201 is used to determine the priority of the converted SQL logical operators, including analyzing the hierarchical structure of the logical operators, so that the logical operation order in the generated SQL query statement meets the query intention of the user; S203, bracket priority processing: supports users to manually set the priority of logical operators by entering brackets. When brackets are detected in user input, it can identify the logical operation in the brackets as the highest priority, and adjust the SQL generation strategy accordingly, so that the logical operation in the brackets is executed before the logical operation outside the brackets; S204, priority adjustment and SQL generation: according to the results of S202 and S203, the priority of the logical operator is adjusted, and combined with the SQL logical operator converted in S100, an SQL query statement that complies with the priority rule is generated.

4. The method for generating SQL based on natural language according to claim 3, characterized in that: The step S201 of constructing a priority identification algorithm includes: S201.1, Logical Operator Context Analysis: By performing context analysis on logical operators in natural language, the context position of each logical operator and the relationship between adjacent words are determined; S201.2, Logical Operator Weight Assignment: According to the type and context position of the logical operator, a weight value is assigned to each logical operator. The weight value reflects the importance of the logical operator and thus affects its execution order in the SQL query statement, i.e., W i =αf(Type i )+β·g(Position i ), where W i represents the weight value of the i-th logical operator. α and β are adjustment factors used to balance the importance of the type and position of the logical operator. i ) is a function of the logical operator type. Different logical operators have different scores. Position i It is a function of the position of the logical operator and the score is calculated based on the position of the logical operator.

5. The method for generating SQL based on natural language according to claim 4, characterized in that: The step S201 of constructing a priority identification algorithm further includes: S201.3 defines a set of priority rules based on the execution order of weight values, which stipulates the default priority order of different logical operators, namely NOT>AND>OR, and allows users to explicitly define the priority through parentheses. For the NOT operator: f(Type i )=3, for AND operator: f(Type i )=2, for OR operator: f(Type i )=1, then the logical operator is inside the brackets: g(Position i )=3, the logical operator is outside the parentheses but adjacent to them: g(Position i )=2, logical operators are far away from brackets: g(Position i )=1, the default priority order is that the NOT operator has the highest priority, the AND operator has the second highest priority, and the OR operator has the lowest priority. The logical operators in parentheses have a higher priority than those outside the parentheses. S201.4, Dynamic Priority Adjustment: Dynamically adjust the priority of logical operators according to the weight values ​​and priority rules of logical operators so that the generated SQL query statements meet the user's query intent. The priority adjustment formula is: P i represents the priority of the i-th logical operator, W i represents the weight value of the i-th logical operator, and n is the number of logical operators.

6. The method for generating SQL based on natural language according to claim 1, characterized in that: The S300 nested logic processing divides the input natural language query statement into logical units through word segmentation and part-of-speech tagging, analyzes each logical unit, identifies the logical operators and their combinations therein, and for each layer of nested logic identified, applies the logical operator identification and conversion in S100 within the layer to convert the logical operators in the natural language into corresponding logical operators in SQL, keeping the relative relationship between the logical operators in the same layer unchanged, and combines the logical operator priority processing mechanism in S200 to prioritize the logical operators in each nested layer. For cross-layer logical operators, ensure that the priority of the outer logical operator is higher than that of the inner logical operator. Based on the converted logical operators and the sorting results, build the SQL nested query statement layer by layer, and generate it step by step from the inner layer to the outer layer, so that each layer of query can be correctly nested in the previous layer of query. For each logical unit, generate the corresponding SQL subquery statement according to its priority and nesting level.

7. The method for generating SQL based on natural language according to claim 1, characterized in that: The S400 negative logic processing performs word segmentation and part-of-speech tagging on the input natural language query statement, identifies negative logic vocabulary, converts the identified negative logic vocabulary into the logical negation operation in SQL, converts it into NOT, analyzes the logical relationship between multiple negative logics, and combines the results of the S300 nested logic processing to process the negative logic in the nested logic structure. For the query statement, the logic within the brackets is processed first, and then the logic outside the brackets is processed. According to the results of the S200 logical operator priority processing, the priority of the negative logic is adjusted to make the priority relationship between the negative logic NOT and the AND and OR logical operators correct.

8. A device for generating SQL based on natural language, characterized in that: The components of the device include: a logic operator identification and conversion module, a logic operator priority processing module, a nested logic processing module, a negation logic processing module, and an error detection and correction module; The logical operator recognition and conversion module is used to recognize logical operators in natural language and convert them into corresponding logical operators in SQL, supporting multiple logical operators, including AND, OR, and NOT; The logic operator priority processing module calculates and identifies the priority of the logic operator, generates an SQL query statement that meets the user's query intent, and supports manually setting the priority of the logic operator through brackets; The nested logic processing module supports the nested use of logic operators, realizes complex logic combinations, and processes nested logic structures; The negation logic processing module accurately processes logical NOT operations, excludes specified conditions in the query results, and supports the combined use of multiple negation logics; The error detection and correction module has a built-in error detection mechanism to identify logical inconsistencies and invalid queries, and automatically correct them, provide error prompts, and guide users to make manual corrections.

9. The device for generating SQL based on natural language according to claim 8, characterized in that: The error detection and correction module has a built-in error detection mechanism, which specifically includes: Logical consistency check: Check the logical consistency principle of the generated SQL statements to make the logical structure of the statements reasonable; Syntax validity verification: Verify that the generated SQL statement complies with SQL syntax rules, including keyword spelling and grammatical structure; Logical operator priority confirmation: according to the priority processing result of the logical operator priority processing module, the logical operator priority in the SQL statement is correct; Nested logic structure verification: based on the processing results of the nested logic processing module, ensure the correctness of the nested logic structure; Negative logic processing verification: Based on the processing results of the negative logic processing module, ensure the correctness of the negative logic in the SQL statement; User manual correction support: Allow users to manually correct SQL statements based on error prompts to make query results more accurate.

10. A computer device for generating SQL based on natural language, characterized in that: The computer device comprises a processor, a memory, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the method for generating SQL based on natural language according to claims 1 to 7 is implemented.

Citation Information

Cited By

  • Computing method for dynamically generating complex expression based on natural language processing

    CN120523895A

  • Computational method for dynamically generating complex expressions based on natural language processing

    CN120523895B

  • Self-adaptive AI output correction query condition conversion method and device

    CN121764953A

  • A query condition conversion method and device for adaptive AI output correction

    CN121764953B