Natural language sentence parsing method, device, electronic device and storage medium
By using a unified model in natural language processing to parse natural language statements, and using slot mentions and relationships to determine SQL statements, the high cost and low accuracy problems of multi-model collaborative work in the prior art are solved, and a more efficient and accurate natural language to SQL conversion is achieved.
Patent Information
- Application Number
- CN202011523884.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-12-18
- Publication Date
- 2025-04-18
- Estimated Expiration
- 2040-12-18
AI Technical Summary
The method of converting natural language statements into SQL statements in the prior art requires multiple dedicated models to work together, resulting in high cost of model training, deployment and maintenance, and insufficient recognition accuracy.
A unified model is used to parse natural language statements, and the target SQL statement is determined through slot mention and slot relationship, improving parsing efficiency and accuracy.
By analyzing natural language statements by unified model, the cost of model training and maintenance is reduced, and the accuracy and efficiency of analysis is improved.
Smart Images

Figure CN112632100B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of natural language processing, and in particular to a natural language sentence parsing method, device, electronic device and storage medium. Background Art
[0002] In the prior art, a method for converting natural language sentences into structured query language (SQL) statements is a framework slot filling method based on deep learning. By establishing several special models, a complete sentence of natural language is input into each special model relatively independently to predict a specific type of slot. This method has the following disadvantages: many (6 or more) special models are used to work together to generate SQL statements, which will bring difficulties in model training, deployment and maintenance in practical applications, and the technical cost is high; the complete sentence of natural language is predicted for a specific type of slot relatively independently, resulting in insufficient recognition accuracy of some slot types. Summary of the invention
[0003] The embodiments of the present application provide a natural language statement parsing method, device, electronic device and storage medium, which can parse natural language into SQL statements through a unified model, improve parsing efficiency, determine target SQL statements by using slot mentions and slot relationships, and improve the accuracy of natural language statement parsing.
[0004] In a first aspect, an embodiment of the present application provides a natural language sentence parsing method, the method comprising:
[0005] Obtain a natural language statement to be parsed; perform slot parsing on the natural language statement to obtain slot mentions and slot relationships of the natural language statement, wherein the slot mentions are structured query language SQL elements represented by each slot after the natural language statement is slot parsed, and the slot relationships are relationships between slot mentions;
[0006] Determine at least one to-be-linked element according to the slot mention and the slot relationship, the to-be-linked element being a SQL element for generating an SQL statement;
[0007] The at least one to-be-linked element is linked to obtain a target SQL statement.
[0008] In a second aspect, an embodiment of the present application provides a natural language sentence parsing device, the natural language sentence parsing device includes an extraction link model, the extraction link model includes an extractor and a linker, wherein:
[0009] The extractor is used to obtain a natural language sentence to be parsed; perform slot parsing on the natural language sentence to obtain slot mentions and slot relationships of the natural language sentence, wherein the slot mentions are structured query language SQL elements represented by each slot after the natural language sentence is slot parsed, and the slot relationships are relationships between slot mentions;
[0010] The linker is used to determine at least one to-be-linked element according to the slot mention and the slot relationship, wherein the to-be-linked element is a SQL element used to generate an SQL statement;
[0011] The linker is further used to link the at least one element to be linked to obtain a target SQL statement.
[0012] In a third aspect, an embodiment of the present application provides an electronic device, comprising: a transceiver, a processor and a memory, wherein the processor is connected to the memory, the memory is used to store a computer program, and the processor is used to execute the computer program stored in the memory, so that the electronic device performs the method described in the first aspect.
[0013] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and the computer program enables a computer to execute the method described in the first aspect.
[0014] In a fifth aspect, an embodiment of the present application provides a computer program product, which includes a non-transitory computer-readable storage medium storing a computer program, and the computer is operable to cause the computer to execute the method described in the first aspect.
[0015] Implementing the embodiments of the present application has the following beneficial effects:
[0016] It can be seen that in the embodiment of the present application, a natural language statement to be parsed is obtained; slot parsing is performed on the natural language statement to obtain slot mentions and slot relationships of the natural language statement, at least one element to be linked is determined according to the slot mentions and the slot relationships, and at least one element to be linked is linked to obtain a target SQL statement. In this way, by adopting a unified model to parse the natural language statement, the parsing efficiency can be improved compared to adopting many dedicated models to work together for slot identification, and the slot mentions and slot relationships are used to parse the natural language statement into a target SQL statement, thereby improving the accuracy of the natural language statement parsing. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0018] Figure 1 A flowchart of a natural language sentence parsing method provided in an embodiment of the present application;
[0019] Figure 2A A schematic diagram showing a BERT-based extractor extracting slot mentions and slot relationships provided in an embodiment of the present application;
[0020] Figure 2B A schematic diagram showing the annotation of a natural language sentence provided in an embodiment of the present application;
[0021] Figure 3 A flowchart of another natural language sentence parsing method provided in an embodiment of the present application;
[0022] Figure 4 A block diagram of the functional units of a natural language sentence parsing device provided in an embodiment of the present application;
[0023] Figure 5 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0024] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0025] The terms "first", "second", "third" and "fourth" etc. in the specification and claims of the present application and the drawings are used to distinguish different objects, rather than to describe a specific order. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not limited to the listed steps or units, but optionally includes steps or units that are not listed, or optionally includes other steps or units inherent to these processes, methods, products or devices.
[0026] Reference to "embodiments" herein means that a particular feature, result, or characteristic described in conjunction with the embodiments may be included in at least one embodiment of the present application. The appearance of the phrase in various locations in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment that is mutually exclusive with other embodiments. It is explicitly and implicitly understood by those skilled in the art that the embodiments described herein may be combined with other embodiments.
[0027] See also Figure 1 , Figure 1 A natural language sentence parsing method is provided in an embodiment of the present application. The method comprises the following steps:
[0028] 101. Obtain a natural language statement to be parsed; perform slot parsing on the natural language statement to obtain slot mentions and slot relationships of the natural language statement, wherein the slot mentions are structured query language SQL elements represented by each slot after the natural language statement is slot parsed, and the slot relationships are relationships between slot mentions.
[0029] Among them, slot mentions include mentions of all columns, values, operators, and aggregate functions in natural language sentences; slot relations include the columns, operators, and values associated with each filter condition in the WHERE clause as filter relations, and aggregate functions and associated columns in the SELECT clause as selection relations.
[0030] The natural language sentence may be, for example: What is the total sum of 50m splits for Josefin Lillhage in lanes above 8?
[0031] In the above example sentence, there are multiple slot mentions, such as two columns "Split(50m)" and "Lanes", two values "JosefinLillhage" and "8", and the operator ">"; slot relations can be used to describe the filter conditions with a triple of <column, operator, value>. Slot mentions with filter relations usually appear adjacent to each other, for example<lanes,above,8> , the three constitute a filtering condition.
[0032] Optionally, in step 101, slot parsing the natural language sentence to obtain slot mentions and slot relationships of the natural language sentence may include:
[0033] 11. Labeling the natural language sentence to obtain a plurality of labels, each label including a function label and a position label;
[0034] 12. Combining the multiple tags into a tag sequence according to the function tag and the position tag of each tag in the multiple tags;
[0035] 13. Convert multiple tags in the tag sequence into slot mentions and slot relationships.
[0036] Among them, multiple tags include role tags and span tags. Role tags are used to indicate slot mentions, and span tags are used to indicate slot relationships.
[0037] In a specific implementation, slot mentions and slot relationships in a natural language sentence are identified as multiple labels, and the slot mentions in the natural language sentence can be identified and represented as role labels; the slot relationships in the natural language sentence are identified and represented as span labels. In terms of description form, each label includes a function label and a position label. For example, a label can be represented as l∈{T×{B,I}, O}, where × represents the Cartesian product of the function label T and the position label {B, I}, B refers to the starting point of a specific annotation, I refers to the continuation content, and the O label is assigned to symbols outside any annotation type of interest, as shown in Table 1 below, which is an example of a label representing slot mentions and slot relationships provided in an embodiment of the present application:
[0038]
[0039] Table 1
[0040] Optionally, the method is applied to an extraction link model, the extraction link model includes an extractor and a linker, the extractor includes a preset language model, an attention layer and a conditional random field CRF layer, and in the above step 11, the natural language sentence is annotated to obtain multiple labels, which may include:
[0041] 14. Input the natural language sentence and at least one table header in a preset SQL database into the language model;
[0042] 15. Encode the natural language sentence and the header by using the language model to obtain a model representation vector of each word in the natural language sentence and a model representation vector of each word in the header;
[0043] 16. Inputting the model representation vector of each word in the natural language sentence and the model representation vector of each word in the header into the attention layer, wherein the words in the natural language sentence include symbols and the words in the header include column names;
[0044] 17. Performing attention encoding on the model representation vector of the symbol in the natural language sentence and the column name in the table header through the attention layer to obtain the representation vector of the symbol and the representation vector of the column name after attention encoding;
[0045] 18. Add the representation vector of the attention-encoded symbol and the representation vector of the column name to the corresponding model representation vector one by one to obtain an enhanced representation vector;
[0046] 19. Input the enhanced representation vector into the conditional random field (CRF) layer, and the CRF layer decodes the enhanced representation vector to obtain multiple labels.
[0047] The above-mentioned preset language model can be, for example, a bidirectional encoder representation from transformers (BERT) model, see Figure 2A , Figure 2A A schematic diagram of a BERT model-based extractor for extracting slot mentions and slot relationships is provided in an embodiment of the present application, wherein a natural language sentence and at least one table header in an SQL database can be input into the BERT model, and the formula of the BERT model is as follows:
[0048] I = [Q; C]
[0049] =q1,q2,…,q L ,[SEP],G 1,1 , C 1,2 ,…,[SEP],c 2,1 , …, C M,m (1)
[0050] Q(|Q|=L)
[0051] C=c1,c2,…,c M (|C|=M)
[0052] Among them, Q represents a natural language sentence, C represents a table header, and each table header c i It can contain multiple words. SEP is a separator symbol used to insert different headers. i Between the question Q and the first header c1, the BERT model outputs the model representation vector of each word, H [CLS] , H What , H is , H the , H [SEP] , H player , H [SEP] , Hcountry .
[0053] The complete formula of the extractor is as follows:
[0054] Q B ;Q C =BERT([Q;C])
[0055] Q att =Attention(Q B , C B , C B )+Q B (2)
[0056] L=CRF(W L Q att )
[0057] Among them, BERT represents the BERT model and CRF represents the CRF layer.
[0058] Among them, the formula for encoding the attention layer is as follows:
[0059] Q′=W Q Q B
[0060] C′=W C C B (3)
[0061] Q att =Softmax(Q′C′ T )C′
[0062] Alternatively, if Figure 2A As shown, the enhanced representation vector for identifying slot mentions and the enhanced representation vector for identifying slot relationships can be input into two independent CRF layers respectively, one CRF layer is used to decode to obtain the role label corresponding to the slot mention, for example, O, O, B, etc., and the other CRF layer is used to decode to obtain the span label corresponding to the slot relationship, for example, O, B, I, etc.
[0063] Optionally, the multiple tags include a role tag for indicating slot mentions and a span tag for indicating slot relationships. In the above step 12, combining the multiple tags into a tag sequence according to the function tag and the position tag of each tag in the multiple tags includes:
[0064] 1201. Searching for role tags and span tags having the same function tag among the multiple tags;
[0065] 1202. According to the position tag, the role tag and the span tag having the same function tag are combined into a tag sequence.
[0066] Among them, the searching for at least one role tag or span tag with the same functional tag among the multiple tags can specifically match the role tags in the multiple tags to obtain at least one role tag with the same functional tag. Specifically, the longest multiple tags can be searched, and the multiple tags all have the same functional tag, and only the position tag of the first tag is B, and the position tag B is used to mark the starting point. For example, the mention of the Select column is searched by matching the BS, IS* regular expression and the tag sequence mentioned in the slot to find a tag sequence of BS, IS, ..., IS.
[0067] Optionally, in the above step 13, converting the multiple tags in the tag sequence into slot mentions and slot relationships may include:
[0068] 1301. Convert the role tags in the tag sequence into symbols and output them as slot mentions;
[0069] 1302. Convert the span tags in the tag sequence into selection spans and filter spans, and represent them using identification methods corresponding to the selection spans and filter spans, respectively.
[0070] In a specific implementation, the symbol of the label can be used as a slot mention. After obtaining the label sequence of BS, IS, ..., IS, the symbol of the label can be used as a mention of the Select column. For example, the sequence labeling result is BS IS, which corresponds to "50m splits" in the natural language question. By converting all role labels in the label sequence, all slot mentions can be obtained.
[0071] The span labels in the label sequence can be converted into selection spans and filter spans, and represented by identification methods corresponding to the selection spans and filter spans, respectively, for example, respectively. Figure 3 The dotted box of the first color and the dotted box of the second color in the figure indicate that the first color may be red and the second color may be black, which is not limited in the embodiment of the present application.
[0072] For an example, see Figure 2B , Figure 2BA demonstration schematic diagram of annotating natural language sentences provided in an embodiment of the present application, wherein a natural language question is parsed into slot mentions and slot relationships, and for slot mentions, aggregation functions are annotated with a third color (e.g., pink), columns are annotated with a fourth color (e.g., blue), operators are annotated with a fourth color (e.g., orange), and values are annotated with a fifth color (e.g., green); for slot relationships, a dotted box of the first color is used to represent a selection span, and a dotted box of the second color is used to represent a filter span; slot mentions are represented by role labels, and slot relationships are represented by span labels.
[0073] For example, for a natural language statement, multiple labels can be converted into selection spans, filter spans, and multiple slot mentions. When performing span label conversion, according to the label examples shown in Table 1, each filter condition in the SELECT clause and the WHERE clause will be identified as a corresponding span and marked as Sel and Cond respectively. In the SELECT clause span, it can be continuously marked as AGG by identifying i Label and S label symbols to extract the select columns and aggregate functions; similarly, in the filter span, C, OP i ,V labels are identified as column names, operators and values, which are constructed according to the corresponding ranges of the prediction span. <C,OP i , V> triples to describe the filter conditions. When the aggregation function and operator are missing, their default values are AGG o (indicating no aggregate function) and OP0 (indicating the "=" operator).
[0074] 102. Determine at least one to-be-linked element according to the slot mention and the slot relationship, where the to-be-linked element is a SQL element used to generate an SQL statement.
[0075] The elements to be linked refer to SQL elements used to link to form SQL statements, and the elements to be linked may include columns to be linked, values to be linked, operators to be linked, and the like.
[0076] In a specific implementation, explicit column mentions and implicit column mentions in the SELECT clause and filter conditions in the WHERE clause may be linked.
[0077] Optionally, the slot mention includes a column mention and a value mention. In the above step 102, determining at least one SQL element to be linked according to the slot mention and the slot relationship includes:
[0078] 21. Mapping the column mention and the value mention to at least one target table header in a preset SQL database according to the slot mention and the slot relationship, wherein the preset SQL database includes a plurality of table headers, and the plurality of table headers include the at least one target table header;
[0079] 22. Determine each target header in the at least one target header as an element to be linked, to obtain at least one element to be linked.
[0080] In a specific implementation, the column reference can be associated with the table header C in the preset SQL database. i A match is performed to obtain a target header having the highest matching value with the column mention, so that for all slot relationships and slot mentions, at least one target header can be matched, and each of the at least one target header is determined as an element to be linked.
[0081] Optionally, the to-be-linked element includes a to-be-linked column, the slot reference includes a column reference, and the slot relationship includes a filter span. In the above step 21, mapping the column reference and the value reference to at least one target table header in a preset SQL database according to the slot reference and the slot relationship includes:
[0082] 2101. If the slot mention includes an explicit column mention, match the explicit column mention with a table header in the SQL database to obtain a first target table header with a highest matching value, where the first target table header is a column to be linked.
[0083] 2102. If there is an implicit column mention in the filter span, the value mention, as well as the symbol and placeholder in the filter span are concatenated to obtain the implicit column mention, and the implicit column mention is matched with the table header in the SQL database to obtain a second target table header with the highest matching value, and the second target table header is the column to be linked.
[0084] In the specific implementation, the table header C in the SQL database will be i The formula to match the column mention is as follows:
[0085]
[0086]
[0087] In specific implementation, matching can also be performed through a preset matching model, and the estimation function is as follows:
[0088] f([C i ; span; Q]) → {0, 1}, where C iis the table header, and span is the extracted column mention (for linking explicit column mentions) or the extracted value v (for linking implicit column mentions).
[0089] 103. Link the at least one element to be linked to obtain a target SQL statement.
[0090] The target SQL statement is obtained by linking at least one element to be linked. For example, for the example sentence mentioned in the embodiment of the present application, after parsing, the target SQL statement obtained may be: SELECT SUM(Split(50m))FROM sometable WHEREName='JosefinLillhage'ANDLane>8.
[0091] Optionally, after step 103, the following steps may also be included:
[0092] Perform string matching on the slot mentions in the target SQL statement and the value mentions and part of the column mentions in the natural language statement to obtain matching items and non-matching items; and find the remaining non-matching items through an aligner;
[0093] The matching items and the non-matching items in the target SQL statement are converted by using the symbols in the preset corpus to obtain the converted target SQL statement;
[0094] The converted target SQL statement is aligned with the natural language statement to obtain an aligned target SQL statement.
[0095] The aligner may be a Berkeley aligner.
[0096] In a specific implementation, slot mentions can be annotated by aligning slots in the target SQL statement, such as columns, values, and aggregate functions in the SELECT / WHERE clause, with symbols in the natural language statement. For example, all values in the target SQL statement appear in exactly the same string form, while the string mentioned in the target SQL statement (e.g., Split(50m) and Lane) is different. The correspondence between the SQL operator ">" and the symbol "above" also appears in other SQL statements, so it can be inferred by their co-occurrence, as can the correspondence between the aggregate function "sum" and the symbol "total sum".
[0097] As an example, an exact string match is performed on two symbol lists that match the value "josefinlillhage" and partial columns (such as Split and 50m).
[0098] Among them, mentions can be annotated by aligning the slots in SQL questions with the symbols in natural language questions, such as columns, values, and aggregate functions in SELECT / WHERE clauses.
[0099] First, string matching is performed on the values and partial columns in the two symbol lists corresponding to the slots in the SQL query statement and the symbols in the natural language question; for example, exact string matching is performed on the lowercase versions of the two symbol lists, which match the value "josefinlillhage" and partial columns (such as Split and 50m); then, the remaining mismatches are found through the Berkeley aligner, where the Berkeley aligner can estimate the co-occurrence of symbols in the parallel corpus. Since keywords such as SELECT, WHERE, and AND represent SQL syntax rather than semantics in natural language, noise may be introduced and can be deleted from the SQL query. For aggregate functions and operators, special symbols can be used to represent them. Applying this transformation to (a) will result in (b). Then (b) is aligned with the natural language question (c) as shown below:
[0100] (a) Target SQL statement: SELECT sum(Split(50m)) FROM TWHERE Name = "JosefinLillhage" AND Lane>8
[0101] (b) Target SQL statement after conversion: AGG4 Split(50m)Name OP0 josefinlillhage LaneOP1 8
[0102] (c) Natural language sentence: What is the total sum of 50m splits for Josefin Lillhage in lanes above 8?
[0103] (d) Statistical alignment: 11-14, 10-13, 9-12, 8-10, 7-9, 6-8, 3-6, 2-5, 0-4, 0-3
[0104] Among them, the output of the aligner is shown in (d), where "ij" indicates that the i-th symbol (indexed from 0) in the converted SQL is aligned with the j-th symbol in the natural language question, and i and j are positive integers. For example, 0-3 and 0-4 indicate that AGG4 is aligned with "total sum", and 10-13 indicates that the second operator OP1 is aligned with "above". It can be seen that the statistical aligner does a good job in aligning operators and aggregate functions. In fact, the aligner can also align some commonly used aliases and many semantically similar words that cannot be found through string matching. The statistical aligner can sometimes produce null alignments for some symbols in some SQLs. For this purpose, another unsupervised algorithm based on word and semantic similarity can be used to supplement the missing alignments; finally, role labels can be generated based on the aligned elements, and span labels are assigned by the minimum text span covering all elements in the SELECT clause or WHERE clause.
[0105] It can be seen that in the embodiment of the present application, a natural language statement to be parsed is obtained; slot parsing is performed on the natural language statement to obtain slot mentions and slot relationships of the natural language statement, at least one element to be linked is determined according to the slot mentions and the slot relationships, and at least one element to be linked is linked to obtain a target SQL statement. In this way, by adopting a unified model to parse the natural language statement, the parsing efficiency can be improved compared to adopting many dedicated models to work together for slot identification, and the slot mentions and slot relationships are used to parse the natural language statement into a target SQL statement, thereby improving the accuracy of the natural language statement parsing.
[0106] See also Figure 3 , Figure 3 A flowchart of a natural language sentence parsing method provided in an embodiment of the present application. The method of this embodiment includes the following steps:
[0107] 301. Input the natural language sentence and at least one table header in a preset SQL database into the language model.
[0108] 302. Encode the natural language sentence and the header using the language model to obtain a model representation vector of each word in the natural language sentence and a model representation vector of each word in the header.
[0109] 303. The language model inputs the model representation vector of each word in the natural language sentence and the model representation vector of each word in the header into the attention layer, wherein the words in the natural language sentence include symbols, and the words in the header include column names.
[0110] 304. Perform attention encoding on the model representation vector of the symbol in the natural language sentence and the column name in the table header through the attention layer to obtain the representation vector of the symbol and the representation vector of the column name after attention encoding.
[0111] 305. Add the representation vector of the attention-encoded symbol and the representation vector of the column name to the corresponding model representation vector one by one to obtain an enhanced representation vector.
[0112] 306. Input the enhanced representation vector into the CRF layer, and the CRF layer decodes the enhanced representation vector to obtain multiple labels.
[0113] 307. The CRF layer combines multiple tags into a tag sequence according to the function tag and the position tag of each tag in the multiple tags.
[0114] 308. The CRF layer converts multiple tags in the tag sequence into slot mentions and slot relations.
[0115] 309. Map the column mention and the value mention to at least one target header in a preset SQL database according to the slot mention and the slot relationship, where the preset SQL database includes multiple headers, and the multiple headers include the at least one target header.
[0116] 310. Link the at least one element to be linked to obtain a target SQL statement.
[0117] Among them, in this embodiment Figure 1 The same contents as those in the illustrated embodiments will not be described again here.
[0118] It can be seen that in the embodiment of the present application, slot mentions and slot relationships of natural language sentences are extracted through the language model, attention layer and CRF layer in the extractor, and at least one element to be linked is determined according to the slot mentions and the slot relationships; the at least one element to be linked is linked to obtain a target SQL statement. Natural language sentences can be parsed by adopting a unified model. Compared with adopting many dedicated models to work together for slot identification, the parsing efficiency can be improved, and the target SQL statement is determined by using slot mentions and slot relationships, thereby improving the accuracy of natural language statement parsing.
[0119] See also Figure 4 , Figure 4 The functional unit composition block diagram of a natural language sentence parsing device provided in an embodiment of the present application. The natural language sentence parsing device 400 includes an extraction link model, and the extraction link model includes an extractor 401 and a linker 402, wherein:
[0120] The extractor 401 is used to obtain a natural language sentence to be parsed; perform slot parsing on the natural language sentence to obtain slot mentions and slot relationships of the natural language sentence, wherein the slot mentions are structured query language SQL elements represented by each slot after the natural language sentence is slot parsed, and the slot relationships are relationships between slot mentions;
[0121] The linker 402 is used to determine at least one to-be-linked element according to the slot mention and the slot relationship, wherein the to-be-linked element is a SQL element used to generate an SQL statement;
[0122] The linker 402 is further used to link the at least one element to be linked to obtain a target SQL statement.
[0123] In some possible implementations, in the aspect of performing slot parsing on the natural language sentence to obtain slot mentions and slot relationships of the natural language sentence, the extractor 401 is specifically configured to:
[0124] Annotating the natural language sentence to obtain a plurality of labels, each label including a function label and a position label;
[0125] combining the multiple tags into a tag sequence according to the function tag and the position tag of each tag in the multiple tags;
[0126] A plurality of tags in the tag sequence is converted into slot mentions and slot relations.
[0127] In some possible implementations, the extractor includes a preset language model, an attention layer, and a conditional random field CRF layer. In the aspect of labeling the natural language sentence to obtain multiple labels, the extractor 401 is specifically used to:
[0128] Inputting the natural language sentence and at least one table header in a preset SQL database into the language model;
[0129] Encoding the natural language sentence and the header by using the language model to obtain a model representation vector of each word in the natural language sentence and a model representation vector of each word in the header;
[0130] Inputting the model representation vector of each word in the natural language sentence and the model representation vector of each word in the header into the attention layer through a language model, wherein the words in the natural language sentence include symbols, and the words in the header include column names;
[0131] Performing attention encoding on the model representation vector of the symbol in the natural language sentence and the column name in the table header through the attention layer to obtain the representation vector of the symbol and the representation vector of the column name after attention encoding;
[0132] Add the representation vector of the attention-encoded symbol and the representation vector of the column name to the corresponding model representation vector one by one to obtain an enhanced representation vector;
[0133] The enhanced representation vector is input into the CRF layer, and the enhanced representation vector is decoded by the CRF layer to obtain multiple labels.
[0134] In some possible implementations, the multiple tags include a role tag for indicating slot mentions and a span tag for indicating slot relationships. In the aspect of combining the multiple tags into a tag sequence according to a function tag and a position tag of each of the multiple tags, the extractor 401 is specifically configured to:
[0135] Finding a role tag and a span tag having the same function tag among the multiple tags;
[0136] The role tags and span tags having the same function tags are combined into a tag sequence according to the position tags.
[0137] In some possible implementations, in converting the multiple tags in the tag sequence into slot mentions and slot relationships, the extractor 401 is specifically configured to:
[0138] Convert the role labels in the label sequence into symbols and output them as slot mentions;
[0139] The span tags in the tag sequence are converted into selection spans and filter spans, and are represented by identification methods corresponding to the selection spans and filter spans, respectively.
[0140] In some possible implementations, the slot mention includes a column mention and a value mention, and in determining at least one SQL element to be linked according to the slot mention and the slot relationship, the linker 402 is specifically configured to:
[0141] Mapping the column mention and the value mention to at least one target table header in a preset SQL database according to the slot mention and the slot relationship, wherein the preset SQL database includes a plurality of table headers, and the plurality of table headers include the at least one target table header;
[0142] Each target header in the at least one target header is determined as an element to be linked, thereby obtaining at least one element to be linked.
[0143] In some possible implementations, the to-be-linked element includes a to-be-linked column, the slot reference includes a column reference, the slot relationship includes a filter span, and in mapping the column reference and the value reference to at least one target table header in a preset SQL database according to the slot reference and the slot relationship, the linker 402 is specifically configured to:
[0144] If the slot mention includes an explicit column mention, matching the explicit column mention with a table header in the SQL database to obtain a first target table header with a highest matching value, where the first target table header is the column to be linked;
[0145] If there is an implicit column mention in the filter span, the value mention, the symbol and the placeholder in the filter span are concatenated to obtain the implicit column mention, and the implicit column mention is matched with the table header in the SQL database to obtain the second target table header with the highest matching value, and the second target table header is the column to be linked.
[0146] It can be understood that the functions of each program module of the natural language sentence parsing device of this embodiment can be specifically implemented according to the method in the above method embodiment, and its specific implementation process can refer to the relevant description of the above method embodiment, which will not be repeated here.
[0147] It should be understood that the natural language sentence parsing device in the present application may include a smart phone (such as an Android phone, an iOS phone, a Windows Phone phone, etc.), a tablet computer, a PDA, a laptop computer, a mobile Internet device MID (Mobile Internet Devices, referred to as: MID) or a wearable device, etc. The above-mentioned natural language sentence parsing device is only an example, not an exhaustive list, including but not limited to the above-mentioned natural language sentence parsing device. In actual applications, the above-mentioned natural language sentence parsing device may also include: an intelligent vehicle-mounted terminal, a computer device, and the like.
[0148] See also Figure 5 , Figure 5 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Figure 5 As shown, the electronic device 500 includes a transceiver 501, a processor 502 and a memory 503. They are connected via a bus 504. The memory 503 is used to store computer programs and data, and can transmit the data stored in the memory 503 to the processor 502.
[0149] The processor 502 is used to read the computer program in the memory 503 and perform the following operations:
[0150] Obtain a natural language statement to be parsed; perform slot parsing on the natural language statement to obtain slot mentions and slot relationships of the natural language statement, wherein the slot mentions are structured query language SQL elements represented by each slot after the natural language statement is slot parsed, and the slot relationships are relationships between slot mentions;
[0151] Determine at least one to-be-linked element according to the slot mention and the slot relationship, the to-be-linked element being a SQL element for generating an SQL statement;
[0152] The at least one to-be-linked element is linked to obtain a target SQL statement.
[0153] In some possible implementations, in the aspect of performing slot parsing on the natural language sentence to obtain slot mentions and slot relationships of the natural language sentence, the processor 502 is configured to read a computer program in the memory 503, and specifically perform the following operations:
[0154] Annotating the natural language sentence to obtain a plurality of labels, each label including a function label and a position label;
[0155] combining the multiple tags into a tag sequence according to the function tag and the position tag of each tag in the multiple tags;
[0156] A plurality of tags in the tag sequence is converted into slot mentions and slot relations.
[0157] In some possible implementations, the electronic device includes an extraction link model, the extraction link model includes an extractor and a linker, and in the aspect of labeling the natural language sentence to obtain a plurality of tags, the processor 502 is used to read the computer program in the memory 503, and specifically perform the following operations:
[0158] Inputting the natural language sentence and at least one table header in a preset SQL database into the language model;
[0159] Encoding the natural language sentence and the header by using the language model to obtain a model representation vector of each word in the natural language sentence and a model representation vector of each word in the header;
[0160] Inputting the model representation vector of each word in the natural language sentence and the model representation vector of each word in the header into the attention layer through a language model, wherein the words in the natural language sentence include symbols, and the words in the header include column names;
[0161] Performing attention encoding on the model representation vector of the symbol in the natural language sentence and the column name in the table header through the attention layer to obtain the representation vector of the symbol and the representation vector of the column name after attention encoding;
[0162] Add the representation vector of the attention-encoded symbol and the representation vector of the column name to the corresponding model representation vector one by one to obtain an enhanced representation vector;
[0163] The enhanced representation vector is input into the CRF layer, and the enhanced representation vector is decoded by the CRF layer to obtain multiple labels.
[0164] In some possible implementations, the multiple tags include a role tag for indicating slot mentions and a span tag for indicating slot relationships. In the aspect of combining the multiple tags into a tag sequence according to a function tag and a position tag of each of the multiple tags, the processor 502 is configured to read a computer program in the memory 503, and specifically perform the following operations:
[0165] Finding a role tag and a span tag having the same function tag among the multiple tags;
[0166] The role tags and span tags having the same function tags are combined into a tag sequence according to the position tags.
[0167] In some possible implementations, in converting the multiple tags in the tag sequence into slot mentions and slot relationships, the processor 502 is further configured to read a computer program in the memory 503 to perform the following operations:
[0168] Convert the role labels in the label sequence into symbols and output them as slot mentions;
[0169] The span tags in the tag sequence are converted into selection spans and filter spans, and are represented by identification methods corresponding to the selection spans and filter spans, respectively.
[0170] In some possible implementations, the slot mention includes a column mention and a value mention, and in determining at least one SQL element to be linked according to the slot mention and the slot relationship, the processor 502 is further configured to read a computer program in the memory 503 to perform the following operations:
[0171] Mapping the column mention and the value mention to at least one target table header in a preset SQL database according to the slot mention and the slot relationship, wherein the preset SQL database includes a plurality of table headers, and the plurality of table headers include the at least one target table header;
[0172] Each target header in the at least one target header is determined as an element to be linked, thereby obtaining at least one element to be linked.
[0173] In some possible implementations, the to-be-linked element includes a to-be-linked column, the slot reference includes a column reference, the slot relationship includes a filter span, and in mapping the column reference and the value reference to at least one target table header in a preset SQL database according to the slot reference and the slot relationship, the processor 502 is further configured to read a computer program in the memory 503 to perform the following operations:
[0174] If the slot mention includes an explicit column mention, matching the explicit column mention with a table header in the SQL database to obtain a first target table header with a highest matching value, where the first target table header is the column to be linked;
[0175] If there is an implicit column mention in the filter span, the value mention, the symbol and the placeholder in the filter span are concatenated to obtain the implicit column mention, and the implicit column mention is matched with the table header in the SQL database to obtain the second target table header with the highest matching value, and the second target table header is the column to be linked.
[0176] Specifically, the transceiver 501 may be Figure 4 The transceiver unit 401 of the natural language sentence parsing device 400 of the embodiment described above, the processor 502 may be Figure 4 The processing unit 402 of the natural language sentence parsing device 400 of the described embodiment.
[0177] It should be noted that, during the implementation process, each step of the above method can be completed by an integrated logic circuit of hardware in a processor or an instruction in the form of software. The steps of the method disclosed in conjunction with the embodiment of the present application can be directly embodied as a hardware processor for execution, or a combination of hardware and software units in a processor for execution. The software unit can be located in a mature storage medium in the art such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, etc. The storage medium is located in a memory, and the processor executes the instructions in the memory, and completes the steps of the above method in conjunction with its hardware. To avoid repetition, it is not described in detail here. The specific implementation steps and other implementation steps in the embodiment of the present application can be referred to the steps in the above method embodiment, and to avoid repetition, it is not described in detail here.
[0178] An embodiment of the present application also provides a computer storage medium, wherein the computer-readable storage medium stores a computer program, and the computer program is executed by a processor to implement part or all of the steps of any natural language sentence parsing method recorded in the above method embodiments.
[0179] An embodiment of the present application also provides a computer program product, which includes a non-transitory computer-readable storage medium storing a computer program, and the computer program is operable to cause a computer to execute part or all of the steps of any natural language sentence parsing method recorded in the above method embodiments.
[0180] It should be noted that, for the aforementioned method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the described order of actions, because according to the present application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all optional embodiments, and the actions and modules involved are not necessarily required by the present application.
[0181] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0182] In the several embodiments provided in the present application, it should be understood that the disclosed device can be implemented in other ways. For example, the device embodiments described above are only schematic, such as the division of the units, which is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, and the indirect coupling or communication connection of the device or unit can be electrical or other forms.
[0183] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0184] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above integrated unit may be implemented in the form of hardware or in the form of a software program module.
[0185] If the integrated unit is implemented in the form of a software program module and sold or used as an independent product, it can be stored in a computer-readable memory. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a memory, including a number of instructions to enable a computer device (which can be a personal computer, server or network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned memory includes: U disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), mobile hard disk, disk or CD-ROM and other media that can store program codes.
[0186] A person skilled in the art can understand that all or part of the steps in the various methods of the above embodiments can be completed by instructing related hardware through a program, and the program can be stored in a computer-readable memory, and the memory can include: a flash drive, a read-only memory (English: Read-Only Memory, abbreviated as: ROM), a random access memory (English: Random Access Memory, abbreviated as: RAM), a magnetic disk or an optical disk, etc.
[0187] The embodiments of the present application are introduced in detail above. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method and core idea of the present application. At the same time, for general technical personnel in this field, according to the idea of the present application, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.
Claims
1. A natural language sentence parsing method, characterized in that: The method comprises: Obtain a natural language statement to be parsed; perform slot parsing on the natural language statement to obtain slot mentions and slot relationships of the natural language statement, wherein the slot mentions are structured query language SQL elements represented by each slot after the natural language statement is slot parsed, the slot mentions include column mentions and value mentions, the slot relationships are relationships between slot mentions, and the slot relationships include filter spans; According to the slot mention and the slot relationship, the column mention and the value mention are mapped to at least one target header in a preset SQL database, wherein the preset SQL database includes a plurality of headers, and the plurality of headers include the at least one target header; and according to the slot mention and the slot relationship, the column mention and the value mention are mapped to at least one target header in the preset SQL database, comprising: If the slot mention includes an explicit column mention, matching the explicit column mention with a table header in the SQL database to obtain a first target table header with a highest matching value, where the first target table header is the column to be linked; If there is an implicit column mention in the filter span, the value mention, the symbol and the placeholder in the filter span are concatenated to obtain the implicit column mention, and the implicit column mention is matched with the table header in the SQL database to obtain a second target table header with the highest matching value, where the second target table header is the column to be linked; Determine each target table header in the at least one target table header as a to-be-linked element, and obtain at least one to-be-linked element, wherein the to-be-linked element is an SQL element for generating an SQL statement, and the to-be-linked element includes a to-be-linked column; The at least one to-be-linked element is linked to obtain a target SQL statement.
2. The method according to claim 1, characterized in that The performing slot parsing on the natural language sentence to obtain slot mentions and slot relationships of the natural language sentence includes: Annotating the natural language sentence to obtain a plurality of labels, each label including a function label and a position label; combining the multiple tags into a tag sequence according to the function tag and the position tag of each tag in the multiple tags; A plurality of tags in the tag sequence is converted into slot mentions and slot relations.
3. The method according to claim 2, characterized in that The method is applied to an extraction link model, the extraction link model includes an extractor and a linker, the extractor includes a preset language model, an attention layer and a conditional random field (CRF) layer, and the natural language sentence is annotated to obtain multiple labels, including: Inputting the natural language sentence and at least one table header in a preset SQL database into the language model; Encoding the natural language sentence and the header by using the language model to obtain a model representation vector of each word in the natural language sentence and a model representation vector of each word in the header; The language model inputs the model representation vector of each word in the natural language sentence and the model representation vector of each word in the header into the attention layer, wherein the words in the natural language sentence include symbols, and the words in the header include column names; Performing attention encoding on the model representation vector of the symbol in the natural language sentence and the column name in the table header through the attention layer to obtain the representation vector of the symbol and the representation vector of the column name after attention encoding; Add the representation vector of the attention-encoded symbol and the representation vector of the column name to the corresponding model representation vector one by one to obtain an enhanced representation vector; The enhanced representation vector is input into the CRF layer, and the CRF layer decodes the enhanced representation vector to obtain multiple labels.
4. The method according to claim 2, characterized in that: The multiple tags include a role tag for indicating slot mentions and a span tag for indicating slot relationships, and the multiple tags are combined into a tag sequence according to a function tag and a position tag of each tag in the multiple tags, including: Finding a role tag and a span tag having the same function tag among the multiple tags; The role tags and span tags having the same function tags are combined into a tag sequence according to the position tags.
5. The method according to claim 2, characterized in that: The converting the multiple tags in the tag sequence into slot mentions and slot relationships includes: Convert the role labels in the label sequence into symbols and output them as slot mentions; The span tags in the tag sequence are converted into selection spans and filter spans, and are represented by identification methods corresponding to the selection spans and filter spans, respectively.
6. A natural language sentence parsing device, characterized in that: The natural language sentence parsing device comprises an extraction link model, wherein the extraction link model comprises an extractor and a linker, wherein: The extractor is used to obtain a natural language statement to be parsed; perform slot parsing on the natural language statement to obtain slot mentions and slot relationships of the natural language statement, wherein the slot mentions are structured query language SQL elements represented by each slot after the natural language statement is slot parsed, the slot mentions include column mentions and value mentions, the slot relationships are relationships between slot mentions, and the slot relationships include filter spans; The linker is used to map the column mention and the value mention to at least one target header in a preset SQL database according to the slot mention and the slot relationship, wherein the preset SQL database includes a plurality of headers, and the plurality of headers include the at least one target header; determine each target header in the at least one target header as an element to be linked, and obtain at least one element to be linked, wherein the element to be linked is an SQL element used to generate an SQL statement, and the element to be linked includes a column to be linked; In the aspect of mapping the column reference and the value reference to at least one target table header in a preset SQL database according to the slot reference and the slot relationship, the linker is specifically configured to: If the slot mention includes an explicit column mention, matching the explicit column mention with a table header in the SQL database to obtain a first target table header with a highest matching value, where the first target table header is the column to be linked; If there is an implicit column mention in the filter span, the value mention, the symbol and the placeholder in the filter span are concatenated to obtain the implicit column mention, and the implicit column mention is matched with the table header in the SQL database to obtain a second target table header with the highest matching value, where the second target table header is the column to be linked; The linker is further used to link the at least one element to be linked to obtain a target SQL statement.
7. An electronic device, characterized in that: include: A transceiver, a processor and a memory, wherein the processor is connected to the memory, the memory is used to store a computer program, and the processor is used to execute the computer program stored in the memory, so that the electronic device executes the method as described in any one of claims 1 to 5.
8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and the computer program is executed by a processor to implement the method according to any one of claims 1 to 5.
Citation Information
Patent Citations
Voice inquiry method and device and electronic device
CN109542929A