Natural Language Processing Method, Apparatus, Device, and Storage Medium
By receiving natural language questions in the intelligent question-answer robot and identifying intent and slot information, directly generating SQL statements for database query, the problem of long-term and poor interpretability of model training in NL2SQL technology is solved, and fast and accurate data query and high interpretability are achieved.
Patent Information
- Application Number
- CN202111525733.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-14
- Publication Date
- 2025-06-24
- Estimated Expiration
- 2041-12-14
AI Technical Summary
When applying NL2SQL technology to intelligent question-and-answer robots, a lot of prior knowledge is required for model training, resulting in poor interpretability and long-term model training.
By receiving the natural language questions to be processed, it is subject to intent recognition, and obtaining the database query intention and the database table to be queryed, and then determining the slot information to be processed based on the pre-configured intention and slot association information table, generating corresponding SQL statements, and querying them directly in the database.
Without model training, natural language questions can be quickly and accurately converted into SQL statements, improving data query efficiency and accuracy, and the generated SQL statements are highly interpretable.
Smart Images

Figure CN114186026B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence technology, and in particular, to a natural language processing method, apparatus, device, and storage medium. Background Art
[0002] Intelligent question-and-answer robots can use artificial intelligence algorithms to perform semantic recognition on questions raised by users, and find the most matching answers from a document library as responses, which have been widely used in various fields.
[0003] Among them, when building an intelligent question-and-answer robot, it is usually necessary to prepare sufficient question-and-answer knowledge base corpus, and a large amount of manual work is required for sorting. However, at present, a large amount of data and knowledge exist in relational databases. Therefore, it is considered to introduce the NL2SQL technology into the intelligent question-and-answer robot, and the existing knowledge base can be directly used to quickly complete the initialization construction of the question-and-answer system without sorting the question-and-answer pairs. When obtaining SQL query questions by using the NL2SQL technology, it is usually implemented based on a deep learning method. This method requires a large number of prior knowledge such as multiple natural language questions and corresponding SQL statements for training to determine the mapping relationship between natural language questions and SQL statements.
[0004] The above method requires a large amount of prior knowledge for model training, and has problems of poor interpretability and long model training time. Summary of the Invention
[0005] The present invention provides a natural language processing method, apparatus, device, and storage medium to solve the problems that when applying the NL2SQL technology to an intelligent question-and-answer robot, a large amount of prior knowledge is required for model training, and it has poor interpretability and long model training time.
[0006] In a first aspect, the present invention provides a natural language processing method, and the method includes:
[0007] Receiving a natural language question to be processed, performing intent recognition on the natural language question to obtain a database query intent and a database table to be queried;
[0008] According to the database table to be queried and the natural language question, determining slot information to be processed from all slot information in a pre-configured intent and slot association information table; the pre-configured slot information at least includes return result slot information and query condition slot information; the return result slot information is each column name entity in the database table to be queried; the query condition slot information is each column name entity in the database table to be queried that determines the return result slot information; each query condition slot information corresponds to a dictionary, and the dictionary stores data entities of column names corresponding to the query condition slot information.
[0009] Generate a corresponding SQL statement according to the database query intent, the database table to be queried, and the slot information to be processed, and query in the database according to the SQL statement to obtain the result corresponding to the natural language question.
[0010] Optionally, determine the slot information to be processed from all the slot information in the pre-configured intent and slot association information table according to the database table to be queried and the natural language question, including:
[0011] Obtain all the slot information in the pre-configured intent and slot association information table according to the database table to be queried;
[0012] Determine the slot information to be processed from all the slot information according to the natural language question.
[0013] Optionally, there is a corresponding relationship between the returned result slot information and the query condition slot information; determining the slot information to be processed from all the slot information according to the natural language question includes:
[0014] Determine at least one keyword corresponding to the natural language question;
[0015] Match the at least one keyword with the dictionary corresponding to the returned result slot information and / or the query condition slot information to obtain the returned result slot information to be processed and at least one query condition slot information;
[0016] If it is determined according to the corresponding relationship between the returned result slot information and the query condition slot information that there is unrecognized query condition slot information, determine the unrecognized query condition slot information by means of multi-round clarification.
[0017] Optionally, the method of determining the unrecognized query condition slot information by means of multi-round clarification includes:
[0018] Output the default reply configured for the unrecognized query condition slot so that the user can input the corresponding natural language according to the default reply;
[0019] Determine the keyword corresponding to the received natural language as the unrecognized query condition slot information.
[0020] Optionally, the database query intent includes: single-column query, aggregation query, and sorting query; performing intent recognition on the natural language question to obtain the database query intent includes:
[0021] Determine whether all keywords corresponding to the natural language question are any one of the multiple first keywords corresponding to the aggregation query and the multiple second keywords corresponding to the sorting query;
[0022] If it is any one of the first keywords, determine the type of the aggregation query according to the first keyword; if it is any one of the second keywords, determine the type of the sorting query according to the second keyword;
[0023] If it is not any one of the first keyword and the second keyword, determine that the database query intent is a single-column query.
[0024] Optionally, perform intent recognition on the natural language question to obtain the database table to be queried, including:
[0025] For each database table, judge the matching degree between all keywords corresponding to the natural language question and the name of the database table. If the matching degree is greater than the preset value, determine the database table as the database table to be queried;
[0026] Judge whether there is a connection field between the determined database table to be queried and all the remaining database tables;
[0027] If there is a connection field, also determine the database table with the connection field as the database table to be queried.
[0028] Optionally, generating a corresponding SQL statement according to the database query intent, the database table to be queried, and the slot information to be processed includes:
[0029] Generate corresponding database query conditions according to the database query intent, the database table to be queried, and the slot information to be processed; the database query conditions include: database table name query conditions, return result conditions, query limit conditions, and sorting conditions;
[0030] Generate an SQL statement according to the database table name query conditions, return result conditions, query limit conditions, and sorting conditions according to a preset template.
[0031] Optionally, generating corresponding database query conditions according to the database query intent, the database table to be queried, and the slot information to be processed includes:
[0032] When the database table to be queried is at least two, generate database table name query conditions according to the database table to be queried and the connection field;
[0033] When the database query intent is a single-column query, put the returned result slot information into the return condition set to generate return result conditions; or, if the database query intent is an aggregation query, determine the aggregation operator according to the type of the aggregation query, and put the aggregation operator and the returned result slot information into the return condition set to generate return result conditions; when the database query intent is a sorting query, put the sorting query operator and the returned result slot information into the sorting condition set to generate sorting conditions.
[0034] Determine the query condition operator corresponding to the query condition slot information according to all keywords corresponding to the natural language question sentence, and put the query condition slot information and the query condition operator into the query condition set to generate query restriction conditions.
[0035] In a second aspect, the present invention provides a natural language processing device, which includes:
[0036] An identification module, configured to receive a natural language question sentence to be processed, perform intent identification on the natural language question sentence, and obtain a database query intent and a database table to be queried.
[0037] A determination module, configured to determine the slot information to be processed from all slot information in a pre-configured intent and slot association information table according to the database table to be queried and the natural language question sentence; the pre-configured slot information at least includes returned result slot information and query condition slot information; the returned result slot information is each column name entity in the database table to be queried; the query condition slot information is each column name entity in the database table to be queried that determines the returned result slot information; each query condition slot information corresponds to a dictionary, and data entities of column names corresponding to the query condition slot information are stored in the dictionary.
[0038] A generation module, configured to generate a corresponding SQL statement according to the database query intent, the database table to be queried, and the slot information to be processed, perform a query in the database according to the SQL statement, and obtain the result corresponding to the natural language question sentence.
[0039] In a third aspect, the present invention provides an electronic device, including: at least one processor and a memory;
[0040] The memory stores computer execution instructions;
[0041] The at least one processor executes the computer execution instructions stored in the memory, so that the at least one processor executes the method according to any item in the first aspect.
[0042] Fourthly, the present invention provides a computer-readable storage medium storing a computer program, which when executed by a processor, implements the method according to any one of the first aspect.
[0043] Fifthly, the present invention provides a computer program product comprising a computer program, which when executed by a processor, implements the method according to any one of the first aspect.
[0044] The present invention provides a natural language processing method, apparatus, device and storage medium. The method includes: receiving a natural language question to be processed, performing intent recognition on the natural language question to obtain a database query intent and a database table to be queried; determining slot information to be processed from all slot information in a pre-configured intent and slot association information table according to the database table to be queried and the natural language question; generating a corresponding SQL statement according to the database query intent, the database table to be queried and the slot information to be processed, querying in the database according to the SQL statement to obtain a result corresponding to the natural language question. By obtaining the slot information to be processed corresponding to the natural language question based on the pre-set intent and slot association information table, and then generating an SQL statement according to different slot information, model training is not required, and the SQL statement determined based on this method has strong interpretability. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following will briefly introduce the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings in the following description are only some embodiments of the present invention, and those of ordinary skill in the art can also obtain other drawings without creative efforts based on these drawings.
[0046] Figure 1 FIG. is a schematic diagram of an application scenario provided by the present invention;
[0047] Figure 2 FIG. is a flowchart of a natural language processing method provided by the present invention;
[0048] Figure 3 FIG. is a schematic diagram of the categories of database query intents provided by the present invention;
[0049] Figure 4 FIG. is a schematic diagram of the principle of a natural language processing method provided by the present invention;
[0050] Figure 5 FIG. is a schematic diagram of the structure of a natural language processing apparatus provided by the present invention;
[0051] Figure 6Schematic diagram of the hardware structure of an electronic device provided by the present invention. Detailed implementation manners
[0052] The technical solutions in the present invention will be clearly and completely described below with reference to the accompanying drawings in the present invention. Apparently, the described embodiments are only a part of the embodiments of the present invention, rather than all of the embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments in the present invention without creative efforts shall fall within the protection scope of the present invention.
[0053] The terms "first", "second", "third", "fourth", etc. (if any) in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects and do not necessarily need to be used to describe a specific order or sequence. It should be understood that such used data may be interchanged under appropriate circumstances so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "comprising" and "having" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device comprising a series of steps or units does not necessarily need to be limited to those steps or units clearly listed, but may include other steps or units not clearly listed or inherent to these processes, methods, products or devices.
[0054] Figure 1 Schematic diagram of an application scenario provided by the present invention. As Figure 1 shown, when a user sends a natural language question to an intelligent Q&A robot, the intelligent Q&A robot can output a corresponding answer according to the natural language question. For example, when the user's natural language question is "Who is the author of 'Ascending the Heights'?", the intelligent Q&A robot can obtain the answer "Du Fu" by querying the database stored in it and output it to the user.
[0055] Among them, in the prior art, the intelligent Q&A robot can answer the user's questions based on the user's prior collation of sufficient Q&A knowledge base corpus. When data or knowledge is stored in a relational database, the NL2SQL technology can be introduced into the intelligent Q&A robot. However, when obtaining SQL statements by using the NL2SQL technology, it is usually implemented based on deep learning, that is, the natural language question and the corresponding SQL statement are input into the intelligent Q&A robot for training, so as to realize the conversion of the natural language question input by the user into an SQL statement. This method has the problems of long training time and poor interpretability.
[0056] Based on the above problems, the natural language processing method provided by the present invention receives a natural language question to be processed, identifies the intention of the natural language question to obtain a database query intention and a database table to be queried, then determines the slot information to be processed from a pre-configured intention and slot association information table according to the natural language question, and finally generates a corresponding SQL statement according to the slot information to be processed, so as to quickly and accurately convert the user's natural language question into an SQL statement, without the need to organize prior knowledge or perform model training, improving the data query efficiency and accuracy, and having no disadvantage of poor interpretability.
[0057] The technical solution of the present invention will be described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments.
[0058] Figure 2 The following is a schematic flow chart of a natural language processing method provided by the present invention, as Figure 2 shown, the method includes:
[0059] Step S201, receive a natural language question to be processed, identify the intention of the natural language question to obtain a database query intention and a database table to be queried.
[0060] In this step, after receiving the natural language question to be processed, the intention of the natural language question can be identified. Among them, the natural language question to be processed can be a natural language question input by the user. Among them, identifying the intention of the natural language question includes: identifying the general intention to obtain a database query intention; and, identifying the intention of the table name to be queried to obtain the database table to be queried. Among them, the determined database query intention can be a single-column query, an aggregation query, and a sorting query. A single-column query refers to a query method for querying each row of data in a database table; an aggregation query refers to a query method for aggregating multiple rows of data through an aggregation function; a sorting query refers to a query method for sorting according to a specified data column. The obtained database table to be queried can be a single database table or multiple database tables with connection fields.
[0061] Specifically, the database query intention and the database table to be queried can be determined according to the keywords corresponding to the natural language question.
[0062] Step S202, determine the slot information to be processed from all the slot information in the pre-configured intention and slot association information table according to the database table to be queried and the natural language question.
[0063] In this step, since the database table to be queried has been determined, the intention and slot association information table corresponding to the database table to be queried can be obtained. Among them, for each database table, the return result slot information and the query condition slot information are pre-configured to obtain the intention and slot association information table. Among them, for the same database table, when the database query intention is different, the intention and slot association information table is the same.
[0064] The content of the pre-configured intention and slot association information table is illustrated by a specific example below. As shown in Table 1 and Table 2, there are a work information table and a person information table stored inside the intelligent Q&A robot. When the work information table is used as the database table to be queried, through the analysis of the work information table, it is found that there is a connection field, author ID, between the person information table and the work information table. Therefore, the pre-configured intention and slot association information table can be obtained based on the work information table and the person information table. Among them, one intention and slot association information table can be configured for the work information table and the person information table.
[0065] Specifically, the return result slot information can be the column name entities of the work information table or the person information table, that is, the column names such as work name, work content, literary genre, creation era, author ID, author name, date of birth, alias, major achievements, etc. The query condition slot information can be each column name entity for determining the return result slot information. For example, when the return result slot information is the author name, the query condition slot information can be the work name or the author alias, etc. And each query condition slot information corresponds to a dictionary. For example, the slot information of the work name will correspond to data entities such as "Ascending the Heights" and "Thoughts in the Silent Night"; the slot information of the author alias will correspond to data entities such as "Du Shaoling" and "Poet Saint".
[0066] Among them, there is a corresponding relationship between the return result slot information and the query condition slot information, that is, the return result slot information can be uniquely determined according to the query condition slot information. For example, the return result slot information can be the work content, and the query condition slot information can be the author name and the work name.
[0067] Among them, after determining the database table to be queried, the corresponding intention and slot association information table can be obtained. Then, according to the natural language question, the slot information in the intention and slot association information table can be identified. For example, when the natural language question is "Who is the author of 'Ascending to a Height'?", the keywords "Ascending to a Height", "author", and "is" can be obtained, and the focus of the result is the author. Therefore, check whether there is an "author name" in the return result slot information in the intention and slot association information table. If it exists, determine that the return result slot is the "author name"; at the same time, check whether there is an "Ascending to a Height" in the dictionary of the query condition slot information. If it exists, determine that the query condition slot information is the query condition slot information corresponding to this dictionary, that is, the query condition slot information is the work name; thus, determine the slot information to be processed as: return result slot information - author name, query condition slot information - work name: Ascending to a Height.
[0068] Table 1 Work Information Table
[0069]
[0070] Table 2 Person Information Table
[0071]
[0072] Step S203: Generate a corresponding SQL statement according to the database query intention, the database table to be queried, and the slot information to be processed, and query in the database according to the SQL statement to obtain the result corresponding to the natural language question.
[0073] After determining the slot information to be processed, the operator information can also be determined according to the user question fragment where the query condition slot information is located. Among them, the operator slot information is an operator representing a connection condition or a comparison condition. For example, when the connection condition is a parallel condition, the operator slot is AND; when the connection condition is an OR condition, the operator is OR; when the comparison condition is equal, the operator slot is equal; when the comparison condition is greater than, the operator slot is greater than; when the comparison condition is less than, the operator slot is less than.
[0074] For example, when the natural language question is: "Who is the author of 'Ascending to a Height'?", after determining that the query condition slot information is the work name - Ascending to a Height, no information keywords such as AND / OR / greater than / less than are detected in the natural language question. Therefore, the operator information is equal. When the natural language question is "Which students are older than 10 years old?", after determining that the query condition slot information is age: 10 years old, when the keyword "greater than" is detected after the age, the operator information can be determined to be greater than.
[0075] Among them, after determining the slot information and operator to be processed, a corresponding SQL statement can be generated according to the slot information to be processed, the database query intention, the database table to be queried, and the operator. For the database table to be queried, the return result slot information, and the query condition slot information, there are corresponding database query conditions. The database table name query condition can be obtained according to the name information of the database table to be queried; the return result condition can be obtained according to the return result slot information and the database query intention; the query restriction condition can be obtained according to the operator and the query condition slot information. In addition, when the database query intention is a sorting query, such as ascending query and descending query, the query condition operator can be determined according to the query intention, and then the sorting condition can be obtained according to the query condition operator and the return result slot information.
[0076] Finally, the SQL statement can be obtained according to each database query condition, and the SQL statement can be executed to obtain the result corresponding to the natural language question.
[0077] In the embodiment of the present application, by receiving a natural language question and performing intention recognition on the natural language question to obtain a database query intention and a database table to be queried, according to the database table to be queried and the natural language question, the slot information to be processed can be determined from all the slot information in the pre-configured intention and slot association information table. Furthermore, the operator information can also be determined. Then, an SQL statement is generated according to the slot information to be processed and the operator information. Thus, a query is performed in the database according to the generated SQL statement. By obtaining the slot information to be processed corresponding to the natural language question based on the pre-set intention and slot association information table, an accurate SQL statement can be generated without model training, and this method has the advantage of strong interpretability.
[0078] The process of determining the slot information to be processed will be described in detail below.
[0079] Optionally, determining the slot information to be processed from all the slot information in the pre-configured intention and slot association information table according to the database query intention, the database table to be queried, and the natural language question includes:
[0080] Obtain all the slot information of the pre-configured intention and slot association information table according to the database table to be queried; determine the slot information to be processed from all the slot information according to the natural language question.
[0081] Among them, after determining the database table to be queried, the corresponding intent and slot association information table can be determined. That is, the intent and slot association information table can be pre-configured inside the intelligent question-answering robot according to the database table to be queried. After determining the database table to be queried, all slot information of the intent and slot association information table corresponding to the database table to be queried can be obtained according to the database table to be queried. Furthermore, the slot information to be processed can be determined according to the keywords in the natural language question.
[0082] By conveniently determining the intent and slot association information table to be queried according to the database table to be queried, the slot information to be processed can be quickly determined.
[0083] Optionally, there is a corresponding relationship between the returned result slot information and the query condition slot information; determining the slot information to be processed from all the slot information according to the natural language question includes:
[0084] Determine at least one keyword corresponding to the natural language question; match the at least one keyword with the dictionary corresponding to the returned result slot information and / or the query condition slot information to obtain the returned result slot information to be processed and at least one query condition slot information; if it is determined that there is unrecognized query condition slot information according to the corresponding relationship between the returned result slot information and the query condition slot information, the unrecognized query condition slot information is determined by means of multi-round clarification.
[0085] Among them, when determining the slot information to be processed, the returned result slot information can be determined first according to the obtained keywords. Among them, the returned result slot information is determined first. For the natural language question "Who is the author of 'Ascending the Height'", according to the keyword "author", the returned result slot information can be determined as "author name". For the query condition slot information, the keyword "Ascending the Height" can be queried in the dictionaries corresponding to each query condition slot information. When it is found that the dictionary corresponding to the query condition slot information of the work name contains the information of "Ascending the Height", the query condition slot information can be determined as the work name - Ascending the Height.
[0086] Among them, there is a corresponding relationship between the returned result slot information and the query condition slot information. It may be necessary to have multiple query condition slot information to uniquely determine the returned result slot information. For example, when there are two poems with the work name "Ascending the Height", when determining the author, the author cannot be determined only according to the work name. Other query condition slot information, such as the content of the work, can also be pre-set. At this time, when the keyword corresponding to the natural language question input by the user only contains the work name Ascending the Height, it is determined that there is unrecognized query condition slot information. At this time, the unrecognized query condition slot information can be determined by means of multi-round clarification.
[0087] The information of the unrecognized query condition slots can be conveniently determined through multiple rounds of clarification, and then the information of the query condition slots can be accurately determined.
[0088] Optionally, determining the information of the unrecognized query condition slots through multiple rounds of clarification includes:
[0089] Output the default reply configured for the unrecognized query condition slots so that the user can input the corresponding natural language according to the default reply; determine the keyword corresponding to the received natural language as the information of the unrecognized query condition slots.
[0090] Among them, when determining the information of the unrecognized query condition slots through multiple rounds of clarification, the default reply corresponding to each query condition slot information can be set in advance. When it is determined that the information of the unrecognized query condition slot is the work content, the default reply "Please say a line of this poem" corresponding to the slot information of the work content can be output. At this time, the user can input the corresponding natural language according to the default reply, such as "The endless river rolls its waves hour after hour". At this time, the intelligent question-and-answer robot can receive the natural language, extract the keyword in the natural language, and determine the keyword as the information of the query condition slot.
[0091] By setting the default reply, it is possible to determine the unrecognized slot information through a multi-round interaction with the user.
[0092] Optionally, the database query intent includes: single-column query, aggregation query, and sorting query; identifying the intent of the natural language question to obtain the database query intent includes:
[0093] Judge whether all the keywords corresponding to the natural language question are any one of the multiple first keywords corresponding to the aggregation query and the multiple second keywords corresponding to the sorting query;
[0094] If it is any one of the first keywords, determine the type of the aggregation query according to the first keyword; if it is any one of the second keywords, determine the type of the sorting query according to the second keyword;
[0095] If it is not any one of the first keyword and the second keyword, determine that the database query intent is a single-column query.
[0096] Among them, when determining the database query intent, the database query intent can be pre-divided into single-column query, aggregation query, and sorting query. Among them, there are corresponding keywords for both the aggregation query and the sorting query. Figure 3 It is a schematic diagram of the category of a database query intent provided by the present invention; such as Figure 3As shown in the figure, the aggregation query includes first keywords related to maximum value, minimum value, quantity statistics, sum statistics, average value, etc., such as keywords like maximum, minimum, how many, total, average, etc. If the natural language question contains the above first keywords, it can be determined that the database query intention is an aggregation query. The sorting query includes ascending sorting and descending sorting, and the sorting query includes second keywords such as the first X or the last X. If the natural language question contains the above second keywords, it can be determined that the database query intention is a sorting query. Specifically, according to the keywords, the specific types of aggregation queries and sorting queries can also be determined.
[0097] In addition, if the database query intention is not one of the above two query intentions, it can be determined that the query intention is a single-column query.
[0098] By comparing the keywords in the natural language question with the keywords of each query intention, the database query intention can be accurately determined.
[0099] Optionally, perform intention recognition on the natural language question to obtain the database table to be queried, including:
[0100] For each database table, judge the matching degree between all the keywords corresponding to the natural language question and the name of the database table. If the matching degree is greater than the preset value, determine that the database table is the database table to be queried; judge whether there is a connection field between the determined database table to be queried and all the remaining database tables; if there is a connection field, also determine the database table with the connection field as the database table to be queried.
[0101] In this step, when determining the database table to be queried, the keywords in the natural language question can be matched with the name of the database table. If the matching degree is greater than a certain value, it can be determined that this database table is the database table to be queried. For example, if the keyword "author" appears in the natural language question, it can be matched with the name of the database table. When matching with the author information table, since both have the keyword "author", the matching degree is relatively high. The preset value can be set according to the actual situation.
[0102] Among them, after determining the database table to be queried, it can also be judged whether there is a connection field between each field of the database table to be queried and all the remaining database tables. Among them, the connection field refers to the field that appears in both the determined database table to be queried and another database table. For example, if the author information table is determined as the database table to be queried, and there is an ID field in the author information table, and there is also an ID field (author ID) in the work information table, then it can be determined that the work information table is also the database table to be queried.
[0103] All database tables to be queried can be determined by judging the connection fields, which is convenient for accurately determining query information subsequently.
[0104] Optionally, generating a corresponding SQL statement according to the database query intention, the database tables to be queried, and the slot information to be processed includes:
[0105] Generating a corresponding database query condition according to the database query intention, the database tables to be queried, and the slot information to be processed; the database query condition includes: a database table name query condition, a return result condition, a query limit condition, and a sorting condition; generating an SQL statement according to the database table name query condition, the return result condition, the query limit condition, and the sorting condition according to a preset template.
[0106] Among them, when generating an SQL statement according to the database query intention, the database tables to be queried, and the slot information to be processed, a database table name query condition (fromCondition), a return result condition (selectCondition), a query limit condition (whereCondition), and a sorting condition (orderCondition) can be generated first.
[0107] Optionally, generating a corresponding database query condition according to the database query intention, the database tables to be queried, and the slot information to be processed includes:
[0108] When there are at least two database tables to be queried, generating a database table name query condition according to the database tables to be queried and the connection fields;
[0109] When the database query intention is a single-column query, putting the return result slot information into a return condition set to generate a return result condition; or, if the database query intention is an aggregation query, determining an aggregation operator according to the type of the aggregation query, and putting the aggregation operator and the return result slot information into a return condition set to generate a return result condition; when the database query intention is a sorting query, putting the sorting query operator and the return result slot information into a sorting condition set to generate a sorting condition;
[0110] Determining a query condition operator corresponding to the query condition slot information according to all keywords corresponding to the natural language question sentence, and putting the query condition slot information and the query condition operator into a query condition set to generate a query limit condition.
[0111] Taking "Who is the author of 'Ascending Heights'?" as an example, the query conditions for each database are described below. When generating the query conditions for the database table name, the database tables to be queried are the author information table and the work information table, and the number is two. Therefore, a condition group ID, such as Group1, can be determined based on the two database tables to be queried, and then the query conditions for the database table name can be obtained based on the join fields.
[0112] For example, the query condition for the database table name fromCondition obtained through the above method is: fromCondition: {"group1": [{"table": "Work Information", "col": "Author ID"}, {"table": "Author Information", "col": "ID"}]}
[0113] Among them, when generating the return result conditions, it needs to be determined according to the database query intention and the return result slot information. When the database query intention is a single-column query, the return result slot information is placed in the return condition set to generate the return result conditions.
[0114] For example, when it is a single-column query, the return result slot information is "Author Name". Since there is no aggregation operator in a single-column query, the content of the "func" field is empty, and the "cols" field is "Author Name". Therefore, the return result condition selectCondition obtained through the above method is: selectCondition: {"func": "null", "cols": ["Author Name"]}
[0115] Among them, when the database query intention is an aggregation query, since the specific type of the aggregation query has been determined, the corresponding aggregation operator can be obtained. For example, when the aggregation query is a maximum value query, the aggregation operator is MAX; when the aggregation query is a minimum value query, the aggregation operator is MIN; when the aggregation query is a quantity statistics query, the aggregation operator is COUNT; when the aggregation query is a sum statistics query, the aggregation operator is SUM; and when the aggregation query is an average value query, the aggregation operator is AVG. Placing the aggregation operator and the return result slot information in the return condition set can obtain the return result conditions. For example, when the natural language question sentence is "How many works does Du Fu have?", the return result slot information is "Work Name", and the aggregation operator is COUNT; therefore, the return result condition selectCondition obtained through the above method is: selectCondition: {"func": "COUNT", "cols": ["Work Name"]}
[0116] When the database query intention is a sorting query, the sorting query operator can be determined according to ascending sorting and descending sorting, and then the sorting operator and the returned result slot information can be put into the sorting condition set. For example, when the natural language question is "What are the top 10 stocks in terms of increase rate?", the returned result slot information is "stocks", and the sorting query operator is "DESC". Therefore, the sorting condition orderCondition obtained through the above method is: orderCondition: [{"col": stocks, "sort": "DESC"}].
[0117] When determining the query restriction conditions, the query condition operator corresponding to the query condition slot information will be determined first. For example, when the keyword after the query condition slot information is a word such as greater than or less than, the query condition operator is greater than or less than. When there are multiple query condition slot information, the query condition operator between multiple query conditions is obtained by identifying the connection relationship before the multiple query condition slot information. For example, when the natural language question is "Which people are under 35 years old and have a height greater than 160 cm?", by analyzing the keywords in the natural language question, it can be obtained that the query condition slot information includes age - 35 years old, and the corresponding query condition operator is less than; the query condition slot information also includes: height - 160 cm, and the corresponding query condition operator is greater than; and the relationship between the two query condition slot information is a parallel condition, and the corresponding query condition operator is AND.
[0118] When generating the query restriction conditions, the query condition slot information and the query condition operator can be directly put into the query condition set. For example, when the natural language question is "What are Du Fu's works?", the query condition slot information is author name - Du Fu, and the query condition operator is equal. Therefore, the query restriction condition whereCondition obtained through the above method is: whereCondition: [{"col": "author name", "value": "Du Fu", "oper": "="}].
[0119] After determining the above database query conditions, they can be used to generate an SQL statement according to a preset template. For example, an SQL statement is obtained according to the template "Select `$COL1`, `$COL2` From `$T` Where `$COL1` = `$VAL1` and `$COL2` = `$VAL2` order by $COL1 desc". And after executing this SQL statement, the result corresponding to the natural language question will be obtained.
[0120] Figure 4 This is the schematic diagram of a natural language processing method provided by the present invention. As Figure 4As shown in the figure, inside the intelligent Q&A robot, there are a general intent engine, a table name intent recognition engine, a slot engine, a query condition recognition engine, and an answer generation engine. After the natural language question is input into the general intent engine, the database query intent can be recognized. At the same time, after the natural language question is input into the table name intent recognition engine, the database table to be queried can be recognized. Then, slot recognition is performed through the slot engine to obtain the returned result slot information and the query condition slot information. If there is still unrecognized query condition slot information, it is determined through multiple rounds of clarification. Then, according to the database query intent and the recognized returned result slot information and query condition slot information, the returned result condition, query restriction condition, and / or sorting condition are determined in the query condition recognition engine. Finally, according to the generated returned result condition, query restriction condition, and / or sorting condition, an SQL statement is obtained in the answer generation engine, and this statement is executed, and finally the answer corresponding to the natural language question is output.
[0121] In the present invention, by configuring all the slot information of the intent and slot association information table, after obtaining the natural language question, the returned result slot information and the query condition slot information can be quickly recognized according to the keywords of the natural language question. And when there is unrecognized query condition slot information, it can also be determined through multiple rounds of clarification. In addition, after determining the slot information to be processed, a database query condition is generated according to the database query intent and the slot information to be processed, and then an SQL statement is generated, making the process of generating the SQL statement have the advantages of being fast and accurate, without the need for model training and manual sorting of the Q&A knowledge base corpus.
[0122] Figure 5 It is a schematic structural diagram of a natural language processing device provided by the present invention. As Figure 5 shown, the natural language processing device 50 may include:
[0123] An identification module 501, configured to receive the natural language question to be processed, perform intent recognition on the natural language question, and obtain a database query intent and a database table to be queried;
[0124] A determination module 502, configured to determine the slot information to be processed from all the slot information of the pre-configured intent and slot association information table according to the database table to be queried and the natural language question; the pre-configured slot information at least includes returned result slot information and query condition slot information; the returned result slot information is each column name entity in the database table to be queried; the query condition slot information is each column name entity in the database table to be queried that determines the returned result slot information; each query condition slot information corresponds to a dictionary, and the dictionary stores data entities of column names corresponding to the query condition slot information;
[0125] A generation module 503 is configured to generate a corresponding SQL statement according to the database query intent, the database table to be queried, and the slot information to be processed, and query the database according to the SQL statement to obtain the result corresponding to the natural language question.
[0126] Optionally, the determination module 502 is specifically configured to:
[0127] Obtain all slot information of a pre-configured intent and slot association information table according to the database table to be queried;
[0128] Determine the slot information to be processed from all the slot information according to the natural language question.
[0129] Optionally, there is a corresponding relationship between the returned result slot information and the query condition slot information; when the determination module 502 determines the slot information to be processed from all the slot information according to the natural language question, it is specifically configured to:
[0130] Determine at least one keyword corresponding to the natural language question;
[0131] Match the at least one keyword with a dictionary corresponding to the returned result slot information and / or the query condition slot information to obtain the returned result slot information to be processed and at least one query condition slot information;
[0132] If it is determined that there is unrecognized query condition slot information according to the corresponding relationship between the returned result slot information and the query condition slot information, the unrecognized query condition slot information is determined by means of multi-round clarification.
[0133] Optionally, when the determination module 502 determines the unrecognized query condition slot information by means of multi-round clarification, it is specifically configured to:
[0134] Output the default reply configured for the unrecognized query condition slot so that the user can input the corresponding natural language according to the default reply;
[0135] Determine the keyword corresponding to the received natural language as the unrecognized query condition slot information.
[0136] Optionally, the database query intent includes: single-column query, aggregation query, and sorting query; when the recognition module 501 performs intent recognition on the natural language question to obtain the database query intent, it is specifically configured to:
[0137] Judge whether all the keywords corresponding to the natural language question are any one of multiple first keywords corresponding to the aggregation query and multiple second keywords corresponding to the sorting query;
[0138] If it is any one of the first keywords, determine the type of aggregation query according to the first keyword; if it is any one of the second keywords, determine the type of sorting query according to the second keyword;
[0139] If it is not any one of the first keyword and the second keyword, determine that the database query intention is a single-column query.
[0140] Optionally, when the recognition module 501 performs intention recognition on the natural language question sentence to obtain the database table to be queried, it is specifically used for:
[0141] For each database table, judge the matching degree between all keywords corresponding to the natural language question sentence and the name of the database table. If the matching degree is greater than the preset value, determine that the database table is the database table to be queried;
[0142] Judge whether there is a connection field between the determined database table to be queried and all the remaining database tables;
[0143] If there is a connection field, also determine the database table with the connection field as the database table to be queried.
[0144] Optionally, when the generation module 503 generates a corresponding SQL statement according to the database query intention, the database table to be queried, and the slot information to be processed, it is specifically used for:
[0145] Generate a corresponding database query condition according to the database query intention, the database table to be queried, and the slot information to be processed; the database query condition includes: database table name query condition, return result condition, query limit condition, and sorting condition;
[0146] Generate an SQL statement according to the database table name query condition, return result condition, query limit condition, and sorting condition according to a preset template.
[0147] Optionally, when the generation module 503 generates a corresponding database query condition according to the database query intention, the database table to be queried, and the slot information to be processed, it is specifically used for:
[0148] When there are at least two database tables to be queried, generate a database table name query condition according to the database tables to be queried and the connection field;
[0149] When the database query intention is a single-column query, put the returned result slot information into the return condition set to generate a return result condition; or, if the database query intention is an aggregation query, determine the aggregation operator according to the type of the aggregation query, and put the aggregation operator and the returned result slot information into the return condition set to generate a return result condition; when the database query intention is a sorting query, put the sorting query operator and the returned result slot information into the sorting condition set to generate a sorting condition.
[0150] Determine the query condition operator corresponding to the query condition slot information according to all the keywords corresponding to the natural language question sentence, and put the query condition slot information and the query condition operator into the query condition set to generate a query restriction condition.
[0151] The natural language processing device provided by the present invention can implement the natural language processing method as Figures 2 to 4 shown. The implementation principle and technical effect are similar, and will not be elaborated here.
[0152] Figure 6 is a schematic hardware structure diagram of the electronic device provided by the present invention. As Figure 6 shown, the electronic device 60 includes: at least one processor 601 and a memory 602. Among them, the processor 601 and the memory 602 are connected through a bus 603.
[0153] In a specific implementation process, at least one processor 601 executes the computer execution instructions stored in the memory 602, so that at least one processor 601 executes the natural language processing method in the above method embodiment.
[0154] The specific implementation process of the processor 601 can be referred to the above method embodiment. The implementation principle and technical effect are similar, and will not be elaborated in this embodiment here.
[0155] In the above Figure 6 shown embodiment, it should be understood that the processor may be a central processing unit (English: Central Processing Unit, abbreviated: CPU), or may also be other general-purpose processors, digital signal processors (English: Digital Signal Processor, abbreviated: DSP), application specific integrated circuits (English: Application SpecificIntegrated Circuit, abbreviated: ASIC), etc. The general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc. The steps of the method disclosed in combination with the invention can be directly embodied as being executed by a hardware processor, or executed by a combination of hardware and software modules in the processor.
[0156] The memory may include high-speed RAM memory and may also include non-volatile storage NVM, such as at least one disk memory.
[0157] The bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, an Extended Industry Standard Architecture (EISA) bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, the buses in the drawings of this application are not limited to only one bus or one type of bus.
[0158] The present invention also provides a computer-readable storage medium storing computer-executable instructions, and when the processor executes the computer-executable instructions, the natural language processing method of the above method embodiment is implemented.
[0159] For the above computer-readable storage medium, the above-readable storage medium 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, a magnetic disk or an optical disc. The readable storage medium can be any available medium accessible by a general-purpose or special-purpose computer.
[0160] An exemplary readable storage medium is coupled to the processor, enabling the processor to read information from the readable storage medium and write information to the readable storage medium. Of course, the readable storage medium can also be a component of the processor. The processor and the readable storage medium can be located in an Application Specific Integrated Circuit (ASIC). Of course, the processor and the readable storage medium can also exist as discrete components in a device.
[0161] An embodiment of the present application provides a computer program product including a computer program, and when the computer program is executed by the processor, it implements the natural language processing method provided in any of the embodiments corresponding to the present application Figures 2 to 4 as described in the embodiments.
[0162] Those of ordinary skill in the art will understand that all or part of the steps for implementing the above method embodiments can be completed by hardware related to program instructions. The foregoing program can be stored in a computer-readable storage medium. When the program is executed, it performs the steps including the above method embodiments; and the foregoing storage medium includes: various media such as ROM, RAM, magnetic disk, or optical disc that can store program code.
[0163] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, and not to limit them; although the present application has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that they can still modify the technical solutions described in the foregoing embodiments, or perform equivalent replacements on some or all of the technical features; and these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present application.
Claims
1. A natural language processing method, characterized in that, The method includes: Receiving a natural language question to be processed, performing intent recognition on the natural language question to obtain a database query intent and a database table to be queried; the database query intent includes: single-column query, aggregation query, and sorting query; Determining slot information to be processed from all slot information in a pre-configured intent and slot association information table according to the database table to be queried and the natural language question; the pre-configured slot information at least includes return result slot information and query condition slot information; the return result slot information is each column name entity in the database table to be queried; the query condition slot information is each column name entity in the database table to be queried that determines the return result slot information; each query condition slot information corresponds to a dictionary, and data entities corresponding to the column names corresponding to the query condition slot information are stored in the dictionary; Generating a corresponding SQL statement according to the database query intent, the database table to be queried, and the slot information to be processed, and performing a query in the database according to the SQL statement to obtain the result corresponding to the natural language question; The performing intent recognition on the natural language question to obtain a database query intent includes: Judging whether all keywords corresponding to the natural language question are any one of multiple first keywords corresponding to the aggregation query and multiple second keywords corresponding to the sorting query; If it is any one of the first keywords, determining the type of the aggregation query according to the first keyword; if it is any one of the second keywords, determining the type of the sorting query according to the second keyword; If it is not any one of the first keywords and the second keywords, determining the database query intent as a single-column query.
2. The method according to claim 1, wherein Determining slot information to be processed from all slot information in a pre-configured intent and slot association information table according to the database table to be queried and the natural language question includes: Obtaining all slot information in the pre-configured intent and slot association information table according to the database table to be queried; Determining the slot information to be processed from all the slot information according to the natural language question.
3. The method according to claim 2, characterized in that, There is a corresponding relationship between the return result slot information and the query condition slot information; Determining the slot information to be processed from all the slot information according to the natural language question includes: Determining at least one keyword corresponding to the natural language question; Matching the at least one keyword with the dictionary corresponding to the return result slot information and / or the query condition slot information to obtain the return result slot information to be processed and at least one query condition slot information; If it is determined that there is unrecognized query condition slot information according to the corresponding relationship between the return result slot information and the query condition slot information, determining the unrecognized query condition slot information by means of multi-round clarification.
4. The method according to claim 3, wherein The determining the unrecognized query condition slot information by means of multi-round clarification includes: Outputting the default reply configured for the unrecognized query condition slot so that the user can input the corresponding natural language according to the default reply; Determine the keyword corresponding to the received natural language as the unrecognized query condition slot information.
5. The method according to claim 1, wherein Perform intent recognition on the natural language question sentence to obtain the database table to be queried, including: For each database table, judge the matching degree between all keywords corresponding to the natural language question sentence and the name of the database table. If the matching degree is greater than a preset value, determine the database table as the database table to be queried; Judge whether there is a connection field between the determined database table to be queried and all the remaining database tables; If there is a connection field, also determine the database table with the connection field as the database table to be queried.
6. The method according to any one of claims 1-5, characterized in that, Generating a corresponding SQL statement according to the database query intent, the database table to be queried, and the slot information to be processed includes: Generate a corresponding database query condition according to the database query intent, the database table to be queried, and the slot information to be processed; the database query condition includes: database table name query condition, return result condition, query restriction condition, and sorting condition; Generate an SQL statement according to the database table name query condition, return result condition, query restriction condition, and sorting condition according to a preset template.
7. The method according to claim 6, characterized in that, Generate a corresponding database query condition according to the database query intent, the database table to be queried, and the slot information to be processed, including: When there are at least two database tables to be queried, generate a database table name query condition according to the database tables to be queried and the connection field; When the database query intent is single-column query, put the return result slot information into the return condition set to generate a return result condition; or, if the database query intent is aggregation query, determine the aggregation operator according to the type of the aggregation query, and put the aggregation operator and the return result slot information into the return condition set to generate a return result condition; when the database query intent is sorting query, put the sorting query operator and the return result slot information into the sorting condition set to generate a sorting condition; Determine the query condition operator corresponding to the query condition slot information according to all keywords corresponding to the natural language question sentence, and put the query condition slot information and the query condition operator into the query condition set to generate a query restriction condition.
8. A natural language processing device, characterized in that, The device includes: An identification module, configured to receive a natural language question sentence to be processed, perform intent recognition on the natural language question sentence, and obtain a database query intent and a database table to be queried; the database query intent includes: single-column query, aggregation query, and sorting query; A determination module, configured to determine slot information to be processed from all slot information in a pre-configured intention and slot association information table according to the database table to be queried and the natural language question; the pre-configured slot information at least includes return result slot information and query condition slot information; the return result slot information is each column name entity in the database table to be queried; the query condition slot information is each column name entity in the database table to be queried for determining the return result slot information; each query condition slot information corresponds to a dictionary, and data entities of column names corresponding to the query condition slot information are stored in the dictionary. A generation module, configured to generate a corresponding SQL statement according to the database query intention, the database table to be queried and the slot information to be processed, and query in the database according to the SQL statement to obtain a result corresponding to the natural language question. The recognition module is specifically configured to determine whether all keywords corresponding to the natural language question are any one of multiple first keywords corresponding to the aggregation query and multiple second keywords corresponding to the sorting query; if it is any one of the first keywords, determine the type of the aggregation query according to the first keyword; if it is any one of the second keywords, determine the type of the sorting query according to the second keyword; if it is not any one of the first keywords and the second keywords, determine that the database query intention is a single-column query.
9. An electronic device, characterized in that, Comprising: At least one processor and a memory; The memory stores computer execution instructions; The at least one processor executes the computer execution instructions stored in the memory, so that the at least one processor executes the method according to any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, The storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1-7 is implemented.
11. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, the method according to any one of claims 1-7 is implemented.
Citation Information
Patent Citations
Natural language processing method and device, computing equipment and storage medium
CN111125309A
Data query method and device based on semantic recognition, equipment and storage medium
CN112380240A