Device and method for converting natural language query into SQL query
Provided are a generator and method of generating a structured query language (SQL) conversion model which converts a natural language query into a SQL query. The method includes labeling, by a training data manager, each of a plurality of keywords included in a second natural language query with a tag, the tag is a first tag means a meaningless keyword in an SQL conversion of a second tag means a meaningful keyword in the SQL conversion; converting, by a SQL converter, the second natural language query into a second SQL query by using the SQL conversion model based on the second natural language query, tags labeled for each of the plurality of keywords included in the second natural language, and a database schema; and training, by a SQL conversion model trainer, the SQL conversion model based on a loss value calculated by comparing the second SQL query and a third SQL query corresponding to the second natural language query.
1 . A method of generating a structured query language (SQL) conversion model which converts a natural language query into a SQL query, the method being performed by a SQL conversion model generator, the method comprising:
labeling, by a training data manager, each of a plurality of keywords included in a second natural language query with a tag, wherein the tag is a first tag meaning a meaningless keyword in an SQL conversion or a second tag meaning a meaningful keyword in the SQL conversion;
converting, by a SQL converter, the second natural language query into a second SQL query by using the SQL conversion model based on the second natural language query, tags labeled for each of the plurality of keywords included in the second natural language, and a database schema; and
training, by a SQL conversion model trainer, the SQL conversion model based on a loss value calculated by comparing the second SQL query and a third SQL query corresponding to the second natural language query;
receiving by a query receiver a first natural language query for searching a database from a user terminal;
generating, by an SOL generator, a first SQL query based on the first natural language query through the SQL conversion model;
searching the database using the first SQL query to obtain a response; and providing the response to the user terminal.
2 . The method of claim 1 , further comprising giving, by the training data manager, a weight to each of the plurality of keywords included in the second natural language query based on the labeling of the plurality of keywords.
3 . The method of claim 1 , wherein the first tag includes 0 tag, and the second tag includes at least one of SA tag, SC-NUM tag, SC-TEXT tag, WO tag, WV tag, WC-NUM tag and WC-TEXT tag,
wherein the SA tag means a keyword corresponding to a SELECT statement function, the SC-NUM tag means keyword corresponding to a SELECT statement table column,
the SC-TEXT tag means keyword corresponding a SELECT statement table column type, the WO tag means a keyword corresponding to a WHERE statement operator,
the WV tag means a keyword corresponding to a WHERE statement data,
the WC-NUM tag means keyword corresponding to a WHERE statement table column,
and the WC-TEXT tag means keyword corresponding to a WHERE statement table column type.
4 . The method of claim 1 , wherein further comprising determining, by a SQL verifier, whether the second SQL query is grammatically correct.
5 . The method of claim 4 , wherein further comprising, by the SQL verifier, setting a response to the second SQL query to NO ANSWER to prevent the second SQL query from being used for a database search based on determining that the second SQL query is grammatically incorrect.
6 . The method of claim 4 , further comprising, by a training data expander, substituting at least on of table columns of the second SQL query and the second natural language query with synonyms to expand a training data for training the SQL conversion model based on determining that the second SQL query is grammatically correct.
7 . The method of claim 4 , wherein the determining whether the second SQL query is grammatically correct comprising:
determining that the second SQL query is grammatically correct when SELECT statement functions of the second SQL query and types of table columns of the SELECT statement corresponding to each other, and
determining that the second SQL query is grammatically incorrect when the SELECT statement functions of the second SQL query and the types of table columns of the SELECT statement do not corresponding to each other.
8 . A structured query language (SQL) conversion model generator generating a SQL conversion model which converts a natural language query into a SQL query, the SQL conversion model generator comprising:
a memory storing instructions; and
one or more processors configured to execute the instructions to:
label each of a plurality of keywords in a second natural language query with a tag, wherein the tag is a first tag meaning a meaningless keyword in an SQL conversion or a second tag meaning a meaningful keyword in the SQL conversion,
convert the second natural language query into a second SQL query by using the SQL conversion model based on the second natural language query, tags labeled for each of the plurality of keywords included in the second natural language, and a database schema,
train the SQL conversion model based on a loss value calculated by comparing the second SQL query and a third SQL query corresponding to the second natural language query,
receive a first natural language query for searching a database from a user terminal, generate a first SOL query based on the first natural language query through the SOL conversion model,
search the database using the first SQL query to obtain a response, and provide the response to the user terminal.
9 . The SQL conversion model generator of claim 8 , wherein the one or more processors are further configured to: give a weight to each of the plurality of keywords included in the second natural language query based on the labeling of the plurality of keywords.
10 . The SQL conversion model generator of claim 8 , wherein the first tag includes 0 tag, and the second tag includes at least one of SA tag, SC-NUM tag, SC-TEXT tag, WO tag, WV tag, WC-NUM tag and WC-TEXT tag,
wherein the SA tag means a keyword corresponding to a SELECT statement function, the SC-NUM tag means keywords corresponding to a SELECT statement table column,
the SC-TEXT tag means keyword corresponding a SELECT statement table column type, the WO tag means a keyword corresponding to a WHERE statement operator,
the WV tag means a keyword corresponding to a WHERE statement data,
the WC-NUM tag means keywords corresponding to a WHERE statement table column,
and the WC-TEXT tag means keyword corresponding to a WHERE statement table column type.
11 . The SQL conversion model generator of claim 8 , wherein the one or more processors are further configured to determine whether the second SQL query is grammatically correct.
12 . The SQL conversion model generator of claim 11 , wherein the one or more processors are further configured to set a response to the second SQL query to NO ANSWER to prevent the second SQL query from being used for a database search based on determining that the second SQL query is grammatically incorrect.
13 . The SQL conversion model generator of claim 11 , wherein the one or more processors are further configured to substitute at least on of table columns of the second SQL query and the second natural language query with synonyms to expand a training data for training the SQL conversion model based on determining that the second SQL query is grammatically correct.
14 . The SQL conversion model generator of claim 11 , wherein the one or more processors are further configured to:
determine that the second SQL query is grammatically correct when SELECT statement functions of the second SQL query and types of table columns of the SELECT statement corresponding to each other, and
determine that the second SQL query is grammatically incorrect when the SELECT statement functions of the second SQL query and the types of table columns of the SELECT statement do not corresponding to each other.
15 . A non-transitory computer-readable recording medium containing instructions for causing a computer to execute the method according to claim 1 .