Method and apparatus for detecting differences in contract documents based on icr character matrix
By employing ICR character matrix technology and a two-step difference point query algorithm, the false alarm problem in existing document tool software when comparing contract documents is solved, enabling effective comparison of scanned contracts and detection of differences that meet human expectations.
Patent Information
- Application Number
- CN202310528742.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-05-11
- Publication Date
- 2026-02-10
- Estimated Expiration
- 2043-05-11
AI Technical Summary
Existing document comparison software tools cannot effectively handle scanned PDF documents and other image-based PDF documents, and cannot ignore semantically inconsequential differences, resulting in a higher number of discrepancies in the feedback results than the actual number.
Text data is extracted from images using ICR character matrix technology. A rule-based layout recognition method is used to split the text data into header, body, footer, and table text parts. A two-step difference point query algorithm is used to detect differences and generate comparison results that meet human expectations.
It can effectively process scanned contract documents, ignore differences that do not affect the semantics of the contract, provide statistical results of differences that meet human expectations, and reduce false alarms of differences.
Smart Images

Figure CN116580414B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of natural language processing technology, and more specifically, to a method and apparatus for detecting differences in contract documents based on an ICR character matrix. Background Technology
[0002] Contract review is frequently required in business transactions, which involves comparing textual differences between two contracts. Manual verification is time-consuming, labor-intensive, and prone to oversights. In the era of electronic offices, business personnel can utilize document processing software to accomplish this task. These tools can identify and ignore headers and footers that may be present in the contract, and handle tables and body text separately. However, they typically cannot handle image-based PDF documents such as scanned copies of contracts; their comparison methods are usually line-by-line, which cannot ignore differences in the contract that do not affect semantics. For example, a line of text within a table may be split into multiple lines, or text may be added or deleted, resulting in missing text at the end of the current line or the addition of text to the next line, causing the number of discrepancies in the feedback results to be greater than the actual number. Summary of the Invention
[0003] The purpose of this application is to provide a method and apparatus for detecting contract document discrepancies based on an ICR character matrix, so as to provide contract discrepancy detection results that are more in line with human expectations.
[0004] To achieve the above objectives, in a first aspect, the present invention provides a method for detecting differences in contract documents based on an ICR character matrix, comprising the following steps:
[0005] S1 converts the PDF files of the original contract and the comparison contract into images, and uses ICR technology to extract the text data of the contract from the images. The information provided by the text data includes the contract text, the position information of the characters in the contract text, and the position information of the cell to which the contract text belongs.
[0006] S2 uses a rule-based layout recognition method to split the text data into four parts: header, body, footer, and table text. These parts are then concatenated into long strings in sequence. Each cell of the table text is concatenated into a single line, while the remaining parts are concatenated into a single text box.
[0007] S3 detects the differences between the original document and the comparison document, and uses a two-step difference point query algorithm to query the difference points from the main text and table text in turn to generate text comparison results.
[0008] In an optional implementation, step S2, the rule-based layout recognition method includes:
[0009] S21, divide text data into tabular text data and non-tabular text data based on whether there is a cell to which it belongs;
[0010] S22, using a rule-based y-coordinate normalization method, obtain the standard y-coordinate of each text box in the non-table text, and sort them from left to right and from top to bottom according to the x-coordinate and standard y-coordinate of the text box;
[0011] S23, using a rule-based header recognition algorithm to identify headers in non-table text data;
[0012] S24, using a rule-based footer recognition algorithm, identifies footers in non-table text data;
[0013] S25, based on the recognition results of the header and footer, further divide the non-table text data into three parts: header, body text, and footer.
[0014] In an optional implementation, step S22, the rule-based ordinate standardization method includes the following steps:
[0015] S221, initialize the row index dictionary. By default, one text box is one line, so the number of dictionary elements is the number of texts on the current page. The key name i represents the i-th text box, and the key value is a list containing element i.
[0016] S222, sequentially traverse each text box on the current page. For text box i, sequentially traverse the subsequent text boxes. If the overlap between text box j and i on the y-axis is greater than the threshold, then merge the key value of i into j in the index dictionary, remove i, and exit the loop.
[0017] If the overlap between text boxes j and i on the y-axis is less than or equal to the threshold, no operation is performed. Continue to examine text boxes i and j+1. If j is already the last text box, then examine text boxes i and i+1.
[0018] S223, Traverse the row index dictionary. For each element i in the dictionary, if the number of text box indices in the key value list is greater than 1, calculate the average of the initial y values of these text boxes and mark it as the standard y value; otherwise, the standard y value of the text box is the initial y value.
[0019] In an optional implementation, step S23, the rule-based header recognition algorithm includes the following steps:
[0020] S231, Initialize the header index dictionary, which is used to record the index of the first text box of the row to be examined on each page, with an initial value of 0;
[0021] S232, based on the header index dictionary and the standard vertical coordinate of the text box, obtain one line of text box for each page. If the text box traversal of a certain page has been completed, then execute step S235; otherwise, extract and concatenate the text in the text box and replace the page number with X.
[0022] S233: Using a rule-based judgment method, determine whether the row to be examined contains a header. If it does, execute S232; otherwise, execute S234.
[0023] S234, use a rule-based text completion method to correct the line to be examined, and use a rule-based judgment method to determine whether the line to be examined contains a header. If it does, execute S232; otherwise, execute S235.
[0024] S235, Output header index dictionary, which records the index of the first text box belonging to the main text on each page.
[0025] In an optional implementation, step S24, the rule-based footer recognition algorithm includes the following steps:
[0026] S241, Initialize the footer index dictionary, which is used to record the reverse index of the first text box of the row to be examined on each page, with an initial value of -1;
[0027] S242, based on the footer index dictionary and the standard vertical coordinate of the text box, obtain one line of text box for each page. If the text box traversal of a certain page has been completed, then execute step S244; otherwise, extract and concatenate the text in the text box and replace the page number with X.
[0028] S243, using a rule-based judgment method, determine whether the row to be examined contains a footer. If it does, execute S242; otherwise, execute S244.
[0029] S244, outputs a footer index dictionary, which records the inverted index of the last text box belonging to the main text on each page.
[0030] In an optional implementation, step S234, the rule-based text completion method includes:
[0031] Count the length of the text to be examined on each page, and denote the number of non-whitespace characters in the text to be examined on the i-th page as n. i The number of non-space characters in the next line is n′. i n i The maximum value is n max ;
[0032] Iterate through each page, if n i +n′ i <n max If the value is -2, then the text in the box on the next line of the i-th page will be extracted and appended to the text currently being examined.
[0033] In an optional implementation, the rule-based judgment method in steps S233 and S243 includes:
[0034] The Levenstein algorithm is used to calculate the similarity sim(i,j) between the examined texts on page i and page j. If the minimum value of sim(i,j) is greater than 0.8, then each page is traversed. If the current page being examined is a header, the key value of the header index dictionary on page i is updated to the maximum value of the examined text box index + 1; if the current page being examined is a footer, the key value of the footer index dictionary on page i is updated to the minimum value of the descending index of the examined text box - 1. The result is fed back as either a header or a footer; otherwise, the following operations are performed.
[0035] Iterate through each page. If the minimum value of sim(i,j) (i,j≠i') is greater than 0.8, then iterate through every page except i'. If the current page being examined is the header, then the key value of the header index dictionary on page i is updated to the maximum value of the examined text box index + 1. If the current page being examined is the footer, then the key value of the footer index dictionary on page i is updated to the minimum value of the descending index of the examined text box - 1. The feedback result is that the header or footer is included; otherwise, the feedback result is that the header or footer is not included.
[0036] In an optional implementation, step S3, the two-step difference point query algorithm includes the following steps:
[0037] S31, using a minimum edit distance algorithm based on dynamic programming, obtains the minimum edit matrix of text in rows;
[0038] S32, obtain the minimum edit path based on the minimum edit matrix;
[0039] S33: Determine whether the text under examination is the main text. If so, merge consecutive difference points in the path using the difference point merging algorithm; otherwise, do not process it.
[0040] S34: Traverse the minimum edit path. For each difference point, if the operation is to add or delete, determine that an addition or deletion difference point has been found and record the query result. If the operation is to replace, perform character-level difference point retrieval. After completing the traversal, the query algorithm ends.
[0041] The character-level difference point retrieval includes the following steps:
[0042] S341, use the Levenstein algorithm to calculate the similarity between the strings to be replaced. If the similarity is less than or equal to the given threshold, it is determined that a replacement difference point has been found and the query result is recorded; otherwise, proceed to S342.
[0043] S342 uses a minimum edit distance algorithm based on dynamic programming to obtain the minimum edit distance matrix in character units;
[0044] S343, obtain the minimum edit path based on the minimum edit distance matrix;
[0045] S344 merges consecutive difference points in the path using a difference point merging algorithm;
[0046] S345: Edit the minimum edit path. For each difference point, determine whether an addition, deletion, or replacement difference point has been found based on the operation: add, delete, or replace. Record the query result. After completing the traversal, end the character-level difference point retrieval.
[0047] In an optional implementation, in steps S33 and S344, the difference point merging algorithm includes:
[0048] Traverse the edit path. If the types of path points i to j are not all "the same", then merge path points i to j into one difference point, and count the number of types n of path points i to j. type If n type If the value is greater than 1, the difference point type is "modified"; otherwise, the difference point type is the same as the type of path points i to j.
[0049] In step S345, the information recorded in the query result includes:
[0050] The type of difference; the text related to the difference; the page number range in the original or comparison document; if the difference type is "addition", the related text and page number in the original document will be empty; if the difference type is "deletion", the related text and page number in the comparison document will be empty; if visualization of the difference detection results is required, the four-point coordinates of the characters in the related text should also be provided, and a preprocessed contract image input into the ICR model should be provided when providing the comparison results.
[0051] Secondly, the present invention also provides a contract document difference detection device based on an ICR character matrix, including a memory and a processor connected to the memory, the processor being configured to execute the above-described contract document difference detection method based on an ICR character matrix.
[0052] The contract document difference detection method and apparatus based on ICR character matrix in this invention utilizes ICR technology to extract text information, thus allowing the reading of scanned documents or even photographs of contracts.
[0053] The comparison rules are designed to take into account the structural characteristics of contract documents, such as headers, footers, and tables, and to consider people's usual understanding of differences. This enables the invention to ignore differences that do not affect the semantics of the contract and to provide statistical results of differences that meet human expectations.
[0054] Other features and advantages of this application will be described in detail in the following detailed description section. Attached Figure Description
[0055] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0056] Figure 1 This is a flowchart of the contract document difference detection method based on the ICR character matrix of the present invention;
[0057] Figure 2 This is a schematic diagram of a text box for reading text data provided in an embodiment of this application;
[0058] Figure 3 This is a schematic diagram of a cell for reading text data provided in an embodiment of this application;
[0059] Figure 4 This is a schematic diagram of the character coordinates of the text data being read in, provided in an embodiment of this application.
[0060] Figure 5 A module block diagram of a contract document difference detection device based on an ICR character matrix provided in this application embodiment;
[0061] Figure 6 This is a schematic diagram illustrating the determination of whether a text box is a single line in this application.
[0062] Figure 7 This is a schematic diagram illustrating the calculation of the overlap between text boxes i and j in this application. Detailed Implementation
[0063] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. The components of the embodiments of this application described and shown in the accompanying drawings can generally be arranged and designed in various different configurations.
[0064] In the description of this application, it should be noted that the terms "inner" and "outer," etc., indicate the orientation or positional relationship based on the orientation or positional relationship shown in the accompanying drawings, or the orientation or positional relationship commonly used when the product is in use. They are used only for the convenience of describing this application and for simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, they should not be construed as limitations on this application. Furthermore, the terms "first," "second," etc., are used only to distinguish descriptions and should not be construed as indicating or implying relative importance.
[0065] In the description of this application, it should also be noted that, unless otherwise expressly specified and limited, the terms "setup" and "connection" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral connection; they can refer to a direct connection or an indirect connection through an intermediate medium; and they can refer to the internal connection of two components. Those skilled in the art can understand the specific meaning of the above terms in this application based on the specific circumstances.
[0066] The contract document difference detection method based on ICR character matrix in this application is specifically a contract document comparison method, the flowchart of which is shown below. Figure 1 As shown, this embodiment does not compare the headers and footers of the original document and the comparison document. Therefore, in the difference detection stage, only the text within the cells of the two documents and the body text are compared separately.
[0067] In business applications requiring document comparison, it's typically unnecessary to mark text page breaks caused by various reasons as differences. Therefore, although header and footer comparisons are not involved, this embodiment still needs to identify headers and footers and remove them from the main text. Additionally, changes in table position, such as moving from above to below a section of text, also do not require reporting differences. Therefore, this application separates the main text and the text within cells for comparison.
[0068] The main process includes the following steps:
[0069] S1. Obtain the text from the PDF file and other information required for document comparison. Existing PDF parsing tools can extract text and coordinate information from editable PDFs, but cannot extract the required information from non-editable PDF files such as scanned documents. To ensure the extraction of relevant information from the documents, this application employs ICR technology. First, the original contract and the comparison contract PDF files are converted into images. Then, ICR technology is used to obtain the contract text data from the images. The information provided by the text data includes the contract text, the position information of the characters in the contract text, and the position information of the cells to which the contract text belongs.
[0070] Specifically, the embodiments of this application use the ICR model to obtain text data. The ICR model has table detection and character detection functions and can provide text box position coordinates, character coordinates and cell coordinates. Figure 2 The image shows the text extraction results of a one-page contract using the ICR model; a red rectangle represents a text box. Figure 3 This demonstrates the table detection capabilities of the ICR model, which can provide the containment relationship between cells and text boxes. The ICR model also provides preprocessed contract images with skew correction, and the relationship between the image and the character vertex coordinates is shown in the reference. Figure 4 .
[0071] S2 uses a rule-based layout recognition method to split the text data into four parts: header, body, footer, and table text. These parts are then concatenated into long strings in sequence. Each cell of the table text is concatenated into a single line, while the remaining parts are concatenated into a single text box.
[0072] In one embodiment, the rule-based layout recognition method in step S2 includes:
[0073] S21, divide text data into tabular text data and non-tabular text data based on whether there is a cell to which it belongs;
[0074] S22, Text Box Sorting. ICR models typically recognize a line of text as a single text box. However, when there are large gaps between text lines, a single line of text may be split into multiple text boxes, and the y-coordinates of these text boxes may not be equal. Therefore, the y-coordinates of the text boxes need to be standardized before sorting. This application utilizes a rule-based y-coordinate standardization method to obtain the standard y-coordinate of each text box in non-table text, and sorts the text boxes from left to right and from top to bottom based on their x-coordinates and standard y-coordinates.
[0075] S23, using a rule-based header recognition algorithm to identify headers in non-table text data;
[0076] S24, using a rule-based footer recognition algorithm, identifies footers in non-table text data;
[0077] S25, based on the recognition results of the header and footer, further divide the non-table text data into three parts: header, body text, and footer.
[0078] In one embodiment, step S22, the rule-based ordinate standardization method includes the following steps:
[0079] S221, initialize the row index dictionary. By default, one text box is one line, so the number of dictionary elements is the number of texts on the current page. The key name i represents the i-th text box, and the key value is a list containing element i.
[0080] S222, sequentially traverse each text box on the current page. For text box i, sequentially traverse the subsequent text boxes. If the overlap between text box j and i on the y-axis is greater than 0.6, then merge the key value of i into j in the index dictionary, remove i, and exit the loop.
[0081] If the overlap between text boxes j and i on the y-axis is less than or equal to 0.6, no operation is performed. Continue to examine text boxes i and j+1. If j is already the last text box, then examine text boxes i and i+1.
[0082] The purpose of the index dictionary is to group text boxes within a single line. OCR typically places a line of text into one text box, but when there are large gaps between text, it will be split into two text boxes. See [link / reference]. Figure 6 As shown.
[0083] Initially, each text box is assumed to be on a line, so the initial state of the index dictionary is: {1:[1],2:[2],3:[3],4:[4]}. Since 2 and 3 are on the same line, we want to classify them into the same list.
[0084] Since we need to determine whether text boxes belong to the same line, we must involve two text boxes, one labeled 'i' and the other labeled 'j'. Figure 6 For example, when i equals 1, the text boxes 2, 3, and 4 after 1 are traversed sequentially. When i = 2, it is found that the overlap between text boxes 2 and 3 is greater than 0.6. The key value of 2 ([2]) in the dictionary is merged into the key value of 3 ([3]), becoming [3,2], indicating that text boxes 2 and 3 belong to the same row. The index dictionary 2 is removed, and the index dictionary is modified to {1:[1],3:[3,2],4:[4]}.
[0085] Specifically, in this embodiment, the overlap between text boxes i and j is defined as:
[0086]
[0087] Among them, i y Let i be the ordinate of the text box i. h The height of text box i;
[0088] j y Let j be the y-coordinate of text box j. h The height of text box j.
[0089] max(x,y) refers to taking the largest value of the internal parameter. If x>=y, then max(x,y)=x, otherwise max(x,y)=y.
[0090] min(x,y) specifies the minimum value of the internal parameters. If x >= y, then min(x,y) = y; otherwise, max(x,y) = x. This formula is used to calculate the overlap between text boxes i and j. Figure 7 As shown.
[0091] S223, Traverse the row index dictionary. For each element i in the dictionary, if the number of text box indices in the key value list is greater than 1, calculate the average of the initial y values of these text boxes and mark it as the standard y value; otherwise, the standard y value of the text box is the initial y value.
[0092] In one embodiment, step S23, the rule-based header recognition algorithm includes the following steps:
[0093] S231, Initialize the header index dictionary, which is used to record the index of the first text box of the row to be examined on each page, with an initial value of 0;
[0094] S232, based on the header index dictionary and the standard vertical coordinate of the text box, obtain one line of text box for each page. If the text box of a certain page has been traversed, then execute step S235. Otherwise, extract and concatenate the text in the text box and replace the page number with X, thereby avoiding the impact of different page numbers on the calculation of text similarity.
[0095] S233: Using a rule-based judgment method, determine whether the row to be examined contains a header. If it does, execute S232; otherwise, execute S234.
[0096] S234, use a rule-based text completion method to correct the line to be examined, and use a rule-based judgment method to determine whether the line to be examined contains a header. If it does, execute S232; otherwise, execute S235.
[0097] S235, Output header index dictionary, which records the index of the first text box belonging to the main text on each page.
[0098] In one embodiment, step S24, the rule-based footer recognition algorithm includes the following steps:
[0099] S241, Initialize the footer index dictionary, which is used to record the reverse index of the first text box of the row to be examined on each page, with an initial value of -1;
[0100] S242, based on the footer index dictionary and the standard vertical coordinate of the text box, obtain one line of text box for each page. If the text box traversal of a certain page has been completed, then execute step S244; otherwise, extract and concatenate the text in the text box and replace the page number with X, thereby avoiding the impact of different page numbers on the calculation of text similarity.
[0101] S243, using a rule-based judgment method, determine whether the row to be examined contains a footer. If it does, execute S242; otherwise, execute S244.
[0102] S244, outputs a footer index dictionary, which records the inverted index of the last text box belonging to the main text on each page.
[0103] In one embodiment, the rule-based text completion method in step S234 includes:
[0104] Count the length of the text to be examined on each page, and denote the number of non-whitespace characters in the text to be examined on the i-th page as n.i The number of non-space characters in the next line is n′. i n i The maximum value is n max ;
[0105] Iterate through each page, if n i +n′ i <n max If the value is -2, then the text in the box on the next line of the i-th page will be extracted and appended to the text currently being examined.
[0106] In one embodiment, the rule-based judgment method in steps S233 and S243 includes:
[0107] The Levenstein algorithm is used to calculate the similarity sim(i,j) between the examined texts on page i and page j. If the minimum value of sim(i,j) is greater than 0.8, then each page is traversed. If the current page being examined is a header, the key value of the header index dictionary on page i is updated to the maximum value of the examined text box index + 1; if the current page being examined is a footer, the key value of the footer index dictionary on page i is updated to the minimum value of the descending index of the examined text box - 1. The result is fed back as either a header or a footer; otherwise, the following operations are performed.
[0108] Iterate through each page. If the minimum value of sim(i,j) (i,j≠i') is greater than 0.8, then iterate through every page except i'. If the current page being examined is the header, then the key value of the header index dictionary on page i is updated to the maximum value of the examined text box index + 1. If the current page being examined is the footer, then the key value of the footer index dictionary on page i is updated to the minimum value of the descending index of the examined text box - 1. The feedback result is that the header or footer is included; otherwise, the feedback result is that the header or footer is not included.
[0109] The parentheses at the end of sim(i,j)(i,j≠i') are limiting conditions. Here, we are examining the minimum value of a series of sim(i,j), but those involving i' are not within the scope of the examination. Therefore, neither i nor j should be equal to i'.
[0110] S3, Detect the differences between the original document and the comparison document. To reduce the time and memory overhead required for document comparison, this application adopts a two-step difference point query algorithm to sequentially query the difference points from the main text and table text to generate text comparison results.
[0111] In one embodiment, step S3 of the two-step difference point query algorithm includes the following steps:
[0112] S31. Using the minimum edit distance algorithm based on dynamic programming, the minimum edit matrix of text in rows is obtained. Here, a text box in the main text is called a row, and the text in a cell of a table is called a row. The header and footer are the same as the main text.
[0113] S32, obtain the minimum edit path based on the minimum edit matrix;
[0114] S33: Determine whether the text under examination is the main text. If so, merge consecutive difference points in the path using the difference point merging algorithm; otherwise, do not process it.
[0115] S34: Traverse the minimum edit path. For each difference point, if the operation is to add or delete, determine that an addition or deletion difference point has been found and record the query result. If the operation is to replace, perform character-level difference point retrieval. After completing the traversal, the query algorithm ends.
[0116] In this embodiment, the character-level difference point retrieval includes the following steps:
[0117] S341. In document comparison, business personnel do not care about the possible identical text between two unrelated paragraphs. Therefore, first calculate the similarity between the original document and the document string of the difference point. If the similarity is less than or equal to the threshold, it is determined that a replacement difference point has been found and the query result is recorded. Otherwise, S342 is executed to locate the difference point more accurately.
[0118] Specifically, the Levenstein algorithm is used to calculate the similarity between the strings to be replaced. If the similarity is less than or equal to 0.5, it is determined that a replacement difference point has been found and the query result is recorded; otherwise, S342 is executed.
[0119] S342 uses a minimum edit distance algorithm based on dynamic programming to obtain the minimum edit distance matrix in character units;
[0120] S343, obtain the minimum edit path based on the minimum edit distance matrix;
[0121] S344 merges consecutive difference points in the path using a difference point merging algorithm;
[0122] S345: Edit the minimum edit path. For each difference point, determine whether an addition, deletion, or replacement difference point has been found based on the operation: add, delete, or replace. Record the query result. After completing the traversal, end the character-level difference point retrieval.
[0123] In steps S33 and S344, the difference point merging algorithm includes:
[0124] Traverse the edit path. If the types of path points i to j are not all "the same", then merge path points i to j into one difference point, and count the number of types n of path points i to j. type If n typeIf the value is greater than 1, the difference point type is "modified"; otherwise, the difference point type is the same as the type of path points i to j.
[0125] Specifically, replacing two lines of text in a contract with one line is usually considered a difference; however, according to the definition of the editing path, it is both a replacement difference and a deletion difference. But consecutive differences within cells are not considered the same difference, therefore the difference lookup algorithm does not merge row-level differences in table text.
[0126] In one embodiment, in step S345, the information recorded in the query result includes: the type of difference point; the text related to the difference point; the page number range of the original document or the comparison document; if the difference point type is "add", then the related text and page number of the original document are empty; if the difference point type is "delete", then the related text and page number of the comparison document are empty; if visualization of the difference detection results is required, the four-point coordinates of the characters in the related text should also be provided, and a preprocessed contract image input to the ICR model should be provided when the comparison results are fed back.
[0127] The minimum edit path query is used to find the minimum modification scheme for the original document to the comparison document. Therefore, the minimum edit distance algorithm based on dynamic programming is needed to obtain the minimum edit matrix, and the matrix is used to obtain the path.
[0128] In the minimum edit distance algorithm of dynamic programming, the core idea of dynamic programming is to decompose the problem into several smaller, interconnected subproblems, and record the solutions to the subproblems in a table to avoid redundant calculations, thereby significantly reducing the time required to find the optimal solution. In addition to saving time, this application adopts this algorithm to obtain a table recording the solutions to the subproblems, i.e., the minimum edit matrix mentioned in the paper, which can be used to find the minimum edit path.
[0129] As document length increases, the time and memory overhead of the minimum edit distance algorithm based on dynamic programming grows exponentially, requiring a considerable amount of time and memory to compare the differences between two documents. The core idea of the two-step difference lookup algorithm is to first locate the row containing the difference, and then further locate the characters involved in the difference within this range, thereby avoiding the construction of an extremely large matrix.
[0130] Suppose two documents each have 11 lines, with 10 characters per line. Using the traditional minimum edit distance algorithm, a 110x110 matrix needs to be constructed, consuming 110x110 units of time and memory. However, by focusing on each line, the time and memory requirements are only 11x11 units. If the difference exists only in one line, only another 10x10 matrix needs to be constructed. Even if only the middle line is identical, constructing two 50x50 matrices requires significantly less time and memory than 110x110. Typically, the two documents being compared do not exhibit large areas of difference, thus this method greatly reduces time and memory consumption.
[0131] When calculating the similarity between strings to be replaced using the Levenstein algorithm, including `Levenstein.ratio(str1, str2)`, the calculation formula includes:
[0132] r = (sum - ldist) / sum, where sum is the sum of the lengths of strings str1 and str2, and ldist is the class edit distance.
[0133] Among them, the distance between the edit class and deletion / insertion is +1, but replacement is +2.
[0134] Figure 5 A block diagram of a contract document discrepancy detection device based on an ICR character matrix is provided. The device includes:
[0135] Memory 201, and processor 202 connected to the memory 201.
[0136] The processor 201 is configured to perform the following method steps:
[0137] S1 converts the PDF files of the original contract and the comparison contract into images, and uses ICR technology to extract the text data of the contract from the images. The information provided by the text data includes the contract text, the position information of the characters in the contract text, and the position information of the cell to which the contract text belongs.
[0138] S2 uses a rule-based layout recognition method to split the text data into four parts: header, body, footer, and table text. These parts are then concatenated into long strings in sequence. Each cell of the table text is concatenated into a single line, while the remaining parts are concatenated into a single text box.
[0139] S3 detects the differences between the original document and the comparison document, and uses a two-step difference point query algorithm to query the difference points from the main text and table text in turn to generate text comparison results.
[0140] In one embodiment, the processor is also configured to perform the following steps:
[0141] In step S2, the rule-based layout recognition method includes:
[0142] S21, divide text data into tabular text data and non-tabular text data based on whether there is a cell to which it belongs;
[0143] S22, using a rule-based y-coordinate normalization method, obtain the standard y-coordinate of each text box in the non-table text, and sort them from left to right and from top to bottom according to the x-coordinate and standard y-coordinate of the text box;
[0144] S23, using a rule-based header recognition algorithm to identify headers in non-table text data;
[0145] S24, using a rule-based footer recognition algorithm, identifies footers in non-table text data;
[0146] S25, based on the recognition results of the header and footer, further divide the non-table text data into three parts: header, body text, and footer.
[0147] In one embodiment, the processor is also configured to perform the following steps:
[0148] In step S22, the rule-based ordinate standardization method includes the following steps:
[0149] S221, initialize the row index dictionary. By default, one text box is one line, so the number of dictionary elements is the number of texts on the current page. The key name i represents the i-th text box, and the key value is a list containing element i.
[0150] S222, sequentially traverse each text box on the current page. For text box i, sequentially traverse the subsequent text boxes. If the overlap between text box j and i on the y-axis is greater than the threshold, then merge the key value of i into j in the index dictionary, remove i, and exit the loop.
[0151] If the overlap between text boxes j and i on the y-axis is less than or equal to the threshold, no operation is performed. Continue to examine text boxes i and j+1. If j is already the last text box, then examine text boxes i and i+1.
[0152] S223, Traverse the row index dictionary. For each element i in the dictionary, if the number of text box indices in the key value list is greater than 1, calculate the average of the initial y values of these text boxes and mark it as the standard y value; otherwise, the standard y value of the text box is the initial y value.
[0153] In one embodiment, the processor is also configured to perform the following steps:
[0154] In step S23, the rule-based header recognition algorithm includes the following steps:
[0155] S231, Initialize the header index dictionary, which is used to record the index of the first text box of the row to be examined on each page, with an initial value of 0;
[0156] S232, based on the header index dictionary and the standard vertical coordinate of the text box, obtain one line of text box for each page. If the text box traversal of a certain page has been completed, then execute step S235; otherwise, extract and concatenate the text in the text box and replace the page number with X.
[0157] S233: Using a rule-based judgment method, determine whether the row to be examined contains a header. If it does, execute S232; otherwise, execute S234.
[0158] S234, use a rule-based text completion method to correct the line to be examined, and use a rule-based judgment method to determine whether the line to be examined contains a header. If it does, execute S232; otherwise, execute S235.
[0159] S235, Output header index dictionary, which records the index of the first text box belonging to the main text on each page.
[0160] In one embodiment, the processor is also configured to perform the following steps:
[0161] In step S24, the rule-based footer recognition algorithm includes the following steps:
[0162] S241, Initialize the footer index dictionary, which is used to record the reverse index of the first text box of the row to be examined on each page, with an initial value of -1;
[0163] S242, based on the footer index dictionary and the standard vertical coordinate of the text box, obtain one line of text box for each page. If the text box traversal of a certain page has been completed, then execute step S244; otherwise, extract and concatenate the text in the text box and replace the page number with X.
[0164] S243, using a rule-based judgment method, determine whether the row to be examined contains a footer. If it does, execute S242; otherwise, execute S244.
[0165] S244, outputs a footer index dictionary, which records the inverted index of the last text box belonging to the main text on each page.
[0166] In one embodiment, the processor is also configured to perform the following steps:
[0167] In step S234, the rule-based text completion method includes:
[0168] Count the length of the text to be examined on each page, and denote the number of non-whitespace characters in the text to be examined on the i-th page as n. i The number of non-space characters in the next line is n′. in i The maximum value is n max ;
[0169] Iterate through each page, if n i +n′ i <n max If the value is -2, then the text in the box on the next line of the i-th page will be extracted and appended to the text currently being examined.
[0170] In one embodiment, the processor is also configured to perform the following steps:
[0171] In steps S233 and S243, the rule-based judgment method includes:
[0172] The Levenstein algorithm is used to calculate the similarity sim(i,j) between the examined texts on page i and page j. If the minimum value of sim(i,j) is greater than 0.8, then each page is traversed. If the current page being examined is a header, the key value of the header index dictionary on page i is updated to the maximum value of the examined text box index + 1; if the current page being examined is a footer, the key value of the footer index dictionary on page i is updated to the minimum value of the descending index of the examined text box - 1. The result is fed back as either a header or a footer; otherwise, the following operations are performed.
[0173] Iterate through each page. If the minimum value of sim(i,j) (i,j≠i') is greater than 0.8, then iterate through every page except i'. If the current page being examined is the header, then the key value of the header index dictionary on page i is updated to the maximum value of the examined text box index + 1. If the current page being examined is the footer, then the key value of the footer index dictionary on page i is updated to the minimum value of the descending index of the examined text box - 1. The feedback result is that the header or footer is included; otherwise, the feedback result is that the header or footer is not included.
[0174] In one embodiment, the processor is also configured to perform the following steps:
[0175] In step S3, the two-step difference point query algorithm includes the following steps:
[0176] S31, using a minimum edit distance algorithm based on dynamic programming, obtains the minimum edit matrix of text in rows;
[0177] S32, obtain the minimum edit path based on the minimum edit matrix;
[0178] S33: Determine whether the text under examination is the main text. If so, merge consecutive difference points in the path using the difference point merging algorithm; otherwise, do not process it.
[0179] S34: Traverse the minimum edit path. For each difference point, if the operation is to add or delete, determine that an addition or deletion difference point has been found and record the query result. If the operation is to replace, perform character-level difference point retrieval. After completing the traversal, the query algorithm ends.
[0180] In one embodiment, the processor is also configured to perform the following steps:
[0181] The character-level difference point retrieval includes the following steps:
[0182] S341, use the Levenstein algorithm to calculate the similarity between the strings to be replaced. If the similarity is less than or equal to the given threshold, it is determined that a replacement difference point has been found and the query result is recorded; otherwise, proceed to S342.
[0183] S342 uses a minimum edit distance algorithm based on dynamic programming to obtain the minimum edit distance matrix in character units;
[0184] S343, obtain the minimum edit path based on the minimum edit distance matrix;
[0185] S344 merges consecutive difference points in the path using a difference point merging algorithm;
[0186] S345: Edit the minimum edit path. For each difference point, determine whether an addition, deletion, or replacement difference point has been found based on the operation: add, delete, or replace. Record the query result. After completing the traversal, end the character-level difference point retrieval.
[0187] In one embodiment, the processor is also configured to perform the following steps:
[0188] In steps S33 and S344, the difference point merging algorithm includes:
[0189] Traverse the edit path. If the types of path points i to j are not all "the same", then merge path points i to j into one difference point, and count the number of types n of path points i to j. type If n type If the value is greater than 1, the difference point type is "modified"; otherwise, the difference point type is the same as the type of path points i to j.
[0190] In one embodiment, the processor is also configured to perform the following steps:
[0191] In step S345, the information recorded in the query result includes:
[0192] The type of difference; the text related to the difference; the page number range in the original or comparison document; if the difference type is "addition", the related text and page number in the original document will be empty; if the difference type is "deletion", the related text and page number in the comparison document will be empty; if visualization of the difference detection results is required, the four-point coordinates of the characters in the related text should also be provided, and a preprocessed contract image input into the ICR model should be provided when providing the comparison results.
[0193] This invention discloses a method and apparatus for detecting differences in contract documents based on an ICR character matrix in the field of natural semantic processing technology. The method includes the following steps: extracting text data from the original contract document and the comparison contract document using ICR technology; segmenting the contract document into four parts: header, body text, footer, and table text, and concatenating each part into a long string; using a two-stage difference detection algorithm to detect differences in the table text, header, body text, and footer, and returning the difference type, related strings, and the position information of related characters.
[0194] Compared with existing document comparison tools, this invention can detect differences in scanned or even photographic electronic contract documents. When designing the comparison rules, it takes into account the structural characteristics of contracts, such as headers, footers, and tables, and ignores differences that do not affect semantics, such as line breaks and page breaks, thereby providing comparison results that meet human expectations.
[0195] It should be noted that, where there is no conflict, the features in the embodiments of this application can be combined with each other.
[0196] The above description is merely a preferred embodiment of this application and is not intended to limit 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 protection scope of this application.
Claims
1. A method for detecting discrepancies in contract documents based on an ICR character matrix, characterized in that, Includes the following steps: S1 converts the PDF files of the original contract and the comparison contract into images, and uses ICR technology to extract the text data of the contract from the images. The information provided by the text data includes the contract text, the position information of the characters in the contract text, and the position information of the cell to which the contract text belongs. S2 uses a rule-based layout recognition method to split the text data into four parts: header, body, footer, and table text. These parts are then concatenated into long strings in sequence. Each cell of the table text is concatenated into a single line, while the remaining parts are concatenated into a single text box. S3, detect the differences between the original document and the comparison document, and use a two-step difference point query algorithm to query the difference points from the main text and table text in turn to generate text comparison results; In step S3, the two-step difference point query algorithm includes the following steps: S31, using a minimum edit distance algorithm based on dynamic programming, obtains the minimum edit matrix of text in rows; S32, obtain the minimum edit path based on the minimum edit matrix; S33: Determine whether the text under examination is the main text. If so, merge consecutive difference points in the path using the difference point merging algorithm; otherwise, do not process it. S34: Traverse the minimum edit path. For each difference point, if the operation is to add or delete, determine that an addition or deletion difference point has been found and record the query result. If the operation is to replace, perform character-level difference point retrieval. After completing the traversal, the query algorithm ends. The character-level difference point retrieval includes the following steps: S341, use the Levenstein algorithm to calculate the similarity between the strings to be replaced. If the similarity is less than or equal to the given threshold, it is determined that a replacement difference point has been found and the query result is recorded; otherwise, proceed to S342. S342 uses a minimum edit distance algorithm based on dynamic programming to obtain the minimum edit distance matrix in character units; S343, obtain the minimum edit path based on the minimum edit distance matrix; S344 merges consecutive difference points in the path using a difference point merging algorithm; S345: Edit the minimum edit path. For each difference point, determine whether an addition, deletion, or replacement difference point has been found based on the operation: add, delete, or replace. Record the query result. After completing the traversal, end the character-level difference point retrieval.
2. The contract document discrepancy detection method according to claim 1, characterized in that, In step S2, the rule-based layout recognition method includes: S21, divide text data into tabular text data and non-tabular text data based on whether there is a cell to which it belongs; S22, using a rule-based y-coordinate normalization method, obtain the standard y-coordinate of each text box in the non-table text, and sort them from left to right and from top to bottom according to the x-coordinate and standard y-coordinate of the text box; S23, using a rule-based header recognition algorithm to identify headers in non-table text data; S24, using a rule-based footer recognition algorithm, identifies footers in non-table text data; S25, based on the recognition results of the header and footer, further divide the non-table text data into three parts: header, body text, and footer.
3. The contract document discrepancy detection method according to claim 2, characterized in that, In step S22, the rule-based ordinate standardization method includes the following steps: S221, initialize the row index dictionary. By default, one text box is one line, so the number of dictionary elements is the number of texts on the current page. The key name i represents the i-th text box, and the key value is a list containing element i. S222, sequentially traverse each text box on the current page. For text box i, sequentially traverse the subsequent text boxes. If the overlap between text box j and i on the y-axis is greater than the threshold, then merge the key value of i into j in the index dictionary, remove i, and exit the loop. If the overlap between text boxes j and i on the y-axis is less than or equal to the threshold, no operation is performed. Continue to examine text boxes i and j+1. If j is already the last text box, then examine text boxes i and i+1. S223, Traverse the row index dictionary. For each element i in the dictionary, if the number of text box indices in the key value list is greater than 1, calculate the average of the initial y values of these text boxes and mark it as the standard y value; otherwise, the standard y value of the text box is the initial y value.
4. The contract document discrepancy detection method according to claim 2, characterized in that, In step S23, the rule-based header recognition algorithm includes the following steps: S231, Initialize the header index dictionary, which is used to record the index of the first text box of the row to be examined on each page, with an initial value of 0; S232, based on the header index dictionary and the standard vertical coordinate of the text box, obtain one line of text box for each page. If the text box traversal of a certain page has been completed, then execute step S235; otherwise, extract and concatenate the text in the text box and replace the page number with X. S233: Using a rule-based judgment method, determine whether the row to be examined contains a header. If it does, execute S232; otherwise, execute S234. S234, use a rule-based text completion method to correct the line to be examined, and use a rule-based judgment method to determine whether the line to be examined contains a header. If it does, execute S232; otherwise, execute S235. S235, Output header index dictionary, which records the index of the first text box belonging to the main text on each page.
5. The contract document discrepancy detection method according to claim 4, characterized in that, In step S24, the rule-based footer recognition algorithm includes the following steps: S241, Initialize the footer index dictionary, which is used to record the reverse index of the first text box of the row to be examined on each page, with an initial value of -1; S242, based on the footer index dictionary and the standard vertical coordinate of the text box, obtain one line of text box for each page. If the text box traversal of a certain page has been completed, then execute step S244; otherwise, extract and concatenate the text in the text box and replace the page number with X. S243, using a rule-based judgment method, determine whether the row to be examined contains a footer. If it does, execute S242; otherwise, execute S244. S244, outputs a footer index dictionary, which records the inverted index of the last text box belonging to the main text on each page.
6. The contract document discrepancy detection method according to claim 5, characterized in that, In step S234, the rule-based text completion method includes: Count the length of the text to be examined on each page, and denote the number of non-whitespace characters in the text to be examined on the i-th page as n. i The number of non-space characters in the next line is n′. i n i The maximum value is n max ; Iterate through each page, if n i +n′ i <n max If the value is -2, then the text in the box on the next line of the i-th page will be extracted and appended to the text currently being examined.
7. The contract document discrepancy detection method according to claim 5, characterized in that, In steps S233 and S243, the rule-based judgment method includes: The Levenstein algorithm is used to calculate the similarity sim(i,j) between the examined texts on page i and page j. If the minimum value of sim(i,j) is greater than 0.8, then each page is traversed. If the current page being examined is a header, the key value of the header index dictionary on page i is updated to the maximum value of the examined text box index + 1; if the current page being examined is a footer, the key value of the footer index dictionary on page i is updated to the minimum value of the descending index of the examined text box - 1. The result is fed back as either a header or a footer; otherwise, the following operations are performed. Iterate through each page. If the minimum value of sim(i,j) (i,j≠i') is greater than 0.8, then iterate through every page except i'. If the current page being examined is the header, then the key value of the header index dictionary on page i is updated to the maximum value of the examined text box index + 1. If the current page being examined is the footer, then the key value of the footer index dictionary on page i is updated to the minimum value of the descending index of the examined text box - 1. The feedback result is that the header or footer is included; otherwise, the feedback result is that the header or footer is not included.
8. The contract document discrepancy detection method according to claim 1, characterized in that, In steps S33 and S344, the difference point merging algorithm includes: Traverse the edit path. If the types of path points i to j are not all "the same", then merge path points i to j into one difference point, and count the number of types n of path points i to j. type If n type If the value is greater than 1, the difference point type is "modified"; otherwise, the difference point type is the same as the type of path points i to j. In step S345, the information recorded in the query result includes: The type of difference; the text related to the difference; the page number range in the original or comparison document; if the difference type is "addition", the related text and page number in the original document will be empty; if the difference type is "deletion", the related text and page number in the comparison document will be empty; if visualization of the difference detection results is required, the four-point coordinates of the characters in the related text should also be provided, and a preprocessed contract image input into the ICR model should be provided when providing the comparison results.
9. A contract document discrepancy detection device based on an ICR character matrix, characterized in that, The system includes a memory and a processor connected to the memory, the processor being configured to perform the contract document difference detection method based on an ICR character matrix as described in any one of claims 1-8.
Citation Information
Patent Citations
Recognition method and device for page headers and page footers of format electronic document
CN104951429A
Document comparison analysis method and system based on table structure analysis
CN114021543A