A multi-stage text snippet merging and HTML structure cleaning method
Patent Information
- Application Number
- CN202611030905.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2026-07-13
- Publication Date
- 2026-09-04
- Estimated Expiration
- 2046-07-13
AI Technical Summary
[0005]有鉴于此,本发明提供了一种多阶段文本片段合并与HTML结构化清洗方法,以解决现有技术中OCR文本片段合并不准确、HTML结构化清洗不安全不完整的技术问题
(1)本发明提出的两阶段文本片段合并方法,在第一阶段通过自适应等间距一致性判定取代传统的固定阈值判定,能够有效适应表格、等宽字体等多种版面中不同绝对间距值的等间距排列模式,显著提高了同行文本片段的合并准确率。
Smart Images

Figure CN122528818B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the fields of document processing and optical character recognition technology, and more specifically to a multi-stage text fragment merging and HTML structured cleaning method. Background Technology
[0002] Optical Character Recognition (OCR) technology is widely used in scanning documents and extracting text from images, serving as a core means of digitizing paper documents. OCR engines typically use text segments as the basic output unit, with each segment containing the recognized text content and its coordinates and size information within the original image. However, limited by the detection granularity and layout analysis capabilities of OCR engines, the same line of text is often split into multiple discrete segments, especially in structured content such as tables, fixed-width fonts, and field name-value pairs, where this over-splitting is more severe. Furthermore, OCR engines may generate duplicate detection boxes for the same text region, further increasing the complexity of subsequent processing.
[0003] Existing text fragment merging methods typically use fixed thresholds for spacing determination, which struggles to adapt to the natural variations in character spacing across different layouts. For scenarios like tables and monospaced fonts, character spacing may be uniform but the absolute value can be large. Fixed threshold schemes either fail to merge text within the same row due to an excessively small threshold or incorrectly merge content from different columns due to an excessively large threshold. Furthermore, existing methods lack consideration for semantic features and cannot effectively handle the issue of OCR engines splitting field names and values due to variations in spacing before and after colons. In HTML document structure cleaning, existing methods often employ pre-order or level-order traversal strategies to process the Document Object Model (DOM) tree. During node deletion operations, the deletion of the current node can easily invalidate the traversal pointers of subsequent sibling nodes, requiring additional caching mechanisms or repeated traversals to ensure accuracy. Moreover, existing cleaning methods often treat inline element merging and chapter title recognition as separate processing steps, failing to consider the possibility that text fragmented into multiple inline elements might form a complete title after merging, leading to the loss of document structural semantics.
[0004] Therefore, how to provide a text fragment merging method that can adapt to different layout features and take into account both geometric and semantic information, and combine it with a safe and efficient DOM tree traversal and cleaning strategy to achieve accurate conversion of OCR documents into high-quality structured HTML, is a problem that urgently needs to be solved by those skilled in the art. Summary of the Invention
[0005] In view of this, the present invention provides a multi-stage text fragment merging and HTML structured cleaning method to solve the technical problems of inaccurate OCR text fragment merging and insecure and incomplete HTML structured cleaning in the prior art.
[0006] To achieve the above objectives, the present invention provides the following technical solution: A multi-stage text fragment merging and HTML structure cleaning method includes the following steps: S1. Perform standard merging based on geometric features on the set of text segments generated by OCR recognition. Traverse all text segments through a greedy algorithm with a single pass sequential scan. Determine whether adjacent segments belong to the same text line based on the segment coordinates and size information. Update the coordinates and concatenate the text for segments that meet the merging conditions to obtain the standard merging result. S2. Perform pattern merging based on semantic features on the standard merge result, identify text segments ending with half-width / full-width colons, relax the merging constraints on subsequent adjacent segments, repair the split field name-field value structure, and obtain the pattern merging result. S3. Perform four-dimensional coordinate approximate matching deduplication on the pattern merging results, remove duplicate detection boxes generated by the OCR engine, and obtain a set of deduplicated text fragments; S4. Convert the deduplicated text fragment set into an HTML document and construct a document object model tree. Perform structured cleaning on the document object model tree using a safe post-order traversal strategy and output a structured HTML document.
[0007] Optionally, in S1, the execution process of the single-pass sequential scan greedy algorithm is as follows: Maintain a continuously updated current merge segment, traverse each segment in the sequence of text segments to be merged, and divide the merging judgment mode into two types based on the number of segments already contained in the current merge segment; If the current merged segment contains only a single segment, the baseline establishment mode is used for merging determination. The determination conditions are: the height value of the segment to be traversed is strictly equal to the height value of the current merged segment, the vertical coordinate deviation ratio between the segment to be traversed and the current merged segment does not exceed the first deviation threshold, and the horizontal distance between the segment to be traversed and the current merged segment does not exceed the first distance threshold. If the current merged segment contains two or more segments, the equal spacing detection mode is used for merging determination. The determination conditions are: the height value of the segment to be traversed is strictly equal to the height value of the current merged segment; the vertical coordinate deviation ratio between the segment to be traversed and the current merged segment does not exceed the first deviation threshold; and the absolute value of the difference between the spacing between the segment to be traversed and the current merged segment and the previous spacing does not exceed the spacing consistency threshold.
[0008] Optionally, the vertical coordinate deviation ratio is calculated as follows: the absolute value of the difference between the vertical coordinate of the segment to be traversed and the vertical coordinate of the currently merged segment, divided by the height value of the currently merged segment; The horizontal spacing is calculated as follows: subtract the horizontal starting coordinate of the segment to be traversed from the horizontal starting coordinate of the current merged segment, and then subtract the width value of the current merged segment. The spacing consistency threshold is determined by the positive or negative value of the spacing between the segment to be traversed and the currently merged segment.
[0009] Optionally, in S1, the specific method for updating coordinates and concatenating text for segments that meet the merging criteria is as follows: The width value after merging is equal to the horizontal starting coordinate of the segment to be traversed plus the width value, and then minus the horizontal starting coordinate of the current segment to be merged; the text content after merging is a direct concatenation of the text of each segment.
[0010] Optionally, in S2, the pattern merging step based on semantic features is as follows: Traverse adjacent fragment pairs within the standard merge result. If the text of the preceding fragment ends with a full-width colon or a half-width colon, trigger the colon pattern merge judgment. The conditions for merging colon patterns are: the horizontal distance between the subsequent adjacent segment and the preceding segment does not exceed the second distance threshold, the vertical coordinate deviation ratio between the subsequent adjacent segment and the preceding segment does not exceed the first deviation threshold, and the heights of the two segments do not need to be equal.
[0011] Optionally, in S3, the four-dimensional coordinate approximate matching deduplication process is as follows: For any two sets of text fragments, compare them in four dimensions: horizontal starting coordinates, vertical starting coordinates, width value, and height value, without comparing the text content. If the absolute value of the difference between the corresponding values of each dimension does not exceed the coordinate approximation threshold, then the two sets of text fragments are determined to be duplicate fragments and one of them is removed.
[0012] Optionally, in S4, the safe post-order traversal strategy is as follows: For any current node in the document object model tree, first recursively process the next sibling node of the current node, then recursively process the first child node of the current node, and finally perform the corresponding processing operation according to the type of the current node. If the current node is an element node, perform the following operations in sequence: label correction, hidden node removal, inline element merging, form value extraction, and empty node cleanup; if the current node is a text node, perform the following operations in sequence: blank cleanup, invalid residue removal, chapter title recognition, and empty text cleanup.
[0013] Optionally, the inline element merging operation for element nodes is as follows: If the element node is a paragraph type node, a transactional merging strategy is used to merge all inline element child nodes under the paragraph type node; after the merging is completed, chapter title recognition is automatically triggered to obtain the complete chapter titles that could not be detected before due to being scattered and fragmented. The transactional merging strategy uses a recursive collection function to traverse all child nodes of a paragraph-type node. The recursive collection function returns a double tuple containing the text content and a success flag for each child node. If a non-inline element node or a non-text node is encountered, a failure flag is immediately returned and the merging is terminated. If all child nodes return a success flag, all the collected text is integrated into a single text node, replacing the original child node tree.
[0014] Optionally, the chapter title recognition operation for text nodes is as follows: After removing all whitespace characters from the text content of the text node, the detection is performed. When the text content meets all the detection conditions, a predefined chapter title style tag is added to the parent element of the text node, thereby completing the cross-processor recognizable document structure semantic tagging. The detection conditions are as follows: the text content length is 4 to 10 characters, the colon is only allowed to appear at the end of the text, and the beginning of the text conforms to the Chinese serial number format. The rules for determining the format of Chinese serial numbers are as follows: the first character belongs to the Chinese numeric character set, the second character is a Chinese numeric character or a separator; if the second character is a Chinese numeric character, then the third character must be a separator.
[0015] Optionally, the tag correction operation for element nodes is as follows: based on the preset tag mapping table, perform standardized replacement on non-standard tags containing namespace prefixes; The hidden node removal operation for element nodes is as follows: extract the style attribute value of the element node, remove all Unicode whitespace characters in the style attribute value, and then perform exact string matching; if a hidden / show style declaration is matched, then remove the element node and all its child nodes. The invalid remnant removal operation for text nodes is as follows: if the complete text content of a text node is completely consistent with the incomplete tag fragment generated by HTML parsing, then the text node is removed.
[0016] As can be seen from the above technical solution, compared with the prior art, the present invention discloses a multi-stage text fragment merging and HTML structured cleaning method, which has the following beneficial effects: (1) The two-stage text fragment merging method proposed in this invention replaces the traditional fixed threshold judgment with adaptive equal spacing consistency judgment in the first stage. It can effectively adapt to the equal spacing arrangement mode with different absolute spacing values in various layouts such as tables and monospaced fonts, and significantly improve the merging accuracy of text fragments in the same row.
[0017] (2) This invention addresses the problem of incorrect splitting of colon-separated field name-value pairs by using semantic feature-driven pattern merging, thereby enhancing the integrity of structured content.
[0018] (3) The safe post-order traversal strategy proposed in this invention fundamentally avoids the pointer invalidation problem caused by node deletion during DOM tree traversal by designing a recursive order of sibling nodes first and child nodes later. It does not require additional node caching or multiple traversals, and maintains the efficiency of single traversal while ensuring traversal safety.
[0019] (4) The transactional inline element merging mechanism proposed in this invention ensures the integrity and consistency of the merging operation through the atomic determination strategy of dual return values. At the same time, the merging and chapter title recognition are cascaded and triggered, which effectively solves the problem of recognizing the complete title after the disassembled text is merged. Attached Figure Description
[0020] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.
[0021] Figure 1 The overall flowchart of the multi-stage text fragment merging and HTML structured cleaning method for OCR documents provided by this invention; Figure 2 The flowchart of the standard merging algorithm based on geometric features provided by this invention; Figure 3 The flowcharts for processing different types of nodes in the document object model tree provided by the present invention are shown, where (a) is a five-step processing pipeline for element nodes and (b) is a four-step processing pipeline for text nodes. Figure 4 The module architecture diagram of the multi-stage text fragment merging and HTML structured cleaning system for OCR documents provided by this invention. Detailed Implementation
[0022] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0023] This invention discloses a multi-stage text fragment merging and HTML structured cleaning method, such as... Figure 1 As shown, it includes the following steps: S1. Perform standard merging based on geometric features on the set of text segments generated by OCR recognition. Traverse all text segments through a greedy algorithm with a single pass sequential scan. Determine whether adjacent segments belong to the same text line based on the segment coordinates and size information. Update the coordinates and concatenate the text for segments that meet the merging conditions to obtain the standard merging result.
[0024] Specifically, the execution process of the single-pass sequential scan greedy algorithm is as follows: maintain a continuously updated current merging segment, traverse each segment in the sequence of text segments to be merged, and divide the merging judgment mode into two types based on the number of segments already contained in the current merging segment: (1) If the current merged segment contains only a single segment, the baseline establishment mode is used for merging judgment. The judgment conditions are: the height value of the segment to be traversed is strictly equal to the height value of the current merged segment, the vertical coordinate deviation ratio between the segment to be traversed and the current merged segment does not exceed the first deviation threshold, and the horizontal distance between the segment to be traversed and the current merged segment does not exceed the first distance threshold.
[0025] (2) If the current merged segment contains two or more segments, the equal spacing detection mode is used for merging judgment. The judgment conditions are: the height value of the segment to be traversed is strictly equal to the height value of the current merged segment; the vertical coordinate deviation ratio between the segment to be traversed and the current merged segment does not exceed the first deviation threshold; and the absolute value of the difference between the spacing between the segment to be traversed and the current merged segment and the previous spacing does not exceed the spacing consistency threshold.
[0026] Furthermore, the first deviation threshold is 0.2; the vertical coordinate deviation ratio is calculated as follows: the absolute value of the difference between the vertical coordinate of the segment to be traversed and the vertical coordinate of the currently merged segment, divided by the height value of the currently merged segment.
[0027] The first spacing threshold is 2 pixels; the horizontal spacing is calculated as follows: the horizontal starting coordinate of the segment to be traversed is subtracted from the horizontal starting coordinate of the current merged segment, and then the width value of the current merged segment is subtracted.
[0028] The spacing consistency threshold is determined based on the sign of the spacing between the segment to be traversed and the currently merged segment. Specifically, if the spacing between the segment to be traversed and the currently merged segment is negative, the spacing consistency threshold is 3 pixels; if the spacing between the segment to be traversed and the currently merged segment is non-negative, the spacing consistency threshold is 2 pixels. This determination method, based on the consistency of spacing differences rather than a fixed absolute threshold, can adaptively handle scenarios with spacing arrangements such as tables and monospaced fonts.
[0029] Furthermore, the specific method for updating coordinates and concatenating text for segments that meet the merging conditions is as follows: the width value after merging is equal to the horizontal starting coordinate of the segment to be traversed plus the width value, and then minus the horizontal starting coordinate of the current merged segment; the width calculation uses high-precision decimal arithmetic to avoid the accumulation of floating-point precision errors, and the merged text content is the direct concatenation of the text of each segment.
[0030] S2. Perform pattern merging based on semantic features on the standard merge result, identify text segments ending with half-width / full-width colons, relax the merging constraints on subsequent adjacent segments, repair the split field name-field value structure, and obtain the pattern merging result.
[0031] Pattern merging is a secondary processing step on the output of standard merging, relaxing the merging conditions for specific text semantic patterns. Specifically, the steps of semantic feature-based pattern merging are as follows: traverse adjacent segment pairs within the standard merging result, and if the preceding segment ends with a full-width colon or a half-width colon, then a colon pattern merging decision is triggered.
[0032] The colon-based merging mechanism is determined as follows: the horizontal spacing between the subsequent adjacent segment and the preceding segment does not exceed the second spacing threshold; the vertical coordinate deviation ratio between the subsequent adjacent segment and the preceding segment does not exceed the first deviation threshold (consistent with standard merging); and their heights do not need to be equal (the height equality constraint is relaxed to accommodate changes in field value font size). The second spacing threshold is 4 pixels, which is more lenient than the 2-pixel threshold in standard merging. This mode effectively solves the common problem of OCR engines incorrectly splitting the field name-field value structure into two independent segments.
[0033] S3. Perform four-dimensional coordinate approximate matching deduplication on the pattern merging results, remove duplicate detection boxes generated by the OCR engine, and obtain a set of deduplicated text fragments.
[0034] Specifically, the four-dimensional coordinate approximation matching deduplication operation is as follows: For any two sets of text fragments, compare their horizontal starting coordinates, vertical starting coordinates, width value, and height value respectively, without comparing the text content; if the absolute value of the difference between the corresponding values in each dimension does not exceed the coordinate approximation threshold, then the two sets of text fragments are determined to be duplicate fragments and one of them is removed. The coordinate approximation threshold is 2 pixels.
[0035] S4. Convert the deduplicated text fragment set into an HTML document and construct a document object model tree. Perform structured cleaning on the document object model tree using a safe post-order traversal strategy and output a structured HTML document.
[0036] Specifically, the core design of the safe post-order traversal strategy is as follows: for any current node in the Document Object Model (DOM) tree, first recursively process the next sibling node (NextSibling), then recursively process the first child node (FirstChild), and finally perform the corresponding processing operation according to the type of the current node. The key innovation of this traversal order is that post-order processing ensures that all its child nodes have been cleaned up when checking whether the parent node is null; the design of recursively processing sibling nodes first and then recursively processing child nodes solves the classic problem of pointer invalidation caused by modifying the linked list during traversal. When deleting the current node in the processing phase, its sibling nodes have already been processed in the previous recursion, and the deletion operation will not cause any traversal pointers of unprocessed nodes to be lost.
[0037] If the current node is an element node, perform the following operations in sequence: tag correction, hidden node removal, inline element merging, form value extraction, and empty node cleanup. Specifically: (1) Tag correction: Based on the preset tag mapping table, perform standardized replacement on non-standard XML tags containing namespace prefixes; for example, replace paragraph tags with namespace prefixes generated by Office with standard HTML paragraph tags.
[0038] (2) Remove hidden nodes: Extract the inline style attribute value of the element node, remove all Unicode whitespace characters in the style attribute value and then perform exact string matching; if the hidden display style declaration is matched, remove the element node and all its child nodes.
[0039] (3) Inline element merging: only triggered for paragraph type nodes. If the element node is a paragraph type node, the transactional merging strategy is used to perform a merging operation on all inline element child nodes under the paragraph type node. After the merging is completed, the chapter title recognition is automatically triggered to obtain the complete chapter title that could not be detected before due to being scattered.
[0040] The transactional merging strategy uses a recursive collection function to traverse all child nodes of a paragraph-type node. The recursive collection function returns a double tuple containing the text content and a success flag for each child node. If a non-inline element node or a non-text node is encountered, a failure flag is immediately returned and the merging is terminated. If all child nodes return a success flag, all the collected text is integrated into a single text node, replacing the original child node tree.
[0041] (4) Form value extraction: Detect HTML form input type element nodes, extract the text content in their value attributes, and replace the input element with a block-level container element containing the same text content.
[0042] (5) Empty node cleanup: A whitelist mechanism is used to determine the type of empty element tags that can be cleaned up. The internal text is extracted recursively for empty detection. When the internal text is empty, the element node is removed.
[0043] If the current node is a text node, perform the following operations in sequence: blank space cleanup, invalid residue removal, chapter title recognition, and empty text cleanup. Specifically: (1) Whitespace cleanup: Remove leading and trailing whitespace characters from the text node content.
[0044] (2) Removal of invalid remnants: If the complete text content of a text node is completely identical to the incomplete tag fragment generated by HTML parsing, the text node is removed; where complete identity means full-text exact match, not partial containment match.
[0045] (3) Intelligent recognition of Chinese chapter titles: After removing all whitespace characters from the text content of the text node, detection is performed. When the text content meets all detection conditions, a predefined chapter title style tag is added to the parent element of the text node to complete the cross-processor recognizable document structure semantic tagging.
[0046] The detection conditions are as follows: the text content length is 4 to 10 characters, the colon is only allowed to appear at the end of the text, and the beginning of the text conforms to the Chinese serial number format. The rules for determining the Chinese serial number format are: the first character belongs to the Chinese numeric character set, the second character is a Chinese numeric character or a separator; if the second character is a Chinese numeric character, then the third character must be a separator.
[0047] (4) Empty text cleanup: Remove text nodes that have become empty after the above processing.
[0048] Next, we will continue with a specific example. Figure 1 The structured cleaning method shown is provided to further understand the technical solution protected by this invention.
[0049] After recognizing the input document image, the OCR engine outputs a set of text fragments. Each text fragment contains five attributes: horizontal starting coordinate X, vertical starting coordinate Y, width W, height H, and the recognized text Text.
[0050] Step S1: Perform standard merging based on geometric features on the set of text fragments generated by OCR recognition.
[0051] See Figure 2The standard merging algorithm uses a greedy merging algorithm with a single-pass sequential scan. The specific process is as follows: During initialization, the first segment in the text segment sequence is taken as the starting segment of the current merging segment, and the number of merged segments, count, is set to 1. Starting from the second segment, the algorithm iterates sequentially. For each segment to be processed, curr, the geometric relationship parameters between it and the current merging segment are calculated: the vertical deviation ratio yRatio is equal to the absolute value of the difference between the Y coordinate of curr and the Y coordinate of the merged segment, divided by the height H of the merged segment; the horizontal spacing gap is equal to the X coordinate of curr minus the X coordinate of the merged segment minus the width W of the merged segment. When count equals 1 (baseline establishment mode), it is determined whether the following three conditions are all met: the height H of curr is equal to the height H of the merged segment; yRatio is less than or equal to 0.2; gap is less than or equal to 2. When count is greater than or equal to 2 (equal spacing mode detection), the difference between the current spacing and the previous spacing, gapDiff, is calculated, and a spacing consistency threshold is determined (the threshold is 3 if gap is less than 0, otherwise it is 2). It is then determined whether the three conditions of equal height, yRatio not exceeding 0.2, and gapDiff not exceeding the threshold are all met. When the merging condition is met, update the width W of the merged fragment to curr.X plus curr.W minus merged fragment.X (using high-precision decimal arithmetic), append the text of curr to the end of the merged fragment text, record the current gap as prevGap, and increment count by 1. When the condition is not met, output the current merged fragment to the result list, start a new merged fragment with curr, and reset count to 1.
[0052] Step S2: Perform pattern merging based on semantic features on the standard merging results.
[0053] Above the standard merged result output in step S1, adjacent fragment pairs are traversed. For each pair of adjacent fragments, prev and curr, it is checked whether the text content of prev ends with a full-width colon or a half-width colon. If so, colon mode merging is triggered, and it is determined whether the horizontal spacing between curr and prev is less than or equal to 4 and the vertical deviation ratio is less than or equal to 0.2 (note that the height equality condition is not checked here). If all conditions are met, curr is merged into prev, and the coordinates and concatenated text are updated in the same way as in step S1. Taking a tender document as an example, when the OCR engine recognizes "Project Manager: and Zhang San" as two separate fragments, it detects that "Project Manager:" ends with a colon, triggering colon mode merging, merging the two fragments into "Project Manager: Zhang San", restoring the integrity of the field.
[0054] Step S3: Perform coordinate deduplication on the pattern merging result.
[0055] Iterate through all text segments output in step S2. For any two segments, segA and segB, calculate the absolute value of the coordinate differences in four dimensions: absX equals the absolute value of the difference between the X coordinates of segA and segB, absY equals the absolute value of the difference between the Y coordinates of segA and segB, absW equals the absolute value of the difference between the W coordinates of segA and segB, and absH equals the absolute value of the difference between the H coordinates of segA and segB. When absX, absY, absW, and absH are all less than or equal to 2, segA and segB are determined to be duplicate segments, and one of them is removed. This deduplication process does not compare the text content of the two segments.
[0056] Step S4: Convert the deduplicated text fragment set into an HTML document and construct a document object model tree, then perform structured cleaning using a safe post-order traversal strategy.
[0057] The recursive process of the safe post-order traversal strategy is defined by a traversal function `traverse(node)`: First, if `node` has a next sibling node, then `traverse(node.NextSibling)` is recursively called; then, if `node` has a first child node, then `traverse(node.FirstChild)` is recursively called; finally, the appropriate processing is performed according to the type of `node`. This traversal order guarantees two key properties: when processing `node`, all subsequent sibling nodes of `node` have been processed, so even if `node` is removed from the child node list of its parent node during processing, it will not affect the traversal of any unprocessed nodes; when processing `node`, all descendant nodes of `node` have been processed, so an accurate result can be obtained when determining whether `node` is an empty node.
[0058] See Figure 3 In (a), when node is an element type node, the following five processing steps are executed sequentially: The first step is tag correction: Maintain a tag mapping table that stores non-standard tags that need correction and their corresponding standard HTML tags. For example, map paragraph tags generated by Office applications with the `o:` namespace prefix to standard `p` paragraph tags. Iterate through the mapping table to check if the tag name of the current element is in the table; if so, replace it with the corresponding standard tag name.
[0059] The second step, hidden node removal, involves reading the inline style property value of the current element and performing full Unicode whitespace removal on the property value string (including but not limited to spaces, tabs, newlines, non-breaking spaces, zero-width spaces, etc.). Then, it checks whether the processed string precisely contains the `display:none` declaration. If it does, the current element node and all its descendant nodes are removed from the DOM tree. This pre-removal of whitespace characters helps combat situations where some document generation tools insert abnormal whitespace characters into style values, improving the robustness of detection.
[0060] The third step, transactional inline element merging, first determines if the current element is a paragraph tag; if not, this step is skipped. If it is a paragraph tag, the recursive collection function `gatherNodeText` is called to traverse the child node tree of the current element. This function starts from the first child node of the current element and checks each child node: if the child node is a text node, its text content is extracted and a success flag is returned; if the child node is an inline element node (such as a `span` tag), `gatherNodeText` is recursively called to collect its internal text and return the corresponding flag; if the child node is any other type of node, empty text and a failure flag are immediately returned. When any child node returns a failure flag, the entire collection process terminates, the merging operation is canceled, and the paragraph structure remains unchanged—this achieves transactional atomic operation semantics. Only when all child nodes return success flags are all the collected text concatenated into a complete string, and a new text node is created to replace the entire child node tree under the paragraph element. After the replacement is complete, the chapter title recognition logic is called on the merged text content.
[0061] Step 4: Extracting Form Values: Check if the current element is an HTML input element. If so, read the value of its `value` attribute. If the `value` attribute is not empty, create a block-level container element, set the `value` to the text content of that container, and then replace the original input element with that container element.
[0062] Step 5: Empty Node Cleanup: Check if the current element's tag name is in the predefined cleanable tag whitelist. The whitelist includes 18 tags: p, span, div, a, font, abbr, b, i, em, strong, small, mark, code, kbd, sup, sub, img, and button. If the current tag is in the whitelist, a recursive function extracts the text content of the current element and all its descendant nodes and concatenates them. The concatenated result is then checked to see if it is an empty string (or contains only whitespace characters). If it is empty, the current element node is removed from the DOM tree.
[0063] See Figure 3In (b) of , when the node is a text-type node, the following four-step processing pipeline is executed sequentially: Step 1: Head and tail whitespace cleaning: Perform a head and tail whitespace clipping operation on the content of the text node, remove leading and trailing whitespace characters such as spaces, tabs and newlines.
[0064] Step 2: Invalid residue removal: Check whether the text content after whitespace cleaning is exactly equal to known HTML parsing residue fragments, including incomplete start parts of closing tags (such as a separate left angle bracket plus slash) and a separate left angle bracket. The determination adopts exact equality (full-text matching) rather than inclusion matching, so as to avoid mistakenly deleting valid text containing normal angle brackets.
[0065] Step 3: Intelligent identification of Chinese chapter titles: First, remove all space characters (including full-width and half-width spaces) from the text content, and then make a comprehensive determination according to the following conditions. Condition 1: Calculate the number of characters (counted by Unicode characters) of the text after removing spaces, which is required to be no less than 4 and no more than 10. Condition 2: Scan colon characters (full-width and half-width) in the text, if the colon appears in a non-end position, the determination fails. Condition 3: Check the Chinese serial number format at the beginning of the text: take the first character and determine whether it belongs to the Chinese numeric character set (一, 二, 三, 四, 五, 六, 七, 八, 九, 十); take the second character and determine whether it is a Chinese numeric character or a boundary separator (such as a顿号). If it is a separator, the serial number part ends and the determination succeeds; if it is a Chinese numeric character, continue to take the third character, which must be a boundary separator, otherwise the determination fails. When all the above conditions are satisfied, obtain the parent element node of the text node, and append the predefined chapter title mark string x-fileconv-chapter-title to the class attribute of the parent element. This mark can be recognized by subsequent processors in the document processing pipeline to perform corresponding operations such as chapter-level formatting or table of contents generation, and realize loose coupling collaboration across processors.
[0066] Step 4: Empty text cleaning: Check whether the content of the text node after the above three steps of processing is an empty string, and if it is empty, remove the text node from the DOM tree.
[0067] All the above processing steps are uniformly scheduled by a pluggable processor engine. The processor engine adopts a registry mode, and each processor registers its own factory creation method to the global registry through the initialization function during the program initialization stage. At runtime, processor instances are found from the registry in order according to the configured processor name string slice and created through the factory method. All processor instances share a context object as a data bus, which holds two types of core data: text fragment set and HTML content. Each processor realizes data transmission and collaboration by reading and writing the context object.
[0068] and Figure 1 Corresponding to the method described above, this embodiment can further provide a multi-stage text fragment merging and HTML structure cleaning system for OCR documents, used for... Figure 1 The specific implementation of the method, the multi-stage text fragment merging and HTML structure cleaning system for OCR documents provided in this embodiment of the invention, can be applied to computer terminals or various mobile devices, such as... Figure 4 As shown, it specifically includes: The standard merging module performs geometric feature-based standard merging on the set of text segments generated by OCR recognition. It adopts a greedy merging algorithm with single-pass sequential scanning, traversing each text segment in turn. Based on the height equality, vertical deviation ratio, and horizontal spacing consistency in the coordinate position and size information of the segments, it determines whether adjacent segments belong to the same text line. For segments that meet the merging conditions, it updates the coordinates and concatenates the text. The module internally maintains a merging state machine, which automatically switches between baseline establishment mode and equal spacing detection mode according to the number of merged segments. It achieves accurate merging of text segments in the same line through strict height equality verification, vertical deviation ratio constraint, and adaptive spacing consistency judgment.
[0069] The pattern merging module performs semantic feature-based pattern merging on the output of the standard merging module. It identifies text segments ending with a colon character, relaxes the spacing threshold and height constraints on subsequent adjacent segments, and corrects cases where field names and values are incorrectly split. Internally, this module maintains a set of pattern matchers. The colon pattern matcher detects text segments ending with a colon and merges subsequent segments with relaxed spacing thresholds and no height constraints, correcting cases where field names and values are incorrectly split.
[0070] The coordinate deduplication module is used to eliminate duplicate detection boxes generated by the OCR engine based on the approximate matching judgment of four-dimensional coordinates. It compares the differences in four dimensions: horizontal starting coordinate, vertical starting coordinate, width value, and height value. When the absolute value of the difference in each dimension is within the approximate threshold, it is determined to be a duplicate and removed. The structured cleaning module is used to convert the deduplicated text fragment set into an HTML document and build a document object model tree. It adopts a safe post-order traversal strategy of recursively processing sibling nodes, then recursively processing child nodes, and finally processing the current node. For element nodes, it sequentially performs tag correction, hidden node removal, transactional inline element merging, form value extraction, and empty node cleanup operations. For text nodes, it sequentially performs blank cleanup, invalid residue removal, intelligent recognition of Chinese chapter titles, and empty text cleanup operations.
[0071] The processor engine module is used to implement pluggable configuration of each processor through a global registry and factory methods, pass collections of text fragments and HTML content between processors through a shared data bus, and configure the execution order of each processor in the pipeline through string slicing.
[0072] Specifically, the pluggable processor engine comprises three core components: a global registry, a factory method, and a shared data bus. The global registry employs a self-registration model during initialization, with each processor automatically registering itself in the registry during the program loading phase. The factory method creates the corresponding processor instance from the registry based on the processor identifier. The shared data bus provides a unified data access interface for all processors, carrying two core data types: collections of text fragments and HTML content. The execution order of processors in the pipeline is configured through string slicing, supporting flexible order adjustments and the addition or removal of processors.
[0073] This embodiment may further provide a computer-readable storage medium storing a computer program thereon. When executed by a processor, the computer program implements the steps of the multi-stage text fragment merging and HTML structured cleaning method for OCR documents described above. The computer-readable storage medium includes, but is not limited to, various media capable of storing program code, such as read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0074] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the systems disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the descriptions are relatively simple; relevant parts can be referred to the method section.
[0075] The above description of the disclosed embodiments enables those skilled in the art to make or use the invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the invention. Therefore, the invention is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A multi-stage text fragment merging and HTML structured cleaning method, characterized in that, Includes the following steps: S1. Perform standard merging based on geometric features on the set of text segments generated by OCR recognition. Traverse all text segments through a greedy algorithm with a single pass sequential scan. Determine whether adjacent segments belong to the same text line based on the segment coordinates and size information. Update the coordinates and concatenate the text for segments that meet the merging conditions to obtain the standard merging result. S2. Perform pattern merging based on semantic features on the standard merge result, identify text segments ending with half-width / full-width colons, relax the merging constraints on subsequent adjacent segments, repair the split field name-field value structure, and obtain the pattern merging result. S3. Perform four-dimensional coordinate approximate matching deduplication on the pattern merging results, remove duplicate detection boxes generated by the OCR engine, and obtain a set of deduplicated text fragments; S4. Convert the deduplicated text fragment set into an HTML document and construct a document object model tree. Use a safe post-order traversal strategy to perform structured cleaning on the document object model tree and output a structured HTML document. In S1, the execution process of the single-pass sequential scan greedy algorithm is as follows: Maintain a continuously updated current merge segment, traverse each segment in the sequence of text segments to be merged, and divide the merging judgment mode into two types based on the number of segments already contained in the current merge segment; If the current merged segment contains only a single segment, the baseline establishment mode is used for merging determination. The determination conditions are: the height value of the segment to be traversed is strictly equal to the height value of the current merged segment; the vertical coordinate deviation ratio between the segment to be traversed and the current merged segment does not exceed the first deviation threshold; and the horizontal spacing between the segment to be traversed and the current merged segment does not exceed the first spacing threshold. The first deviation threshold is 0.2, and the first spacing threshold is 2 pixels. If the current merged segment contains two or more segments, the equal spacing detection mode is used for merging determination. The determination conditions are: the height value of the segment to be traversed is strictly equal to the height value of the current merged segment; the vertical coordinate deviation ratio between the segment to be traversed and the current merged segment does not exceed the first deviation threshold; and the absolute value of the difference between the spacing between the segment to be traversed and the current merged segment and the previous spacing does not exceed the spacing consistency threshold. The spacing consistency threshold is determined based on whether the spacing between the segment to be traversed and the current merged segment is positive or negative. Specifically: if the spacing between the segment to be traversed and the current merged segment is negative, the spacing consistency threshold is 3 pixels; if the spacing between the segment to be traversed and the current merged segment is non-negative, the spacing consistency threshold is 2 pixels. In S2, the pattern merging steps based on semantic features are as follows: Traverse adjacent fragment pairs within the standard merge result. If the text of the preceding fragment ends with a full-width colon or a half-width colon, trigger the colon pattern merge judgment. The colon pattern merging criteria are as follows: the horizontal distance between the subsequent adjacent segment and the preceding segment does not exceed the second distance threshold, the vertical coordinate deviation ratio between the subsequent adjacent segment and the preceding segment does not exceed the first deviation threshold, and the heights of the two segments do not need to be equal; where the second distance threshold is 4 pixels. In S4, the safe post-order traversal strategy is as follows: For any current node in the document object model tree, first recursively process the next sibling node of the current node, then recursively process the first child node of the current node, and finally perform the corresponding processing operation according to the type of the current node. If the current node is an element node, perform the following operations in sequence: label correction, hidden node removal, inline element merging, form value extraction, and empty node cleanup; if the current node is a text node, perform the following operations in sequence: blank cleanup, invalid residue removal, chapter title recognition, and empty text cleanup.
2. The multi-stage text fragment merging and HTML structured cleaning method according to claim 1, characterized in that, The vertical coordinate deviation ratio is calculated as follows: the absolute value of the difference between the vertical coordinate of the segment to be traversed and the vertical coordinate of the currently merged segment, divided by the height value of the currently merged segment; The horizontal spacing is calculated as follows: subtract the horizontal starting coordinate of the segment to be traversed from the horizontal starting coordinate of the current merged segment, and then subtract the width value of the current merged segment.
3. The multi-stage text fragment merging and HTML structured cleaning method according to claim 1, characterized in that, In S1, the specific method for updating coordinates and concatenating text in segments that meet the merging criteria is as follows: The width value after merging is equal to the horizontal starting coordinate of the segment to be traversed plus the width value, and then minus the horizontal starting coordinate of the current segment to be merged; the text content after merging is a direct concatenation of the text of each segment.
4. The multi-stage text fragment merging and HTML structured cleaning method according to claim 1, characterized in that, In S3, the four-dimensional coordinate approximate matching and deduplication process is as follows: For any two sets of text fragments, compare them in four dimensions: horizontal starting coordinates, vertical starting coordinates, width value, and height value, without comparing the text content. If the absolute value of the difference between the corresponding values of each dimension does not exceed the coordinate approximation threshold, then the two sets of text fragments are determined to be duplicate fragments and one of them is removed.
5. The multi-stage text fragment merging and HTML structured cleaning method according to claim 1, characterized in that, The specific steps for merging inline elements in an element node are as follows: If the element node is a paragraph type node, a transactional merging strategy is used to merge all inline element child nodes under the paragraph type node; after the merging is completed, chapter title recognition is automatically triggered to obtain the complete chapter titles that could not be detected before due to being scattered and fragmented. The transactional merging strategy uses a recursive collection function to traverse all child nodes of a paragraph-type node. The recursive collection function returns a double tuple containing the text content and a success flag for each child node. If a non-inline element node or a non-text node is encountered, a failure flag is immediately returned and the merging is terminated. If all child nodes return a success flag, all the collected text is integrated into a single text node, replacing the original child node tree.
6. The multi-stage text fragment merging and HTML structured cleaning method according to claim 1, characterized in that, The specific steps for recognizing chapter titles in text nodes are as follows: After removing all whitespace characters from the text content of the text node, the detection is performed. When the text content meets all the detection conditions, a predefined chapter title style tag is added to the parent element of the text node, thereby completing the cross-processor recognizable document structure semantic tagging. The detection conditions are as follows: the text content length is 4 to 10 characters, the colon is only allowed to appear at the end of the text, and the beginning of the text conforms to the Chinese serial number format. The rules for determining the format of Chinese serial numbers are as follows: the first character belongs to the Chinese numeric character set, the second character is a Chinese numeric character or a separator; if the second character is a Chinese numeric character, then the third character must be a separator.
7. The multi-stage text fragment merging and HTML structured cleaning method according to claim 1, characterized in that, The specific steps of the element node label correction operation are as follows: based on the preset label mapping table, perform standardized replacement on non-standard labels containing namespace prefixes; The hidden node removal operation for element nodes is as follows: extract the style attribute value of the element node, remove all Unicode whitespace characters in the style attribute value, and then perform exact string matching; if a hidden / show style declaration is matched, then remove the element node and all its child nodes. The invalid remnant removal operation for text nodes is as follows: if the complete text content of a text node is completely consistent with the incomplete tag fragment generated by HTML parsing, then the text node is removed.
Citation Information
Patent Citations
Word document format conversion method and system capable of configuring rules and cooperating with natural language
CN121706728A
An AI-based word document intelligent analysis and structured storage method
CN122366447A