Syntax parsing method, device and readable storage medium

By entering the second-level grammar rule file into the pre-constructed dictionary and grammar tree, and using the first-level grammar rules to parse the second-level grammar rules, the problem of low grammar parsing efficiency in the existing technology is solved, and the first-level grammar parsing is not modified Improve syntax parsing efficiency under the condition of level grammar rules.

CN114548082BActive Publication Date: 2024-11-26ECARX (HUBEI) TECHCO LTD
View PDF 7 Cites 0 Cited by

Patent Information

Application Number
CN202210193390.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-02-28
Publication Date
2024-11-26
Estimated Expiration
2042-02-28

AI Technical Summary

Technical Problem

In the natural language understanding process based on grammar rules, users need to pre-defined matching rules and generate dictionary and grammar trees, resulting in low grammar parsing efficiency. After modifying the rules, dictionary and grammar trees need to be generated again, affecting efficiency.

Method used

Provide a grammatical analysis method, by inputting the user's second-level grammatical rule file into a pre-constructed dictionary and grammatical tree for lexical analysis, and using the first-level grammatical rules to parse the second-level grammatical rules to obtain the intention of the statement. , and there is no need to modify the first-level grammar rules when modifying the second-level grammar rules, avoiding regenerating dictionaries and grammar trees.

Benefits of technology

It improves the efficiency of grammar parsing, reduces the need for modifying first-level grammar rules and dictionary and grammar tree generation, and improves processing speed and flexibility.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114548082B_ABST
    Figure CN114548082B_ABST
Patent Text Reader

Abstract

The present application provides a syntax parsing method, device and readable storage medium, which inputs the user's second-level syntax rule file into a pre-built dictionary for lexical analysis to obtain matching lexical units. The lexical units are input into a pre-built syntax tree for syntax analysis to obtain the first matching path of the lexical units on the syntax tree, and the sentence input by the user is matched with the first matching path to obtain the second matching path and the intent of the sentence. The second-level syntax rules are parsed using the first-level syntax rules to obtain the path of each rule in the second-level syntax rules on the syntax tree, and then the sentence input by the user is matched with each path to obtain the matching path. Since each path has a corresponding intent, the intent of the sentence can be obtained, and the parsing of the sentence is realized. When the user's second-level syntax rule file needs to be modified, there is no need to generate the dictionary and syntax tree again, thereby improving the efficiency of syntax parsing.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a syntax parsing method, device and readable storage medium. Background Art

[0002] Natural Language Understanding (NLU) studies the use of electronic computers to simulate the human language communication process, enabling computers to understand and use the natural language of human society, and realize natural language communication between humans and computers, thereby replacing part of human mental work, including querying information, answering questions, compiling information, and processing all natural language information.

[0003] The natural language understanding process based on grammatical rules generally defines the user's grammatical rules first, then compiles the user's grammatical rules to generate a dictionary and a grammar tree, and uses the dictionary and grammar tree to parse the user's sentences.

[0004] However, this requires the user to predefine all matching rules and generate corresponding dictionaries and syntax trees. If the matching rules are modified, the corresponding dictionary and syntax tree need to be generated again, resulting in low syntax parsing efficiency. Summary of the invention

[0005] The present application provides a syntax parsing method, device and readable storage medium to improve syntax parsing efficiency.

[0006] In a first aspect, the present application provides a syntax parsing method, comprising:

[0007] Receiving a second-level grammar rule file from a user, inputting the second-level grammar rule file into a pre-built dictionary for lexical analysis, and obtaining matching lexical units;

[0008] Inputting the lexical unit into a pre-constructed syntax tree for syntax analysis to obtain a first matching path of the lexical unit on the syntax tree, wherein the syntax tree is generated according to a first syntax rule file;

[0009] Matching the sentence input by the user with the first matching path to obtain a second matching path matched by the sentence, and obtaining the intention of the sentence according to the second matching path;

[0010] Among them, the user's second-level grammar rule file is a file generated by the user through the parsing rules determined by the task, the dictionary is generated according to the first-level grammar rule file, and the first-level grammar rule file is a file generated according to the parsing rules determined by the application scenario, and the application scenario includes the task.

[0011] Optionally, obtaining the intention of the statement according to the second matching path specifically includes:

[0012] The intent corresponding to the second matching path is obtained according to the correspondence between the first matching path and the intent, and the intent corresponding to the second matching path is used as the intent of the sentence.

[0013] Optionally, matching the statement input by the user with the first matching path to obtain a second matching path matching the statement specifically includes:

[0014] Acquire a first fixed character and a first entity of a sentence input by a user, and a second fixed character and a second entity of a first matching path;

[0015] Matching the first fixed character with the second fixed character, and matching the first entity with the second entity;

[0016] A first matching path having the largest number of second fixed characters matching the first fixed characters is selected as the second matching path, and / or a first matching path having the largest number of second entities matching the first entity is selected as the second matching path.

[0017] Optionally, before obtaining the intention of the statement according to the second matching path, the method further includes:

[0018] A correspondence between the first matching path, the intent, and the entity is established, and each first matching path has a corresponding intent and at least one second entity.

[0019] Optionally, the method further includes:

[0020] The sentence input by the user is processed according to the intent and entity of the sentence.

[0021] Optionally, the step of inputting the second-level grammar rule file into a pre-built dictionary to obtain matching lexical units specifically includes:

[0022] The second-level grammar rule file is input into a pre-built dictionary to match at least one of identifiers, numbers, string constants, spaces and comments in the second-level grammar rules with lexical units in the pre-built dictionary to obtain matching lexical units.

[0023] Optionally, before inputting the second-level grammar rule file into a pre-built dictionary for lexical analysis, the method further includes:

[0024] Define the first-level grammar rules according to the application scenario and generate the first-level grammar rule file;

[0025] Build a dictionary and syntax tree based on the first-level grammar rule file.

[0026] Optionally, constructing a dictionary and a syntax tree according to the first-level syntax rule file specifically includes:

[0027] The first-level grammar rule file is input into a language recognition tool to obtain the dictionary and the grammar tree.

[0028] In a second aspect, the present application provides a grammar parsing device, comprising:

[0029] A first analysis module is used to receive a second-level grammar rule file from a user, input the second-level grammar rule file into a pre-built dictionary for lexical analysis, and obtain matching lexical units;

[0030] A second analysis module is used to input the lexical unit into a pre-built syntax tree for syntax analysis to obtain a first matching path of the lexical unit on the syntax tree, wherein the syntax tree is generated according to the first syntax rule file;

[0031] An acquisition module, used to match the sentence input by the user with the first matching path, obtain a second matching path matched by the sentence, and obtain the intention of the sentence according to the second matching path;

[0032] Among them, the user's second-level grammar rule file is a file generated by the user through the parsing rules determined by the task, the dictionary is generated according to the first-level grammar rule file, and the first-level grammar rule file is a file generated according to the parsing rules determined by the application scenario, and the application scenario includes the task.

[0033] In a third aspect, the present application provides an electronic device, including: a memory and a processor;

[0034] The memory is used to store instructions; the processor is used to call the instructions in the memory to execute the syntax parsing method in the first aspect and any possible design of the first aspect.

[0035] In a fourth aspect, the present application provides a computer-readable storage medium, which stores computer instructions. When at least one processor of an electronic device executes the computer instructions, the electronic device executes the grammatical parsing method in the first aspect and any possible design of the first aspect.

[0036] In a fifth aspect, the present application provides a computer program product, which includes computer instructions. When at least one processor of an electronic device executes the computer instructions, the electronic device executes the grammatical parsing method in the first aspect and any possible design of the first aspect.

[0037] The grammar parsing method provided by the present application receives the second-level grammar rule file of the user, inputs the second-level grammar rule file into a pre-built dictionary for lexical analysis, and obtains matching lexical units. Then the lexical units are input into a pre-built syntax tree for syntax analysis, and the first matching path of the lexical units on the syntax tree is obtained, and the sentence input by the user is matched with the first matching path to obtain the second matching path matching the sentence, and the intention of the sentence is obtained according to the second matching path. Since the dictionary and the syntax tree are generated according to the first-level grammar rule file, the second-level grammar rule file of the user is input into the dictionary and the syntax tree, that is, the second-level grammar rule is parsed using the first-level grammar rule, and the path of each rule in the second-level grammar rule on the syntax tree generated by the first-level grammar rule is obtained, and then the sentence input by the user is matched with each path to obtain the matching path, and since each path has a corresponding intention, the intention of the sentence can be obtained, and the parsing of the sentence is realized, and when the second-level grammar rule file of the user needs to be modified, there is no need to modify the first-level grammar rule file again, and then there is no need to generate the dictionary and syntax tree again, thereby improving the efficiency of grammar parsing. BRIEF DESCRIPTION OF THE DRAWINGS

[0038] In order to more clearly illustrate the technical solutions in the present application or the prior art, a brief introduction will be given below to the drawings required for use in the embodiments or the description of the prior art. 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.

[0039] Figure 1 A flowchart of a grammar parsing method provided in one embodiment of the present application;

[0040] Figure 2 A flowchart of a grammar parsing method provided in one embodiment of the present application;

[0041] Figure 3 A schematic diagram of the structure of a grammar parsing device provided in one embodiment of the present application;

[0042] Figure 4 A schematic diagram of the hardware structure of an electronic device provided in one embodiment of the present application. DETAILED DESCRIPTION

[0043] In order to make the purpose, technical solutions and advantages of this application clearer, the technical solutions in this application will be clearly and completely described below in conjunction with the drawings in this application. Obviously, the described embodiments are part of the embodiments of this application, not all of the embodiments. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0044] The natural language understanding process based on grammar rules generally defines the user's grammar rules first, then compiles the user's grammar rules to generate a dictionary and a grammar tree, and uses the dictionary and the grammar tree to parse the user's sentences. However, this requires the user to pre-define all matching rules and generate the corresponding dictionary and grammar tree. If the matching rules are modified, the corresponding dictionary and grammar tree need to be generated again, resulting in low grammar parsing efficiency.

[0045] In view of the above problems, the present application proposes a grammatical parsing method, which inputs the user's second-level grammatical rule file into a pre-built dictionary for lexical analysis, and obtains the lexical units in the first-level grammatical rules that match the lexical units in the second-level grammatical rules. Then, the lexical units in the matched first-level grammatical rules are input into a pre-built syntax tree for grammatical analysis, and the first matching path of the lexical units on the syntax tree is obtained, that is, the path of each rule in the second-level grammatical rules on the syntax tree generated by the first-level grammatical rules is obtained. Then, the sentence input by the user is matched with the first matching path, and the second matching path matching the sentence is obtained, and the intention of the sentence is obtained according to the second matching path. Since the first-level grammatical rules are used to parse the second-level grammatical rules, if the user modifies the second-level grammatical rules, there is no need to modify the first-level grammatical rules, and there is no need to generate the dictionary and syntax tree again, thereby improving the efficiency of grammatical parsing.

[0046] The technical solution of the present application is described in detail with specific embodiments below. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described in detail in some embodiments.

[0047] Figure 1 FIG. 1 is a flowchart of a syntax parsing method provided by an embodiment of the present application. Figure 1 As shown, with the server as the execution subject, the method of this embodiment may include the following steps:

[0048] S101, receiving a second-level grammar rule file from a user, inputting the second-level grammar rule file into a pre-built dictionary for lexical analysis, and obtaining matching lexical units.

[0049] The second-level grammar rule file is a file generated based on the second-level grammar rules. The second-level grammar rules are parsing rules determined by users through tasks. The dictionary is generated based on the first-level grammar rule file. The first-level grammar rule file includes the first-level grammar rules. The first-level grammar rules are parsing rules determined based on application scenarios. The application scenarios include tasks, such as the application scenario is a language assistant application, and the tasks are querying or buying train tickets.

[0050] In this embodiment, the defined second-level grammar rules for querying and purchasing train tickets include:

[0051] [1]namespace train

[0052] [2]Arabic numeral digit: ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9");

[0053] [3]Arabic numeral Chinese character: ("一" | "二" | "三" | "四" | "五" | "六" | "七" | "八" | "九" | "〇");

[0054] [4]Arabic numeral capital: ("壹" | "贰" | "叁" | "肆" | "伍" | "陆" | "柒" | "捌" | "玖" | "零");

[0055] [5]Arabic numeral: (Arabic numeral digit | Arabic numeral Chinese character | Arabic numeral capital);

[0056] [6]Lowercase letter: ("a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z");

[0057] [7]Uppercase letter: ("A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z");

[0058] [8]Letter: (Lowercase letter | Uppercase letter);

[0059] [9]Please start: ("请帮我" | "能不能帮我" | "帮我" | "帮忙" | "请");

[0060]

[10] Check: (("查" | "看" | "搜" | "找") "一下"?);

[0061]

[11] @noskip;

[0062]

[12] Any content:.+;

[0063]

[13] @type(name="date");

[0064]

[14] <time point>: ("today" | "tomorrow" | "the day after tomorrow" | ("next week" | "next" ? "week") Arabic numerals | Arabic numerals "month" Arabic numerals ("day" | "date"));

[0065]

[15] @type(name="place");

[0066]

[16] <city name>: (any content);

[0067]

[17] @type(name="code");

[0068]

[18] <Train number>:((lowercase letter)*(Arabic numeral)+);

[0069]

[19] @attr(intention="booking");

[0070]

[20] @production;

[0071]

[21] Ticket booking: (please start)? ("book a ticket"|"book a ticket"|"book one ticket")<time>? ("from"|"place of departure")? <city name>("go to"|"to"|"destination")<city name>"of"("ticket"|"ticket"|"train""ticket"?|"high-speed train""ticket"?);

[0072]

[22] @attr(intention="check ticket");

[0073]

[23] @production;

[0074]

[24] Check tickets: (please start)? Check (<time point>)? ("from"|"place of departure")? <city name>("to"|"to"|"destination")<city name>"'s" ("ticket"|"ticket"|"train""ticket"?|"high-speed train""ticket"?);

[0075]

[25] @attr(intention="Train tickets can be matched at will");

[0076]

[26] @production;

[0077]

[27] Train tickets match any content: (please start with)? Any content ("ticket"|"ticket"|"train""ticket"?|"high-speed train""ticket"?|"high-speed rail""ticket"?);

[0078] The second-level grammatical rules for querying and purchasing train tickets may include 27 rules, such as Arabic numerals, Arabic numerals and Chinese characters, etc. Correspondingly, the lexical units in the second-level grammatical rules include identifiers, numbers, string constants, spaces, comments, etc. Lexical units can also be understood as individual rules in the second-level grammatical rules, such as @production, Arabic numerals, ticket checking, and ticket booking. It should be noted that users can define the corresponding second-level grammatical rules according to specific tasks and can modify the second-level grammatical rules.

[0079] After receiving the second-level grammar rules from the user, the second-level grammar rules are input into a pre-built dictionary to compare the lexical units in the second-level grammar rules with the lexical units in the first-level grammar rules in the dictionary, and obtain the lexical units in the first-level grammar rules that match the lexical units in the second-level grammar rules. For ease of description, the lexical units in the second-level grammar rules are referred to as second-level lexical units, and the lexical units in the first-level grammar rules are referred to as first-level lexical units, and the second-level grammar rules are input into the pre-built dictionary to obtain the first-level lexical units that match the second-level lexical units.

[0080] For example, the lexical rules in the first level grammar rules include:

[0081] INT:('-'|'+')? ('0'|[1-9][0-9]*);

[0082] FLOAT:INT'.'[0-9]+([Ee]INT)? ;

[0083] BOOLEAN:'true'|'false';

[0084] STRING:'"'(~[\\r\n"]|'\\'('"'|'r'|'n'|'\\'))*?'"'|'\"(~[\\\r\n"]|'\\'('\"|'r'|'n'|'\\'))*?'\";

[0085] ID:[_a-zA-Z\u4e00-\u9fa5][_0-9a-zA-Z\u4e00-\u9fa5]*;

[0086] NAMESPACE:ID('.'ID)+;

[0087] LINECOMMENT:' / / '~[\r\n]*->channel(HIDDEN);

[0088] BLOCKCOMMENT:' / *'.*? '* / '->channel(HIDDEN);

[0089] SPACES:[\t\r\n]+->channel(HIDDEN);

[0090] REGEX:' / '.*?' / ';

[0091] For example, the second-level grammar rule defines "Arabic numerals: ("0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9");", then Arabic numerals are parsed as IDs, and numbers such as 0 are parsed as STRINGs, that is, the second-level grammar rule is input into the pre-built dictionary, and the matching first-level lexical units obtained include IDs and STRINGs.

[0092] As an implementation method, after the second-level grammar rule file is input into a pre-built dictionary, at least one of the identifiers, numbers, string constants, spaces, and comments in the second-level grammar rule file is matched with the pre-built lexical units to obtain first-level lexical units that match the second-level lexical units. For example, the identifiers in the second-level grammar rule file are compared with the identifiers in the dictionary, and the numbers in the second-level grammar rule file are compared with the numbers in the dictionary.

[0093] S102: Input the lexical unit into a pre-built syntax tree for syntax analysis to obtain a first matching path of the lexical unit on the syntax tree.

[0094] Syntactic analysis refers to the analysis of the specific meaning expressed by a series of word symbols. In grammar and language applications, word order and word dependencies include sequence, selection, lexical symbol dependency and nested structure. A sequence refers to a series of elements, such as an array initialization list in a programming language, and consecutive matching of multiple string constants in natural language processing. Selection refers to making a choice among multiple options, such as if and else if statements in a programming language. Lexical symbol dependency means that a lexical symbol needs to be paired with another lexical symbol somewhere, such as left and right bracket matching in a programming language. A nested structure refers to a self-similar language structure, such as nested calculation expressions or nested statement blocks in a programming language.

[0095] The matching lexical units are input into a pre-constructed syntax tree to obtain the path of each matching lexical unit on the syntax tree, that is, the path of each rule in the second-level syntax rules on the syntax tree is obtained. The number of the first matching paths may include one or more.

[0096] S103: Match the sentence input by the user with the first matching path to obtain a second matching path for sentence matching, and obtain the intention of the sentence according to the second matching path.

[0097] Match the sentence input by the user with the first matching path to obtain a second matching path in the first matching path that matches the sentence input by the user. If the first matching path has a corresponding intent, then the second matching path has a corresponding intent. The intent of the sentence input by the user can be obtained based on the intent of the second matching path.

[0098] As an implementation manner, after obtaining the first matching path of each rule in the second-level grammar rule on the grammar tree, the first matching path is generated into a search list, as shown in Table 1, including three first matching paths, item1, item2 and item3.

[0099] Table 1

[0100]

[0101]

[0102] The correspondence between each first matching path and the intent and entity is pre-constructed. Each first matching path has a corresponding intent and at least one second entity. As shown in Table 2, the intent corresponding to item 1 is to check tickets, and the corresponding entities include data (date and time), place (place of departure) and place (destination); the intent corresponding to item 2 is to buy tickets, and the corresponding entities include data (date and time), place (place of departure) and place (destination); the intent corresponding to item 3 is any match for train tickets, and there is no corresponding entity. Therefore, after obtaining the second matching path corresponding to the sentence input by the user, the intent corresponding to the second matching path can be obtained according to the correspondence between the first matching path and the intent. The intent corresponding to the second matching path is the intent of the sentence input by the user. For example, if the sentence input by the user is "book a train ticket from Shanghai to Beijing", it matches item 2 in the second row of Table 1. In Table 2, item 2 represents the intent of buying tickets. For example, if the sentence input by the user is "help me check the train ticket from Shanghai to Hangzhou tomorrow", it matches item 1 in the first row of Table 1. In Table 2, item 1 represents the intent of checking tickets.

[0103] Table 2

[0104]

[0105] In this embodiment, after receiving the sentence input by the user, the first fixed character and the first entity of the sentence input by the user, as well as the second fixed character and the second entity of the first matching path are obtained, the first fixed character and the second fixed character are matched, the first entity and the second entity are matched, and the first matching path with the largest number of second fixed characters matching the first fixed character is selected as the second matching path, and / or the first matching path with the largest number of second entities matching the first entity is selected as the second matching path. For example, if the number of first fixed characters is 3, the number of second fixed characters matching the first fixed character in one first matching path is 3, and the number of second fixed characters matching the first fixed character in another first matching path is 2, then the other first matching path is used as the second matching path. For example, if the number of first entities is 2, the number of second entities matching the first entity in one first matching path is 1, and the number of second entities matching the first entity in another first matching path is 0, then one first matching path is used as the second matching path.

[0106] For example, the sentence input by the user is "Please help me check the train ticket from Shanghai to Hangzhou tomorrow", and its first fixed characters include "please help me", "check", "tomorrow", "from", "Shanghai", "to", "Hangzhou", "of", "train ticket", and its entities include "tomorrow", "Shanghai", "Hangzhou". It can match item 1 in Table 1, and the second fixed characters matching the first fixed characters in item 1 include "please help me", "check", "from", "to", "of", "train ticket", and the second entity matching the first entity in item 1 includes "<time point>", "<city name>", "<city name>", then item 1 matches 12 characters and 3 entities with the sentence input by the user. It can also match item 3 in Table 1, and the second fixed characters matching the first fixed characters in item 3 include "please help me", "train ticket", and there is no second entity matching the first entity in item 3, then item 3 only matches 6 characters with the sentence input by the user, and there is no matching entity. Therefore, item 1 is determined as the second matching path matching the sentence input by the user according to the number of matching fixed characters and / or the number of matching entities.

[0107] As an implementation method, after obtaining the intention of the sentence input by the user, the sentence can also be processed according to the intention and entity of the sentence input by the user. For example, if the user's intention is to check tickets, and the entity includes <time> and <city name> of the departure place and the destination, then the corresponding database can be searched for the corresponding train tickets.

[0108] The grammar parsing method provided by the present application utilizes the first-level grammar rules to parse the second-level grammar rules, obtains the path of each rule in the second-level grammar rules on the grammar tree generated by the first-level grammar rules, and then matches the sentence input by the user with each path to obtain the matched path. Since each path has a corresponding intention, the intention of the sentence can be obtained, and the sentence parsing is realized. When the user's second-level grammar rule file needs to be modified, there is no need to modify the first-level grammar rule file again, and thus there is no need to generate the dictionary and grammar tree again, thereby improving the efficiency of grammar parsing.

[0109] Figure 2 FIG. 1 is a flowchart of a syntax parsing method provided by an embodiment of the present application. Figure 2 As shown, with the server as the execution subject, the method of this embodiment may include the following steps:

[0110] S201. Define first-level grammar rules according to application scenarios, and generate a first-level grammar rule file.

[0111] The first-level grammar rule file includes the first-level grammar rules. Each application scenario has a corresponding first-level grammar rule. The application scenarios include, for example, voice assistants, navigation, music, etc. The first-level grammar rules include lexical rules and grammar rules. When defining the first-level grammar rules, the grammar header is first declared. The grammar header includes keywords and grammar file names. The grammar file name is the name of the first-level grammar rule file. For example, if the first-level grammar rule file is Sense.g4 and the grammar header is grammar Sense, then grammar is the keyword and Sense is the name of the first-level grammar rule file. Then, define the grammar rules. The grammar rules include grammar rule names and grammar rule contents. The grammar rule names start with lowercase letters and can be followed by letters, numbers, and underscores, such as expr, simpleDeclarator, d2, header_file. The grammar rule contents are constructed according to the grammar in the application scenario. Then, define the lexical rules. The lexical rules include lexical rule names and lexical rule contents. The lexical rule names start with uppercase letters. The lexical rule contents are constructed according to the lexical units in the application scenario. Lexical units include identifiers, numbers, strings, spaces, comments, etc.

[0112] For example, the first-level grammar rule file is Sense.g4, which contains the following grammar rules:

[0113]

[0114]

[0115] Among them, line [1] represents the grammar header, lines [2]-

[16] represent grammar rules, and lines

[17] -

[26] represent lexical rules. Line [2] defines the grammar rule sense. Sense is the rule entry of the first-level grammar rule, including all the rules in the first-level grammar rule. The grammar rule content matched by sense is nsSentence(primeSentence|productionSentence)*. nsSentence represents a larger category of application scenarios, such as navigation and music. (primeSentence|productionSentence) represents the rule corresponding to a specific intent or word slot. The * after the brackets indicates that the content of the brackets appears 0 or more times. It should be noted that the space character is used as a separator between words, "*" indicates that it appears 0 or more times, and the symbol "|" indicates selection. When multiple sub-rules in the grammar rule are matched simultaneously in a sentence, the sub-rules are matched first in the order in which they are defined, that is, the priority is determined according to the order in which they are defined. For example, if multiplication and division are defined first and addition and subtraction are defined later, then multiplication and division are better than addition and subtraction.

[0116] Lines [3]-[5] define nsSentence, primeSentence, and productionSentence. The names between single quotes are constant names. Lines [4] and [5] include annotation. Line [6] defines annotation. “?” indicates 0 or 1 occurrence. Line [6] includes assigns. Line [7] defines assigns. Line 7 includes assign. Line [8] defines assign. Line [8] includes value. Line [9] defines value. INT represents an integer variable, FLOAT represents a floating point variable, BOOLEAN represents a Boolean variable, STRING represents a constant string, array represents an array variable, and dict represents a variable type. Line

[10] defines array. Line

[11] defines dict. Line

[11] includes dictItem. Line

[12] defines dictItem.

[0117] Lines [4] and [5] include the rule expr, and lines

[13] -

[16] describe the rule expr, which consists of multiple optional lines and can match the content of any line. Line

[13] means that it consists of two or more consecutive repUnits, and "+" means one or more of the previous content, that is, one or more repUnits. Line

[14] means that it consists of multiple optional branches. Line

[16] has only one ";", indicating the end of the rule expr. Among them, "+" means that it appears once or more, and the content after # is used to explain the content before #.

[0118] Lines

[17] -

[26] represent lexical rules, where “\n” represents a carriage return and line feed, “\t” represents a horizontal jump to the next tab position, “\r” represents a carriage return, and “~” represents a negation symbol.

[0119] S202: Construct a dictionary and a syntax tree according to the first-level syntax rule file.

[0120] The first-level grammar rule file is input into a language recognition tool to obtain a dictionary and a grammar tree. For example, the first-level grammar rule file is input into ANTLR (ANother Tool for Language Recognition) to generate a java code file and a dictionary file, and the dictionary file includes any rule that appears in the first-level grammar rule, such as any character, number, etc. Then, the first-level grammar tree is generated using the java code file, and the first-level grammar tree data structure can be stored in the memory.

[0121] S203: Receive the second-level grammar rule file from the user, input the second-level grammar rule file into a pre-built dictionary for lexical analysis, and obtain matching lexical units.

[0122] S204: Input the lexical unit into a pre-built syntax tree for syntax analysis to obtain a first matching path of the lexical unit on the syntax tree.

[0123] S205: Match the sentence input by the user with the first matching path to obtain a second matching path for sentence matching, and obtain the intention of the sentence according to the second matching path.

[0124] Among them, step S203 and Figure 1 Step S101 in the embodiment is implemented in a similar manner, and step S204 is implemented in a similar manner. Figure 1 Step S102 in the embodiment is implemented in a similar manner, and step S205 is implemented in a similar manner. Figure 1 The implementation of step S103 in the embodiment is similar and will not be described in detail in this embodiment.

[0125] The grammar parsing method provided by the present application defines first-level grammar rules according to application scenarios, constructs a dictionary and a grammar tree using the first-level grammar rules, and then inputs the second-level grammar rules into the dictionary and the grammar tree. Therefore, if the second-level grammar rules are parsed using the first-level grammar rules, the path of each rule in the second-level grammar rules on the grammar tree generated by the first-level grammar rules is obtained, and then the sentence input by the user is matched with each path to obtain the matching path, thereby obtaining the intention of the sentence. When the user's second-level grammar rule file needs to be modified, there is no need to modify the first-level grammar rule file again, and thus there is no need to generate a dictionary and a grammar tree again, thereby improving the efficiency of grammar parsing.

[0126] Figure 3 A schematic diagram of the structure of a grammar parsing device provided in an embodiment of the present application is shown. Figure 3 As shown, the grammar parsing device 10 of this embodiment is used to implement the operation corresponding to the electronic device in any of the above method embodiments. The grammar parsing device 10 of this embodiment includes:

[0127] The first analysis module 11 is used to receive the second-level grammar rule file of the user, input the second-level grammar rule file into a pre-built dictionary for lexical analysis, and obtain matching lexical units;

[0128] A second analysis module 12 is used to input the lexical unit into a pre-constructed syntax tree for syntax analysis, and obtain a first matching path of the lexical unit on the syntax tree, wherein the syntax tree is generated according to the first syntax rule file;

[0129] An acquisition module 13, configured to match the sentence input by the user with the first matching path, obtain a second matching path matched by the sentence, and obtain the intention of the sentence according to the second matching path;

[0130] Among them, the user's second-level grammar rule file is a file generated by the user through the parsing rules determined by the task, the dictionary is generated according to the first-level grammar rule file, and the first-level grammar rule file is a file generated according to the parsing rules determined by the application scenario, and the application scenario includes the task.

[0131] The grammar parsing device 10 provided in the embodiment of the present application can execute the above method embodiment. Its specific implementation principle and technical effects can be found in the above method embodiment, and this embodiment will not be repeated here.

[0132] Figure 4 FIG. 1 shows a hardware structure diagram of an electronic device provided in an embodiment of the present application. Figure 4 As shown, the electronic device 20 is used to implement the operations corresponding to the electronic device in any of the above method embodiments. The electronic device 20 of this embodiment may include: a memory 21, a processor 22 and a communication interface 23.

[0133] The memory 21 is used to store computer instructions. The memory 21 may include a high-speed random access memory (RAM), and may also include a non-volatile memory (NVM), such as at least one disk memory, and may also be a USB flash drive, a mobile hard disk, a read-only memory, a disk or an optical disk.

[0134] The processor 22 is used to execute the computer instructions stored in the memory to implement the grammatical parsing method in the above embodiment. For details, please refer to the relevant description in the above method embodiment. The processor 22 can be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), etc. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor, etc. The steps of the method disclosed in the invention can be directly embodied as being executed by a hardware processor, or can be executed by a combination of hardware and software modules in the processor.

[0135] Optionally, the memory 21 may be independent or integrated with the processor 22 .

[0136] The communication interface 23 may be connected to the processor 22. The processor 22 may control the communication interface 23 to implement the functions of receiving and sending information signals.

[0137] The electronic device provided in this embodiment can be used to execute the above-mentioned syntax parsing method, and its implementation method and technical effect are similar, which will not be described in detail in this embodiment.

[0138] The present application also provides a computer-readable storage medium, in which computer instructions are stored. When the computer instructions are executed by a processor, they are used to implement the methods provided in the various embodiments described above.

[0139] The present application also provides a computer program product, which includes computer instructions stored in a computer-readable storage medium. At least one processor of a device can read the computer instructions from the computer-readable storage medium, and at least one processor executes the computer instructions so that the device implements the methods provided in the various embodiments described above.

[0140] An embodiment of the present application also provides a chip, which includes a memory and a processor, wherein the memory is used to store computer instructions, and the processor is used to call and execute the computer instructions from the memory, so that a device equipped with the chip executes the methods described in the various possible implementation modes above.

[0141] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the above embodiments, a person of ordinary skill in the art should understand that the technical solutions described in the above embodiments can still be modified, or some or all of the technical features can be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present application.

Claims

1. A syntax parsing method, characterized in that: The method comprises: Receiving a second-level grammar rule file from a user, inputting the second-level grammar rule file into a pre-built dictionary for lexical analysis, and obtaining matching lexical units; Inputting the lexical unit into a pre-constructed syntax tree for syntax analysis to obtain a first matching path of the lexical unit on the syntax tree, wherein the syntax tree is generated according to a first syntax rule file; Matching the sentence input by the user with the first matching path to obtain a second matching path matched by the sentence, and obtaining the intention of the sentence according to the second matching path; Among them, the user's second-level grammar rule file is a file generated by the user according to the parsing rules determined by the task, the dictionary is generated according to the first-level grammar rule file, and the first-level grammar rule file is a file generated according to the parsing rules determined by the application scenario, and the application scenario includes the task.

2. The method according to claim 1, characterized in that The obtaining the intention of the statement according to the second matching path specifically includes: The intent corresponding to the second matching path is obtained according to the correspondence between the first matching path and the intent, and the intent corresponding to the second matching path is used as the intent of the sentence.

3. The method according to claim 2, characterized in that Matching the statement input by the user with the first matching path to obtain a second matching path matching the statement specifically includes: Acquire a first fixed character and a first entity of a sentence input by a user, and a second fixed character and a second entity of a first matching path; Matching the first fixed character with the second fixed character, and matching the first entity with the second entity; A first matching path having the largest number of second fixed characters matching the first fixed characters is selected as the second matching path, and / or a first matching path having the largest number of second entities matching the first entity is selected as the second matching path.

4. The method according to claim 3, characterized in that Before obtaining the intention of the statement according to the second matching path, the method further includes: A correspondence between the first matching path, the intent, and the entity is established, and each first matching path has a corresponding intent and at least one second entity.

5. The method according to claim 3, characterized in that: The method further comprises: The sentence input by the user is processed according to the intent and entity of the sentence.

6. The method according to any one of claims 1 to 5, characterized in that: The step of inputting the second-level grammar rule file into a pre-built dictionary to obtain matching lexical units specifically includes: The second-level grammar rule file is input into a pre-built dictionary to match at least one of identifiers, numbers, string constants, spaces and comments in the second-level grammar rules with lexical units in the pre-built dictionary to obtain matching lexical units.

7. The method according to any one of claims 1 to 5, characterized in that: Before inputting the second-level grammar rule file into a pre-built dictionary for lexical analysis, the method further includes: Define the first-level grammar rules according to the application scenario and generate the first-level grammar rule file; Build a dictionary and syntax tree based on the first-level grammar rule file.

8. The method according to claim 7, characterized in that The step of constructing a dictionary and a syntax tree according to the first-level syntax rule file specifically includes: The first-level grammar rule file is input into a language recognition tool to obtain the dictionary and the grammar tree.

9. An electronic device, characterized in that: include: A processor, and a memory communicatively connected to the processor; The memory stores computer-executable instructions; The processor executes the computer-executable instructions stored in the memory to implement the grammatical parsing method according to any one of claims 1 to 8.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, which are used to implement the grammatical parsing method according to any one of claims 1 to 8 when executed by a processor.

Citation Information

Patent Citations

  • Interaction intention path mining method and device

    CN111046145A

  • Inquiry information processing method and device, storage medium and computer equipment

    CN111415740A

  • Man-machine interaction question-answering method and system based on complex intention intelligent identification

    CN111708874A

  • Generation method of target syntax tree and related equipment

    CN112860233A

  • Training method, device and system of intelligent voice robot

    CN112992128A