A table analysis method and apparatus

By using OCR technology to identify and cluster text boxes in table files, target clusters are filtered out and text boxes are merged, solving the problems of low parsing efficiency and large memory consumption in existing technologies, and achieving efficient and dependency-free table parsing.

CN116416606BActive Publication Date: 2025-10-21BEIJING WODONG TIANJUN INFORMATION TECH CO LTD +1
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202210009492.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-01-05
Publication Date
2025-10-21
Estimated Expiration
2042-01-05

AI Technical Summary

Technical Problem

Existing technologies require third-party software and consume a large amount of memory when parsing tables from PDF documents and images. They also have low parsing efficiency and cannot process scanned PDF documents.

Method used

OCR technology is used to identify table files, cluster and filter out target clusters that can form tables, and merge text boxes to complete table parsing. This avoids subsequent operations on the original file, reduces memory usage, and does not rely on third-party software and libraries.

Benefits of technology

It enables table parsing without saving the original file in memory, reducing memory usage, improving parsing efficiency, and is applicable to various file formats. It also reduces dependence on the production environment and supports fast parsing of massive amounts of table files.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116416606B_ABST
    Figure CN116416606B_ABST
Patent Text Reader

Abstract

The application discloses a table analysis method and device, and relates to the technical field of computers. A specific implementation of the method comprises the following steps: performing OCR identification on a table file to obtain a text box for representing a text area of the table file and position information of the text box; clustering a plurality of text boxes according to the position information of the text box to obtain a cluster set corresponding to the plurality of text boxes; traversing the clusters in the cluster set, and screening out, from the cluster set, a cluster meeting a table formation condition as a target cluster according to the set table formation condition; traversing the text boxes in the target cluster, determining that a current text box traversed is internal and there is no text box in the same row as the current text box, and then calculating the distance between the current text box and the text boxes above and below the current text box, and merging the current text box into the nearest text box to obtain a table structure. The implementation does not need to operate on the original file after OCR identification, reduces memory occupation, and does not need to include third-party software and third-party libraries in a production environment.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a table parsing method and device. Background Art

[0002] As a form of structured data, tables are simple and standardized. Parsing tables from PDF documents, images, and other files is a challenging problem. Existing techniques for extracting tables from PDF documents typically use third-party software to locate the table's location and structure. For parsing tables from images, edge detection algorithms can be used to determine the rows and columns within the image, followed by morphological post-processing such as erosion and dilation to determine the table area and structure. Alternatively, a machine learning model can be trained to detect the table's location and determine its structure.

[0003] In the process of implementing the present invention, there are at least the following problems in the prior art:

[0004] When parsing a table from a PDF document, the PDF text itself must be a standard document obtained by typesetting software, and scanned PDF documents cannot be parsed. The above table parsing method requires the use of third-party software or third-party libraries, which require support from the production environment. The above table parsing method requires the original file to be stored in memory for use throughout the entire process, which takes up a large amount of memory and has low parsing efficiency. Summary of the Invention

[0005] In view of this, an embodiment of the present invention provides a table parsing method and device, which uses OCR technology to identify table files, then clusters the identified text boxes, and screens out target clusters that can constitute a table, and then merges the text boxes in the target cluster to complete the table parsing and extract the table structure. The above process does not require further operation on the original file after OCR recognition, and does not require the original file to be kept in memory all the time, reducing memory usage. At the same time, it does not limit the file format to be parsed, and the production environment does not need to include third-party software and third-party libraries.

[0006] To achieve the above objective, according to one aspect of an embodiment of the present invention, a table parsing method is provided.

[0007] A table parsing method according to an embodiment of the present invention includes: performing OCR recognition on a table file to obtain a text box used to represent a text area in the table file and position information of the text box; clustering a plurality of the text boxes according to the position information of the text boxes to obtain a cluster set corresponding to the plurality of the text boxes; traversing the clusters in the cluster set and, based on set table composition conditions, selecting a cluster that meets the table composition conditions from the cluster set as a target cluster; traversing the text boxes in the target cluster to determine that the current text box traversed is internal and there is no text box in the same row as it, then calculating the distance between the current text box and the text boxes above and below it, merging the current text box into the text box closest to it, and obtaining a table structure.

[0008] Optionally, the plurality of text boxes are clustered according to the position information of the text boxes to obtain a cluster set corresponding to the plurality of text boxes, including: drawing a circle with the text box as the center and a set neighborhood radius to obtain the EPS neighborhood of the text box; wherein the neighborhood radius is set according to the position information of the text box; calculating a first number of text boxes included in the EPS neighborhood, comparing the first number with a set density threshold, and determining whether the text box belongs to a core point or a boundary point based on the comparison result; calculating the distance between two text boxes belonging to the core point, and determining that the distance is less than the density threshold, connecting the two text boxes belonging to the core point to form an initial cluster set; and assigning the text box belonging to the boundary point to the initial cluster corresponding to the text box that is closest to the core point to obtain a final cluster set.

[0009] Optionally, the position information of the text box includes the vertex coordinates of the text box; the calculation of the distance between two text boxes belonging to the core point includes: determining whether the two text boxes belonging to the core point are located in the same row or the same column; if the two text boxes are located in the same row, the distance between the two text boxes is the minimum value of the difference between the horizontal coordinates of the vertices of the two text boxes; if the two text boxes are located in the same column, the distance between the two text boxes is the minimum value of the difference between the vertical coordinates of the vertices of the two text boxes; if the two text boxes are located in different rows and columns, the distance between the two text boxes is the Manhattan distance between the center points of the two text boxes.

[0010] Optionally, the determination of whether the two text boxes belonging to the core point are located in the same row or the same column includes: determining whether the difference in the vertical coordinates of the center points of the two text boxes is less than or equal to a set first distance threshold; if the difference in the vertical coordinates is less than or equal to the first distance threshold, the two text boxes are located in the same row; if the difference in the vertical coordinates is greater than the first distance threshold, the two text boxes are located in different rows; determining whether the difference in the horizontal coordinates of the center points of the two text boxes is less than or equal to a set second distance threshold; if the difference in the horizontal coordinates is less than or equal to the second distance threshold, the two text boxes are located in the same column; if the difference in the vertical coordinates is greater than the second distance threshold, the two text boxes are located in different columns.

[0011] Optionally, the table composition condition includes a first sub-condition for constraining the number of text boxes and a second sub-condition for constraining the layout of text boxes; according to the set table composition condition, a cluster that meets the table composition condition is screened out from the cluster set as a target cluster, including: counting the second number of text boxes contained in the current cluster traversed, and when it is determined that the second number meets the first sub-condition, hierarchical clustering is performed on the text boxes contained in the current cluster to obtain text box row and column information; judging whether the text box row and column information meets the second sub-condition, if the text box row and column information meets the second sub-condition, taking the current cluster as the target cluster, and then continuing to traverse until the end; if the text box row and column information does not meet the second sub-condition, continuing to traverse until the end.

[0012] Optionally, performing hierarchical clustering on the text boxes contained in the current cluster to obtain text box row and column information includes: performing hierarchical clustering on the horizontal coordinates of the center points of the text boxes contained in the current cluster to obtain text box row number information; performing hierarchical clustering on the vertical coordinates of the center points of the text boxes contained in the current cluster to obtain text box column number information.

[0013] Optionally, the distance between the current text box and the text boxes above and below it includes a vertical distance and / or a semantic distance; calculating the distance between the current text box and the text boxes above and below it, and merging the current text box into the text box with the closest distance includes: when the distance is a vertical distance, calculating the vertical distance between the current text box and the text boxes above and below it, and merging the current text box into the text box with the closest vertical distance; when the distance is a semantic distance, calculating the semantic distance between the text information of the current text box and the text information of the text boxes above and below it, and merging the current text box into the text box with the closest semantic distance; wherein, the text information is obtained through OCR recognition.

[0014] To achieve the above object, according to another aspect of an embodiment of the present invention, a table parsing device is provided.

[0015] A table parsing device according to an embodiment of the present invention includes: an OCR recognition module for performing OCR recognition on a table file to obtain a text box used to represent a text area in the table file and position information of the text box; a text box clustering module for clustering a plurality of text boxes according to the position information of the text boxes to obtain a cluster set corresponding to the plurality of text boxes; a target cluster screening module for traversing the clusters in the cluster set and screening out a cluster that meets the table composition condition from the cluster set as a target cluster according to a set table composition condition; and a table generation module for traversing the text boxes in the target cluster and determining that the current text box traversed is internal and there is no text box in the same row as it, then calculating the distance between the current text box and the text boxes above and below it, merging the current text box into the text box closest to it, and obtaining a table structure.

[0016] Optionally, the text box clustering module is further used to draw a circle with the text box as the center and a set neighborhood radius to obtain the EPS neighborhood of the text box; wherein the neighborhood radius is set according to the position information of the text box; calculate the first number of text boxes contained in the EPS neighborhood, compare the first number with the size of the set density threshold, and determine whether the text box belongs to the core point or the boundary point according to the comparison result; calculate the distance between two text boxes belonging to the core point, and if it is determined that the distance is less than the density threshold, the two text boxes belonging to the core point are connected to form an initial cluster set; assign the text box belonging to the boundary point to the initial cluster corresponding to the text box that is closest and belongs to the core point, to obtain the final cluster set.

[0017] Optionally, the position information of the text box includes the vertex coordinates of the text box; the text box clustering module is further used to determine whether two text boxes belonging to the core point are located in the same row or the same column. If the two text boxes are located in the same row, the distance between the two text boxes is the minimum value of the difference between the horizontal coordinates of the vertices of the two text boxes; if the two text boxes are located in the same column, the distance between the two text boxes is the minimum value of the difference between the vertical coordinates of the vertices of the two text boxes; if the two text boxes are located in different rows and columns, the distance between the two text boxes is the Manhattan distance between the center points of the two text boxes.

[0018] Optionally, the text box clustering module is also used to determine whether the difference in the vertical coordinates of the center points of the two text boxes is less than or equal to a set first distance threshold. If the difference in the vertical coordinates is less than or equal to the first distance threshold, the two text boxes are located in the same row; if the difference in the vertical coordinates is greater than the first distance threshold, the two text boxes are located in different rows; determine whether the difference in the horizontal coordinates of the center points of the two text boxes is less than or equal to a set second distance threshold. If the difference in the horizontal coordinates is less than or equal to the second distance threshold, the two text boxes are located in the same column; if the difference in the vertical coordinates is greater than the second distance threshold, the two text boxes are located in different columns.

[0019] Optionally, the table composition condition includes a first sub-condition for constraining the number of text boxes and a second sub-condition for constraining the layout of text boxes; the target cluster screening module is further used to count the second number of text boxes contained in the current cluster traversed, and when it is determined that the second number meets the first sub-condition, hierarchically cluster the text boxes contained in the current cluster to obtain text box row and column information; judge whether the text box row and column information meets the second sub-condition, if the text box row and column information meets the second sub-condition, take the current cluster as the target cluster, and then continue traversing until the end; if the text box row and column information does not meet the second sub-condition, continue traversing until the end.

[0020] Optionally, the target cluster screening module is further configured to perform hierarchical clustering on the horizontal coordinates of the center points of the text boxes included in the current cluster to obtain information on the number of text box rows; and perform hierarchical clustering on the vertical coordinates of the center points of the text boxes included in the current cluster to obtain information on the number of text box columns.

[0021] Optionally, the distance between the current text box and the text boxes above and below it includes a vertical distance and / or a semantic distance; the table generation module is further used to, when the distance is a vertical distance, calculate the vertical distance between the current text box and the text boxes above and below it, and merge the current text box into the text box with the shortest vertical distance; when the distance is a semantic distance, calculate the semantic distance between the text information of the current text box and the text information of the text boxes above and below it, and merge the current text box into the text box with the shortest semantic distance; wherein, the text information is obtained through OCR recognition.

[0022] To achieve the above objective, according to another aspect of the embodiments of the present invention, an electronic device is provided.

[0023] An electronic device according to an embodiment of the present invention includes: one or more processors; and a storage device for storing one or more programs. When the one or more programs are executed by the one or more processors, the one or more processors implement a table parsing method according to an embodiment of the present invention.

[0024] To achieve the above objective, according to another aspect of the embodiments of the present invention, a computer-readable medium is provided.

[0025] A computer-readable medium according to an embodiment of the present invention stores a computer program, which, when executed by a processor, implements a table parsing method according to an embodiment of the present invention.

[0026] One embodiment of the above invention has the following advantages or beneficial effects: table files are identified through OCR technology, and then the identified text boxes are clustered, and target clusters that can constitute a table are screened out, and then the text boxes in the target clusters are merged to complete the table parsing and extract the table structure. The above process does not require further operation on the original file after OCR recognition, and there is no need to keep the original file in the memory all the time, which reduces memory usage. At the same time, the file format to be parsed is not limited, and the production environment does not need to include third-party software and third-party libraries.

[0027] Using density-based clustering, we determine whether a text box belongs to a core point or a boundary point, thereby clustering the text boxes. This reduces the impact of noise on the clustering results and can detect clusters of arbitrary shapes and sizes. Different distance calculation methods are used for text boxes belonging to the same row, text boxes belonging to the same column, and text boxes belonging to different rows and columns, which better reflects the degree of standardization and compactness of the arrangement of text boxes.

[0028] By comparing the difference in the ordinates or abscissas of the center points of two text boxes with a set distance threshold, we can determine whether the two text boxes belong to the same row or column, thereby facilitating the text box layout. Using table composition conditions to constrain the number and layout of text boxes that can form a table, we can filter out target clusters that can form a table from a cluster set.

[0029] By performing hierarchical clustering on the text boxes in the target cluster, we obtain their row and column information, which facilitates further determination of whether the text boxes in the target cluster can form a table, improving the accuracy of the judgment results. By calculating the vertical distance or semantic distance between the current text box and the text boxes above and below it, we can merge the text boxes and further ensure the accuracy of the parsed table structure.

[0030] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with specific embodiments. BRIEF DESCRIPTION OF THE DRAWINGS

[0031] The accompanying drawings are provided for a better understanding of the present invention and are not intended to limit the present invention.

[0032] Figure 1 is a schematic diagram of the main steps of the table parsing method according to an embodiment of the present invention;

[0033] Figure 2 This is a schematic diagram of the main flow of the table parsing method according to an embodiment of the present invention;

[0034] Figure 3 is a schematic diagram of the OCR recognition result of an embodiment of the present invention;

[0035] Figure 4 This is a schematic diagram of text box merging according to an embodiment of the present invention;

[0036] Figure 5 This is a schematic diagram of the main process of clustering multiple text boxes according to an embodiment of the present invention;

[0037] Figure 6 is a schematic diagram of main modules of a table parsing device according to an embodiment of the present invention;

[0038] Figure 7 is an exemplary system architecture diagram in which embodiments of the present invention may be applied;

[0039] Figure 8 It is a schematic structural diagram of a computer device suitable for implementing an electronic device according to an embodiment of the present invention. DETAILED DESCRIPTION

[0040] The following description of exemplary embodiments of the present invention is made in conjunction with the accompanying drawings, in which various details of the embodiments of the present invention are included to facilitate understanding. These details should be considered as merely exemplary. Therefore, it should be appreciated by those skilled in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for the sake of clarity and conciseness, descriptions of well-known functions and structures are omitted in the following description.

[0041] The following explains the terms involved in the embodiments of the present invention.

[0042] PDF: It is the abbreviation of Portable Document Format, which means portable document format.

[0043] PDFMiner: is a Python PDF parser that can extract information from PDF documents.

[0044] OCR: It is the abbreviation of Optical Character Recognition, which means optical character recognition.

[0045] DBSCAN: Density-Based Spatial Clustering of Application with Noise, a density-based clustering algorithm.

[0046] OPTICS: It is the abbreviation of Ordering Points To Identify the Clustering Structure and is an extended algorithm of the DBSCAN algorithm.

[0047] OpenCV: is a cross-platform computer vision and machine learning software library.

[0048] BERT: It is the abbreviation of Bidirectional Encoder Representation from Transformers and is a pre-training model.

[0049] Figure 1 FIG. 4 is a schematic diagram of main steps of a table parsing method according to an embodiment of the present invention.

[0050] like Figure 1 As shown, the table parsing method of the embodiment of the present invention mainly includes the following steps:

[0051] Step S101: Perform OCR on a table file to obtain text boxes representing text areas in the table file and location information of the text boxes. A table file refers to a file containing a table, such as a PDF file or image file containing a table. Perform OCR on the table file to obtain multiple text boxes and corresponding location information. For example, the OCR results of the table file include: text box 1 and corresponding location information 1, text box 2 and corresponding location information 2, text box 3 and corresponding location information 3, and so on.

[0052] Step S102: Cluster the multiple text boxes based on the position information of the text boxes to obtain a set of clusters corresponding to the multiple text boxes. Based on the position information of the text boxes, the distance between any two text boxes is calculated, and a clustering algorithm is used to divide the multiple text boxes into different clusters to obtain a set of clusters. In an embodiment, the clustering algorithm can be a density-based clustering algorithm, such as the DBSCAN algorithm and its extended algorithm OPTICS.

[0053] When calculating the distance between two text boxes, first determine whether the two text boxes are in the same row or the same column. If the two text boxes are in the same row, the minimum difference between the horizontal coordinates of the vertices of the two text boxes is used as the distance between the two text boxes; if the two text boxes are in the same column, the minimum difference between the vertical coordinates of the vertices of the two text boxes is used as the distance between the two text boxes; if the two text boxes are neither in the same row nor in the same column, the Manhattan distance between the center points of the two text boxes is used as the distance between the two text boxes.

[0054] Step S103: Traverse the clusters in the cluster set and, based on the set table composition conditions, select a cluster from the cluster set that meets the table composition conditions as a target cluster. The table composition conditions are the conditions required to construct a table, abstracted from the table structure, and include a first sub-condition for constraining the number of text boxes and a second sub-condition for constraining the layout of the text boxes.

[0055] In this step, the first sub-condition is first used to preliminarily determine whether the text boxes in each cluster have the possibility of forming a table. Then, the second sub-condition is used to further determine whether the clusters that have the possibility of forming a table can actually form a table. The clusters that can actually form a table are used as target clusters.

[0056] Step S104: Traverse the text boxes in the target cluster, and if it is determined that the current text box is internal and there is no text box in the same row, then calculate the distance between the current text box and the text boxes above and below it, and merge the current text box into the text box closest to it to obtain a table structure. Traverse the text boxes in the target cluster, and if the current text box is internal (i.e., not a text box in the first or last row or column) and there is no text box in the same row, then calculate the distance between the current text box and the text boxes above and below it, and then merge the current text box into the text box closest to it to complete the table parsing and obtain the final table structure.

[0057] The above embodiment only needs to operate the original table file during OCR recognition to complete the table parsing task. After OCR recognition, there is no need to operate the original table file, so there is no need to keep the original file in the memory, which reduces memory usage and does not limit the file format to be parsed. At the same time, the table parsing task of the present invention can be achieved using the information extracted by OCR recognition, without the need to include third-party software such as PDFMiner and third-party libraries such as OpenCV in the production environment. In addition, for the network service that completes the table parsing task, this embodiment transmits the information extracted by OCR recognition between network nodes, rather than the original file, which has a fast transmission speed, saves resources, and improves the efficiency of table parsing.

[0058] Figure 2FIG. 1 is a schematic diagram of the main flow of the table parsing method according to an embodiment of the present invention. Figure 2 As shown, the table parsing method of the embodiment of the present invention mainly includes the following steps:

[0059] Step S201: Perform OCR on the table file to obtain a text box representing the text area in the table file, the location information of the text box, and the text information within the text box. OCR recognition includes text detection and character recognition. It first extracts features from the image and detects the text area (i.e., the text box). It then segments and classifies the characters in the text area and returns the location information and text information of the text area. Given that mature OCR recognition technology is already available in the prior art, it will not be described in detail here.

[0060] It can be understood that, in this embodiment, while identifying text boxes from a table file, the position information of each text box is also determined to facilitate subsequent clustering calculations; and the text information of each text box is determined to facilitate subsequent merging of text boxes.

[0061] Figure 3 Schematic diagram of the OCR recognition result of the embodiment of the present invention. Figure 3 As shown, the left side is a table file in image format, and the right side is the text boxes recognized by OCR (ie, rectangular areas containing text information) and the text information of each text box.

[0062] Step S202: Based on the location information of the text boxes, the DBSCAN algorithm is used to cluster the multiple text boxes to obtain a cluster set corresponding to the multiple text boxes. The location information of the text box includes the vertex coordinates of the four vertices of the text box. The DBSCAN algorithm has two parameters, namely the neighborhood radius eps and the density threshold MinPts. The core idea of ​​the algorithm is to first find points with higher density, and then gradually connect the similar high-density points into one piece, thereby generating various clusters. The specific implementation of this step is shown in Figure 5 Related instructions.

[0063] Step S203: Traverse the clusters in the cluster set and select the clusters that meet the table formation conditions from the cluster set as target clusters according to the set table formation conditions. The table formation conditions include a first sub-condition for constraining the number of text boxes and a second sub-condition for constraining the layout of the text boxes.

[0064] For example, a common table structure consists of a header and at least one row of data. In this case, the table contains four or more cells, two or more rows, and two or more columns. The first sub-condition can be set to require that the number of text boxes in the cluster is greater than or equal to four, and the second sub-condition can be set to require that the number of text box rows is greater than or equal to two, and the number of text box columns is greater than or equal to two.

[0065] It is understandable that the table composition conditions are set here using a general table as an example. In actual applications, the table composition conditions can also be set according to other table structures.

[0066] In this step, the first sub-condition is used to preliminarily determine whether the text boxes in each cluster have the potential to form a table. Specifically, the second number of text boxes contained in the current cluster is counted to determine whether the second number meets the first sub-condition. If the second number meets the first sub-condition, the text boxes in the current cluster have the potential to form a table. If the second number does not meet the first sub-condition, the text boxes in the current cluster do not have the potential to form a table, and traversal continues to the next cluster.

[0067] When it is determined that the second number meets the first sub-condition, hierarchical clustering is performed on the text boxes contained in the current cluster to obtain text box row and column information; then, it is determined whether the text box row and column information meets the second sub-condition. If the text box row and column information meets the second sub-condition, the current cluster is used as the target cluster, and the traversal is continued until the end; if the text box row and column information does not meet the second sub-condition, the traversal is directly continued until the end.

[0068] The text box row and column information includes the number of text box rows and columns. If the number of text box rows is less than 2, the current cluster does not form a table; if the number of text box columns is less than 2, the current cluster does not form a table. If the number of text box rows is greater than or equal to 2 and the number of text box columns is greater than or equal to 2, the current cluster can form a table.

[0069] The specific implementation of hierarchical clustering of the text boxes contained in the current cluster to obtain the text box row and column information can be: hierarchical clustering of the horizontal coordinates of the center points of the text boxes contained in the current cluster to obtain the text box row number information; and hierarchical clustering of the vertical coordinates of the center points of the text boxes contained in the current cluster to obtain the text box column number information.

[0070] In an embodiment, hierarchical clustering can be implemented based on a hierarchical clustering algorithm, such as the BIRCH algorithm or the single linkage algorithm. Taking the single linkage algorithm as an example, the algorithm has three parameters: a data matrix X, a third distance threshold t, and a criterion. The algorithm uses Euclidean distance to calculate the distance between each element in the data matrix X and gradually merges text boxes to form clusters based on the distance. The clustering process terminates when the distance between the closest clusters exceeds the third distance threshold t, resulting in the final clustering result.

[0071] When performing hierarchical clustering on the horizontal coordinates of the center points, X is an array of the horizontal coordinates of all text box center points, t is the minimum width of the text box in the current cluster, and the criterion is set to "distance". When performing hierarchical clustering on the vertical coordinates of the center points, X is an array of the vertical coordinates of all text box center points, t is the minimum height of the text box in the current cluster, and the criterion is set to "distance".

[0072] Step S204: traverse the text boxes in the target cluster, determine that the current text box is internal and there is no text box in the same row, then calculate the distance between the current text box and the text boxes above and below it, merge the current text box with the text box closest to it, and obtain a table structure.

[0073] If the current text box is located at the first or last row or column of the table, the process continues to traverse the next text box in the target cluster. If the current text box is located outside the first or last row or column of the table, it is considered to be a text box inside the table. A check is performed to see if there is a text box in the same row to the left of the current text box. If so, the process continues to traverse the next text box in the target cluster. If not, the process calculates the distance between the current text box and the text boxes above and below it, merges the current text box with the text box closest to it, and continues to traverse the next text box in the target cluster. After the traversal is completed, the table structure is obtained.

[0074] In an embodiment, the distance between the current text box and the text boxes above and below it can be measured using a vertical distance, or it can be measured using a semantic distance. In the case where the distance is a vertical distance, the vertical distance between the current text box and the text boxes above and below it is calculated, and the current text box is merged into the text box with the shortest vertical distance; in the case where the distance is a semantic distance, the semantic distance between the text information of the current text box and the text information of the text boxes above and below it is calculated, and the current text box is merged into the text box with the shortest semantic distance. In an embodiment, there are many mature ways to calculate semantic distance, such as using a BERT model for calculation.

[0075] Figure 4 This is a schematic diagram of text box merging according to an embodiment of the present invention. Figure 4 As shown, the text box in the 2nd column and the 5th row is taken as the current text box, and the distance between it and the text box above (i.e., the text box in the 2nd column and the 4th row) is calculated (set as distance 1), and the distance between it and the text box below (i.e., the text box in the 2nd column and the 6th row) is calculated (set as distance 2). Assuming that distance 1 is less than distance 2, the current text box is merged into the text box above.

[0076] This embodiment uses OCR technology to obtain the position and text information of the text box, and then completes the table parsing by analyzing the text box layout and the semantic distance of the text information. After OCR recognition, this embodiment does not need to use any information in the original table file and can complete the parsing operation of dozens of tables in 1 second. Compared with the existing technology that takes 1 to 3 seconds to complete the parsing of a table, it greatly improves the parsing efficiency and supports the parsing operation of massive table files. In addition, this embodiment does not rely on the visual features of the image, has low requirements for image quality, and is suitable for application scenarios that lack visual information.

[0077] Figure 5 This is a schematic diagram of the main process of clustering multiple text boxes in an embodiment of the present invention. Figure 5 As shown, the clustering process (i.e., step S202) of the embodiment of the present invention mainly includes the following steps:

[0078] Step S501: Draw a circle with the text box as the center and the set neighborhood radius to obtain the EPS neighborhood of the text box. The neighborhood radius can be set according to the position information of the text box. In this embodiment, the neighborhood radius is set to a set multiple of the minimum width of all text boxes, such as 3 times.

[0079] Step S502: Calculate a first number of text boxes within the EPS neighborhood, compare the first number with a set density threshold, and determine whether the text box is a core point or a boundary point based on the comparison result. The density threshold can be set as needed, for example, to 3.

[0080] If the number of text boxes in an EPS neighborhood (i.e., the first number) is greater than or equal to the density threshold, the text box corresponding to the center of the EPS neighborhood can be determined to be a high-density point, i.e., a core point. If the number of text boxes in the EPS neighborhood of a text box is less than the density threshold, the text box can be determined to be a low-density point. If the point also falls within the EPS neighborhood of a core point, the text box is a boundary point. Text boxes that are neither core points nor boundary points are noise points. If a text box is a noise point, it is deleted.

[0081] Step S503: Calculate the distance between two text boxes belonging to the core point. If the distance is less than the density threshold, connect the two text boxes belonging to the core point to form an initial cluster set. If the distance between the core points is less than the density threshold, connect the two core points together to form several initial clusters.

[0082] In one embodiment, a custom distance function is used to calculate the distance between text boxes. Specifically, a determination is first made as to whether two text boxes are located in the same row or column. If the two text boxes are located in the same row, the distance between the two text boxes is the minimum difference between the horizontal coordinates of the vertices of the two text boxes. If the two text boxes are located in the same column, the distance between the two text boxes is the minimum difference between the vertical coordinates of the vertices of the two text boxes. If the two text boxes are located in neither the same row nor the same column, i.e., in different rows and columns, the distance between the two text boxes is the Manhattan distance between the center points of the two text boxes.

[0083] In an optional embodiment, whether two text boxes are located in the same row can be determined by determining whether the difference in the vertical coordinates of the center points of the two text boxes is less than or equal to a set first distance threshold. If the difference in the vertical coordinates is less than or equal to the first distance threshold, the two text boxes are located in the same row; if the difference in the vertical coordinates is greater than the first distance threshold, the two text boxes are located in different rows. The first distance threshold can be customized, such as 20 pixels.

[0084] In an optional embodiment, whether two text boxes are located in the same column can be determined by determining whether the difference in the horizontal coordinates of the center points of the two text boxes is less than or equal to a set second distance threshold. If the difference in the horizontal coordinates is less than or equal to the second distance threshold, the two text boxes are located in the same column; if the difference in the vertical coordinates is greater than the second distance threshold, the two text boxes are located in different columns. The second distance threshold can also be customized, such as 20 pixels.

[0085] Step S504: Assign the text boxes belonging to the boundary points to the initial cluster corresponding to the text boxes belonging to the core points closest to the boundary points to obtain the final cluster set. Assign the boundary points to the initial cluster corresponding to the core points closest to them to obtain the final clustering result.

[0086] The clustering method of this embodiment can effectively identify noise points, reduce the impact of noise on clustering results, and discover clusters of arbitrary shapes and sizes. Furthermore, the custom distance function of this embodiment can determine the degree of standardization and compactness of the arrangement between two text boxes. Since the Manhattan distance measures the sum of the absolute distances between two points in a standard coordinate system, using the Manhattan distance to calculate the distance between two text boxes in different rows and columns can better reflect the standardization of the arrangement between the text boxes.

[0087] Figure 6 FIG. 1 is a schematic diagram of the main modules of the table parsing device according to an embodiment of the present invention. Figure 6 As shown, the table parsing device 600 of the embodiment of the present invention mainly includes:

[0088] The OCR recognition module 601 is used to perform OCR recognition on a table file to obtain a text box used to represent the text area in the table file and the position information of the text box. A table file refers to a file containing a table, such as a PDF file containing a table, an image file, etc. OCR recognition is performed on the table file to obtain multiple text boxes and corresponding position information. For example, the recognition results of the OCR recognition of the table file include: text box 1 and corresponding position information 1, text box 2 and corresponding position information 2, text box 3 and corresponding position information 3, ...

[0089] The text box clustering module 602 is configured to cluster the multiple text boxes based on their position information to obtain a set of clusters corresponding to the multiple text boxes. Based on the text box position information, the distance between any two text boxes is calculated, and a clustering algorithm is used to divide the multiple text boxes into different clusters to obtain a set of clusters. In one embodiment, the clustering algorithm may be a density-based clustering algorithm, such as the DBSCAN algorithm and its extended algorithm, OPTICS.

[0090] When calculating the distance between two text boxes, first determine whether the two text boxes are in the same row or the same column. If the two text boxes are in the same row, the minimum difference between the horizontal coordinates of the vertices of the two text boxes is used as the distance between the two text boxes; if the two text boxes are in the same column, the minimum difference between the vertical coordinates of the vertices of the two text boxes is used as the distance between the two text boxes; if the two text boxes are neither in the same row nor in the same column, the Manhattan distance between the center points of the two text boxes is used as the distance between the two text boxes.

[0091] The target cluster screening module 603 is configured to traverse the clusters in the cluster set and, based on the set table formation conditions, screen out clusters that meet the table formation conditions as target clusters. The table formation conditions are the conditions required to form a table, abstracted from the table structure, and include a first sub-condition for constraining the number of text boxes and a second sub-condition for constraining the layout of the text boxes.

[0092] This module first uses the first sub-condition to preliminarily determine whether the text boxes in each cluster have the possibility of forming a table. It then uses the second sub-condition to further determine whether the clusters that have the possibility of forming a table can actually form a table. The clusters that can actually form a table are used as target clusters.

[0093] The table generation module 604 is used to traverse the text boxes in the target cluster and determine that the current text box is internal and there is no text box in the same row as it, then calculate the distance between the current text box and the text boxes above and below it, and merge the current text box into the text box closest to it to obtain a table structure. The text boxes in the target cluster are traversed. If the current text box is internal (i.e., not in the first or last row or column) and there is no text box in the same row as it, then calculate the distance between the current text box and the text boxes above and below it, and then merge the current text box into the text box closest to it to complete the table parsing and obtain the final table structure.

[0094] From the above description, it can be seen that by using OCR technology to identify table files, clustering the identified text boxes, and screening out the target clusters that can constitute the table, and then merging the text boxes in the target clusters, the table parsing can be completed and the table structure can be extracted. The above process does not require any further operation on the original file after OCR recognition, and there is no need to keep the original file in memory all the time, which reduces memory usage. At the same time, there is no limit on the file format to be parsed, and the production environment does not need to include third-party software and third-party libraries.

[0095] Figure 7 An exemplary system architecture 700 is shown to which the table parsing method or table parsing apparatus according to the embodiment of the present invention can be applied.

[0096] like Figure 7 As shown, system architecture 700 may include terminal devices 701, 702, 703, a network 704, and a server 705. Network 704 is used to provide a medium for communication links between terminal devices 701, 702, 703 and server 705. Network 704 may include various connection types, such as wired or wireless communication links or fiber optic cables.

[0097] Users can use terminal devices 701, 702, and 703 to interact with server 705 via network 704 to receive or send messages, etc. Terminal devices 701, 702, and 703 can be various electronic devices with display screens and support web browsing, including but not limited to smartphones, tablet computers, laptop computers, and desktop computers, etc.

[0098] Server 705 may be a server that provides various services, such as a background management server that processes table files sent by users using terminal devices 701, 702, and 703. The background management server may perform OCR recognition, text box clustering, target cluster screening, and table generation on the table files, and feed back the processing results (e.g., the generated table structure) to the terminal device.

[0099] It should be noted that the table parsing method provided in the embodiment of the present application is generally executed by the server 705, and accordingly, the table parsing device is generally provided in the server 705. The table parsing method provided in the embodiment of the present application can also be executed by the terminal devices 701, 702, and 703, and accordingly, the table parsing device is provided in the terminal devices 701, 702, and 703.

[0100] It should be understood that Figure 7 The number of terminal devices, networks and servers in the embodiment is merely illustrative. Any number of terminal devices, networks and servers may be provided as required.

[0101] According to an embodiment of the present invention, the present invention further provides an electronic device and a computer-readable medium.

[0102] The electronic device of the present invention includes: one or more processors; a storage device for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement a table parsing method of an embodiment of the present invention.

[0103] The computer-readable medium of the present invention stores a computer program thereon, and when the program is executed by a processor, a table parsing method according to an embodiment of the present invention is implemented.

[0104] Reference below Figure 8 , which shows a schematic structural diagram of a computer system 800 suitable for implementing an electronic device according to an embodiment of the present invention. Figure 8 The electronic device shown is only an example and should not limit the functions and scope of use of the embodiments of the present invention.

[0105] like Figure 8 As shown, the computer system 800 includes a central processing unit (CPU) 801, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 802 or a program loaded from a storage unit 808 into a random access memory (RAM) 803. Various programs and data required for the operation of the computer system 800 are also stored in the RAM 803. The CPU 801, the ROM 802, and the RAM 803 are connected to each other via a bus 804. An input / output (I / O) interface 805 is also connected to the bus 804.

[0106] The following components are connected to the I / O interface 805: an input section 806 including a keyboard, a mouse, and the like; an output section 807 including devices such as a cathode ray tube (CRT), a liquid crystal display (LCD), and a speaker; a storage section 808 including a hard disk; and a communication section 809 including a network interface card such as a LAN card or a modem. The communication section 809 performs communication processing via a network such as the Internet. A drive 810 is also connected to the I / O interface 805 as needed. A removable medium 811, such as a magnetic disk, an optical disk, a magneto-optical disk, or a semiconductor memory, is installed in the drive 810 as needed, so that computer programs read therefrom can be installed into the storage section 808 as needed.

[0107] In particular, according to the embodiments disclosed herein, the processes described in the main step diagrams above can be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program product comprising a computer program carried on a computer-readable medium, the computer program comprising program code for executing the methods shown in the main step diagrams. In such embodiments, the computer program can be downloaded and installed from a network via the communication section 809, and / or installed from a removable medium 811. When the computer program is executed by the central processing unit (CPU) 801, the above-mentioned functions defined in the system of the present invention are executed.

[0108] It should be noted that the computer-readable medium described in the present invention can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of computer-readable storage media can include, but are not limited to, an electrical connection having one or more conductors, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof. In the present invention, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. In the present invention, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. This propagated data signal can take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium that can transmit, propagate, or transport a program for use by or in conjunction with an instruction execution system, apparatus, or device. Program code embodied on a computer-readable medium may be transmitted using any suitable medium, including but not limited to wireless, wireline, optical fiber cable, RF, or any suitable combination thereof.

[0109] The flowcharts and block diagrams in the accompanying drawings illustrate the possible implementation architecture, functions and operations of the systems, methods and computer program products according to various embodiments of the present invention. In this regard, each box in the flowchart or block diagram can represent a module, program segment, or a part of code, and the above-mentioned module, program segment, or a part of code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in an order different from that marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flowchart, and the combination of boxes in the block diagram or flowchart, can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0110] The modules involved in the embodiments of the present invention may be implemented in software or hardware. The modules described may also be provided in a processor. For example, they may be described as follows: a processor including an OCR recognition module, a text box clustering module, a target cluster screening module, and a table generation module. The names of these modules do not, in some cases, constitute a limitation on the modules themselves. For example, the OCR recognition module may also be described as a module that performs OCR recognition on a table file, obtains a text box for representing a text area in the table file, and obtains position information of the text box.

[0111] As another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiment; or it may exist independently and not be assembled into the device. The above computer-readable medium carries one or more programs. When the above one or more programs are executed by a device, the device includes: performing OCR recognition on a table file to obtain a text box used to represent the text area in the table file and the position information of the text box; clustering multiple text boxes according to the position information of the text box to obtain a cluster set corresponding to multiple text boxes; traversing the clusters in the cluster set, and filtering out the cluster that meets the table composition condition from the cluster set as the target cluster according to the set table composition condition; traversing the text boxes in the target cluster, determining that the current text box traversed is internal and there is no text box in the same row, then calculating the distance between the current text box and the text boxes above and below it, merging the current text box into the text box with the closest distance, and obtaining a table structure.

[0112] According to the technical solution of an embodiment of the present invention, table files are identified through OCR technology, and then the identified text boxes are clustered, and the target clusters that can constitute the table are screened out, and then the text boxes in the target clusters are merged to complete the table parsing and extract the table structure. The above process does not require further operation on the original file after OCR recognition, and there is no need to keep the original file in the memory, which reduces memory usage. At the same time, there is no limit on the file format to be parsed, and the production environment does not need to include third-party software and third-party libraries.

[0113] The above-mentioned product can execute the method provided by the embodiment of the present invention, and has the functional modules and beneficial effects corresponding to the execution method. For technical details not fully described in this embodiment, please refer to the method provided by the embodiment of the present invention.

[0114] The above specific embodiments do not limit the scope of protection of the present invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions may occur depending on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention are intended to be included within the scope of protection of the present invention.

Claims

1. A table parsing method, characterized in that: include: Performing OCR on the table file to obtain a text box representing a text area in the table file and position information of the text box; Clustering the plurality of text boxes according to the position information of the text boxes to obtain cluster sets corresponding to the plurality of text boxes; Traversing the clusters in the cluster set, and selecting a cluster that meets the table composition condition from the cluster set as a target cluster according to a set table composition condition; Traversing the text boxes in the target cluster, determining that the current text box is an internal text box and that there is no text box in the same row as the current text box, calculating the distance between the current text box and the text boxes above and below it, merging the current text box with the text box closest to it, and obtaining a table structure, where the internal text box is a text box that is not in the first or last row or the first or last column; The table formation condition includes a first sub-condition for constraining the number of text boxes and a second sub-condition for constraining the layout of the text boxes; The step of selecting, from the cluster set according to the set table formation condition, a cluster that meets the table formation condition as a target cluster includes: Counting a second number of text boxes contained in the traversed current cluster, and if it is determined that the second number meets the first sub-condition, performing hierarchical clustering on the text boxes contained in the current cluster to obtain text box row and column information; Determine whether the text box row and column information meets the second sub-condition. If so, use the current cluster as the target cluster and continue traversing until the end. If not, continue traversing until the end.

2. The method according to claim 1, characterized in that Clustering the plurality of text boxes according to the position information of the text boxes to obtain cluster sets corresponding to the plurality of text boxes includes: Draw a circle with the text box as the center and the set neighborhood radius to obtain the EPS neighborhood of the text box; wherein the neighborhood radius is set according to the position information of the text box; Calculating a first number of text boxes contained in the EPS neighborhood, comparing the first number with a set density threshold, and determining whether the text box is a core point or a boundary point based on the comparison result; Calculate the distance between two text boxes belonging to the core point, and if the distance is less than a distance threshold, connect the two text boxes belonging to the core point to form an initial cluster set; The text boxes belonging to the boundary points are assigned to the initial cluster corresponding to the text boxes belonging to the core points that are closest to them, and the final cluster set is obtained.

3. The method according to claim 2, characterized in that The position information of the text box includes the vertex coordinates of the text box; The calculating of the distance between two text boxes belonging to the core point includes: Determine whether the two text boxes belonging to the core point are in the same row or column. If the two text boxes are in the same row, the distance between the two text boxes is the minimum difference between the horizontal coordinates of the vertices of the two text boxes; If two text boxes are located in the same column, the distance between the two text boxes is the minimum difference between the vertical coordinates of the vertices of the two text boxes; If two text boxes are located in different rows and columns, the distance between the two text boxes is the Manhattan distance between the center points of the two text boxes.

4. The method according to claim 3, characterized in that The determining whether two text boxes belonging to the core point are located in the same row or the same column includes: Determine whether the difference in the vertical coordinates of the center points of the two text boxes is less than or equal to a set first distance threshold; if the difference in the vertical coordinates is less than or equal to the first distance threshold, the two text boxes are located in the same row; if the difference in the vertical coordinates is greater than the first distance threshold, the two text boxes are located in different rows; Determine whether the horizontal coordinate difference between the center points of the two text boxes is less than or equal to a set second distance threshold. If the horizontal coordinate difference is less than or equal to the second distance threshold, the two text boxes are located in the same column; if the vertical coordinate difference is greater than the second distance threshold, the two text boxes are located in different columns.

5. The method according to claim 1, wherein The hierarchical clustering of the text boxes contained in the current cluster to obtain text box row and column information includes: Performing hierarchical clustering on the horizontal coordinates of the center points of the text boxes included in the current cluster to obtain information on the number of text box rows; Hierarchical clustering is performed on the vertical coordinates of the center points of the text boxes included in the current cluster to obtain information on the number of text box columns.

6. The method according to any one of claims 1 to 5, characterized in that The distance between the current text box and the text boxes above and below it includes a vertical distance and / or a semantic distance; The calculating the distance between the current text box and the text boxes above and below it, and merging the current text box into the text box closest to it, includes: In the case where the distance is a vertical distance, calculating the vertical distance between the current text box and the text boxes above and below it, and merging the current text box into the text box with the shortest vertical distance; When the distance is a semantic distance, the semantic distance between the text information of the current text box and the text information of the text boxes above and below it is calculated, and the current text box is merged into the text box with the closest semantic distance; wherein the text information is obtained through OCR recognition.

7. A table parsing device, characterized in that: include: An OCR recognition module is used to perform OCR recognition on the table file to obtain a text box representing a text area in the table file and position information of the text box; A text box clustering module, configured to cluster the plurality of text boxes according to position information of the text boxes to obtain cluster sets corresponding to the plurality of text boxes; a target cluster screening module, configured to traverse the clusters in the cluster set and, based on a set table formation condition, screen out a cluster that meets the table formation condition from the cluster set as a target cluster; A table generation module is configured to traverse the text boxes in the target cluster, determine if the current text box is an internal text box and there is no text box in the same row as the current text box, calculate the distance between the current text box and the text boxes above and below it, merge the current text box with the text box closest to it, and obtain a table structure, where the internal text box is a text box that is not in the first or last row or the first or last column; The table formation condition includes a first sub-condition for constraining the number of text boxes and a second sub-condition for constraining the layout of the text boxes; The target cluster screening module is further configured to: Counting a second number of text boxes contained in the traversed current cluster, and if it is determined that the second number meets the first sub-condition, performing hierarchical clustering on the text boxes contained in the current cluster to obtain text box row and column information; Determine whether the text box row and column information meets the second sub-condition. If so, use the current cluster as the target cluster and continue traversing until the end. If not, continue traversing until the end.

8. An electronic device, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 6.

9. A computer-readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 6 is implemented.

Citation Information

Patent Citations

  • Table identification method and system based on clustering

    CN113283398A