Data parsing methods, apparatus, electronic devices and storage devices
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- WANGYIYOUDAO INFORMATION TECH BEIJING CO LTD
- Filing Date
- 2022-12-07
- Publication Date
- 2026-05-26
AI Technical Summary
Existing technologies, when parsing PGN files, generate branch paths with an exponentially increasing computational load, resulting in poor code usability and readability, and failing to efficiently implement branch path functionality.
By constructing a trie, the data chain of the target data element is obtained based on the data file, and branch data is added to the original data chain. The position of the target data element is determined by the trie, enabling rapid reconstruction and generation of branch data.
It improves the efficiency of data parsing, facilitates the review of data files and the addition of new ideas, and can quickly and accurately reproduce chessboard information.
Smart Images

Figure CN116108228B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of this application relate to the field of data processing, and more specifically, the embodiments of this application relate to data parsing methods, apparatus, electronic devices, and storage devices. Background Technology
[0002] This section is intended to provide background or context for the embodiments of this application set forth in the claims. The description herein may include concepts that may be explored, but not necessarily concepts that have been previously conceived or explored. Therefore, unless otherwise stated, what is described in this section is not prior art for the purposes of the specification and claims of this application, and is not acknowledged as prior art simply by virtue of its inclusion in this section.
[0003] With the rapid development of online education in my country, online platforms for various chess games have sprung up like mushrooms after rain. In chess, situations such as taking back moves or reviewing the game often occur, requiring the generation of branch moves. PGN stands for "Portable Game Notation," a standard setting for representing chess game data in ASCII text files. However, if PGN is simply parsed using a chain structure, the inherent incompatibility between the chain structure and the branch move data structure leads to a significant amount of additional segmentation, parsing, and insertion calculations. Moreover, the computational load increases exponentially with the depth of the branch moves, resulting in extremely poor code usability, readability, and maintainability, making it impossible to efficiently generate branch moves.
[0004] Therefore, there is an urgent need to propose a data parsing method that can analyze PGN and generate branch moves, which is conducive to quickly and accurately reproducing the board position information and facilitating the generation of new moves. Summary of the Invention
[0005] To overcome the problems existing in related technologies, the embodiments of this application aim to provide a data parsing method, apparatus, electronic device, and storage device. This data parsing method can easily reconstruct the data chain corresponding to the target data element and can realize the function of adding branch data on the original data chain, which facilitates the review of data files or the addition of new ideas and improves the data parsing efficiency.
[0006] In a first aspect of the embodiments of this application, a data parsing method is provided, comprising:
[0007] Retrieve the data file; the data file contains several interrelated data elements;
[0008] Construct a trie based on the data file;
[0009] The data chain containing the target data element is determined based on the trie;
[0010] Branch data is generated based on the data chain in which the target data element is located.
[0011] In one embodiment of this application, the data file is a PGN file;
[0012] Constructing a trie based on a data file includes:
[0013] The PGN file is parsed to obtain the PGN header information and PGN token data;
[0014] Determine the token sequence based on PGN token data;
[0015] Create tree nodes corresponding to each token element based on each token element in the token sequence;
[0016] Create an empty node as the root node of the trie;
[0017] A trie is constructed based on the root node and the tree nodes corresponding to each token element.
[0018] In one embodiment of this application, a tree node corresponding to each token element is created based on each token element in the token sequence, including:
[0019] If the current token element is move position information, then create the tree node corresponding to the current token element, and record the information carried by the tree node in the tree node corresponding to the current token element. The information carried by the tree node includes character information, child node array, parent node, and historical string information; and add the tree node corresponding to the current token element to the child node array of the parent node of the tree node corresponding to the current token element.
[0020] If the current token element is a left parenthesis, the tree node corresponding to the current token element is not created. Instead, the previous token element of the current token element is pushed into the preset token stack, and the parent node of the tree node corresponding to the previous token element is used as the parent node of the next token element of the previous token element.
[0021] If the current token element is a right parenthesis, the tree node corresponding to the current token element is not created. Instead, the top token element of the preset token stack is popped and used as the parent node of the next token element of the current token element.
[0022] In one embodiment of this application, determining a token sequence based on PGN token data includes:
[0023] Each token element in the PGN token data is obtained by sequentially matching using regular expressions, forming a token sequence.
[0024] In one embodiment of this application, character parsing of a PGN file includes:
[0025] Verify the validity of the left and right square brackets in the PGN file;
[0026] If the PGN file is valid, extract the PGN header information from the PGN file based on the left and right square brackets; and
[0027] Extract PGN token data from PGN files based on left and right square brackets.
[0028] In one embodiment of this application, verifying the legality of the left and right square brackets of the PGN file includes:
[0029] Maintain a left square bracket stack, a right square bracket stack, and a stack containing the total number of square brackets;
[0030] Scan all characters in the PGN file sequentially;
[0031] If a left square bracket is encountered during scanning, a flag is pushed onto the stack of square bracket counts.
[0032] If a right square bracket is encountered during scanning, a flag is popped from the total number of square brackets stack;
[0033] If the total number of square brackets stack is empty when scanning to the right square bracket, or if the total number of square brackets stack is not empty when scanning is completed, then the PGN file is determined to be invalid.
[0034] In one embodiment of this application, extracting PGN header information from a PGN file based on a left square bracket and a right square bracket includes:
[0035] Search the PGN file for every left square bracket and every right square bracket that matches each left square bracket;
[0036] Extract the characters between the left and right square brackets of each pair of matches to obtain the PGN header information.
[0037] In one embodiment of this application, extracting PGN token data from a PGN file based on a left square bracket and a right square bracket includes:
[0038] Determine if a left square bracket exists in the PGN file;
[0039] If it exists, start extracting characters from the character following the last right square bracket in the PGN header information until all characters have been extracted to obtain the PGN token data;
[0040] If it does not exist, then all characters in the PGN file are determined to be PGN token data.
[0041] In one embodiment of this application, when the data file is a PGN file, the target data element is the specified move position information;
[0042] Determining the data chain containing the target data element based on the trie includes:
[0043] Obtain the tree node information carried by the tree node corresponding to the target data element;
[0044] Determine the trajectory of historical child nodes based on the historical string information carried by the tree nodes;
[0045] Determine all child nodes of the tree node corresponding to the target data element based on the trie;
[0046] A data chain is formed based on the historical child node trajectory, and / or based on all child nodes of the tree node corresponding to the target data element.
[0047] In one embodiment of this application, generating branch data based on the data chain in which the target data element is located includes:
[0048] Create tree nodes on the data chain and obtain updated tree nodes;
[0049] Record the information carried by the tree node in the updated tree node, and add the updated tree node to the child node array of the parent node of the updated tree node.
[0050] In a second aspect of the embodiments of this application, a data parsing apparatus is provided for performing the data parsing method as described in any one of the first aspects, comprising:
[0051] The data acquisition module is used to acquire data files; the data files contain several interrelated data elements.
[0052] The trie building module is used to build tries based on data files;
[0053] The data chain determination module is used to determine the data chain in which the target data element is located based on the trie.
[0054] The branch data generation module is used to generate branch data based on the data chain in which the target data element is located.
[0055] In one embodiment of this application, the data file is a PGN file;
[0056] The trie building module is used for:
[0057] The PGN file is parsed to obtain the PGN header information and PGN token data;
[0058] Determine the token sequence based on PGN token data;
[0059] Create tree nodes corresponding to each token element based on each token element in the token sequence;
[0060] Create an empty node as the root node of the trie;
[0061] A trie is constructed based on the root node and the tree nodes corresponding to each token element.
[0062] In one embodiment of this application, the trie construction module is used for:
[0063] If the current token element is move position information, then create the tree node corresponding to the current token element, and record the information carried by the tree node in the tree node corresponding to the current token element. The information carried by the tree node includes character information, child node array, parent node, and historical string information; and add the tree node corresponding to the current token element to the child node array of the parent node of the tree node corresponding to the current token element.
[0064] If the current token element is a left parenthesis, the tree node corresponding to the current token element is not created. Instead, the previous token element of the current token element is pushed into the preset token stack, and the parent node of the tree node corresponding to the previous token element is used as the parent node of the next token element of the previous token element.
[0065] If the current token element is a right parenthesis, the tree node corresponding to the current token element is not created. Instead, the top token element of the preset token stack is popped and used as the parent node of the next token element of the current token element.
[0066] In one embodiment of this application, the trie construction module is used for:
[0067] Each token element in the PGN token data is obtained by sequentially matching using regular expressions, forming a token sequence.
[0068] In one embodiment of this application, the trie construction module is used for:
[0069] Verify the validity of the left and right square brackets in the PGN file;
[0070] If the PGN file is valid, extract the PGN header information from the PGN file based on the left and right square brackets; and
[0071] Extract PGN token data from PGN files based on left and right square brackets.
[0072] In one embodiment of this application, the trie construction module is used for:
[0073] Maintain a left square bracket stack, a right square bracket stack, and a stack containing the total number of square brackets;
[0074] Scan all characters in the PGN file sequentially;
[0075] If a left square bracket is encountered during scanning, a flag is pushed onto the stack of square bracket counts.
[0076] If a right square bracket is encountered during scanning, a flag is popped from the total number of square brackets stack;
[0077] If the total number of square brackets stack is empty when scanning to the right square bracket, or if the total number of square brackets stack is not empty when scanning is completed, then the PGN file is determined to be invalid.
[0078] In one embodiment of this application, the trie construction module is used for:
[0079] Search the PGN file for every left square bracket and every right square bracket that matches each left square bracket;
[0080] Extract the characters between the left and right square brackets of each pair of matches to obtain the PGN header information.
[0081] In one embodiment of this application, the trie construction module is used for:
[0082] Determine if a left square bracket exists in the PGN file;
[0083] If it exists, start extracting characters from the character following the last right square bracket in the PGN header information until all characters have been extracted to obtain the PGN token data;
[0084] If it does not exist, then all characters in the PGN file are determined to be PGN token data.
[0085] In one embodiment of this application, when the data file is a PGN file, the target data element is the specified move position information;
[0086] The data link determination module is also used for:
[0087] Obtain the tree node information carried by the tree node corresponding to the target data element;
[0088] Determine the trajectory of historical child nodes based on the historical string information carried by the tree nodes;
[0089] Determine all child nodes of the tree node corresponding to the target data element based on the trie;
[0090] A data chain is formed based on the historical child node trajectory, and / or based on all child nodes of the tree node corresponding to the target data element.
[0091] In one embodiment of this application, the branch data generation module is used for:
[0092] Create tree nodes on the data chain and obtain updated tree nodes;
[0093] Record the information carried by the tree node in the updated tree node, and add the updated tree node to the child node array of the parent node of the updated tree node.
[0094] A third aspect of this application provides an electronic device, comprising:
[0095] Processor; and
[0096] A memory that stores executable code, which, when executed by the processor, causes the processor to perform the method described above.
[0097] A fourth aspect of this application provides a non-transitory machine-readable storage medium having executable code stored thereon, which, when executed by a processor of an electronic device, causes the processor to perform the method described above.
[0098] The data parsing method, apparatus, electronic device, and storage device provided in this application acquire a data file containing several interrelated data elements, construct a trie based on the data file, determine the data chain in which the target data element is located based on the trie, and generate branch data based on the data chain in which the target data element is located. This enables convenient reconstruction of the data chain corresponding to the target data element and allows for the addition of branch data to the existing data chain, facilitating the review of data files or adding new ideas, and improving the efficiency of data parsing. Attached Figure Description
[0099] The above and other objects, features, and advantages of exemplary embodiments of this application will become readily apparent from the following detailed description taken in conjunction with the accompanying drawings. Several embodiments of this application are illustrated in the drawings by way of example and not limitation, in which:
[0100] Figure 1 A block diagram schematically illustrates an exemplary computing system 100 suitable for implementing embodiments of this application;
[0101] Figure 2 A schematic flowchart of a data parsing method according to another embodiment of this application is shown.
[0102] Figure 3 A schematic flowchart of a data parsing method according to yet another embodiment of this application is shown.
[0103] Figure 4 A schematic flowchart of a data parsing method according to another embodiment of this application is shown;
[0104] Figure 5 A schematic diagram of the structure of a data parsing apparatus according to another embodiment of this application is shown;
[0105] Figure 6 A schematic block diagram of an electronic device according to an embodiment of this application is shown.
[0106] In the accompanying drawings, the same or corresponding reference numerals indicate the same or corresponding parts. Detailed Implementation
[0107] The principles and spirit of this application will now be described with reference to several exemplary embodiments. It should be understood that these embodiments are provided merely to enable those skilled in the art to better understand and implement this application, and are not intended to limit the scope of this application in any way. Rather, these embodiments are provided to make this disclosure more thorough and complete, and to fully convey the scope of this disclosure to those skilled in the art.
[0108] Figure 1 A block diagram of an exemplary computing system 100 suitable for implementing embodiments of this application is shown. Figure 1 As shown, the computing system 100 may include: a central processing unit (CPU) 101, random access memory (RAM) 102, read-only memory (ROM) 103, a system bus 104, a hard disk controller 105, a keyboard controller 106, a serial interface controller 107, a parallel interface controller 108, a display controller 109, a hard disk 110, a keyboard 111, a serial external device 112, a parallel external device 113, and a display 114. Among these devices, the CPU 101, RAM 102, ROM 103, hard disk controller 105, keyboard controller 106, serial controller 107, parallel controller 108, and display controller 109 are coupled to the system bus 104. The hard disk 110 is coupled to the hard disk controller 105, the keyboard 111 is coupled to the keyboard controller 106, the serial external device 112 is coupled to the serial interface controller 107, the parallel external device 113 is coupled to the parallel interface controller 108, and the display 114 is coupled to the display controller 109. It should be understood that... Figure 1 The structural diagrams described are for illustrative purposes only and are not intended to limit the scope of this application. In some cases, certain devices may be added or removed depending on the specific circumstances.
[0109] Those skilled in the art will recognize that embodiments of this application can be implemented as a system, method, or computer program product. Therefore, this disclosure can be specifically implemented as entirely hardware, entirely software (including firmware, resident software, microcode, etc.), or a combination of hardware and software, generally referred to herein as a "circuit," "module," or "system." Furthermore, in some embodiments, this application can also be implemented as a computer program product contained in one or more computer-readable media, which includes computer-readable program code.
[0110] Any combination of one or more computer-readable media may be used. A computer-readable medium can be a computer-readable signal medium or a computer-readable storage medium. A computer-readable storage medium can be, for example,, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples (not exhaustive) of a computer-readable storage medium may include: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this document, a computer-readable storage medium can be any tangible medium that contains or stores a program that can be used by or in connection with an instruction execution system, apparatus, or device.
[0111] Computer-readable signal media may include data signals propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals may take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, capable of sending, propagating, or transmitting programs for use by or in connection with an instruction execution system, apparatus, or device.
[0112] Program code contained on a computer-readable medium may be transmitted using any suitable medium, including but not limited to wireless, wire, optical fiber, RF, etc., or any suitable combination thereof.
[0113] Computer program code for performing the operations of this application can be written in one or more programming languages or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, and C++, and conventional procedural programming languages such as "C" or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as a standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving remote computers, the remote computer can be connected to the user's computer via any type of network (including a local area network (LAN) or a wide area network (WAN)), or it can be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0114] The embodiments of this application will now be described with reference to flowchart illustrations and block diagrams of apparatus (or systems) according to embodiments of this application. It should be understood that each block of the flowcharts and / or block diagrams, and combinations of blocks in the flowcharts and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing apparatus to produce a machine that, when executed by a computer or other programmable data processing apparatus, creates means for implementing the functions / operations specified in the blocks of the flowcharts and / or block diagrams.
[0115] These computer program instructions may also be stored in a computer-readable medium that enables a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable medium produce a product comprising an instruction apparatus that implements the functions / operations specified in the boxes of a flowchart and / or block diagram.
[0116] Computer program instructions may also be loaded onto a computer, other programmable data processing apparatus, or other device to cause a series of operational steps to be performed on the computer, other programmable data processing apparatus, or other device to produce a computer-implemented process, such that the instructions that execute on the computer or other programmable apparatus can provide a process for implementing the functions / operations specified in the boxes of a flowchart and / or block diagram.
[0117] According to an embodiment of this application, a data parsing method and apparatus are proposed.
[0118] In this article, it is important to understand that any number of elements in the accompanying figures is for illustrative purposes and not for limitation, and any naming is for distinction only and has no limiting meaning.
[0119] The principles and spirit of this application will be explained in detail below with reference to several representative embodiments. Invention Overview
[0121] The applicant discovered that if PGN is parsed in a chain, when faced with the need to generate branch paths, a large amount of additional splitting, parsing, and insertion calculations are required because the chain structure and the branch path data structure are inherently incompatible. Moreover, as the branch path level increases, the amount of computation increases exponentially, resulting in extremely poor usability, readability, and maintainability of the code, making it impossible to efficiently implement the function of generating branch paths.
[0122] Based on this, the technical solution of this application obtains a data file containing several interrelated data elements, constructs a trie based on the data file, determines the data chain in which the target data element is located based on the trie, and generates branch data based on the data chain in which the target data element is located. This allows for easy reconstruction of the data chain corresponding to the target data element and enables the addition of branch data to the original data chain, providing convenience for reviewing data files or adding new ideas and improving data parsing efficiency.
[0123] After introducing the basic principles of this application, the various non-limiting embodiments of this application will be described in detail below.
[0124] Application Scenarios Overview
[0125] The data parsing method of this application is applicable to scenarios that require data interpretation of data files containing several interrelated data elements and the ability to reconstruct data chains based on the parsing results. Examples include parsing chess notation files or Chinese chess notation files to reconstruct the moves before and / or after a target move; or parsing movement path recording files to reconstruct the position records before and / or after a target position. This method has numerous applications. Accordingly, the data parsing method of this application can be applied to chess teaching equipment, chess teaching websites, navigators, etc., to facilitate users in reviewing chess games or movement paths, enabling users to add new ideas or approaches.
[0126] Exemplary methods
[0127] The following is for reference. Figure 2 This application describes a data parsing method according to exemplary embodiments thereof. It should be noted that the above application scenarios are shown only to facilitate understanding of the spirit and principles of this application, and the embodiments of this application are not limited in any way. Rather, the embodiments of this application can be applied to any applicable scenario.
[0128] Figure 2 A schematic flowchart illustrating a data parsing method according to another embodiment of this application is shown below. Figure 2 The data parsing method illustrated in this application embodiment may include:
[0129] In step 201, the data file is obtained.
[0130] The data file contains several interrelated data elements. For example, assuming the data file is a chess notation file, the data elements could be the moves for each piece, such as C4, where C4 is the row and column number on the chessboard, and each move is interconnected. Alternatively, assuming the data file is a motion path recording file, the data elements could be each position recording point, such as 40N116E, which represents the position at 40° North latitude and 116° East longitude, and each position recording point is interconnected. It is understood that, besides chess notation files and motion path recording files, the data file in this application can also be in other forms. In practical applications, the form of the data file needs to be determined according to the specific application situation, and no single limitation is made here.
[0131] In step 202, a trie is constructed based on the data file.
[0132] A Trie, also known as a word lookup tree or word tree, is a tree-like data structure and a variant of a hash tree. It is an abstract data type or data structure used to simulate a data set with tree-like properties. It consists of a hierarchical set of n (n>0) finite nodes. The root node of a Trie contains no character, and each node except the root node contains only one character. The characters along the path from the root node to a given node, when concatenated, form the string corresponding to that node. All child nodes of each node contain distinct characters. Each combination of data elements in a data file can be considered as a separate string; therefore, a Trie can be constructed based on each string in the data file.
[0133] In step 203, the data chain in which the target data element is located is determined based on the trie.
[0134] It is understandable that the data chain containing the target data element can be a data chain consisting of the target data element and all the data elements it traverses before, a data chain consisting of the target data element and all the data elements it traverses after, or a complete data chain consisting of the target data element and all the data elements it traverses both before and after. For example, assuming the target data element is C4, and its previous data elements are A1, B7, and R5, and its subsequent data elements are S8, O2, and D2, then the data chain containing the target data element could be A1, B7, R5, C4; C4, S8, O2, D2; or A1, B7, R5, C4, S8, O2, D2. It is understood that in practical applications, the appropriate data chain should be selected based on the specific application requirements; no single limitation is made here.
[0135] In step 204, branch data is generated based on the data chain in which the target data element is located.
[0136] In this embodiment, branch data can be added starting from any data element in the data chain. Branch data can consist of one or more new data elements. For example, assuming the data file is a chess notation file, the target data element is C4, and the selected data chain is the chain formed by the target data element and all the data elements it has previously traversed, say A1, B7, R5, C4. If the next move after R5 could be E9, then a branch path E9 can be added starting from R5. Similarly, if the subsequent moves after C4 could be S8, O2, D2, or F6, L1, then a branch path F6, L1 can be added starting from C4. It is understood that in practical applications, the form of the starting data element when adding branch data is diverse and depends on the specific application; no single limitation is made here.
[0137] By acquiring a data file containing several interconnected data elements, a trie is constructed based on the data file. Then, the data chain containing the target data element is determined based on the trie, and branch data is generated based on the data chain containing the target data element. This allows for easy reconstruction of the data chain corresponding to the target data element and enables the addition of branch data to the existing data chain. This facilitates the review of data files or the addition of new ideas, and improves the efficiency of data parsing.
[0138] In some embodiments, the data file can be a PGN file, in which case the trie will be built based on the PGN file. Figure 3 A schematic flowchart illustrating a data parsing method according to another embodiment of this application is shown below. Figure 3 The data parsing method illustrated in this application embodiment may include:
[0139] In step 301, the PGN file is parsed to obtain the PGN header information and PGN token data.
[0140] PGN stands for "Portable Game Notation," a standard notation for representing chess game data using ASCII text files. PGN is designed to be easy for humans to read and write, and easy for computer programs to parse and generate. In this embodiment, the PGN file does not only refer to chess game data, but can also be Chinese chess game data, military chess game data, or other chess game data. In practical applications, the specific choice depends on the application; no single limitation is made here.
[0141] During character parsing, the validity of the left and right square brackets in the PGN file is first verified. If the PGN file is determined to be invalid, an error message can be displayed, or the character parsing process can be terminated, depending on the actual application. Specifically, a left square bracket stack, a right square bracket stack, and a total square bracket count stack are maintained. All characters in the PGN file are then scanned sequentially. If a left square bracket is encountered, a flag is pushed onto the total square bracket count stack, and the number of left square brackets in the left square bracket stack is decremented by one. If a right square bracket is encountered, a flag is popped from the total square bracket count stack, and the number of right square brackets in the right square bracket stack is decremented by one. This continues until the number of square brackets in both the left and right square bracket stacks is zero. If the total square bracket count stack is empty when a right square bracket is encountered, or if the total square bracket count stack is not empty after the scan is complete, it indicates that the number of left and right square brackets does not match, and the PGN file is determined to be invalid.
[0142] Then, if the PGN file is valid, the PGN header information is extracted from it based on the left and right square brackets. Specifically, the PGN file is searched for each left square bracket and each right square bracket that matches it. The characters between each pair of matching left and right square brackets are then extracted to obtain the PGN header information. For example, suppose the PGN file is:
[0143] [Event"F / S Return Match"]
[0144] [Site"Belgrade,Serbia JUG"]
[0145] [Date: 1992.11.04]
[0146] [Round "29"]
[0147] [White "Fischer, Robert J."]
[0148] [Black"Spassky, Boris V."]
[0149] [Result"1 / 2-1 / 2"]
[0150] 1.c4 b5 2.a4(2.g4 f5 3.h4(3.Nc3 Nf6 4.Nf3)3...e5)2...d5 3.f4 g5(3...Kd7 4.Qb3 g5)
[0151] It is understood that in the example PGN file provided above, the information from Event "F / S Return Match" to Result "1 / 2-1 / 2" constitutes the PGN header information. This header information describes the game information, which may include, but is not limited to, the players, game name, game time, and game result, etc., and is not uniquely limited. The characters Event, Site, Date, etc., can be considered as keys in the PGN header information, and correspondingly, "F / SReturn Match", "Belgrade,Serbia JUG", and "1992.11.04" can be considered as the values corresponding to those keys.
[0152] Next, PGN token data is extracted from the PGN file based on the left and right square brackets. Specifically, it is determined whether a left square bracket exists in the PGN file. If it does, characters are extracted starting from the character following the last right square bracket in the PGN header information until all characters are extracted, resulting in PGN token data. As shown in the example PGN file provided above, the PGN file contains a left square bracket, and the last right square bracket in the PGN header information is the right square bracket in [Result"1 / 2-1 / 2"]. Therefore, the character following this right square bracket is 1, which is used as the starting point for character extraction until all characters are extracted, resulting in the following PGN token data: 1.c4 b5 2.a4(2.g4 f5 3.h4(3.Nc3 Nf6 4.Nf3)3...e5)2...d5 3.f4 g5(3...Kd7 4.Qb3g5). If no left square bracket exists, it means that there is no PGN header information in the PGN file, and all characters in the PGN file are determined to be PGN token data.
[0153] In step 302, the token sequence is determined based on the PGN token data.
[0154] The PGN token data is sequentially matched using regular expressions to form a token sequence. A regular expression (also known as a regex, regexp, or RE in code) is a text pattern that includes ordinary characters (such as letters from a to z) and special characters. Regular expressions use a single string to describe and match a series of strings that match a certain syntax rule. They are typically used to retrieve and replace text that conforms to a specific pattern or rule. As shown in the example PGN file provided in step 301, excluding the numerical codes such as "1.", "2...", and "3...", the rest are token elements. The final extracted token sequence could be {c4b5 a4(g4 f5 h4(Nc3 Nf6 Nf3)e5)d5f4 g5(Kd7 Qb3 g5)}, where {} are sequence symbols, and the characters between {} are the individual token elements. It is understandable that there are various ways to match each token element in the PGN token data. In practical applications, in addition to regular expressions, other methods can also be used for matching, and no single method is specified here.
[0155] In step 303, tree nodes corresponding to each token element are created based on each token element in the token sequence.
[0156] If the current token element is move position information, such as c4, b5, etc. as shown in the example PGN file provided in step 301, then a tree node corresponding to the current token element is created, and the information carried by the tree node is recorded in the tree node corresponding to the current token element. The information carried by the tree node includes, but is not limited to, character information, child node array, parent node, and history string information. It may also include Index information, ForceMoveNumber information, and StepText information, etc. Among them, the character information is the move position information carried by the current tree node; the child node array is the set of child nodes of the current tree node. The parent node is the previous tree node of the current tree node; the history string information refers to all tree nodes traversed from the root node to the current tree node; the index information is the move number corresponding to the move position information carried by the current tree node, which is used to record how many moves the current move is in the entire game; the ForceMoveNumber information records the judgment information of whether the move index needs to be displayed; the StepText information represents the move index, as shown in the example PGN file provided in step 301, the move index is "1.", "2...", and "3...", etc. Furthermore, the tree node corresponding to the current token element is added to the child node array of the parent node of the tree node corresponding to the current token element, that is, it indicates that the tree node corresponding to the current token element is a child node of its previous tree node, thus establishing a connection between each tree node.
[0157] If the current marker element is a left parenthesis, it indicates that there are branching moves at the current move position. In this case, the tree node corresponding to the current marker element is not created. Instead, the previous marker element is pushed onto the preset marker stack, and the parent node of the tree node corresponding to the previous marker element is used as the parent node of the next marker element of the previous marker element. For example, as shown in the PGN file provided in step 301, if the previous marker element of the left parenthesis is a4, then a4 is pushed onto the preset marker stack. The parent node of the tree node corresponding to a4, which is the tree node corresponding to b5, is used as the parent node of the tree node corresponding to the next marker element g4 of the left parenthesis. That is, the tree node corresponding to b5 is the parent node of the tree node corresponding to g4, indicating that the next move of b5 can be either a4 or g4. Subsequent moves starting from g4 are considered as subsequent branching moves of the move b5.
[0158] If the current marker element is a right parenthesis, it indicates the end of the branching process. Therefore, the tree node corresponding to the current marker element is not created. Instead, the top marker element of the preset marker stack is popped, and this top marker element becomes the parent node of the next marker element after the current marker element. It can be understood that the marker element that first enters the preset marker stack will be at the bottom of the stack. As marker elements are continuously pushed in, they will stack on top of the existing marker elements, with the top marker element becoming the top marker element. For example, as shown in the exemplary PGN file provided in step 301, if the top marker element is h4, then h4 is popped from the preset marker stack, and the tree node corresponding to h4 becomes the parent node of the tree node corresponding to e5, which is the next marker element after the right parenthesis. In other words, the parent node of the tree node corresponding to e5 is the tree node corresponding to h4.
[0159] In step 304, an empty node is created as the root node of the trie.
[0160] In step 305, a trie is constructed based on the root node and the tree nodes corresponding to each token element. Starting from the root node, the tree nodes are connected together according to the parent-child relationships between them to form the trie.
[0161] In some embodiments, when the data file is a PGN file, the target data element is the specified move position information, which enables the determination of the move trajectory where the specified move position information is located based on the trie, thereby enabling the reproduction of the corresponding game or the addition of new branch moves on the move trajectory. Figure 4 A schematic flowchart illustrating a data parsing method according to another embodiment of this application is shown below. Figure 4 The data parsing method illustrated in this application embodiment may include:
[0162] In step 401, the tree node information carried by the tree node corresponding to the target data element is obtained.
[0163] In step 402, the historical child node trajectory is determined based on the historical string information carried by the tree node. It is understandable that, since the PGN header information is unchanging, the PGN header information obtained during character parsing can be saved as a template for later retrieval, or it can be obtained through re-character parsing, depending on the actual application; no single limitation is made here.
[0164] Determining the historical child node trajectory allows us to determine the historical movement trajectory corresponding to the specified move position information. For example, as shown in the example PGN file provided in step 301, assuming the target data element is a4, the historical child node trajectory is the tree node corresponding to c4, the tree node corresponding to b5, and the tree node corresponding to a4. Thus, it is confirmed that the historical movement trajectory corresponding to a4 is from c4 to b5 to a4. After splicing the PGN header information, we can determine the PGN information of the game state corresponding to the specified move position information.
[0165] In step 403, all child nodes of the tree node corresponding to the target data element are determined based on the trie.
[0166] By determining all child nodes of the tree node corresponding to the target data element using a trie, all subsequent moves after the specified move position information can be displayed. The move trajectory of the first child node can be regarded as the trunk, and the remaining moves in parentheses are branches. Then, the tree nodes in the trunk are spliced together, and the tree nodes in the branches are spliced together and then connected to the trunk, so as to determine the PGN information of the subsequent moves after the specified move position information.
[0167] It is understandable that there is no strict execution sequence restriction between steps 402 and 403. They can be executed simultaneously, sequentially, or one can be executed at a time, depending on the actual application. No single restriction is made here.
[0168] In step 404, a data chain is formed based on the historical child node trajectory, and / or based on all child nodes of the tree node corresponding to the target data element.
[0169] It's understandable that determining the historical child node trajectory is equivalent to determining the data chain formed by the target data element and all the data elements it has traversed. Similarly, determining all the child nodes of the tree node corresponding to the target data element is equivalent to determining the data chain formed by the target data element and all the data elements it has traversed. The historical child node trajectory and all the child nodes of the tree node corresponding to the target data element together form a complete data chain—that is, a complete data chain formed by the target data element and all the data elements it has traversed before and after it.
[0170] In this embodiment, historical move trajectories or complete trie can be concatenated with PGN header information. This allows the optimal data structure required for rendering the game record style to be deduced from the trie, thus reproducing the game situation corresponding to the specified move position information. This facilitates the user's undo function and allows the user to add new ideas to the game situation corresponding to the specified move position information. It also helps to re-display the game record based on the optimal data structure required for rendering the game record style and implement branching move functions, thereby improving the matching degree between the data structure and the game record data file.
[0171] In step 405, branch data is generated based on the data chain in which the target data element is located.
[0172] Tree nodes are created on the data chain to obtain updated tree nodes. The updated tree nodes record the information they carry, and are added to the child node array of their parent nodes. For example, as shown in the PGN file provided in step 301, assuming the target data element is the specified move position information b5, and the data chain currently in which the target data element is located is a chain consisting of the target data element and all the data elements it has previously encountered (the data chain could be c4, b5), and the user discovers that the subsequent move for b5 is not necessarily a4, but could also be j8, then an updated tree node corresponding to j8 is created. This updated tree node records the information it carries, including the character "j8," and is added to the child node array of the tree node corresponding to b5, making the tree node corresponding to b5 the parent node of the updated tree node corresponding to j8, forming a relationship. If there are other moves after j8, more updated tree nodes are created based on the updated tree node corresponding to j8. This enables the branching move functionality.
[0173] Understandably, when reviewing a game and discovering moves that should not have been made, the corresponding branch can be deleted by deleting the corresponding tree node and the corresponding records of its parent node's child nodes.
[0174] Exemplary device
[0175] After introducing the methods of exemplary embodiments of this application, the following references are made. Figure 5 and Figure 6 Products related to the data parsing method of exemplary embodiments of the present invention will be described.
[0176] Figure 5 A schematic diagram illustrating the structure of a data parsing method according to another embodiment of this application is shown. Please refer to... Figure 5 The data parsing apparatus shown in the embodiments of this application may include:
[0177] The data acquisition module 501 is used to acquire a data file; the data file contains several interrelated data elements;
[0178] Trie building module 502 is used to build trie based on data files;
[0179] The data chain determination module 503 is used to determine the data chain in which the target data element is located based on the trie;
[0180] The branch data generation module 504 is used to generate branch data based on the data chain in which the target data element is located.
[0181] The data parsing device proposed in this application retrieves a data file containing several interrelated data elements, constructs a trie based on the data file, determines the data chain containing the target data element based on the trie, and generates branch data based on the data chain containing the target data element. This allows for easy reconstruction of the data chain corresponding to the target data element and enables the addition of branch data to the existing data chain, facilitating the review of data files or adding new ideas, and improving the efficiency of data parsing.
[0182] Figure 6 A schematic block diagram of an electronic device according to an embodiment of this application is shown. Please refer to... Figure 6 The electronic device 600 may include:
[0183] Processor 601; and
[0184] The memory 602 stores computer instructions that, when executed by the processor 601, cause the electronic device 600 to perform the methods described according to the foregoing embodiments.
[0185] In some implementation scenarios, electronic device 600 may include server or terminal devices, such as physical servers, cloud servers, server clusters, data processing devices, application testing robots, computer terminals, smart terminals, PC devices, and Internet of Things terminals, etc.
[0186] Processor 601 can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor can be a microprocessor or any conventional processor.
[0187] Based on the foregoing, this application also discloses a computer-readable storage medium containing program instructions that, when executed by a processor, cause the methods described according to the foregoing embodiments to be implemented.
[0188] In some implementation scenarios, the aforementioned computer-readable storage medium can be any suitable magnetic or magneto-optical storage medium, such as Resistive Random Access Memory (RRAM), Dynamic Random Access Memory (DRAM), Static Random-Access Memory (SRAM), Enhanced Dynamic Random Access Memory (EDRAM), High-Bandwidth Memory (HBM), Hybrid Memory Cube (HMC), etc., or any other medium that can be used to store required information and can be accessed by an application, module, or both. Any such computer storage medium can be part of a device or accessible to or connected to a device. Any application or module described in this invention can be implemented using computer-readable / executable instructions that can be stored or otherwise maintained by such a computer-readable medium.
[0189] It should be noted that although several devices or sub-devices of the data parsing apparatus have been mentioned in the detailed description above, this division is not mandatory. In fact, according to the embodiments of this application, the features and functions of two or more devices described above can be embodied in one device. Conversely, the features and functions of one device described above can be further divided and embodied by multiple devices.
[0190] Furthermore, although the operations of the method of this application are described in a specific order in the accompanying drawings, this does not require or imply that these operations must be performed in that specific order, or that all of the operations shown must be performed to achieve the desired result. On the contrary, the steps depicted in the flowchart may be performed in a different order. Additionally or alternatively, certain steps may be omitted, multiple steps may be combined into one step, and / or one step may be broken down into multiple steps.
[0191] The use of the verbs "including" and "contains" and their inflections in the application documents does not preclude the existence of elements or steps other than those described in the application documents. The article "a" or "one" preceding an element does not preclude the existence of multiple such elements.
[0192] While the spirit and principles of this application have been described with reference to several specific embodiments, it should be understood that this application is not limited to the disclosed specific embodiments, and the division of aspects does not imply that features in these aspects cannot be combined for benefit; such division is merely for convenience of expression. This application is intended to cover various modifications and equivalent arrangements included within the spirit and scope of the appended claims. The scope of the appended claims is to be interpreted in the broadest sense, thereby encompassing all such modifications and equivalent structures and functions.
Claims
1. A data parsing method, characterized in that, include: Obtain a data file; the data file contains several interrelated data elements; Construct a dictionary tree based on the data file; The data chain containing the target data element is determined based on the trie; Branch data is generated based on the data chain in which the target data element is located; The data file is a PGN file; The construction of the trie based on the data file includes: The PGN file is parsed to obtain the PGN header information and PGN token data; Determine the token sequence based on the PGN token data; Create tree nodes corresponding to each token element based on each token element in the token sequence; Create an empty node as the root node of the trie; The trie is constructed based on the root node and the tree nodes corresponding to each token element.
2. The data parsing method according to claim 1, characterized in that, The step of creating tree nodes corresponding to each token element based on each token element in the token sequence includes: If the current token element is move position information, then create a tree node corresponding to the current token element, and record the information carried by the tree node in the tree node corresponding to the current token element. The information carried by the tree node includes character information, child node array, parent node, and historical string information; and add the tree node corresponding to the current token element to the child node array of the parent node of the tree node corresponding to the current token element. If the current token element is a left parenthesis, the tree node corresponding to the current token element is not created. Instead, the previous token element of the current token element is pushed into the preset token stack, and the parent node of the tree node corresponding to the previous token element is used as the parent node of the next token element of the previous token element. If the current token element is a right parenthesis, then the tree node corresponding to the current token element is not created. Instead, the top token element of the preset token stack is popped and the top token element is used as the parent node of the next token element of the current token element.
3. The data parsing method according to claim 1, characterized in that, The step of determining the token sequence based on the PGN token data includes: Each token element in the PGN token data is obtained by sequentially matching using regular expressions, forming the token sequence.
4. The data parsing method according to claim 1, characterized in that, The character parsing of the PGN file includes: Verify the validity of the left and right square brackets in the PGN file; If the PGN file is valid, then extract the PGN header information from the PGN file based on the left and right square brackets; and The PGN token data is extracted from the PGN file based on the left and right square brackets.
5. The data parsing method according to claim 4, characterized in that, The verification of the validity of the left and right square brackets of the PGN file includes: Maintain a left square bracket stack, a right square bracket stack, and a stack containing the total number of square brackets; Scan all characters in the PGN file sequentially; If the left square bracket is encountered during scanning, a flag is pushed onto the stack of total square bracket counts. If the right square bracket is encountered during scanning, a flag is popped from the stack of total square bracket counts. If the total number of square brackets stack is empty when the right square bracket is scanned, or if the total number of square brackets stack is not empty when the scan is completed, then the PGN file is determined to be invalid.
6. The data parsing method according to claim 4, characterized in that, The extraction of the PGN header information from the PGN file based on the left and right square brackets includes: Search the PGN file for each left square bracket and each right square bracket that matches each left square bracket; The characters between the left and right square brackets of each pair of matching characters are extracted to obtain the PGN header information.
7. The data parsing method according to claim 4, characterized in that, The extraction of the PGN token data from the PGN file based on the left and right square brackets includes: Determine whether the left square bracket exists in the PGN file; If it exists, extract characters starting from the character after the last right square bracket in the PGN header information until all characters have been extracted to obtain the PGN token data; If it does not exist, then all characters in the PGN file are determined to be the PGN token data.
8. The data parsing method according to claim 2, characterized in that, When the data file is a PGN file, the target data element is the specified move position information; Determining the data chain containing the target data element based on the trie includes: Obtain the tree node information carried by the tree node corresponding to the target data element; The trajectory of historical child nodes is determined based on the historical string information carried by the tree nodes; Based on the trie, determine all child nodes of the tree node corresponding to the target data element; The data chain is formed based on the historical child node trajectory, and / or based on all child nodes of the tree node corresponding to the target data element.
9. The data parsing method according to claim 8, characterized in that, The step of generating branch data based on the data chain in which the target data element is located includes: Create a tree node on the data chain to obtain an updated tree node; Record the information carried by the tree node in the updated tree node, and add the updated tree node to the child node array of the parent node of the updated tree node.
10. A data parsing device, characterized in that, A method for performing the data parsing method as described in any one of claims 1-9, comprising: A data acquisition module is used to acquire a data file; the data file contains several interrelated data elements; The trie construction module is used to construct a trie based on the data file; A data chain determination module is used to determine the data chain in which the target data element is located based on the trie; A branch data generation module is used to generate branch data based on the data chain in which the target data element is located. The data file is a PGN file; The trie construction module is used for: The PGN file is parsed to obtain the PGN header information and PGN token data; Determine the token sequence based on the PGN token data; Create tree nodes corresponding to each token element based on each token element in the token sequence; Create an empty node as the root node of the trie; The trie is constructed based on the root node and the tree nodes corresponding to each token element.
11. The data parsing apparatus according to claim 10, characterized in that, The trie construction module is used for: If the current token element is move position information, then create a tree node corresponding to the current token element, and record the information carried by the tree node in the tree node corresponding to the current token element. The information carried by the tree node includes character information, child node array, parent node, and historical string information; and add the tree node corresponding to the current token element to the child node array of the parent node of the tree node corresponding to the current token element. If the current token element is a left parenthesis, the tree node corresponding to the current token element is not created. Instead, the previous token element of the current token element is pushed into the preset token stack, and the parent node of the tree node corresponding to the previous token element is used as the parent node of the next token element of the previous token element. If the current token element is a right parenthesis, then the tree node corresponding to the current token element is not created. Instead, the top token element of the preset token stack is popped and the top token element is used as the parent node of the next token element of the current token element.
12. The data parsing apparatus according to claim 10, characterized in that, The trie construction module is used for: Each token element in the PGN token data is obtained by sequentially matching using regular expressions, forming the token sequence.
13. The data parsing apparatus according to claim 10, characterized in that, The trie construction module is used for: Verify the validity of the left and right square brackets in the PGN file; If the PGN file is valid, the PGN header information is extracted from the PGN file based on the left square bracket and the right square bracket; as well as The PGN token data is extracted from the PGN file based on the left and right square brackets.
14. The data parsing apparatus according to claim 13, characterized in that, The trie construction module is used for: Maintain a left square bracket stack, a right square bracket stack, and a stack containing the total number of square brackets; Scan all characters in the PGN file sequentially; If the left square bracket is encountered during scanning, a flag is pushed onto the stack of total square bracket counts. If the right square bracket is encountered during scanning, a flag is popped from the stack of total square bracket counts. If the total number of square brackets stack is empty when the right square bracket is scanned, or if the total number of square brackets stack is not empty when the scan is completed, then the PGN file is determined to be invalid.
15. The data parsing apparatus according to claim 13, characterized in that, The trie construction module is used for: Search the PGN file for each left square bracket and each right square bracket that matches each left square bracket; The characters between the left and right square brackets of each pair of matching characters are extracted to obtain the PGN header information.
16. The data parsing apparatus according to claim 13, characterized in that, The trie construction module is used for: Determine whether the left square bracket exists in the PGN file; If it exists, extract characters starting from the character after the last right square bracket in the PGN header information until all characters have been extracted to obtain the PGN token data; If it does not exist, then all characters in the PGN file are determined to be the PGN token data.
17. The data parsing apparatus according to claim 11, characterized in that, When the data file is a PGN file, the target data element is the specified move position information; The data link determination module is used for: Obtain the tree node information carried by the tree node corresponding to the target data element; The trajectory of historical child nodes is determined based on the historical string information carried by the tree nodes; Based on the trie, determine all child nodes of the tree node corresponding to the target data element; The data chain is formed based on the historical child node trajectory, and / or based on all child nodes of the tree node corresponding to the target data element.
18. The data parsing apparatus according to claim 17, characterized in that, The branch data generation module is used for: Create a tree node on the data chain to obtain an updated tree node; Record the information carried by the tree node in the updated tree node, and add the updated tree node to the child node array of the parent node of the updated tree node.
19. An electronic device, characterized in that, include: processor; as well as A memory having executable code stored thereon, which, when executed by the processor, causes the processor to perform the method as described in any one of claims 1-9.
20. A non-transitory machine-readable storage medium having executable code stored thereon, which, when executed by a processor of an electronic device, causes the processor to perform the method as described in any one of claims 1-9.