Multi-mode matching method, device, electronic device and storage medium
By symbolizing the text to be matched and using dictionary tree matching, the existing multi-pattern matching algorithm is solved and the problem of inefficiency and non-supporting regular expressions is achieved, efficient multi-pattern matching and powerful pattern expression capabilities are achieved.
Patent Information
- Application Number
- CN202110693846.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-06-22
- Publication Date
- 2025-02-28
- Estimated Expiration
- 2041-06-22
AI Technical Summary
Existing multi-pattern matching algorithms are inefficient when processing long input text and multiple patterns and do not support regular expressions.
By symbolizing the text to be matched into a symbol sequence and using a dictionary tree for matching, the matching strategy is determined based on each symbol type in the symbol sequence, and matching of regular expressions is supported.
It improves the efficiency of multi-pattern matching and the expression ability of patterns, supports regular expressions, and reduces the maintenance cost of patterns.
Smart Images

Figure 00000000_0000_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of multi-mode matching, and in particular to a multi-mode matching method, device, electronic equipment and storage medium. Background Art
[0002] At present, multi-pattern matching is a very important basic work with a wide range of applications. It can be applied to text segmentation, text matching, dialogue systems, etc. Multi-pattern matching means that for a given input text T, such as "ABCDEF", some string patterns are provided, such as "ABC", "CD", "FG", and the pattern that can match the text T is found from these patterns. For example, in the example, "ABC" and "CD" are both patterns that match T. When the input text T is very long and there are many patterns, a certain method is needed to solve it. At present, the better solutions include AC method, Wu Manber method, etc.
[0003] The current multi-pattern matching algorithm is mainly optimized for efficiency, but does not support regular expressions. Summary of the invention
[0004] The present invention provides a multi-mode matching method, device, electronic equipment and storage medium, which can solve the technical problems in the above-mentioned multi-mode matching.
[0005] The technical solution of the present invention to solve the above technical problems is as follows:
[0006] In a first aspect, an embodiment of the present invention provides a multi-mode matching method, comprising:
[0007] Get the text to be matched;
[0008] Tokenize the text to be matched into a symbol sequence;
[0009] Starting from the root node of the dictionary tree, the child nodes corresponding to each symbol in the symbol sequence are matched in sequence. During the matching process, a matching strategy is determined according to the type of each symbol in the symbol sequence;
[0010] A matching result is obtained from the path from the root node to the child node.
[0011] In some embodiments, in the above multi-mode matching method, symbolizing the text to be matched into a symbol sequence includes:
[0012] Using a symbolization tool to segment the character expression of the text to be matched into different symbol types;
[0013] Each symbol in the symbol sequence is separated by a first separator;
[0014] Each symbol includes two parts, a first part is the symbol itself, and a second part is the symbol type, and the first part and the second part are separated by a second separator;
[0015] The symbol types include: text type, number type, interval type or activity node.
[0016] In some embodiments, in the above multi-pattern matching method, starting from the root node of the dictionary tree, matching the sub-nodes corresponding to each symbol in the symbol sequence in sequence includes:
[0017] Starting from the root node of the dictionary tree, match the child node corresponding to the first symbol in the symbol sequence;
[0018] After matching the child node corresponding to the first symbol, match the child node of the second symbol in the symbol sequence starting from the child node;
[0019] Match in sequence until the sub-node corresponding to the last symbol in the symbol sequence is matched to obtain the matching result.
[0020] In some embodiments, in the above multi-mode matching method, determining a matching strategy according to each symbol type in the symbol sequence includes:
[0021] When the symbol type is an active node, if the current character matches the active node successfully, the next character is still matched at the active node;
[0022] When the symbol type is an interval node, the current character is matched with every character in the interval node.
[0023] When the symbol types include at least two, the current character is transcribed or retained for matching.
[0024] In some embodiments, in the above-mentioned multi-mode matching method, after the to-be-matched text is symbolized and converted into a symbol sequence, the symbol sequence is placed on the dictionary tree through a tree construction tool.
[0025] In some embodiments, in the above multi-mode matching method, after obtaining the matching result from the path from the root node to the child node, the method further includes: determining the final matching result through a result selection unit.
[0026] In some embodiments, in the above-mentioned multi-mode matching method, matching the sub-nodes corresponding to each symbol in the symbol sequence in sequence starting from the root node of the dictionary tree includes: matching through a dictionary tree matching logic unit.
[0027] In a second aspect, an embodiment of the present invention provides a multi-mode matching device, including:
[0028] Acquisition module: used to obtain the text to be matched;
[0029] A conversion module; used for converting the to-be-matched text into a symbol sequence;
[0030] Matching module: used to match the child nodes corresponding to each symbol in the symbol sequence in sequence starting from the root node of the dictionary tree, and in the matching process, determine the matching strategy according to the type of each symbol in the symbol sequence;
[0031] Obtaining module: used to obtain the matching result from the path from the root node to the child node.
[0032] In a third aspect, an embodiment of the present invention further provides an electronic device, including: a processor and a memory;
[0033] The processor is used to execute any one of the multi-mode matching methods described above by calling the program or instruction stored in the memory.
[0034] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a program or instruction, wherein the program or instruction enables a computer to execute any of the multi-mode matching methods described above.
[0035] The beneficial effects of the present invention are: by acquiring the text to be matched; symbolizing the text to be matched and converting it into a symbol sequence; matching the child nodes corresponding to each symbol in the symbol sequence in sequence starting from the root node of the dictionary tree, and in the matching process, determining the matching strategy according to each symbol type in the symbol sequence; and obtaining the matching result after the matching is completed in sequence. The present application can convert the regular expression into a symbol sequence by symbolizing the text to be matched and converting it into a symbol sequence, and then matching the symbol sequence in sequence on the dictionary tree, and adopting different matching strategies for different symbol types, such as the matching strategies of interval nodes, active nodes, etc., which are completely consistent with the regular grammar, not only with a fast matching speed, but also matching based on the expression ability of the dictionary tree, which greatly enhances the expression ability of the pattern and reduces the maintenance cost of the pattern. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] Figure 1 A multi-mode matching method provided by an embodiment of the present invention Figure 1 ;
[0037] Figure 2 A multi-mode matching method provided by an embodiment of the present invention Figure 2 ;
[0038] Figure 3 A multi-mode matching method provided by an embodiment of the present invention Figure 3 ;
[0039] Figure 4A diagram of a multi-mode matching device provided by an embodiment of the present invention;
[0040] Figure 5 A schematic block diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0041] The principles and features of the present invention are described below in conjunction with the accompanying drawings. The examples given are only used to explain the present invention and are not used to limit the scope of the present invention.
[0042] In order to more clearly understand the above-mentioned purpose, features and advantages of the present application, the present disclosure is further described in detail below in conjunction with the accompanying drawings and examples. It is to be understood that the described embodiments are part of the embodiments of the present disclosure, rather than all of the embodiments. The specific embodiments described herein are merely used to explain the present disclosure, rather than to limit the present application. Based on the described embodiments of the present application, all other embodiments obtained by those of ordinary skill in the art belong to the scope of protection of the present application.
[0043] It should be noted that, in this document, relational terms such as “first” and “second” are merely used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations.
[0044] Figure 1 A multi-mode matching method provided by an embodiment of the present invention Figure 1 ;
[0045] In a first aspect, an embodiment of the present invention provides a multi-mode matching method, combining Figure 1 ,include:
[0046] S101: Obtain text to be matched.
[0047] Specifically, the text to be matched in the embodiment of the present application may be a regular expression such as BCD.
[0048] S102: Tokenize the text to be matched into a symbol sequence.
[0049] Exemplarily, the text to be matched is converted into a symbol sequence after BCD symbolization, such as S=[B|text, C|text, D|text].
[0050] S103: starting from the root node of the dictionary tree, matching the child nodes corresponding to each symbol in the symbol sequence in sequence. During the matching process, determining the matching strategy according to the type of each symbol in the symbol sequence.
[0051] Specifically, in the embodiment of the present application, after symbolization, the symbol sequence S = [B|text, C|text, D|text] will be placed on the dictionary tree through the tree construction tool. For the first symbol, starting from the root node of the dictionary tree, find the child node with key B, and find B; then for the second character of S, start from node B of the dictionary tree and find C; search in sequence until the last character is found. It should be understood that the above example is just a simple regular expression example. The following describes the process of determining the matching strategy according to each symbol type in the symbol sequence during the matching process.
[0052] S104: Obtain a matching result from the path from the root node to the child node.
[0053] At this point, the symbol sequence S corresponds to the path B->C->D on the dictionary tree, and the matching result BCD is obtained.
[0054] In some embodiments, in the above multi-mode matching method, symbolizing the text to be matched into a symbol sequence includes:
[0055] The character expression of the text to be matched is segmented by a symbolization tool to form different symbol types.
[0056] Exemplarily, the text to be matched is a regular expression, such as "A\dE[az]+", which becomes the following symbol sequence after being symbolized by the symbolization tool: A (text), \d (number), E (text), [az]+ (interval node is also an active node).
[0057] Each symbol in the symbol sequence is separated by the first separator.
[0058] Specifically, each symbol in the symbol sequence A (text), \d (number), E (text) [az] + (interval node is also an active node) is separated by the first separator comma. Specifically, the first separator can be flexibly selected.
[0059] Each symbol in the symbol sequence includes two parts, the first part is the symbol itself, and the second part is the symbol type, and the first part and the second part are separated by a second separator.
[0060] The symbol types include: text type, number type, interval type or activity node type.
[0061] For example, the symbol sequence S = [B|text, C|text, D|text] is obtained.
[0062] As shown above, the square brackets represent a sequence of symbols, and each symbol is separated by ",". Each symbol is divided into two parts, separated by "|", the front part is the text of the symbol itself, and the part after "|" indicates the type of symbol. Specifically, the segmentation symbols can be flexibly determined according to the specific circumstances, and the protection scope of this application is not limited thereto. Symbol types include: text type, number type, interval type or activity node type.
[0063] Figure 2 A multi-mode matching method provided by an embodiment of the present invention Figure 2 .
[0064] In some embodiments, in the above multi-mode matching method,
[0065] Starting from the root node of the dictionary tree, the child nodes corresponding to each symbol in the symbol sequence are matched in sequence, including: starting from the root node of the dictionary tree, matching the child node corresponding to the first symbol in the symbol sequence; when the child node corresponding to the first symbol is matched, matching the child node of the second symbol in the symbol sequence from the child node; matching in sequence until the child node corresponding to the last symbol in the symbol sequence is matched to obtain a matching result.
[0066] Exemplary, combined Figure 2 , for the first symbol in the symbol sequence, start from the root node of the dictionary tree, find the child node with key B, and find B; then for the second character in the symbol sequence, start from node B in the dictionary tree and find C; then for the third character in the symbol sequence, find node D in the dictionary tree. So far, S is matched, and the corresponding path on the dictionary tree is B->C->D, and the matching result is BCD.
[0067] Figure 3 A multi-mode matching method provided by an embodiment of the present invention Figure 3 .
[0068] In some embodiments, in the above multi-mode matching method, determining a matching strategy according to each symbol type in the symbol sequence includes:
[0069] When the symbol type is an active node, if the current character matches the active node successfully, the next character is still matched at the active node.
[0070] When the symbol type is an interval node, the current character is matched with every character in the interval node.
[0071] When the symbol types include at least two, the current character is transcribed or retained for matching.
[0072] For example, the input text is "AC5dfE". The process is similar to that described above. First, the input text is symbolized into a symbol sequence to obtain the symbol sequence: [A|text, C|text, 5|number or text, d|text, f|text, E|text]. After symbolization, the symbol sequence [A|text, C|text, 5|number or text, d|text, f|text, E|text] is matched on the dictionary tree. When "5|number or text" is matched, 5 will be transcribed into \d or retained. The next node on the dictionary tree happens to be \d, so the match is successful.
[0073] When the character "d|text" successfully matches the node "\d", the subsequent matching starts from this node, [az]+, which is an interval node. The current character d matches each character in the interval node. If the match is successful, [az]+ is still an active node. If the current character matches the active node successfully, the next character is still matched at the active node, so f is still matched at the node [az]+. If the match is successful, the next symbol E|text does not belong to the interval [az]. The dictionary tree needs to check whether the child node of the active node [az]+ can match E. It is found that there is indeed a child node E that can match the symbol E|text, so the match is successful. In this way, the pattern "AC\d[az]+E" can match the input text "AC5dfE".
[0074] In some embodiments, in the above-mentioned multi-mode matching method, after the to-be-matched text is symbolized and converted into a symbol sequence, the symbol sequence is placed on the dictionary tree through a tree construction tool.
[0075] After symbolization, such as the symbol sequence S = [B|text, C|text, D|text], it will be placed on the dictionary tree through the tree building tool.
[0076] In some embodiments, in the above multi-mode matching method, after the matching is completed in sequence and the matching result is obtained, it also includes: determining the final matching result through a result selection unit.
[0077] Exemplarily, the result selection unit may select the result with the longest match. For example, if the input string is ABCDEF and there are two matching results, AB and ABC, the result selection unit selects ABC because ABC is longer.
[0078] In some embodiments, in the above-mentioned multi-mode matching method, matching the sub-nodes corresponding to each symbol in the symbol sequence in sequence starting from the root node of the dictionary tree includes: matching through a dictionary tree matching logic unit.
[0079] Specifically, for each input word, after it is symbolized, it passes through the dictionary tree matching logic unit to match the corresponding node on the dictionary tree. When the text can match a complete path of the dictionary tree, it is output as a result.
[0080] Figure 4 A diagram of a multi-mode matching device provided in an embodiment of the present invention.
[0081] In a second aspect, an embodiment of the present invention provides a multi-mode matching device, Figure 4 ,include:
[0082] Acquisition module 401: used to acquire the text to be matched;
[0083] Specifically, the text to be matched in the embodiment of the present application may be a regular expression such as BCD, and the text to be matched is obtained by the obtaining module 401 .
[0084] The conversion module 402 is used to convert the to-be-matched text into a symbol sequence;
[0085] Exemplarily, the text to be matched is converted into a symbol sequence after BCD symbolization by the conversion module 502, such as S=[B|text, C|text, D|text].
[0086] Matching module 403: used to match the child nodes corresponding to each symbol in the symbol sequence in sequence starting from the root node of the dictionary tree, and in the matching process, determine the matching strategy according to the type of each symbol in the symbol sequence;
[0087] Specifically, in the embodiment of the present application, after symbolization, the sequence S = [B|text, C|text, D|text] will be placed on the dictionary tree through the tree construction tool. For the first symbol, the matching module 503 starts from the root node of the dictionary tree, finds the child node with key B, and finds B; then for the second character of S, starts from node B of the dictionary tree and finds C; then for the third character of S, finds node D from the dictionary tree. It should be understood that the above example is just a simple regular expression example, and the above has introduced the process of determining the matching strategy according to each symbol type in the symbol sequence during the matching process.
[0088] Obtaining module 404: used to obtain the matching result after the matching is completed in sequence.
[0089] At this point, S is matched, and the corresponding path on the dictionary tree is B->C->D, and module 504 obtains the matching result BCD.
[0090] In a third aspect, an embodiment of the present invention further provides an electronic device, including: a processor and a memory;
[0091] The processor is used to execute any one of the multi-mode matching methods described above by calling the program or instruction stored in the memory.
[0092] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a program or instruction, wherein the program or instruction enables a computer to execute any of the multi-mode matching methods described above.
[0093] Figure 5 It is a schematic block diagram of an electronic device provided by an embodiment of the present disclosure.
[0094] like Figure 5 As shown, the electronic device includes: at least one processor 501, at least one memory 502 and at least one communication interface 503. The various components in the electronic device are coupled together through a bus system 504. The communication interface 503 is used to transmit information between external devices. It can be understood that the bus system 504 is used to achieve connection and communication between these components. In addition to the data bus, the bus system 504 also includes a power bus, a control bus and a status signal bus. However, for the sake of clarity, Figure 5 Various buses are labeled as bus system 504 .
[0095] It can be understood that the memory 502 in this embodiment can be a volatile memory or a non-volatile memory, or can include both volatile and non-volatile memories.
[0096] In some implementations, the memory 502 stores the following elements, executable units or data structures, or a subset thereof, or an extended set thereof: an operating system and application programs.
[0097] Among them, the operating system includes various system programs, such as a framework layer, a core library layer, a driver layer, etc., which are used to implement various basic services and process hardware-based tasks. The application program includes various application programs, such as a media player (Media Player), a browser (Browser), etc., which are used to implement various application services. A program that implements any method in a multi-mode matching method provided in an embodiment of the present application can be included in the application program.
[0098] In the embodiment of the present application, the processor 501 calls the program or instructions stored in the memory 502, specifically, the program or instructions stored in the application, and the processor 501 is used to execute the steps of each embodiment of the multi-mode matching method provided in the embodiment of the present application.
[0099] Get the text to be matched;
[0100] Tokenize the text to be matched into a symbol sequence;
[0101] Starting from the root node of the dictionary tree, the child nodes corresponding to each symbol in the symbol sequence are matched in sequence. During the matching process, a matching strategy is determined according to the type of each symbol in the symbol sequence;
[0102] After the matching is completed in sequence, the matching result is obtained.
[0103] Any of the multi-mode matching methods provided in the embodiments of the present application can be applied to the processor 501, or implemented by the processor 501. The processor 501 can be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by an integrated logic circuit of hardware in the processor 501 or an instruction in the form of software. The above-mentioned processor 501 can be a general-purpose processor, a digital signal processor (Digital Signal Processor, DSP), an application-specific integrated circuit (Application Specific Integrated Circuit, ASIC), a field programmable gate array (Field Programmable Gate Array, FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor, etc.
[0104] The steps of any method in the multi-mode matching method provided in the embodiment of the present application can be directly embodied as being executed by a hardware decoding processor, or being executed by a combination of hardware and software units in the decoding processor. The software unit can be located in a storage medium mature 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 the memory 502, and the processor 501 reads the information in the memory 502 and completes the steps of the multi-mode matching method in combination with its hardware.
[0105] Those skilled in the art will appreciate that although some embodiments described herein include certain features included in other embodiments but not other features, the combination of features from different embodiments is meant to be within the scope of the present application and to form different embodiments.
[0106] Those skilled in the art will appreciate that the description of each embodiment has its own emphasis, and for parts not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0107] Although the embodiments of the present application are described in conjunction with the accompanying drawings, those skilled in the art may make various modifications and variations without departing from the spirit and scope of the present application, and such modifications and variations all fall within the scope defined by the attached claims. The above are only specific embodiments of the present invention, but the protection scope of the present invention is not limited thereto. Any technician familiar with the technical field can easily think of various equivalent modifications or substitutions within the technical scope disclosed by the present invention, and these modifications or substitutions should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.
[0108] The above are only specific embodiments of the present invention, but the protection scope of the present invention is not limited thereto. Any technician familiar with the technical field can easily think of various equivalent modifications or replacements within the technical scope disclosed by the present invention, and these modifications or replacements should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention shall be based on the protection scope of the claims.
Claims
1. A multi-mode matching method, characterized in that: include: Get the text to be matched; Symbolize the text to be matched and convert it into a symbol sequence; Starting from the root node of the dictionary tree, the child nodes corresponding to each symbol in the symbol sequence are matched in sequence. During the matching process, a matching strategy is determined according to the type of each symbol in the symbol sequence; Obtaining a matching result from the path from the root node to the child node; The symbol type includes: text type, number type, interval type or activity node; The to-be-matched text is symbolized and converted into a symbol sequence, including: Using a symbolization tool to segment the character expression of the text to be matched into different symbol types; Each symbol in the symbol sequence is separated by a first separator; Each symbol includes two parts, a first part is the symbol itself, and a second part is the symbol type, and the first part and the second part are separated by a second separator; Determining a matching strategy according to each symbol type in the symbol sequence includes: When the symbol type is an active node, if the current character matches the active node successfully, the next character is still matched at the active node; When the symbol type is an interval node, the current character is matched with each character in the interval node; When the symbol types include at least two, the current character is transcribed or retained for matching.
2. The multi-mode matching method according to claim 1, characterized in that: The step of sequentially matching the sub-nodes corresponding to each symbol in the symbol sequence starting from the root node of the dictionary tree includes: Starting from the root node of the dictionary tree, matching the child node corresponding to the first symbol in the symbol sequence; After matching the child node corresponding to the first symbol, matching the child node of the second symbol in the symbol sequence from the child node; Match in sequence until the sub-node corresponding to the last symbol in the symbol sequence is matched to obtain the matching result.
3. The multi-mode matching method according to claim 1, characterized in that: After the to-be-matched text is symbolically converted into a symbol sequence, the symbol sequence is placed on a dictionary tree through a tree construction tool.
4. The multi-mode matching method according to claim 1, characterized in that: After obtaining the matching result from the path from the root node to the child node, the method further includes: determining a final matching result by a result selection unit.
5. The multi-mode matching method according to claim 1, characterized in that: The matching of the sub-nodes corresponding to each symbol in the symbol sequence in sequence starting from the root node of the dictionary tree includes: matching through a dictionary tree matching logic unit.
6. A multi-mode matching device, using the multi-mode matching method according to any one of claims 1 to 5, characterized in that: include: Acquisition module: used to obtain the text to be matched; Conversion module: used for converting the to-be-matched text into a symbol sequence; Matching module: used to match the child nodes corresponding to each symbol in the symbol sequence in sequence starting from the root node of the dictionary tree, and in the matching process, determine the matching strategy according to the type of each symbol in the symbol sequence; Obtaining module: used to obtain the matching result from the path from the root node to the child node; The symbol type includes: text type, number type, interval type or activity node; The to-be-matched text is symbolized and converted into a symbol sequence, including: Using a symbolization tool to segment the character expression of the text to be matched into different symbol types; Each symbol in the symbol sequence is separated by a first separator; Each symbol includes two parts, a first part is the symbol itself, and a second part is the symbol type, and the first part and the second part are separated by a second separator; Determining a matching strategy according to each symbol type in the symbol sequence includes: When the symbol type is an active node, if the current character matches the active node successfully, the next character is still matched at the active node; When the symbol type is an interval node, the current character is matched with each character in the interval node; When the symbol types include at least two, the current character is transcribed or retained for matching.
7. An electronic device, characterized in that: include: Processor and memory; The processor is used to execute a multi-mode matching method as claimed in any one of claims 1 to 5 by calling the program or instruction stored in the memory.
8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a program or instruction, and the program or instruction enables a computer to execute a multi-mode matching method as claimed in any one of claims 1 to 5.
Citation Information
Patent Citations
Text regularization model training method and apparatus, and text regularization method and apparatus
CN107680579A
Dictionary tree construction method, statement search method and device, equipment and storage medium
CN109740165A
Cited By
High-speed mode matching device and method based on FPGA
CN120123291A
A High-Speed Pattern Matching Device and Method Based on FPGA
CN120123291B