Document comparison method and device based on element matching and multi-level comparison and storage medium

CN118569239APending Publication Date: 2024-08-30DATAGRAND TECH INC
0 Cites 0 Cited by

Patent Information

Application Number
CN202410448195.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-04-15
Publication Date
2024-08-30

AI Technical Summary

Technical Problem

Existing document comparison algorithms take a long time to compare long documents, and the accuracy decreases due to incorrect matching caused by repeated or similar fragments, making it impossible to achieve globally optimal matching results.

Method used

Using a method based on element matching and multi-level comparison, through the two-level comparison strategy of Myers and LCS algorithms, combined with jaccard similarity, text similarity and text similarity matching rules with context, the matching stringency is gradually improved and parallelized Calculation to reduce time overhead.

Benefits of technology

It significantly improves the accuracy and efficiency of document comparison, shortens the comparison time, and realizes a top-down, first coarse and then fine comparison mode.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118569239A
    Figure CN118569239A
Patent Text Reader

Abstract

The invention relates to a document comparison method and device based on element matching and multi-level comparison and a storage medium, and the method comprises the steps: 1, carrying out the preprocessing of a source document of a PDF and a target document of a PFD, and converting the source document and the target document into structural data; the structured data comprises elements and characters contained in the elements; 2, fusing a multi-level element matching rule to perform element matching of the source document and the target document, wherein the strict degree of the element matching rule is increased level by level; step 3, based on the matched elements, performing two-stage comparison in sequence, the first-stage comparison being Myers comparison and the second-stage comparison being LCS comparison to obtain difference points between the matched elements of the target document and the source document; and step 4, summarizing the difference points, calculating and generating position information of the difference points, and outputting a comparison result. By fusing element matching of multiple similarities, the matching elements can be accurately detected, and the comparison accuracy is improved; based on two-stage comparison of the Myers algorithm and the LCS algorithm, top-to-bottom comparison is realized, and comparison time consumption is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of electronic document processing, and in particular to a document comparison method, device and storage medium based on element matching and multi-level comparison. Background Art

[0002] With the rapid development of the digital age, electronic documents are widely used. Document comparison is a common task in document processing. Existing text comparison methods include the maximum common subsequence algorithm, the maximum common substring algorithm, and the Myers difference algorithm.

[0003] The main difficulty of document comparison is the comparison of long documents, which usually have more than 500 pages. When comparing long documents, the existing text comparison algorithms have the problem of taking a long time to input text. For example, for a 500-page PDF document, the comparison time may reach more than half an hour. Moreover, as the input content grows, long documents contain more repeated or similar segments, which will lead to incorrect matching of common substrings, resulting in a significant decrease in the accuracy of the comparison results, and it is impossible to achieve the global optimal matching result. Summary of the invention

[0004] In order to solve the problems of low efficiency and low accuracy in long document comparison in the prior art, the present invention provides a document comparison method, device and storage medium based on element matching and multi-level comparison, which integrates multiple similarity document element matching to improve accuracy and comparison rate, and then combines Myers and LCS two-level comparison strategies to shorten the comparison time.

[0005] The technical objectives of the present invention are achieved through the following technical solutions:

[0006] A document rapid comparison method based on document element matching and multi-level comparison strategy, the method comprising the following steps:

[0007] Step 1: pre-process the source document of PDF and the target document of PDF, and convert the source document and the target document into structured data respectively; the structured data includes elements and characters contained in the elements;

[0008] Step 2: Multi-level element matching rules are integrated to match the elements of the source document and the target document, and the strictness of the element matching rules increases step by step;

[0009] Step 3: Based on the matched elements, two-level comparisons are performed in sequence. The first-level comparison is Myers comparison, and the second-level comparison is LCS comparison, to obtain the difference points between the matching elements of the target document and the source document.

[0010] Step 4: Summarize the difference points, calculate and generate the location information of the difference points, and output the comparison results.

[0011] Furthermore, the matching rules include jaccard similarity matching, text similarity matching and text similarity matching with context, and the matching strictness of jaccard similarity matching, text similarity matching and text similarity matching with context increases in sequence.

[0012] Furthermore, when performing element matching, element matching is performed in order of the strictness of the matching rules from low to high; after traversing and matching all elements according to the same matching rule, element similarity matching is performed for unmatched elements using the next level element matching rule.

[0013] Furthermore, when performing element matching according to each level of matching rules, the elements of the source document are matched with the elements of the target document respectively for similarity, and the two elements in the target document with the greatest similarity to the elements of the source document are obtained, which are respectively denoted as j1 and j2 according to the similarity, the similarity of j1 is sj1, the similarity of j2 is sj2, and sj1>sj2;

[0014] If sj1-sj2>set threshold k, the element j1 matches the element in the source document with high confidence.

[0015] Furthermore, when sj1-sj2>set threshold k, element j1 is matched with all elements in the source document for similarity, and the two elements in the source document with the greatest similarity to element j1 are calculated and recorded as i1 and i2 respectively, the similarity between element i1 and element j1 is recorded as si1, and the similarity between element i2 and element j1 is recorded as si2, si1>si2;

[0016] If si1-si2>set threshold k, the matching credibility of element j1 and element i1 is high, and element j1 matches element i1;

[0017] After removing the matched elements from the source document and the target document, the matching of the elements in the source document and the elements in the target document is repeated until the element matching under the matching rules of the current level is completed.

[0018] Furthermore, in step 3, the differences between the element pairs matched in the source document and the target document in step 2 are calculated, and the differences between the matching element pairs are calculated in parallel, which significantly reduces the time overhead during comparison.

[0019] Furthermore, when performing Myers alignment, element pairs are split into sequence pairs according to punctuation marks, and the sequence pairs are input into the Myers algorithm to obtain sentence-level difference points; the difference points include adding type difference points, deleting type difference points, and replacing type difference points; adding type difference points and deleting type difference points are used as the difference point results of the first-level comparison, and replacing type difference points are used for the second-level comparison.

[0020] Furthermore, when the replacement type difference points are compared at the second level, the replacement type difference points are input into the LCS algorithm to obtain a character-level difference result.

[0021] The present invention also provides a document rapid comparison device based on document element matching and multi-level comparison strategy, which is characterized by comprising:

[0022] The document structure parsing module is used to call the parsing tool to convert the PDF source document and the PDF target document into structured data;

[0023] Document element matching module, which is used to match elements between source documents and target documents based on multi-level element matching rules;

[0024] A text comparison module, including a first-level text comparison module and a second-level text comparison module. The first-level text comparison module obtains sentence-level difference points of matching elements based on the Myers algorithm, and the second-level text comparison module obtains character-level difference points based on the LCS algorithm.

[0025] The difference information output module is used to generate the location information of the difference point and output the difference information.

[0026] The present invention also provides a storage medium, including a storage unit, wherein the storage unit stores computer software, and the computer software is executed according to a document rapid comparison method based on document element matching and multi-level comparison strategy.

[0027] Compared with the prior art, the present invention has the following beneficial effects:

[0028] 1. The present invention matches the elements of the source document and the target document through multi-level element matching rules, which can detect the matching elements more accurately and improve the accuracy of the comparison.

[0029] 2. Based on the two-level comparison of Myers algorithm and LCS algorithm, the matching elements are compared for differences. First, the sentence granularity is compared through Myers algorithm, and then the character granularity is compared through LCS, realizing a top-down, coarse-first-fine-later comparison mode, which significantly reduces the time consumption during comparison. BRIEF DESCRIPTION OF THE DRAWINGS

[0030] Figure 1 It is a flow chart of the document rapid comparison method based on document element matching and multi-level comparison strategy of the present invention. DETAILED DESCRIPTION

[0031] The technical solution of the present invention is further described below in conjunction with specific embodiments:

[0032] A fast document comparison method based on document element matching and multi-level comparison strategy, such as Figure 1As shown, the method comprises the following steps:

[0033] Step 1: Preprocess the source PDF document and the target PDF document, and convert the source document and the target document into structured data respectively through document parsing tools (such as SolidFramework) or open source code libraries. Structured data includes elements and the characters contained in the elements. The elements mainly include paragraphs, tables, titles, headers, footers, etc. The characters mainly contain attributes such as character content, index values, and position coordinate information. After the structured data conversion is completed, the elements that do not need to be compared, such as headers and footers, can be filtered out.

[0034] Step 2: Multi-level element matching rules are integrated to match the elements of the source document and the target document, and the strictness of the element matching rules increases step by step;

[0035] Jaccard similarity, also known as Jaccard similarity coefficient, is a similarity similar to a character set. It does not consider the order of characters, has a fast calculation speed, and is a relatively rough similarity.

[0036] Text similarity is the ratio of the total length of all common substrings of two texts to the sum of the lengths of the two texts obtained based on the LCS (largest common substring) algorithm. It can be used to compare the similarity between real reflection texts. Due to the expansion of content, the calculation time is longer than the jaccard similarity calculation.

[0037] Text similarity with context refers to the text similarity calculated after an element is combined with the text of a certain length before and after it. The calculation time is longer than the text similarity calculation.

[0038] When performing element matching, element matching is performed in the order of the strictness of the matching rules from low to high; after traversing and matching all elements according to the same matching rule, the next level element matching rule is used to perform element similarity matching for unmatched elements.

[0039] In this embodiment, the multi-level element matching can be two-level or three-level, and the two-level can be jaccard similarity matching and text similarity matching, text similarity matching and text similarity matching with context, or jaccard similarity matching and text similarity matching with context.

[0040] Preferably, the present embodiment includes three levels of matching, namely, jaccard similarity matching, text similarity matching and text similarity matching with context, and the matching strictness of jaccard similarity matching, text similarity matching and text similarity matching with context increases in sequence.

[0041] When performing element matching according to each level of matching rules, each element i of the source document is matched with each element j of the target document for similarity. The two elements in the target document with the greatest similarity to the elements in the source document and their similarities are (j1, sj1) and (j2, sj2), respectively, sj1>sj2;

[0042] If sj1-sj2> the set threshold k (k is 0.1), the matching credibility of element j1 and element i in the source document is high, and the most realistic element of element j1 in the source document can be further searched:

[0043] When sj1-sj2>set threshold k, element j1 is matched with all elements i in the source document for similarity, and the two elements with the greatest similarity to element j1 in the source document and their similarities are calculated to be (i1, si1) and (i2, si2), si1>si2;

[0044] If si1-si2>set threshold k, where the threshold k is consistent with the previously set threshold k, then the matching reliability of element j1 and element i1 is higher, and the matching of element j1 and element i1 is recorded as (i1, j1); if i1==i, it means that i and j1 are reliable matches in each other's document elements.

[0045] The matched elements i1 and j1 are removed from the elements of the source document and the target document respectively. The remaining elements i in the source document are traversed again and matched with the remaining elements j in the target document according to the above method. The source document is traversed in turn to match the elements and finally a list of matched element pairs [(i1, j1), (i2.j2), (i3, j3)…] is obtained.

[0046] After removing the matched elements from the source document and the target document, repeat the matching of the elements in the source document and the elements in the target document until the element matching under the current level matching rules is completed. The unmatched elements are matched according to the next level matching rules. If the elements are still not matched after all levels of matching rules are completed, the corresponding difference points can be directly constructed. For example, after all levels of matching rules are completed, there are unmatched elements i5 and i8 in the source document, and there are unmatched elements j4 and j7 in the target document. The corresponding difference points are i5 deleted, i8 deleted, j4 added, and j7 added.

[0047] Step 3: Based on the matched elements, two-level comparisons are performed in sequence. The first-level comparison is Myers comparison, and the second-level comparison is LCS comparison, to obtain the difference points between the matching elements of the target document and the source document.

[0048] For the element pairs matched in step 2 [(i1, j1), (i2.j2), (i3, j3)…], the difference points of each element pair are calculated respectively. Since the difference calculations of each element pair are independent of each other, the difference calculations of each matching element pair are calculated in parallel to reduce time overhead.

[0049] First, Myers alignment is performed. Myers alignment uses the Myers difference algorithm, which is an algorithm for calculating the difference between two sequences. It can achieve a time complexity of O(ND), where N is the sum of the lengths of the two input sequences and D is the total number of differences. For element pairs, the element pair (i, j) is split into sequence pairs ([i1, i2, ...in], [j1, j2 ...jm]) according to punctuation marks such as commas and periods, and the sequence pairs are input into the Myers algorithm to obtain sentence-level difference points; the difference points include adding type difference points, deleting type difference points, and replacing type difference points; adding type difference points and deleting type difference points are used as the difference point results of the first-level comparison, and replacing type difference points are used for the second-level comparison.

[0050] The LCS algorithm is the maximum common substring algorithm. It searches for all common substrings of two sequences in an iterative manner, and finally the part other than the common substring is their difference. The average time complexity of the LCS algorithm is O(n2), where n is the maximum length of the two input sequences. The substitution type difference points obtained by the first-level comparison are input into the LCS algorithm to obtain the character-level difference results.

[0051] Step 4: Summarize all the difference points obtained in the above steps, including the difference points constructed in step 2 and the difference points calculated in step 3, calculate the location information of the difference points, and output the document difference results. When outputting, the difference results of the comparison are output to the front end, and the front end renders and highlights the document difference points through the comparison results. The format of the output difference points is as follows: {'type':'replace','source_text':'…','source_start':2,'source_end':12,'target_text':'…','target_start':6,'target_end':15}.

[0052] This embodiment also provides a document rapid comparison device based on document element matching and multi-level comparison strategy, including:

[0053] The document structure parsing module is used to call the parsing tool to convert the PDF source document and the PDF target document into structured data;

[0054] Document element matching module, which is used to match elements between source documents and target documents based on multi-level element matching rules;

[0055] A text comparison module, including a first-level text comparison module and a second-level text comparison module. The first-level text comparison module obtains sentence-level difference points of matching elements based on the Myers algorithm, and the second-level text comparison module obtains character-level difference points based on the LCS algorithm.

[0056] The difference information output module is used to generate the location information of the difference point and output the difference information.

[0057] This embodiment also provides a storage medium, which includes a storage unit. The storage medium can be a computer, a mobile phone, a tablet computer, a mobile hard disk, a mobile terminal, a vehicle-mounted computer device, a network storage space, etc. The storage unit stores computer software, and the computer software is executed according to the above-mentioned document rapid comparison method based on document element matching and multi-level comparison strategy.

[0058] This embodiment is only a further explanation of the present invention, not a limitation of the present invention. After reading this specification, those skilled in the art may make non-creative modifications to this embodiment as needed, but as long as it is within the scope of the claims of the present invention, it will be protected by the patent law.

Claims

1. A fast document comparison method based on document element matching and multi-level comparison strategy, It is characterized in that The method comprises the following steps: Step 1: Preprocess the source document of PDF and the target document of PFD, and convert the source document and the target document into structured data respectively; the structured data includes elements and characters contained in the elements; Step 2: Multi-level element matching rules are integrated to match the elements of the source document and the target document, and the strictness of the element matching rules increases step by step; Step 3: Based on the matched elements, two-level comparisons are performed in sequence. The first-level comparison is Myers comparison, and the second-level comparison is LCS comparison, to obtain the difference points between the matching elements of the target document and the source document. Step 4: Summarize the difference points, calculate and generate the location information of the difference points, and output the comparison results.

2. A method for rapid document comparison based on document element matching and multi-level comparison strategy according to claim 1, It is characterized in that The matching rules include jaccard similarity matching, text similarity matching and text similarity matching with context, and the matching strictness of jaccard similarity matching, text similarity matching and text similarity matching with context increases in sequence.

3. A method for rapid document comparison based on document element matching and multi-level comparison strategy according to claim 2, It is characterized in that When performing element matching, element matching is performed in the order of the strictness of the matching rules from low to high; after traversing and matching all elements according to the same matching rule, the next level element matching rule is used to perform element similarity matching for unmatched elements.

4. According to claim 3, a method for rapid document comparison based on document element matching and multi-level comparison strategy, It is characterized in that When matching elements according to each level of matching rules, the elements of the source document are matched with the elements of the target document respectively to obtain the two elements in the target document with the greatest similarity to the elements of the source document. They are denoted as j1 and j2 according to the similarity. The similarity of j1 is sj1, and the similarity of j2 is sj2, and sj1>sj2; If sj1-sj2>set threshold k, the element j1 matches the element in the source document with high confidence.

5. A method for rapid document comparison based on document element matching and multi-level comparison strategy according to claim 4, It is characterized in that When sj1-sj2>set threshold k, element j1 is matched with all elements in the source document for similarity, and the two elements with the greatest similarity to element j1 in the source document are calculated and recorded as i1 and i2 respectively, the similarity between element i1 and element j1 is recorded as si1, and the similarity between element i2 and element j1 is recorded as si2, si1>si2; If si1-si2>set threshold k, the matching credibility of element j1 and element i1 is high, and element j1 matches element i1; After removing the matched elements from the source document and the target document, the matching of the elements in the source document and the elements in the target document is repeated until the element matching under the matching rules of the current level is completed.

6. A method for rapid document comparison based on document element matching and multi-level comparison strategy according to claim 5, It is characterized in that In step 3, the differences between the element pairs matched in the source document and the target document in step 2 are calculated, and the differences between the matching element pairs are calculated in parallel.

7. A method for rapid document comparison based on document element matching and multi-level comparison strategy according to claim 6, It is characterized in that When performing Myers alignment, element pairs are split into sequence pairs according to punctuation marks, and the sequence pairs are input into the Myers algorithm to obtain sentence-level difference points; the difference points include adding type difference points, deleting type difference points, and replacing type difference points; adding type difference points and deleting type difference points are used as the difference point results of the first-level comparison, and replacing type difference points are used for the second-level comparison.

8. A method for rapid document comparison based on document element matching and multi-level comparison strategy according to claim 7, It is characterized in that When the replacement type difference points are used for the second level comparison, the replacement type difference points are input into the LCS algorithm to obtain the character level difference results.

9. A document rapid comparison device based on document element matching and multi-level comparison strategy, It is characterized in that include: The document structure parsing module is used to call the parsing tool to convert the PDF source document and the PDF target document into structured data; Document element matching module, which is used to match elements between source documents and target documents based on multi-level element matching rules; A text comparison module, including a first-level text comparison module and a second-level text comparison module. The first-level text comparison module obtains sentence-level difference points of matching elements based on the Myers algorithm, and the second-level text comparison module obtains character-level difference points based on the LCS algorithm. The difference information output module is used to generate the location information of the difference point and output the difference information.

10. A storage medium, It is characterized in that The method comprises a storage unit storing computer software, wherein the computer software is executed according to the method according to any one of claims 1 to 8.