A text comparison method, device and equipment based on a diff-match-patch algorithm and a medium
By using the diff-match-patch algorithm for file comparison and leveraging virtual lists and buffer regions, the problem of low efficiency in comparing large files is solved, achieving efficient text comparison and rendering.
Patent Information
- Application Number
- CN202311380637.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-10-23
- Publication Date
- 2025-11-25
- Estimated Expiration
- 2043-10-23
AI Technical Summary
Existing file comparison methods are inefficient for large files, causing page lag or even crashes, especially when using the code-diff component in the React or Vue frameworks.
The diff-match-patch algorithm is adopted. By initializing the file line number markers, a virtual list is generated. During each diff operation, the text content is used as the visible area for local comparison, generating a buffer area for rendering.
It improves the efficiency of comparing large text files, avoids page lag and crashes, and enhances rendering efficiency.
Smart Images

Figure CN117290317B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, specifically to a text comparison method, apparatus, device, and medium based on the diff-match-patch algorithm. Background Technology
[0002] In front-end components, it's often necessary to compare old and new files to identify specific changes and then convert the old file into the new one through a series of operations. Currently, most common file comparison methods use the `code-diff` component in the React framework or the `vue-code-diff` component in the Vue framework. However, these components integrate a significant amount of functionality, leading to repetitive traversal. Furthermore, when comparing text, they compare all files at once, which can result in inefficient comparisons for large files, causing page lag or even crashes. Summary of the Invention
[0003] To address the aforementioned issues, this application proposes a text comparison method based on the diff-match-patch algorithm, comprising:
[0004] The original file and the current file to be compared are loaded into a preset visualization interface, and the line number markers in the original file and the current file are initialized.
[0005] Starting from the initial line number, a diff operation is performed on the original file and the current file, and a virtual list corresponding to the diff operation is generated so that the text content in the original file and the current file that matches the length of the virtual list is used as the visible area during each diff operation.
[0006] The original file and the current file within the visible area are compared. If the original file and the current file are inconsistent, the target areas that are inconsistent in the original file and the current file are determined respectively, and the target areas are rendered.
[0007] In one implementation of this application, after initializing the line number markers in the original file and the current file, the method further includes:
[0008] Determine the initial line number marker and the termination line number marker in the original file and the current file respectively; and determine the number of lines in the original file and the number of lines in the current file respectively based on the initial line number marker and the termination marker.
[0009] Based on the number of lines in the original file and the number of lines in the current file, the lengths of the first and second regions corresponding to the diff operation in the original file and the current file, respectively, are determined. By using the first and second region lengths, the original file and the current file can complete the comparison of all file contents with the same number of diff operations.
[0010] In one implementation of this application, starting from the initial line number marker, a diff operation is performed on the original file and the current file, specifically including:
[0011] Starting from the initial line number, perform a diff operation on the original file and the current file respectively, so that in each diff operation, slide down the file content corresponding to the length of the first region and the length of the second region respectively, until there is no file content after the visible region.
[0012] In one implementation of this application, a virtual list corresponding to the diff operation is generated, so that during each diff operation, the text content in the original file and the current file that matches the length of the virtual list is used as a visible area, specifically including:
[0013] Generate a corresponding virtual list based on the line number markers covered by the text region corresponding to the current diff operation;
[0014] During each diff operation, the text content in the original file and the current file that matches the length of the virtual list is taken as the visible area, and it is determined whether there is file content before and after the visible area;
[0015] If so, a corresponding buffer region is generated, and the buffer region is made invisible.
[0016] In one implementation of this application, generating a corresponding buffer area specifically includes:
[0017] The text area containing the text content preceding the visible area in the visualization page is designated as the upper buffer area;
[0018] The text area containing the text content located after the visible area on the visualization page is designated as the lower buffer area.
[0019] In one implementation of this application, the line number markers in the original file and the current file are initialized, specifically including:
[0020] Determine the text height and preset line height corresponding to the original file and the current file, respectively;
[0021] Based on the text height and the line height, determine the number of lines in the original file and the number of lines in the current file that can be accommodated, respectively.
[0022] The line number markers in the original file and the current file are initialized based on the number of lines in the original file and the number of lines in the current file, respectively.
[0023] In one implementation of this application, the length of the visible area is greater than the line height but less than the text height.
[0024] This application provides a text comparison device based on the diff-match-patch algorithm, the device comprising:
[0025] An initialization unit is used to load the original file and the current file to be compared into a preset visualization interface, and to initialize the line number markers in the original file and the current file.
[0026] The diff operation unit is used to perform a diff operation on the original file and the current file starting from the initial line number mark, and generate a virtual list corresponding to the diff operation, so that the text content in the original file and the current file that matches the length of the virtual list is used as the visible area during each diff operation.
[0027] The rendering unit is used to compare the text of the original file and the current file within the visible area, and if the original file and the current file are inconsistent, to determine the target areas that are inconsistent between the original file and the current file, and to render the target areas.
[0028] This application provides a text comparison device based on the diff-match-patch algorithm, comprising:
[0029] At least one processor; and,
[0030] A memory communicatively connected to the at least one processor; wherein,
[0031] The memory stores instructions executable by the at least one processor, which, when executed by the at least one processor, enable the at least one processor to:
[0032] The original file and the current file to be compared are loaded into a preset visualization interface, and the line number markers in the original file and the current file are initialized.
[0033] Starting from the initial line number, a diff operation is performed on the original file and the current file, and a virtual list corresponding to the diff operation is generated so that the text content in the original file and the current file that matches the length of the virtual list is used as the visible area during each diff operation.
[0034] The original file and the current file within the visible area are compared. If the original file and the current file are inconsistent, the target areas that are inconsistent in the original file and the current file are determined respectively, and the target areas are rendered.
[0035] This application provides a non-volatile computer storage medium storing computer-executable instructions, characterized in that the computer-executable instructions are configured as follows:
[0036] The original file and the current file to be compared are loaded into a preset visualization interface, and the line number markers in the original file and the current file are initialized.
[0037] Starting from the initial line number, a diff operation is performed on the original file and the current file, and a virtual list corresponding to the diff operation is generated so that the text content in the original file and the current file that matches the length of the virtual list is used as the visible area during each diff operation.
[0038] The original file and the current file within the visible area are compared. If the original file and the current file are inconsistent, the target areas that are inconsistent in the original file and the current file are determined respectively, and the target areas are rendered.
[0039] The text comparison method based on the diff-match-patch algorithm proposed in this application can bring the following benefits:
[0040] When the text is large, the concept of a virtual list is used for rendering. That is, only the visible area is diffed, and the temporarily invisible parts are not diffed. Compared with diffing all files at once, local diffing and rendering improve the efficiency of text comparison and avoid the page crash problem caused by comparing large files at once. Attached Figure Description
[0041] The accompanying drawings, which are included to provide a further understanding of this application and form part of this application, illustrate exemplary embodiments and are used to explain this application, but do not constitute an undue limitation of this application. In the drawings:
[0042] Figure 1 A flowchart illustrating a text comparison method based on the diff-match-patch algorithm provided in this application embodiment;
[0043] Figure 2 A schematic diagram of the visible area at the start position of a file provided in an embodiment of this application;
[0044] Figure 3 A schematic diagram of the visible area at a non-starting position of a document provided in an embodiment of this application;
[0045] Figure 4 A schematic diagram of the structure of a text comparison device based on the diff-match-patch algorithm provided in an embodiment of this application;
[0046] Figure 5 This is a schematic diagram of the structure of a text comparison device based on the diff-match-patch algorithm provided in an embodiment of this application. Detailed Implementation
[0047] To make the objectives, technical solutions, and advantages of this application clearer, the technical solutions of this application will be clearly and completely described below in conjunction with specific embodiments and corresponding drawings. Obviously, the described embodiments are only a part of the embodiments of this application, and not all of them. Based on the embodiments in this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0048] The technical solutions provided by the various embodiments of this application are described in detail below with reference to the accompanying drawings.
[0049] like Figure 1 As shown in the embodiment of this application, a text comparison method based on the diff-match-patch algorithm is provided, comprising:
[0050] S101: Load the original file and the current file to be compared into the preset visualization interface, and initialize the line number markers in the original file and the current file.
[0051] Before comparing text content in the front-end component, the server first needs to load the original file and the current file to be compared into a preset visualization interface and initialize the line number markers in the original file and the current file. Initialization is essentially the process of determining the file line numbers, which requires determining the text height and preset line height for the original file and the current file, respectively. Based on the text height and line height, the number of lines in the original file and the current file that can be accommodated are determined, respectively. Then, based on the number of lines in the original file and the current file, the line number markers in the original file and the current file are initialized, respectively.
[0052] S102: Starting from the initial line number mark, perform a diff operation on the original file and the current file, and generate a virtual list corresponding to the diff operation, so that the text content in the original file and the current file that matches the length of the virtual list is used as the visible area during each diff operation.
[0053] After initializing the original and current files, the initial and final line number markers can be determined. The diff-match-patch algorithm is used to compare and match text. It effectively identifies differences between two text segments. Its core idea is to divide the text into small segments, then compare and match these segments. By comparing the differences between the original and current files, a series of operations are generated to transform the original file into the current file. The diff operation is used to match differences. Starting from the initial line number markers, the diff operation is performed on the original and current files, generating a virtual list corresponding to the diff operation. During each diff operation, the text content in the original and current files that matches the length of the virtual list is used as the visible area.
[0054] Since traditional text comparison components perform diffing on all file contents at once, resulting in a time complexity of O(n) traversal, this application embodiment adopts a partitioned diffing method when performing the diffing operation, which can effectively reduce computing resources and improve the comparison efficiency of large text files.
[0055] Before performing a diff operation, it's crucial to define the coverage area of the text region for each diff operation. This means determining the initial and final line number markers in both the original and current files. Based on these markers, the number of lines in the original and current files are then determined. Finally, the lengths of the first and second regions in each file are determined to ensure that the original and current files can be compared using the same number of diff operations. For example, if the original file has 100 lines and the current file has 150 lines, the first and second region lengths can be set to 20 and 30 lines respectively. This way, five diff operations on each file will be sufficient to compare all file content.
[0056] During the diff operation, the server starts from the initial line number and performs diff operations on both the original file and the current file. This means that in each diff operation, the file content corresponding to the lengths of the first and second regions is scrolled down by the corresponding number of lines. If text content still exists beyond the visible area after a diff operation, the scrolling continues until no more text content remains beyond the visible area.
[0057] The server generates a virtual list based on the line number markers covered by the text region corresponding to the current diff operation. During each diff operation, the text content in both the original and current files that matches the length of the virtual list is designated as the visible region. Only text content within this visible region is compared; text content before and after the visible region is not included in the comparison. Therefore, after determining the visible region, it's necessary to check if there is any file content before or after it. If so, a corresponding buffer region needs to be generated and made invisible.
[0058] The buffer area is divided into an upper buffer area and a lower buffer area based on the order of text content within the visible area. The text area containing text content preceding the visible area is the upper buffer area, and the text area containing text content following the visible area is the lower buffer area. For example... Figure 2 and Figure 3 As shown in the diagram of the visible area, for the file content at the starting position, there is only a lower buffer area. For the file content at a non-starting position, the visible area is divided into an upper buffer area and a lower buffer area. The text content within the buffer area is not visible. This ensures the locality of the diff operation, ensures the efficiency of text comparison, and avoids page lag and crashes.
[0059] It should be noted that, in order to ensure the efficiency of text comparison, the length of the visible area should be greater than the line height. On the other hand, in order to prevent page lag during text comparison, the length of the visible area should be less than the text height.
[0060] S103: Compare the text of the original file and the current file within the visible area. If the original file and the current file are inconsistent, identify the target areas that are inconsistent in the original file and the current file respectively, and render the target areas.
[0061] After determining the current visible area through the diff operation, the server directly compares the original file and the current file within the visible area during the diff process. If the original and current files are inconsistent, the server identifies the discrepancies between the two files and renders these discrepancies. By rendering these target areas, inconsistent text content can be highlighted. This splits the entire file into several visible areas for local rendering, significantly improving rendering efficiency compared to global rendering. Furthermore, subsequent text conversion only requires processing the text within the target areas, thus increasing the speed of text conversion.
[0062] The above are embodiments of the methods proposed in this application. Based on the same idea, some embodiments of this application also provide apparatus, devices, and non-volatile computer storage media corresponding to the above methods.
[0063] Figure 4 This is a schematic diagram of the structure of a text comparison device based on the diff-match-patch algorithm provided in an embodiment of this application. Figure 4 As shown, the device includes:
[0064] The initialization unit is used to load the original file and the current file to be compared into a preset visualization interface, and to initialize the line number markers in the original file and the current file.
[0065] The diff operation unit is used to perform diff operations on the original file and the current file starting from the initial line number mark, and generate a virtual list corresponding to the diff operation, so that the text content in the original file and the current file that matches the length of the virtual list is used as the visible area during each diff operation.
[0066] The rendering unit is used to compare the text of the original file and the current file within the visible area. If the original file and the current file are inconsistent, it determines the target areas that are inconsistent between the original file and the current file, and renders the target areas.
[0067] Figure 5 This is a schematic diagram of a text comparison device structure based on the diff-match-patch algorithm provided in an embodiment of this application. Figure 5 As shown, it includes:
[0068] At least one processor; and,
[0069] At least one processor-communication-connected memory; wherein,
[0070] The memory stores instructions that can be executed by at least one processor, and the instructions, when executed by at least one processor, enable at least one processor to:
[0071] Load the original file and the current file to be compared into the preset visualization interface, and initialize the line number markers in the original file and the current file;
[0072] Starting from the initial line number, perform a diff operation on the original file and the current file, and generate a virtual list corresponding to the diff operation. In each diff operation, the text content in the original file and the current file that matches the length of the virtual list is used as the visible area.
[0073] The original file and the current file within the visible area are compared. If the original file and the current file are inconsistent, the target areas that are inconsistent in the original file and the current file are identified and rendered.
[0074] This application provides a non-volatile computer storage medium storing computer-executable instructions, which are configured as follows:
[0075] Load the original file and the current file to be compared into the preset visualization interface, and initialize the line number markers in the original file and the current file;
[0076] Starting from the initial line number, perform a diff operation on the original file and the current file, and generate a virtual list corresponding to the diff operation. In each diff operation, the text content in the original file and the current file that matches the length of the virtual list is used as the visible area.
[0077] The original file and the current file within the visible area are compared. If the original file and the current file are inconsistent, the target areas that are inconsistent in the original file and the current file are identified and rendered.
[0078] The various embodiments in this application are described in a progressive manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the device and medium embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the description of the method embodiments.
[0079] The devices and media provided in this application are one-to-one with the methods. Therefore, the devices and media also have similar beneficial technical effects as their corresponding methods. Since the beneficial technical effects of the methods have been described in detail above, the beneficial technical effects of the devices and media will not be repeated here.
[0080] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0081] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations 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, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart... Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0082] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0083] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0084] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0085] Memory may include non-persistent storage in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM. Memory is an example of computer-readable media.
[0086] Computer-readable media includes both permanent and non-permanent, removable and non-removable media that can store information using any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic magnetic disk storage or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined herein, computer-readable media does not include transient computer-readable media, such as modulated data signals and carrier waves.
[0087] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0088] The above description is merely an embodiment of this application and is not intended to limit the scope of this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of the claims of this application.
Claims
1. A text comparison method based on the diff-match-patch algorithm, characterized in that, The method includes: The original file and the current file to be compared are loaded into a preset visualization interface, and the line number markers in the original file and the current file are initialized. Starting from the initial line number, a diff operation is performed on the original file and the current file, and a virtual list corresponding to the diff operation is generated so that the text content in the original file and the current file that matches the length of the virtual list is used as the visible area during each diff operation. The original file and the current file within the visible area are compared. If the original file and the current file are inconsistent, the target areas that are inconsistent in the original file and the current file are determined respectively, and the target areas are rendered.
2. The text comparison method based on the diff-match-patch algorithm according to claim 1, characterized in that, After initializing the line number markers in the original file and the current file, the method further includes: Determine the initial line number marker and the terminating line number marker in the original file and the current file, and determine the number of lines in the original file and the number of lines in the current file respectively based on the initial line number marker and the terminating line number marker; Based on the number of lines in the original file and the number of lines in the current file, the lengths of the first and second regions corresponding to the diff operation in the original file and the current file, respectively, are determined. By using the first and second region lengths, the original file and the current file can complete the comparison of all file contents with the same number of diff operations.
3. The text comparison method based on the diff-match-patch algorithm according to claim 2, characterized in that, Starting from the initial line number, a diff operation is performed on the original file and the current file, specifically including: Starting from the initial line number, perform a diff operation on the original file and the current file respectively, so that in each diff operation, slide down the file content corresponding to the length of the first region and the length of the second region respectively, until there is no file content after the visible region.
4. The text comparison method based on the diff-match-patch algorithm according to claim 1, characterized in that, Generate a virtual list corresponding to the diff operation, so that during each diff operation, the text content in the original file and the current file that matches the length of the virtual list is used as the visible area, specifically including: Generate a corresponding virtual list based on the line number markers covered by the text region corresponding to the current diff operation; During each diff operation, the text content in the original file and the current file that matches the length of the virtual list is taken as the visible area, and it is determined whether there is file content before and after the visible area; If so, a corresponding buffer region is generated, and the buffer region is made invisible.
5. A text comparison method based on the diff-match-patch algorithm according to claim 1, characterized in that, The corresponding buffer area is generated, specifically including: The text area containing the text content located before the visible area in the visualization interface is designated as the upper buffer area; The text area containing the text content located after the visible area in the visualization interface is used as the lower buffer area.
6. A text comparison method based on the diff-match-patch algorithm according to claim 1, characterized in that, Initialize the line number markers in the original file and the current file, specifically including: Determine the text height and preset line height corresponding to the original file and the current file, respectively; Based on the text height and the line height, determine the number of lines in the original file and the number of lines in the current file that can be accommodated, respectively. The line number markers in the original file and the current file are initialized based on the number of lines in the original file and the number of lines in the current file, respectively.
7. A text comparison method based on the diff-match-patch algorithm according to claim 6, characterized in that, The length of the visible area is greater than the line height but less than the text height.
8. A text comparison device based on the diff-match-patch algorithm, characterized in that, The device includes: An initialization unit is used to load the original file and the current file to be compared into a preset visualization interface, and to initialize the line number markers in the original file and the current file. The diff operation unit is used to perform a diff operation on the original file and the current file starting from the initial line number mark, and generate a virtual list corresponding to the diff operation, so that the text content in the original file and the current file that matches the length of the virtual list is used as the visible area during each diff operation. The rendering unit is used to compare the text of the original file and the current file within the visible area, and if the original file and the current file are inconsistent, to determine the target areas that are inconsistent between the original file and the current file, and to render the target areas.
9. A text comparison device based on the diff-match-patch algorithm, characterized in that, include: At least one processor; as well as, A memory communicatively connected to the at least one processor; wherein, The memory stores instructions executable by the at least one processor, which, when executed by the at least one processor, enable the at least one processor to: The original file and the current file to be compared are loaded into a preset visualization interface, and the line number markers in the original file and the current file are initialized. Starting from the initial line number, a diff operation is performed on the original file and the current file, and a virtual list corresponding to the diff operation is generated so that the text content in the original file and the current file that matches the length of the virtual list is used as the visible area during each diff operation. The original file and the current file within the visible area are compared. If the original file and the current file are inconsistent, the target areas that are inconsistent in the original file and the current file are determined respectively, and the target areas are rendered.
10. A non-volatile computer storage medium storing computer-executable instructions, characterized in that, The computer-executable instructions are set as follows: The original file and the current file to be compared are loaded into a preset visualization interface, and the line number markers in the original file and the current file are initialized. Starting from the initial line number, a diff operation is performed on the original file and the current file, and a virtual list corresponding to the diff operation is generated so that the text content in the original file and the current file that matches the length of the virtual list is used as the visible area during each diff operation. The original file and the current file within the visible area are compared. If the original file and the current file are inconsistent, the target areas that are inconsistent in the original file and the current file are determined respectively, and the target areas are rendered.
Citation Information
Patent Citations
File difference comparison method, storage medium, electronic equipment and system
CN107463541A
Super-large text file comparison method
CN109783454A