Method for converting natural language query to SQL and device thereof

KR103004027B1Active Publication Date: 2026-08-1442 MARU INC
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
KR1020240174300
Authority / Receiving Office
KR · KR
Patent Type
Patents
Current Assignee / Owner
Filing Date
2024-11-29
Publication Date
2026-08-14
Estimated Expiration
2041-11-29

Smart Images

  • Figure 112024132164388-PAT00001_ABST
    Figure 112024132164388-PAT00001_ABST
Patent Text Reader

Abstract

A method for generating an SQL conversion model that converts a natural language query into SQL comprises: a step of labeling each keyword included in a second natural language query with a first tag indicating that the keyword is meaningless in the conversion to SQL or a second tag indicating that the keyword is meaningful in the conversion to SQL; a step of converting the second natural language query into a second SQL query using the SQL conversion model based on the second natural language query, the tags labeled for each keyword, and a DB schema; a step of processing the second SQL query with incorrect syntax so that it is not used for database search if the syntax of the second SQL query is incorrect; and a step of training the SQL conversion model based on a loss value calculated by comparing the second SQL query with correct syntax and a third SQL query corresponding to the second natural language query if the syntax of the second SQL query is correct.
Need to check novelty before this filing date? Find Prior Art

Description

Technology Field

[0001] The present invention relates to a method and apparatus for converting natural language queries into SQL, and more specifically, to a method and apparatus for improving accuracy through preprocessing and postprocessing of an NL2SQL model that generates SQL through natural language queries. Background Technology

[0002] Recently, Natural Language to SQL (NL2SQL) technology has emerged, which converts natural language queries into SQL, the management language of relational databases. This enables users without specialized database knowledge to more conveniently explore information within databases using natural language. However, NL2SQL has only recently appeared, and due to the high technical difficulty, it is not highly accurate for application in actual services. Furthermore, there are difficulties in implementation, such as high difficulty and high costs, as building training data for NL2SQL requires specialized knowledge and time. The problem to be solved

[0004] The present invention aims to solve the aforementioned problems by converting natural language queries into SQL for database searching.

[0005] In addition, the present invention aims to improve the accuracy of the SQL conversion process of natural language queries through preprocessing and postprocessing of training data. means of solving the problem

[0006] A method for generating an SQL conversion model that converts a natural language query into SQL comprises: a step of labeling each keyword included in a second natural language query with a first tag indicating that the keyword is meaningless in the conversion to SQL or a second tag indicating that the keyword is meaningful in the conversion to SQL; a step of converting the second natural language query into a second SQL query using the SQL conversion model based on the second natural language query, the tags labeled for each keyword, and a DB schema; a step of processing the second SQL query with incorrect syntax so that it is not used for database search if the syntax of the second SQL query is incorrect; and a step of training the SQL conversion model based on a loss value calculated by comparing the second SQL query with correct syntax and a third SQL query corresponding to the second natural language query if the syntax of the second SQL query is correct.

[0007] An apparatus for generating an SQL conversion model that converts a natural language query into SQL comprises: a memory for storing instructions; and one or more processors for executing said instructions, wherein the one or more processors are configured to label each keyword included in the second natural language query with a first tag indicating that the keyword is meaningless in the conversion to SQL or a second tag indicating that the keyword is meaningful in the conversion to SQL, and to convert the second natural language query into a second SQL query using said SQL conversion model based on the second natural language query, the tags labeled on each keyword, and a DB schema, and if the grammar of said second SQL query is inappropriate, process said second SQL query with inappropriate grammar so as not to be used for database search, and if the grammar of said second SQL is appropriate, train said SQL conversion model based on a loss value calculated by comparing said second SQL query with appropriate grammar and a third SQL query corresponding to said second natural language query. Effects of the invention

[0008] According to the present invention as described above, natural language queries can be converted into SQL for database searching, and the accuracy of the natural language query SQL conversion process can be improved through preprocessing and postprocessing of training data. Brief explanation of the drawing

[0009] FIG. 1 is a diagram illustrating the approximate configuration of a natural language query SQL conversion device according to an embodiment of the present invention. FIG. 2 is a flowchart for performing a method for converting a natural language query into SQL according to an embodiment of the present invention, FIG. 3 is a flowchart for generating an SQL conversion model that performs SQL conversion of a natural language query according to an embodiment of the present invention. Specific details for implementing the invention

[0010] The aforementioned objectives, features, and advantages are described in detail below with reference to the attached drawings, thereby enabling a person skilled in the art to easily implement the technical concept of the present invention. In describing the present invention, detailed descriptions of known technologies related to the present invention are omitted if it is determined that such descriptions would unnecessarily obscure the essence of the invention.

[0011] Identical reference numerals in the drawings are used to denote identical or similar components, and all combinations described in the specification and claims may be combined in any manner. Furthermore, unless otherwise specified, it should be understood that references to the singular may include one or more, and references to singular expressions may also include plural expressions.

[0012] The terms used in this specification are intended solely to describe specific exemplary embodiments and are not intended to be limiting. Singular expressions as used in this specification may also be intended to include a plural meaning unless explicitly otherwise indicated in the text. The terms “and / or” and “and / or” include all combinations and any one of the items listed in relation thereto. Terms such as “include,” “include,” “included,” “composed,” “having,” “possessing,” etc., have connotative meanings and, accordingly, specify the described features, integers, steps, actions, elements, and / or components, and do not exclude the presence or addition of one or more other features, integers, steps, actions, elements, components, and / or groups thereof. The steps, processes, and actions of the method described in this specification should not be interpreted as requiring that they be performed in the specific order discussed or illustrated, unless the specific order of execution is determined. It should also be understood that additional or alternative steps may be used.

[0013] In addition, each component can be implemented as a hardware processor, the above components can be integrated to be implemented as a single hardware processor, or the above components can be combined to be implemented as multiple hardware processors.

[0014] Hereinafter, preferred embodiments according to the present invention will be described in detail with reference to the attached drawings.

[0015] FIG. 1 is a diagram showing the approximate configuration of a natural language query SQL conversion device according to an embodiment of the present invention. Referring to FIG. 1, the SQL conversion device may include a query receiving unit (100), an SQL generating unit (200), an SQL conversion model generating unit (300), and a database (400).

[0016] The query receiving unit (100) can receive a first natural language query from a user. The query receiving unit (100) can receive a first natural language query that the user wants to search for in a database through a user terminal such as a smart terminal or a PC.

[0017] The SQL generation unit (200) can generate a first SQL based on a first natural language query received from a user. The SQL generation unit (200) can generate a first SQL based on the first natural language query through an SQL conversion model, which is an NL2SQL model that converts the natural language query into SQL. The first natural language query and the first SQL used at this time can be further used as training data to train the SQL conversion model.

[0018] The SQL conversion model generation unit (300) can generate an SQL conversion model that enables the natural language query in the SQL generation unit (200) to be converted into SQL. The SQL conversion model generation unit (300) may include a training data management unit (310), an SQL conversion unit (320), an SQL verification unit (330), and a training data expansion unit (340).

[0019] The training data management unit (310) is for managing training data for training a SQL conversion model and can label each of the second natural language queries included in the training data. The training data includes a second user query, a DB schema, and a third SQL corresponding to the second user query, and may further include tags labeled below.

[0020] Specifically, the learning data management unit (310) can extract multiple keywords included in the second natural language query and label tags corresponding to each keyword. The tags used in one embodiment of the present invention include at least one of 0, SA, SC-NUM / TEXT, WO, WV, and WC-NUM / TEXT. The 0 tag represents a keyword that is meaningless in SQL conversion, the SA tag represents a keyword corresponding to a SELECT clause function in SQL, the SC-NUM / TEXT tag represents a keyword corresponding to a SELECT clause table column and type in SQL, the WO tag represents a keyword corresponding to a WHERE clause operator in SQL, the WV tag represents a keyword corresponding to a WHERE clause data in SQL, and the WC-NUM / TEXT tag represents a keyword corresponding to a WHERE clause table column and type in SQL.

[0021] In this case, SC-NUM / TEXT and WC-NUM / TEXT will be used as SC-NUM, SC-TEXT, WC-NUM, and WC-TEXT depending on the type of the table column.

[0022] For example, in the case of training data containing a second user query such as “What was the average Game, when the attendance was higher than 600?”, the training data management unit (310) can label What, was, the, when, the, and was with 0 tags, average with SA tags, Game with SC-NUM tags, attendance with WC-NUM tags, higher and than with WO tags, and 600 with WV tags.

[0023] The training data management unit (310) will be based on SQL syntax when performing the process of labeling tags in the second natural language query. The training data management unit (310) may use a separate algorithm for labeling tags based on SQL syntax, and the algorithm used in this case may adopt a conventional method.

[0024] The learning data management unit (310) can label tags on natural language queries, thereby giving more weight to each keyword in the natural language query, which can make the learning of the SQL conversion model more efficient.

[0025] The SQL conversion unit (320) can generate an SQL conversion model that generates a second SQL based on the second natural language query of the training data, the DB schema, and the labeled tags. The SQL conversion unit (320) can convert the second natural language query of the training data into a second SQL based on the SQL conversion model.

[0026] While conventional NL2SQL generates SQL based on natural language queries and DB schemas, the SQL conversion model according to one embodiment of the present invention can convert natural language queries into SQL based on tags labeled in the training data management unit (310) as well as natural language queries and DB schemas. That is, because the SQL conversion unit (320) of the present invention uses tags in addition to generating SQL, it enables the training of the SQL conversion model based on each training data to be performed accurately and quickly.

[0027] As a simple example, the labeled tag defines whether the keyword included in the natural language query is a function (SA) of the SELECT clause, a table column (SC-NUM / TEXT), an operator (WO) of the WHERE clause, data (WV), or a table column (WC-NUM / TEXT), thereby allowing it to determine whether each keyword is meaningful in the generation of SQL, so that the learning of the SQL conversion unit (320) can be performed more accurately and quickly.

[0028] The SQL verification unit (330) can verify the second SQL generated by the SQL conversion unit (320). Searching for SQL that does not conform to the syntax in the database causes unnecessary load, and even if searched in the database, errors occur, so the speed of obtaining a response corresponding to the user's query from the database may be reduced. Therefore, the SQL conversion device according to one embodiment of the present invention can verify whether the syntax of the second SQL generated through the SQL verification unit (330) is appropriate.

[0029] The SQL verification unit (330) can verify whether the function of the SELECT clause of the second SQL and the type of the table column correspond to each other. When the function of the SELECT clause is AVG, the SQL verification unit (330) will determine that the syntax of the second SQL is valid if the type of the table column of the SELECT clause is NUMERIC, and otherwise will determine that the syntax of the second SQL is incorrect.

[0030] The SQL verification unit (330) can set the response to the second SQL to NO ANSWER if it determines that the syntax of the second SQL is incorrect. Since the SQL verification unit (330) provides only a fixed response (NO ANSWER) so that non-syntactic SQL is not used in database search, the response speed for database search of natural language queries can be improved.

[0031] The training data expansion unit (340) can expand the training data by replacing the table columns of the second natural language query and the second SQL with synonyms or similar words. The training data expansion unit (340) can replace the table columns of the second natural language query and the second SQL with synonyms or similar words by using synonyms / synonyms learned through a conventionally constructed synonym / synonym dictionary and / or word embeddings to obtain synonyms or similar words.

[0032] Since users make queries using different expressions, the learning data expansion unit (340) can continuously learn identical or similar word expressions to expand the number of learning data.

[0033] The learning data expansion unit (340) can store the expanded learning data in the database (300).

[0034] The SQL conversion model learning unit (350) can learn the SQL conversion model by comparing the second SQL and the third SQL corresponding to the second user query of the learning data.

[0035] The SQL conversion model learning unit (350) can calculate a loss value by comparing the second SQL with the third SQL of the training data. The SQL conversion model learning unit (350) will follow a conventional method in calculating the loss value.

[0036] The database (400) can store training data required for training the SQL conversion model training unit (350), and the SQL generation unit (200) can store basic data for converting a user query received by the query receiving unit (100) into SQL.

[0037] FIG. 2 is a flowchart illustrating a method for converting a natural language query into SQL according to an embodiment of the present invention. Hereinafter, the method for converting a natural language query into SQL will be described with reference to FIG. 2. In describing the method for converting a natural language query into SQL, detailed embodiments that overlap with the device for converting a natural language query into SQL described above may be omitted.

[0038] In step 100, the SQL converter can receive a first natural language query from a user. The SQL converter can receive a first natural language query that the user wishes to search for in a database through a user terminal such as a smart terminal or a PC.

[0039] In step 200, the SQL translator can generate a first SQL based on a first natural language query received from the user. The SQL translator will be based on an SQL translator model that generates SQL through a natural language query.

[0040] Below, a method for training the SQL transformation model used in step 200 is specifically explained through Figure 3.

[0041] Referring to FIG. 3, in step 310, the SQL conversion device can label a second natural language query included in the training data. The SQL conversion device can extract multiple keywords included in the second natural language query and label tags corresponding to each keyword. At this time, the SQL conversion device may use an algorithm based on SQL syntax in the process of labeling tags to the second natural language query. The tags used in one embodiment of the present invention include at least one of 0, SA, SC-NUM / TEXT, WO, WV, and WC-NUM / TEXT.

[0042] In step 320, the SQL conversion device can convert the second natural language query into second SQL based on the second natural language query, DB schema, and labeled tags of the training data.

[0043] In step 330, the SQL converter can verify the second SQL generated in step 320. The SQL converter can verify whether the function in the SELECT clause of the second SQL corresponds to the type of the table column. The SQL converter will determine that the syntax of the second SQL is valid if the function in the SELECT clause is AVG and the type of the table column in the SELECT clause is NUMERIC, and will determine that the syntax of the second SQL is incorrect otherwise. In this case, if it is determined that the syntax of the second SQL is incorrect, the SQL converter can set the response for the second SQL to NO ANSWER.

[0044] In step 340, the SQL conversion device can expand the training data by replacing the table columns of the second natural language query and the second SQL with synonyms or similar words. To obtain synonyms or similar words, the SQL conversion device can replace the table columns of the second natural language query and the second SQL with synonyms or similar words using synonyms / synonyms learned through a conventionally constructed synonym / synonym dictionary and / or word embeddings.

[0045] In step 350, the SQL conversion device can train an SQL conversion model by comparing a second SQL generated by the training data with a third SQL included in the training data. The SQL conversion device can train an SQL conversion model by calculating the loss values ​​of the second SQL and the third SQL.

[0046] The embodiments of the present invention disclosed in this specification and drawings are provided merely as specific examples to facilitate the explanation of the technical content of the present invention and to aid in understanding the present invention, and are not intended to limit the scope of the present invention. It is obvious to those skilled in the art that other variations based on the technical concept of the present invention are possible in addition to the embodiments disclosed herein.

Claims

Claim 1 A method for generating an SQL conversion model that converts a natural language query into SQL, comprising: a step of labeling each keyword included in a second natural language query with a first tag indicating that the keyword is meaningless in the conversion to SQL or a second tag indicating that the keyword is meaningful in the conversion to SQL; a step of converting the second natural language query into a second SQL query using the SQL conversion model based on the second natural language query, the tags labeled on each keyword, and a DB schema; a step of processing the second SQL query with incorrect syntax so that it is not used for database search if the syntax of the second SQL query is incorrect; and a step of training the SQL conversion model based on a loss value calculated by comparing the second SQL query with correct syntax and a third SQL query corresponding to the second natural language query if the syntax of the second SQL query is correct. Claim 2 A method for generating an SQL transformation model according to claim 1, further comprising the step of assigning weights to each keyword based on tags labeled to each keyword. Claim 3 A method for generating an SQL conversion model according to claim 1, wherein the first tag includes a 0 tag, and the second tag includes one or more of an SA tag, an SC-NUM tag, an SC-TEXT tag, a WO tag, a WV tag, a WC-NUM tag, and a WC-TEXT tag, wherein the SA tag signifies that it is a keyword corresponding to a SELECT clause function, the SC-NUM tag signifies that it is a keyword corresponding to a SELECT clause table column, the SC-TEXT tag signifies that it is a keyword corresponding to a SELECT clause table column type, the WO tag signifies that it is a keyword corresponding to a WHERE clause operator, the WV tag signifies that it is a keyword corresponding to WHERE clause data, the WC-NUM tag signifies that it is a keyword corresponding to a WHERE clause table column, and the WC-TEXT tag signifies that it is a keyword corresponding to a WHERE clause table column type. Claim 4 A method for generating an SQL conversion model according to claim 1, further comprising the step of determining that the syntax of the second SQL query is inappropriate when the function of the SELECT clause of the second SQL query and the type of the table column of the SELECT clause do not correspond to each other. Claim 5 A method for generating an SQL conversion model according to claim 1, wherein the processing step includes the step of setting NO ANSWER as a response to the second SQL query that has incorrect grammar. Claim 6 A method for generating an SQL conversion model according to claim 1, further comprising the step of determining that the syntax of the second SQL query is appropriate when the function of the SELECT clause of the second SQL query and the type of the table column of the SELECT clause correspond to each other. Claim 7 A method for generating an SQL conversion model according to claim 6, further comprising the step of expanding training data for training the SQL conversion model by replacing at least one of the table column of the second SQL query and the second natural language query, which are grammatically appropriate, with a synonym or a similar word. Claim 8 In claim 1, the learned SQL transformation model is a method for generating an SQL transformation model used to generate a first SQL query from a first natural language query received from a user. Claim 9 A device for generating an SQL conversion model that converts a natural language query into SQL, comprising: a memory for storing instructions; and one or more processors for executing the instructions, wherein the one or more processors are configured to label each keyword included in the second natural language query with a first tag indicating that the keyword is meaningless in the conversion to SQL or a second tag indicating that the keyword is meaningful in the conversion to SQL, and to convert the second natural language query into a second SQL query using the SQL conversion model based on the second natural language query, the tags labeled on each keyword, and a DB schema, and if the grammar of the second SQL query is inappropriate, process the second SQL query with inappropriate grammar so as not to be used for database search, and if the grammar of the second SQL is appropriate, train the SQL conversion model based on a loss value calculated by comparing the second SQL query with appropriate grammar and a third SQL query corresponding to the second natural language query. Claim 10 In claim 9, the SQL transformation model generating device, wherein the one or more processors are further configured to assign weights to each keyword based on tags labeled to each keyword. Claim 11 A SQL conversion model generating device according to claim 9, wherein the first tag includes a 0 tag, and the second tag includes one or more of an SA tag, an SC-NUM tag, an SC-TEXT tag, a WO tag, a WV tag, a WC-NUM tag, and a WC-TEXT tag, wherein the SA tag signifies that it is a keyword corresponding to a SELECT clause function, the SC-NUM tag signifies that it is a keyword corresponding to a SELECT clause table column, the SC-TEXT tag signifies that it is a keyword corresponding to a SELECT clause table column type, the WO tag signifies that it is a keyword corresponding to a WHERE clause operator, the WV tag signifies that it is a keyword corresponding to WHERE clause data, the WC-NUM tag signifies that it is a keyword corresponding to a WHERE clause table column, and the WC-TEXT tag signifies that it is a keyword corresponding to a WHERE clause table column type. Claim 12 In claim 9, the SQL conversion model generating device is further configured such that the one or more processors determine that the syntax of the second SQL query is invalid when the function of the SELECT clause of the second SQL query and the type of the table column of the SELECT clause do not correspond to each other. Claim 13 In paragraph 12, the above one or more processors are further configured to set NO ANSWER in response to the above second SQL query which is grammatically incorrect, an SQL conversion model generating device. Claim 14 In claim 9, the SQL conversion model generating device is further configured such that the one or more processors determine that the syntax of the second SQL query is appropriate when the function of the SELECT clause of the second SQL query and the type of the table column of the SELECT clause correspond to each other. Claim 15 In claim 14, the SQL conversion model generating device is further configured such that one or more processors are configured to expand training data for training the SQL conversion model by replacing at least one of the table column of the second SQL query and the second natural language query, which are grammatically appropriate, with a synonym or a similar word. Claim 16 In claim 9, the learned SQL transformation model is a SQL transformation model generating device used to generate a first SQL from a first natural language query received from a user. Claim 17 A non-transient computer-readable recording medium comprising instructions for executing on a computer the method for generating an SQL conversion model described in any one of claims 1 through 8.