Natural language query and conversion control system and method
By identifying and analyzing natural language and SQL conversion records, determining feature coefficient thresholds and placeholder language fields, the problem of inaccurate conversion of natural language to SQL statements is solved, and a more efficient and accurate conversion effect is achieved.
Patent Information
- Application Number
- CN202411046098.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-01
- Publication Date
- 2025-03-21
- Estimated Expiration
- 2044-08-01
AI Technical Summary
The prior art has problems with inaccurate conversion of natural language to SQL statements, especially when dealing with complex natural language queries.
By collecting natural language and SQL conversion records, the placeholder SQL fields and placeholder language fields are identified, the characteristic coefficient threshold is calculated, the characteristic coefficients of the target natural language field are obtained, and the placeholder field is determined based on semantic analysis. Finally, the natural language field and placeholder field are input into the language model to generate the SQL field containing the placeholder.
Improves the accuracy of natural language to SQL statement conversion, especially when dealing with natural language queries of complex logic, reduces errors and improves conversion efficiency.
Smart Images

Figure CN118916382B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of natural language query and conversion control, and in particular to a natural language query and conversion control system and method. Background Art
[0002] Natural language query statements refer to query statements written in natural language by users, which are used to ask questions or request information to computer systems. Users can use natural language query statements to inquire about information in the database, and then the system needs to convert this natural language query into an SQL query statement in order to obtain the corresponding data from the database. However, for some complex natural language to SQL statement conversion schemes, due to the complex logic, there will be problems with inaccurate conversion from natural language to SQL. The use of placeholders can increase the flexibility and accuracy of the conversion to a certain extent. Through placeholders, the basic structure and logic of the statement can be determined first, and then the placeholders can be filled according to the specific input values. Therefore, it is very important to analyze the natural language field and determine whether to use the placeholder method according to its structure or complexity, so as to obtain more accurate SQL statements. Summary of the invention
[0003] The object of the present invention is to provide a natural language query and conversion control system and method to solve the problems raised in the above background technology.
[0004] In order to solve the above technical problems, the present invention provides the following technical solutions:
[0005] The natural language query and conversion control method comprises the following steps:
[0006] Step S100: collecting a number of natural language conversion records, the conversion records including natural language fields and corresponding SQL fields, identifying placeholder SQL fields in the SQL fields, and placeholder language fields corresponding to the placeholder SQL fields in the natural language fields;
[0007] It should be noted that the SQL field is a data query statement obtained after the natural language field is converted. The placeholder SQL field is a sub-segment in the SQL field, which is usually a part of the SQL field with a more complex structure and is used to represent the part of the SQL field that can be used as a placeholder.
[0008] Step S200: according to the conversion record, all characteristic entities in the natural language field are obtained, and then according to the characteristic entity, characteristic coefficients corresponding to the conversion record are obtained, and then characteristic coefficient thresholds are obtained;
[0009] It should be noted that the characteristic coefficient threshold is obtained from the natural language field that can be used as a placeholder in step S100, so in this solution, the characteristic coefficient threshold is an important criterion for judging whether the natural language field is used as a placeholder. As shown in the following step S300, when the characteristic coefficient is not greater than the characteristic coefficient threshold, the placeholder cannot be used. First, all entities in the natural language field are obtained. Since the placeholder language field is a part of the natural language field and corresponds to the placeholder SQL field, the characteristic entity is the entity in the placeholder language field that has a high degree of matching with the variable name in the placeholder SQL field.
[0010] Step S300: Acquire a target natural language field, obtain a characteristic coefficient corresponding to the target natural language field, compare the characteristic coefficient with a characteristic coefficient threshold, and then obtain a placeholder language field in the target natural language field;
[0011] Step S400: Input the target natural language field and the placeholder language field into a pre-trained language model, represent the placeholder language field part with a placeholder, convert the target natural language field into a SQL field represented by the placeholder, and obtain the SQL fragment corresponding to the placeholder language field according to the language model, and then obtain the complete SQL field.
[0012] Furthermore, step S100 includes:
[0013] Step S110: obtaining a conversion record NR, the conversion record NR including a natural language field NF and a corresponding SQL field NSQL; obtaining all keywords in the field NSQL, the keywords including a number of characteristic keywords, dividing the field NSQL into a number of SQL clauses according to the characteristic keywords, each SQL clause corresponding to a number of keywords and variable names; if the number of keywords corresponding to a certain SQL clause is greater than a first quantity threshold and the number of variable names is greater than a second quantity threshold, taking the certain SQL clause as a placeholder SQL field NP corresponding to the field NSQL;
[0014] In the SQL field, keywords are words with specific meanings and uses that are pre-defined by the SQL language; feature keywords are keywords that contain specific meanings and can divide the SQL field into several SQL clauses; variable names are names used to identify and reference specific data values.
[0015] Step S120: Obtain all variable names corresponding to the field NP, and all entities and definition relationships corresponding to the field NF; obtain the mth variable name VN m The corresponding word definition is obtained, and the similarity between the word definition and each entity is obtained. The similarity is normalized, and if the maximum similarity among them is greater than the similarity threshold, the entity corresponding to the maximum similarity is marked; and then several marked entities corresponding to the field NF are obtained;
[0016] Step S130: Set the initial weights of all definition relationships to 0. If a definition relationship has one or more corresponding tagged entities, add up the number of all tagged entities to obtain the total weight of the definition relationship. If there are tagged entities at the beginning and end of the field NF, the field between the leftmost and rightmost tagged entities is used as the tagged entity field. If a definition relationship exists between the tagged entity fields and the corresponding total weight is greater than the weight threshold, the tagged entity field is used as the placeholder language field NFL corresponding to the field NF.
[0017] Further, step S200 includes:
[0018] Step S210: Obtain the database table corresponding to the conversion record NR and all column names corresponding to the database table. If the similarity between an entity in the field NFL and any column name corresponding to the database table is greater than a similarity threshold, take the entity as a feature entity, and then obtain all feature entities corresponding to the field NF.
[0019] Step S220: Set the initial weight of each feature entity to 1. If, in the field NF, the ath feature entity must be screened before the bth feature entity can be screened, then add 1 to the weight of the ath feature entity to obtain the weight corresponding to each feature entity.
[0020] Step S230: Obtain characteristic entity CE q , through the language model, we get the characteristic entity CE q The corresponding identifiers are based on the characteristic entity CE q The corresponding weight W q , get the characteristic entity CE q The corresponding entity information volume is: EV q =K1*W q *DN q , and normalized, where K1 is the feature entity CE q The corresponding information coefficient, DN q It is a characteristic entity CE q The number of corresponding identifiers;
[0021] The identifier is the characteristic entity CE q The corresponding database name object, where symbols, variable names and quantities can be used as identifiers, and the feature entity CE q The corresponding identifiers are characteristic entities CE q and their corresponding symbols and quantities.
[0022] Step S240: Then, according to the entity information amount of each characteristic entity corresponding to the field NF, add up and calculate the average value to obtain the characteristic coefficient corresponding to the field NF; and according to a number of conversion records, the minimum value of the characteristic coefficient corresponding to the conversion record is used as the characteristic coefficient threshold C0.
[0023] The amount of entity information represents the information richness of the feature entity. If the information richness is high, it means that the field NF corresponding to the feature entity is more complex. Then, the feature coefficient threshold C0 is obtained based on the entity information amount of each feature entity. The feature coefficient threshold C0 is used as an important criterion for whether the following target natural language field should use a placeholder.
[0024] Furthermore, the step of acquiring the target natural language field in step S300 and obtaining the characteristic coefficient corresponding to the target natural language field includes:
[0025] Step S311: Obtain all target entities corresponding to the target natural language field DNL, set the initial weight of each target entity to 0, and according to all column names of the database table corresponding to the field DNL, if the similarity between a certain target entity and any column name corresponding to the database table is greater than a similarity threshold, add 1 to the weight of the certain target entity, thereby obtaining the weight corresponding to each target entity;
[0026] Step S312: Obtain target entity TE p , through the language model, we get the target entity TE p The corresponding identifiers are based on the target entity TE p The corresponding weight W p , get the target entity TE p The corresponding entity information volume is: EV p =K2*W p *DN p , and normalized, where K2 is the target entity TE p The corresponding information coefficient, DN p Is the target entity TE p The corresponding number of identifiers; and then according to the entity information amount corresponding to each target entity, the highest entity information amount is obtained as the characteristic coefficient C1 corresponding to the field DNL.
[0027] The characteristic coefficient C1 reflects the complexity of the field DNL. If the complexity is relatively large, it means that the field DNL logic is relatively complex. If the placeholder rule is not adopted, confusion and inaccuracy may occur. At this time, the field DNL should adopt the placeholder rule. If the degree of relationship is relatively small, the field DNL logic is relatively simple, and there is no need to use placeholders.
[0028] Furthermore, in step S300, the feature coefficient is compared with the feature coefficient threshold, and the placeholder language field in the target natural language field is obtained, including: if the feature coefficient C1 is not greater than the feature coefficient threshold C0, it is determined that the field DNL has no corresponding placeholder language field; otherwise, if the feature coefficient C1 is greater than the feature coefficient threshold C0, the field DNL is divided into several language clauses according to semantic analysis, each language clause has several target entities, and the target entity corresponding to the maximum entity information amount in the field DNL is obtained, and the clause corresponding to the target entity is used as the placeholder language field of the field DNL.
[0029] According to semantic analysis, the natural language field is divided into several clauses including: query clause (clause about which column name to query), table name clause (clause about which table to query from), and condition clause (clause about the query condition).
[0030] In order to facilitate understanding of this solution, the overall explanation is as follows: the conversion record NR includes a natural language field NF and a field NSQL. The placeholder SQL field NP in the field NSQL is identified according to the field NSQL, and then the placeholder language field NFL in the natural language field NF is derived from the placeholder SQL field NP, that is, step S100 in this solution; then the characteristic coefficient threshold C0 is calculated from the natural language field NF and the placeholder language field NFL, and the characteristic coefficient threshold C0 is used as a standard for judging whether the natural language field is represented by a placeholder, that is, step S200 in this solution is obtained; further, the target natural language field DNL is obtained, and the characteristic coefficient C1 corresponding to the field DNL is calculated, and then the characteristic coefficient C1 is compared with the characteristic coefficient threshold C0. If C1≯C0, it means that the field DNL does not need to be represented by a placeholder at this time. If C1>C0, it means that the field DNL needs to be represented by a placeholder at this time, and the placeholder language field corresponding to the field DNL is obtained.
[0031] In this solution, the placeholder language field is first represented by a placeholder, and the target natural language field is converted into an SQL field containing a placeholder. This is easy to understand and reduces errors. The placeholder language field is then converted into a corresponding SQL fragment, and the SQL fragment is then substituted into the SQL field containing the placeholder to obtain a complete SQL field. The present invention first analyzes the conversion record to obtain a characteristic coefficient threshold for placeholders, and then finds the placeholder language field in the target natural language field, and then obtains the SQL field. According to whether the placeholder is used, a more accurate conversion from natural language to SQL statement is achieved.
[0032] A natural language query and conversion control system, including a placeholder language field module, a characteristic coefficient threshold module, a target natural language field module, and an SQL field module;
[0033] Placeholder language field module: used to collect conversion records of several natural languages, the conversion records include natural language fields and corresponding SQL fields, identify placeholder SQL fields in SQL fields, and placeholder language fields corresponding to the placeholder SQL fields in natural language fields;
[0034] Feature coefficient threshold module: used to obtain all feature entities in the natural language field according to the conversion record, and then obtain the feature coefficient corresponding to the conversion record according to the feature entity, and then obtain the feature coefficient threshold;
[0035] Target natural language field module: used to obtain the target natural language field, obtain the characteristic coefficient corresponding to the target natural language field, compare the characteristic coefficient with the characteristic coefficient threshold, and then obtain the placeholder language field in the target natural language field;
[0036] SQL field module: used to input the target natural language field and the placeholder language field into the pre-trained language model, represent the placeholder language field part with a placeholder, convert the target natural language field into an SQL field represented by the placeholder, and obtain the SQL fragment corresponding to the placeholder language field according to the language model, and then obtain the complete SQL field.
[0037] Further, the placeholder language field module includes a placeholder SQL field unit, a tag entity unit and a placeholder language field unit;
[0038] Placeholder SQL field unit: used for collecting a number of natural language conversion records, the conversion records include natural language fields and corresponding SQL fields, and identifying placeholder SQL fields in the SQL fields;
[0039] Marked entity unit: used to obtain all variable names corresponding to the placeholder SQL field, and all entities and definition relationships corresponding to the natural language field, to obtain each marked entity;
[0040] Placeholder language field unit: used to obtain the placeholder language field corresponding to the placeholder SQL field in the natural language field.
[0041] Further, the target natural language field module includes a target entity weight unit and a feature coefficient unit;
[0042] Target entity weight unit: used to obtain all target entities corresponding to the target natural language field, and obtain the weight corresponding to each target entity according to all column names of the database table corresponding to the target natural language field;
[0043] Feature coefficient unit: used to obtain the feature coefficient corresponding to the target natural language field and perform normalization.
[0044] Compared with the prior art, the beneficial effects achieved by the present invention are as follows: the present invention provides a natural language query and conversion control system and method, including: collecting a number of natural language conversion records, the records include natural language fields and SQL fields, identifying placeholder SQL fields and placeholder language fields; obtaining all characteristic entities in the natural language field, obtaining a characteristic coefficient threshold; obtaining a target natural language field, obtaining a characteristic coefficient corresponding to the target natural language field, and obtaining a placeholder language field in the target natural language field; inputting the target natural language field and the placeholder language field into a pre-trained language model to obtain a complete SQL field. The present invention first analyzes the conversion record to obtain a characteristic coefficient threshold that can be used for placeholder, then finds the placeholder language field in the target natural language field, and then obtains the SQL field, and according to whether the placeholder is used, a more accurate conversion from natural language to SQL statement is achieved, which not only saves time for target natural language fields with simpler logic, but also improves accuracy for target natural language fields with more complicated logic. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] The accompanying drawings are used to provide a further understanding of the present invention and constitute a part of the specification. Together with the embodiments of the present invention, they are used to explain the present invention and do not constitute a limitation of the present invention. In the accompanying drawings:
[0046] Figure 1 It is a flow chart of the natural language query and conversion control method of the present invention;
[0047] Figure 2 is a structural diagram of the natural language query and conversion control system of the present invention;
[0048] Figure 3 It is a schematic diagram of the flow structure of the natural language query and conversion control system and method of the present invention. DETAILED DESCRIPTION
[0049] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0050] See also Figure 1-Figure 3 The present invention provides a natural language query and conversion control system and method, and the corresponding natural language query and conversion control method flowchart is as follows: Figure 1 As shown. The following steps are included:
[0051] Step S100: Collect several natural language conversion records, the conversion records include natural language fields and corresponding SQL fields, identify placeholder SQL fields in the SQL fields, and placeholder language fields corresponding to the placeholder SQL fields in the natural language fields.
[0052] It should be noted that, in this embodiment, the SQL field is a data query statement obtained after the natural language field is converted. The SQL field usually includes three parts: specifying which columns to obtain, specifying which table to obtain data from, and setting the filtering conditions. For example, 'SELECT name,salary FROM employees WHERE salary>10000AND(hire_dateBETWEEN'2020-01-01'AND'2023-12-31')' is an SQL field, and its corresponding natural language field is 'Get the name and salary of employees in the employee table whose salary is greater than 10000 and whose joining time is between 2020-01-01 and 2023-12-31'.
[0053] The placeholder SQL field is a sub-segment in the SQL field. The structure of the placeholder SQL field is usually complex. For example, 'salary>10000AND(hire_date BETWEEN'2020-01-01'AND'2023-12-31')' is a placeholder SQL field, and its corresponding placeholder language field is 'Salary is greater than 10000 and the employment date is between 2020-01-01 and 2023-12-31'.
[0054] Step S110: obtaining a conversion record NR, the conversion record NR includes a natural language field NF and a corresponding SQL field NSQL; obtaining all keywords in the field NSQL, the keywords include a number of characteristic keywords, and dividing the field NSQL into a number of SQL clauses according to the characteristic keywords, each SQL clause corresponding to a number of keywords and variable names; if the number of keywords corresponding to a certain SQL clause is greater than a first quantity threshold and the number of variable names is greater than a second quantity threshold, then using the certain SQL clause as a placeholder SQL field NP corresponding to the field NSQL.
[0055] In the SQL field, a keyword is a word with a specific meaning and purpose, which is predefined by the SQL language; a characteristic keyword is a keyword that contains a specific meaning in the keyword, and the SQL field can be divided into several SQL clauses; a variable name is a name used to identify and reference a specific data value. This embodiment takes the MySQL database as an example. Common keywords for query in MySQL include: SELECT (used to specify the column to be retrieved), FROM (specify which table to obtain data from), WHERE (used to set filtering conditions), HAVING (filtering the results of the grouping after grouping), LIMIT (used to limit the number of rows returned), etc.; in this embodiment, according to the characteristic keywords of SELECT, FROM and WHERE, the field NSQL is divided into several SQL clauses according to the characteristic keywords, including a query clause (a clause about which column name to query), a table name clause (a clause about which table to query from), and a condition clause (a clause about the query condition). There may be many keywords corresponding to each SQL clause, or there may be only one. And the field corresponding to each SQL clause is a logical SQL field containing the corresponding characteristic keyword. For example, in: 'SELECT name,salary FROM employees WHERE salary>10000AND(hire_date BETWEEN'2020-01-01'AND'2023-12-31')', the corresponding query clause is: 'SELECT name,salary'; the corresponding table name clause is: 'FROM employees'; the corresponding condition clause is: 'WHERE salary>10000AND(hire_date BETWEEN'2020-01-01'AND'2023-12-31')'. In this embodiment, the first quantity threshold is 2 and the second quantity threshold is 5, which means that if the number of keywords corresponding to a certain SQL clause is not greater than 2 and the number of corresponding variable names is not greater than 5, it means that the logic of the SQL clause is relatively simple and there is no need to placeholder the clause. Otherwise, the SQL clause is used as a placeholder SQL field.
[0056] Step S120: Obtain all variable names corresponding to the field NP, and all entities and definition relationships corresponding to the field NF; obtain the mth variable name VN m The corresponding word definition is obtained, and the similarity between the word definition and each entity is obtained. The similarity is normalized, and if the maximum similarity is greater than the similarity threshold, the entity corresponding to the maximum similarity is marked; and then several marked entities corresponding to the field NF are obtained. Among them, the similarity between the word definition and each entity can be achieved through the existing word vector model technology. Since the similarity value is [0,1], in the embodiment, the similarity threshold is 0.8.
[0057] Step S130: Set the initial weights of all definition relationships to 0. If a definition relationship has one or more corresponding tagged entities, add up the number of all tagged entities to obtain the total weight of the definition relationship. If there are tagged entities at the beginning and end of the field NF, the field between the leftmost and rightmost tagged entities is used as the tagged entity field. If there is a definition relationship between the tagged entity fields and the corresponding total weight is greater than the weight threshold, the tagged entity field is used as the placeholder language field NFL corresponding to the field NF. In the embodiment, the weight threshold is 4.
[0058] Step S200: According to the conversion record, all characteristic entities in the natural language field are obtained, and then according to the characteristic entity, characteristic coefficients corresponding to the conversion record are obtained, and then characteristic coefficient thresholds are obtained.
[0059] It should be noted that the characteristic coefficient threshold is obtained from the natural language field that can be used as a placeholder in step S100, so in this scheme the characteristic coefficient threshold is an important criterion for determining whether a natural language field is used as a placeholder. As shown in the following step S300, when the characteristic coefficient is not greater than the characteristic coefficient threshold, placeholder cannot be used.
[0060] Among them, all entities in the natural language field are obtained first. Since the placeholder language field is a part of the natural language field and corresponds to the placeholder SQL field, the characteristic entity is the entity in the placeholder language field that has a high degree of matching with the variable name in the placeholder SQL field. For example, if 'salary' has a high degree of matching with 'salary', then 'salary' is the characteristic entity, and 'salary' is the variable name in the placeholder SQL field. In the SQL field, the variable name is the name used to identify and reference a specific data value.
[0061] Step S210: Obtain the database table corresponding to the conversion record NR and all column names corresponding to the database table. If the similarity between an entity in the field NFL and any column name corresponding to the database table is greater than the similarity threshold, then take the entity as a feature entity, and then obtain all feature entities corresponding to the field NF.
[0062] Step S220: Set the initial weight of each feature entity to 1. If in field NF, the ath feature entity must be screened before the bth feature entity can be screened, then add 1 to the weight of the ath feature entity to obtain the weight corresponding to each feature entity.
[0063] The a-th characteristic entity must be filtered before the b-th characteristic entity can be filtered. For example, if you want to obtain the math scores of the top 10 students in Class 3, then in this embodiment, the class is the characteristic entity a and the ranking is the characteristic entity b. You must first obtain that the class is Class 3 before you can get the math scores of the top ten students. At this time, since the class must be filtered before the ranking can be filtered, the weight of the characteristic entity corresponding to the class is added by 1.
[0064] Step S230: Obtain characteristic entity CE q , through the language model, we get the characteristic entity CE q The corresponding identifiers are based on the characteristic entity CE q The corresponding weight W q , get the characteristic entity CE q The corresponding entity information volume is: EV q =K1*W q *DN q , and normalized, where K1 is the feature entity CE q The corresponding information coefficient, DN q It is a characteristic entity CE q The corresponding number of identifiers.
[0065] Among them, the identifier is the characteristic entity CE q The corresponding database name object, where symbols, variable names and quantities can be used as identifiers, and the feature entity CE q The corresponding identifiers are characteristic entities CE q For example, in 'SELECT name,salary FROM employees WHERE salary>10000AND(hire_dateBETWEEN'2020-01-01'AND'2023-12-31');', if 'hire_date' is a feature entity, then its corresponding identifiers are 'hire_date', 'BETWEEN', '2020-01-01', 'AND' and '2023-12-31'.
[0066] Step S240: Then, according to the entity information amount of each characteristic entity corresponding to the field NF, the average value is added to obtain the characteristic coefficient corresponding to the field NF; and according to a number of conversion records, the minimum value of the characteristic coefficient corresponding to the conversion record is used as the characteristic coefficient threshold C0. The characteristic coefficient threshold C0 can be specifically determined according to actual conditions.
[0067] The amount of entity information represents the information richness of the feature entity. If the information richness is high, it means that the field NF corresponding to the feature entity is more complex. Then, the feature coefficient threshold C0 is obtained based on the entity information amount of each feature entity. The feature coefficient threshold C0 is used as an important criterion for whether the following target natural language field should use a placeholder.
[0068] Step S300: Acquire a target natural language field, obtain a characteristic coefficient corresponding to the target natural language field, compare the characteristic coefficient with a characteristic coefficient threshold, and then obtain a placeholder language field in the target natural language field.
[0069] The step of acquiring the target natural language field and obtaining the characteristic coefficient corresponding to the target natural language field includes:
[0070] Step S311: Obtain all target entities corresponding to the target natural language field DNL, set the initial weight of each target entity to 0, and according to all column names of the database table corresponding to the field DNL, if the similarity between a target entity and any column name corresponding to the database table is greater than the similarity threshold, add 1 to the weight of the target entity, and then obtain the weight corresponding to each target entity. The weight corresponding to each target entity is greater than or equal to 0.
[0071] Step S312: Obtain target entity TE p , through the language model, we get the target entity TE p The corresponding identifiers are based on the target entity TE p The corresponding weight W p , get the target entity TE p The corresponding entity information volume is: EV p =K2*W p *DN p , and normalized, where K2 is the target entity TE p The corresponding information coefficient, DN p Is the target entity TE p The corresponding number of identifiers; and then according to the entity information amount corresponding to each target entity, the highest entity information amount is obtained as the characteristic coefficient C1 corresponding to the field DNL.
[0072] The characteristic coefficient C1 reflects the complexity of the field DNL. If the complexity is relatively large, it means that the field DNL logic is relatively complex. If the placeholder rule is not used, confusion and inaccuracy may occur. At this time, the field DNL should use the placeholder rule. If the degree of this relationship is relatively small, the field DNL logic is relatively simple, and there is no need to use a placeholder. The characteristic coefficient C1 can be adjusted according to the actual situation.
[0073] Among them, the characteristic coefficient is compared with the characteristic coefficient threshold, and then the placeholder language field in the target natural language field is obtained: if the characteristic coefficient C1 is not greater than the characteristic coefficient threshold C0, it is determined that the field DNL has no corresponding placeholder language field; otherwise, if the characteristic coefficient C1 is greater than the characteristic coefficient threshold C0, the field DNL is divided into several language clauses according to semantic analysis, and there are several target entities in each language clause, and the target entity corresponding to the maximum entity information amount in the field DNL is obtained, and the clause corresponding to the target entity is used as the placeholder language field of the field DNL.
[0074] In this embodiment, according to semantic analysis, the natural language field is divided into several clauses including: query clause (a clause about which column name to query), table name clause (a clause about which table to query from), and condition clause (a clause about the query condition). Taking 'get the name and salary of employees in the employee table whose salary is greater than 10,000 and whose joining time is between 2020-01-01 and 2023-12-31' as an example, the corresponding query clause is: 'get the employee name and salary'; the corresponding table name clause is: 'get in the employee table'; the corresponding condition clause is: 'salary greater than 10,000 and joining time is between 2020-01-01 and 2023-12-31'.
[0075] In order to facilitate understanding of steps S100 to S300, this embodiment also provides a flow chart of a natural language query and conversion control system and method, as shown in the attached figure. Figure 3 As shown in the figure, the overall process of this solution is:
[0076] The conversion record NR includes a natural language field NF and a field NSQL. The placeholder SQL field NP in the field NSQL is identified according to the field NSQL, and then the placeholder language field NFL in the natural language field NF is derived from the placeholder SQL field NP, that is, step S100 in this solution; then the characteristic coefficient threshold C0 is calculated from the natural language field NF and the placeholder language field NFL, and the characteristic coefficient threshold C0 is used as a standard for judging whether the natural language field is represented by a placeholder, that is, step S200 in this solution is obtained; further obtain the target natural language field DNL, and calculate the characteristic coefficient C1 corresponding to the field DNL, and then compare the characteristic coefficient C1 with the characteristic coefficient threshold C0. If C1≯C0, it means that the field DNL does not need to be represented by a placeholder at this time. If C1>C0, it means that the field DNL needs to be represented by a placeholder at this time, and the placeholder language field corresponding to the field DNL is obtained.
[0077] Step S400: Input the target natural language field and the placeholder language field into a pre-trained language model, represent the placeholder language field part with a placeholder, convert the target natural language field into a SQL field represented by the placeholder, and obtain the SQL fragment corresponding to the placeholder language field according to the language model, and then obtain the complete SQL field.
[0078] In this solution, the placeholder language field is first represented by a placeholder, and the target natural language field is converted into an SQL field containing a placeholder. This is easy to understand and reduces errors. The placeholder language field is then converted into a corresponding SQL fragment, and the SQL fragment is then substituted into the SQL field containing the placeholder to obtain a complete SQL field. The present invention first analyzes the conversion record to obtain a characteristic coefficient threshold for placeholders, and then finds the placeholder language field in the target natural language field, and then obtains the SQL field. According to whether the placeholder is used, a more accurate conversion from natural language to SQL statement is achieved.
[0079] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device.
[0080] Finally, it should be noted that the above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art can still modify the technical solutions described in the aforementioned embodiments or replace some of the technical features therein by equivalents. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A natural language query and conversion control method, characterized in that: The following steps are involved: Step S100: collecting a number of natural language conversion records, wherein the conversion records include a natural language field and a corresponding SQL field, identifying a placeholder SQL field in the SQL field, and a placeholder language field corresponding to the placeholder SQL field in the natural language field; Step S110: obtaining a conversion record NR, wherein the conversion record NR includes a natural language field NF and a corresponding SQL field NSQL; Acquire all keywords in the field NSQL, wherein the keywords include a number of characteristic keywords, and divide the field NSQL into a number of SQL clauses according to the characteristic keywords, wherein each SQL clause corresponds to a number of keywords and variable names; if the number of keywords corresponding to a certain SQL clause is greater than a first quantity threshold and the number of variable names is greater than a second quantity threshold, use the certain SQL clause as a placeholder SQL field NP corresponding to the field NSQL; Step S120: Obtain all variable names corresponding to the field NP, and all entities and definition relationships corresponding to the field NF; obtain the mth variable name VN m The corresponding word definition is obtained, and the similarity between the word definition and each entity is obtained, the similarity is normalized, and if the maximum similarity among them is greater than the similarity threshold, the entity corresponding to the maximum similarity is marked; and then a number of marked entities corresponding to the field NF are obtained; Step S130: setting the initial weights of all the defined relationships to 0; if a defined relationship has one or more corresponding marked entities, adding up the numbers of all marked entities to obtain the total weight of the defined relationship; If there are no marked entities at the beginning and end of the field NF, the field between the leftmost and rightmost marked entities is used as the marked entity field. If there is a definition relationship between the marked entity fields and the corresponding total weight is greater than the weight threshold, the marked entity field is used as the placeholder language field NFL corresponding to the field NF; Step S200: according to the conversion record, all characteristic entities in the natural language field are obtained, and then according to the characteristic entities, characteristic coefficients corresponding to the conversion record are obtained, and then characteristic coefficient thresholds are obtained; Step S210: Obtain the database table corresponding to the conversion record NR and all column names corresponding to the database table. If the similarity between an entity in the field NFL and any column name corresponding to the database table is greater than a similarity threshold, take the entity as a feature entity, and then obtain all feature entities corresponding to the field NF. Step S220: Set the initial weight of each feature entity to 1. If, in the field NF, the ath feature entity must be screened before the bth feature entity can be screened, then add 1 to the weight of the ath feature entity to obtain the weight corresponding to each feature entity. Step S230: Obtain characteristic entity CE q , through the language model, we get the characteristic entity CE q The corresponding identifiers are based on the characteristic entity CE q The corresponding weight W q , get the characteristic entity CE q The corresponding entity information volume is: EV q =K1*W q *DN q , and normalized, where K1 is the feature entity CE q The corresponding information coefficient, DN q It is a characteristic entity CE q The number of corresponding identifiers; Step S240: then, according to the entity information amount of each characteristic entity corresponding to the field NF, add and calculate the average value to obtain the characteristic coefficient corresponding to the field NF; and according to the plurality of conversion records, use the minimum value of the characteristic coefficient corresponding to the conversion record as the characteristic coefficient threshold C0; Step S300: acquiring a target natural language field, obtaining a characteristic coefficient corresponding to the target natural language field, comparing the characteristic coefficient with the characteristic coefficient threshold, and then obtaining a placeholder language field in the target natural language field; Step S400: Input the target natural language field and the placeholder language field into a pre-trained language model, represent the placeholder language field part with a placeholder, convert the target natural language field into a SQL field represented by the placeholder, and obtain the SQL fragment corresponding to the placeholder language field according to the language model, and then obtain the complete SQL field.
2. The natural language query and conversion control method according to claim 1, characterized in that: The step of acquiring the target natural language field in step S300 and obtaining the characteristic coefficient corresponding to the target natural language field includes: Step S311: Obtain all target entities corresponding to the target natural language field DNL, set the initial weight of each target entity to 0, and according to all column names of the database table corresponding to the field DNL, if the similarity between a certain target entity and any column name corresponding to the database table is greater than a similarity threshold, add 1 to the weight of the certain target entity, thereby obtaining the weight corresponding to each target entity; Step S312: Obtain target entity TE p , through the language model, we get the target entity TE p The corresponding identifiers are based on the target entity TE p The corresponding weight W p , get the target entity TE p The corresponding entity information volume is: EV p =K2*W p *DN p , and normalized, where K2 is the target entity TE p The corresponding information coefficient, DN p Is the target entity TE p The corresponding number of identifiers; and then according to the entity information amount corresponding to each target entity, the highest entity information amount is obtained as the characteristic coefficient C1 corresponding to the field DNL.
3. The natural language query and conversion control method according to claim 2, characterized in that: In step S300, the feature coefficient is compared with the feature coefficient threshold, and then the placeholder language field in the target natural language field is obtained, including: if the feature coefficient C1 is not greater than the feature coefficient threshold C0, it is determined that the field DNL has no corresponding placeholder language field; otherwise, if the feature coefficient C1 is greater than the feature coefficient threshold C0, the field DNL is divided into a number of language clauses according to semantic analysis, each language clause has a number of target entities, and the target entity corresponding to the maximum entity information amount in the field DNL is obtained, and the clause corresponding to the target entity is used as the placeholder language field of the field DNL.
4. A natural language query and conversion control system, used to execute the natural language query and conversion control method according to any one of claims 1 to 3, characterized in that: The system includes a placeholder language field module, a characteristic coefficient threshold module, a target natural language field module and an SQL field module; Placeholder language field module: used to collect conversion records of several natural languages, wherein the conversion records include natural language fields and corresponding SQL fields, identify placeholder SQL fields in SQL fields, and placeholder language fields corresponding to the placeholder SQL fields in natural language fields; A characteristic coefficient threshold module: used to obtain all characteristic entities in the natural language field according to the conversion record, and then obtain the characteristic coefficient corresponding to the conversion record according to the characteristic entity, and then obtain the characteristic coefficient threshold; Target natural language field module: used to obtain the target natural language field, obtain the characteristic coefficient corresponding to the target natural language field, compare the characteristic coefficient with the characteristic coefficient threshold, and then obtain the placeholder language field in the target natural language field; SQL field module: used to input the target natural language field and the placeholder language field into a pre-trained language model, represent the placeholder language field part with a placeholder, convert the target natural language field into an SQL field represented by the placeholder, and obtain the SQL fragment corresponding to the placeholder language field according to the language model, and then obtain the complete SQL field.
5. The natural language query and conversion control system according to claim 4, characterized in that: The placeholder language field module includes a placeholder SQL field unit, a tag entity unit and a placeholder language field unit; A placeholder SQL field unit is used to collect a number of natural language conversion records, wherein the conversion records include a natural language field and a corresponding SQL field, and identify a placeholder SQL field in the SQL field; Marked entity unit: used to obtain all variable names corresponding to the placeholder SQL field, and all entities and definition relationships corresponding to the natural language field, to obtain each marked entity; Placeholder language field unit: used to obtain the placeholder language field corresponding to the placeholder SQL field in the natural language field.
6. The natural language query and conversion control system according to claim 5, characterized in that: The target natural language field module includes a target entity weight unit and a feature coefficient unit; Target entity weight unit: used to obtain all target entities corresponding to the target natural language field, and obtain the weight corresponding to each target entity according to all column names of the database table corresponding to the target natural language field; Feature coefficient unit: used to obtain the feature coefficient corresponding to the target natural language field and perform normalization.
Citation Information
Patent Citations
Voice recognition method and device, medium and electronic equipment
CN110503956A
Data processing method, system and device, storage medium and program product
CN118132684A