Disclosure of Invention
In view of this, the present application provides a method and an apparatus for extracting a nested table, and a storage medium.
According to a first aspect of the present application, there is provided a method for extracting a nested table, including:
reading and analyzing data content in a first type file, and determining line segment coordinate information contained in the analyzed data content;
grouping the line segments by taking the display unit of the first type file as a reference;
for the line segments in each group, merging the line segments of which the transverse or longitudinal distance of the adjacent or phase connection line segments is less than a first set threshold value and the interval between the adjacent end points of the adjacent line segments is less than a second set threshold value based on the coordinates of the line segments;
traversing all the combined line segments, classifying according to whether the line segments are intersected or not, generating N sets, wherein the line segments in the sets are intersected, and the line segments between the sets are not intersected; n is an integer greater than or equal to 1;
traversing the line segments in the set, determining the intersection points between the combined intersected line segments, traversing all the intersection points, and determining the intersection points forming the rectangular frame;
generating a table by taking the intersection point of which the area of the formed rectangular frame is larger than the third threshold value as a reference;
based on the generated tables, inclusion relationships between the tables are calculated, and nested tables are formed based on the inclusion relationships of the tables.
As an implementation, the method further comprises:
acquiring text information and coordinate information of the text contained in the analyzed data content, merging the text according to the coordinate information of the text, and calculating a character outline;
determining text and/or picture information located in the table area according to coordinate information of the outline of the text and/or picture coordinate information contained in the analyzed data content;
and filling the determined text and/or picture in a corresponding area of the table.
As one implementation, the table-based containment relationship forms a nested table, including:
according to the coordinate information of the rectangular frame, integrating the data outside the table for analysis, and calculating a basic cell width and height list;
based on the basic cell width and height list, calculating the nesting relation between the tables, and setting rectangular area borders and merging cells according to the sequence of the rectangular area from small to large to generate a nested table format.
As an implementation, the method further comprises:
and storing the text information outside the table in the first type file, and storing the table and the text and/or the picture in the table as table data.
As one implementation, the determining an intersection point between the merged line segments includes:
dividing transverse line segments contained in the analyzed data content into a first set, dividing longitudinal line segments into a second set, and sequencing the line segments in the first set and the second set according to line segment coordinates;
and determining all line segments with intersection points for any line segment in the first set or the second set, and forming an intersection point set by intersection points formed among the line segments with the intersection points.
According to a second aspect of the present application, there is provided an extraction apparatus of a nested table, comprising:
the reading and analyzing unit is used for reading and analyzing the data content in the first type file;
a first determining unit, configured to determine line segment coordinate information included in the analyzed data content;
the grouping unit is used for grouping the line segments by taking the display unit of the first type file as a reference;
the first merging unit is used for merging the line segments with the transverse or longitudinal distance of the adjacent or connected line segments smaller than a first set threshold value and the adjacent endpoint interval of the adjacent line segments smaller than a second set threshold value based on the coordinates of the line segments in each group;
the set generation unit is used for traversing all the combined line segments, classifying according to whether the line segments are intersected or not, and generating N sets, wherein the line segments in the sets are intersected, and the line segments between the sets are not intersected; n is an integer greater than or equal to 1;
the second determining unit is used for traversing the line segments in the set, determining the intersection points between the combined intersected line segments, traversing all the intersection points and determining the intersection points forming the rectangular frame;
a generating unit, configured to generate a table based on an intersection point where the area of the rectangular frame is larger than a third threshold;
and the forming unit is used for calculating the inclusion relation between the tables based on the generated tables and forming the nested tables based on the inclusion relation of the tables.
As an implementation, the apparatus further comprises:
the acquisition unit is used for acquiring the text information contained in the analyzed data content and the coordinate information of the text;
the second merging unit is used for merging the texts according to the coordinate information of the texts and calculating character outlines;
a third determining unit, configured to determine text and/or picture information located in the table area according to coordinate information of an outline of a text and/or picture coordinate information included in the parsed data content;
and the filling unit is used for filling the determined text and/or picture in the corresponding area of the table.
As an implementation, the forming unit is further configured to:
according to the coordinate information of the rectangular frame, integrating the data outside the table for analysis, and calculating a basic cell width and height list;
based on the basic cell width and height list, calculating the nesting relation between the tables, and setting rectangular area borders and merging cells according to the sequence of the rectangular area from small to large to generate a nested table format.
As an implementation, the apparatus further comprises:
and the storage unit is used for storing the text information outside the table in the first type file, and the table and the text and/or picture in the table are stored as table data.
As an implementation manner, the second determining unit is further configured to:
dividing transverse line segments contained in the analyzed data content into a first set, dividing longitudinal line segments into a second set, and sequencing the line segments in the first set and the second set according to line segment coordinates;
and determining all line segments with intersection points for any line segment in the first set or the second set, and forming an intersection point set by intersection points formed among the line segments with the intersection points.
According to a third aspect of the present application, there is provided a storage medium having stored thereon an executable program which, when executed by a processor, implements the steps of the method of extracting nested tables.
According to the method and the device for extracting the nested table and the storage medium, the line segment information is extracted from the data content in the analyzed non-editable text, the line segments are merged based on the coordinate positions of the line segments, the intersection points between the merged line segments are determined, the corresponding table structure is determined and generated based on the intersection point information, and the corresponding characters or pictures and the like are filled in the regenerated table structure according to the coordinate information of the analyzed text, so that the non-editable character text and the table content can be extracted, for example, the structure of the extracted table can be directly imported into an Excel file, and the like. The embodiment of the application improves the accurate identification of the table in the non-editable text, can be directly used without redrawing, greatly facilitates the use of a user, improves the identification accuracy of the non-editable text, and lightens the editing work of the user.
Detailed Description
The following explains the essence of the technical solution of the embodiments of the present application in detail with reference to examples.
Fig. 1 is a schematic flowchart of an extraction method of a nested table provided in an embodiment of the present application, and as shown in fig. 1, the extraction method of the nested table in the embodiment of the present application includes the following processing steps:
step 101, reading and analyzing data content in a first type file, and determining line segment coordinate information contained in the analyzed data content.
In the embodiment of the present application, the first type file includes non-editable text such as PDF text, screen capture text, pictures, and other text.
The first type file can contain text, tables, particularly nested tables, pictures and the like. In the embodiment of the present application, the table includes not only a general table but also a frame containing characters.
And 102, grouping the line segments by taking the display unit of the first type file as a reference.
In this embodiment of the application, the display unit may be a page of the first type file, that is, each page constitutes one display unit, and the display unit may also be a portion of the first type file displayed in the current display interface of the electronic device. In the embodiment of the application, after the first type file is analyzed, the line segments in the first type file are grouped according to the display unit, so that the tables and the like in the first type file are processed according to the groups.
When the display unit is the current display interface, the user can display the table in the first type file to be analyzed in the current display interface, and perform text analysis by using the current display interface as a unit so as to analyze the table displayed by the current display interface.
And 103, merging the line segments in each group, wherein the transverse or longitudinal distance of the adjacent or connected line segments is less than a first set threshold value, and the interval between the adjacent end points of the adjacent line segments is less than a second set threshold value, based on the coordinates of the line segments.
In the embodiment of the present application, the line segments in the table should be, in theory, either parallel or perpendicular to each other. For line segments in which there is no continuity but the spacing is particularly close, the parallel distances are also particularly close, merged into one line segment.
And 104, traversing all the combined line segments, classifying according to whether the line segments are intersected or not, generating N sets, wherein the line segments in the sets are intersected, and the line segments between the sets are not intersected.
Wherein N is an integer of 1 or more.
In the embodiment of the application, all line segments in one display unit needing to be traversed and analyzed are classified according to whether the line segments are intersected or not after all transverse lines and vertical lines are traversed, and N sets are generated, wherein the line segments in the sets are intersected, and the line segments between the sets are not intersected. The set without intersection among the line segments is the precursor of a plurality of mutually nested tables to be extracted.
And 105, traversing the line segments in the set, determining intersection points among the combined intersected line segments, traversing all the intersection points, and determining the intersection points forming the rectangular frame.
In the embodiment of the application, the transverse line segments contained in the analyzed data content are divided into a first set, the longitudinal line segments are divided into a second set, and the line segments in the first set and the line segments in the second set are sequenced according to line segment coordinates; and determining all line segments with intersection points for any line segment in the first set or the second set, and forming an intersection point set by intersection points formed among the line segments with the intersection points.
And 106, generating a table by taking the intersection point of which the area of the formed rectangular frame is larger than the third threshold value as a reference.
In the embodiment of the present application, for the intersection point capable of forming the rectangular frame, the size of the rectangular frame needs to be determined, and for the rectangular frame smaller than the minimum text outline, an analysis error obviously occurs, and a non-table line segment part may be added to the line segment set, so that the rectangular frame with the area smaller than the third threshold value should be removed. In the embodiment of the present application, because the table structure included in the first type file is mainly configured as a rectangular frame, the table structure can be regenerated by determining the corresponding rectangular frame through the intersection point set.
And step 107, calculating the inclusion relationship between the tables based on the generated tables, and forming a nested table based on the inclusion relationship of the tables.
In the embodiment of the application, according to the coordinate information of the rectangular frame, integrating the data outside the table for analysis, and calculating a basic cell width and height list; based on the basic cell width and height list, calculating the nesting relation between the tables, and setting the rectangular area border and the merging cells according to the sequence of the rectangular area from small to large, thereby generating the nested table format.
In the embodiment of the present application, on the basis of the foregoing steps, the method further includes: acquiring text information and coordinate information of the text contained in the analyzed data content, merging the text according to the coordinate information of the text, and calculating a character outline;
determining text and/or picture information located in the table area according to coordinate information of the outline of the text and/or picture coordinate information contained in the analyzed data content;
and filling the determined text and/or picture in a corresponding area of the table.
And after the table structure is determined, acquiring the text content in the table area, filling the text and/or picture information in the table area into the table according to the corresponding coordinate information of the text and/or picture.
In this embodiment of the application, after the text and table analysis processing is completed on the first type file, the text information outside the table in the first type file may be separately stored, and the table and the text and/or the picture in the table are stored as table data.
The embodiments of the present application will be described in further detail below with reference to specific examples.
The embodiment of the present application takes a PDF document as an example for description, and it should be noted that other non-editable texts, such as a screenshot, a picture and other texts, may use the technical means of the embodiment of the present application.
Fig. 2 is a flowchart of a specific example of an extraction method of a nested table according to an embodiment of the present application, and as shown in fig. 2, the extraction method of table data and a nested table in a PDF document according to the embodiment of the present application includes the following specific steps:
reading a PDF file to a memory of the electronic equipment, reading each page of the PDF, and analyzing data of each page one by one.
Analyzing all data information in the PDF page, wherein the data information comprises but is not limited to attribute information of characters, line segments and pictures and coordinate information of the characters, the line segments and the pictures.
Analyzing and acquiring coordinate information of all line segments in the PDF page, and sorting out a set of all horizontal lines (horizontal lines) and vertical lines or vertical lines (vertical lines). The line segments acquired here may be discontinuous data information. Such as: [ (X1, Y1), (X2, Y1) ], [ (X2, Y1), (X3, Y1) ], where X represents an X-axis coordinate value of an end point of the segment, Y represents a Y-axis coordinate value of an end point of the segment, e.g., (X1, Y1) represents a coordinate of a first end point of the segment, and (X2, Y1) represents a coordinate of a second end point of the segment. The coordinate system here may be a coordinate system of a display screen in the electronic device, such as a coordinate system in units of pixels, or may be a coordinate system specifically provided in the electronic device.
And for the condition that the horizontal lines and the vertical lines in the page cannot be aligned, judging the set coordinate points of the horizontal lines or the vertical lines, and combining the parallel line segments of which the horizontal and vertical distances are less than a certain set threshold value. As in the above coordinate case, the horizontal line segments are overlapped, and if X3> X2> X1, the data can be directly recorded as [ (X1, Y1), (X3, Y1) ], and recorded as a line segment; if a segment [ (X1, Y1), (X2, Y1) ], [ (X2, Y2), (X3, Y2) ],. Y1 and Y2 are present in the set, the difference between these segments can also be recorded as [ (X1, Y1), (X3, Y1) ]. The second set threshold here may be set according to a specific resolution precision, such as several pixel units, such as 100 pixel units, etc. And extending and connecting the line segments of which the distance between the end points is less than a first set threshold value in the two parallel line segments, so that the parallel line segments are fused into one line segment.
The line segments of the horizontal and vertical sets are sorted according to the coordinates to obtain two ordered sets of line segments, as shown in fig. 3 and 4, examples of the horizontal line set and the vertical line set are respectively shown.
Whether the horizontal lines and the vertical lines on the current page have intersection points is judged, and five conditions are adopted. Fig. 5 is a schematic diagram of a line segment intersection situation provided in the embodiment of the present application, and as shown in fig. 5, five cases where there are intersections between two line segments include:
case 1 is a case where the horizontal line segment and the vertical line segment do not intersect or where one line segment has been determined to intersect with other line segments and is set in the set of intersecting line segments, and the determination of the intersection point of the subsequent horizontal and vertical lines continues to be circulated regardless of the resetting of the two line segments to the set of intersecting line segments, and still remains in the original set of line segments.
Case 2 is that the horizontal line segment and the vertical line segment intersect, and are not included in other intersecting line segment sets, so that two line segments that intersect in case 2 need to be arranged in the intersecting line segment set, and the set is guaranteed to be formed by the intersecting line segments.
Case 3 is where the vertical line segment does not belong to any set of line segments and the horizontal line segment belongs to a set of line segments, then the vertical line segment is placed in the set of horizontal line segments.
Case 4 is where the vertical line segment belongs to a set of line segments and the horizontal line segment does not belong to any set of line segments, then the horizontal line segment is set to the set of vertical line segments.
Case 5 is where the vertical and horizontal line segments intersect and are in different sets of line segments, then the two sets of line segments where the vertical and horizontal line segments are located are merged.
The line segment set shown in fig. 5 may be considered that a plurality of line segments located under the same X-axis coordinate form a vertical line segment set, a plurality of line segments located under the same Y-axis coordinate form a horizontal line segment set, and the coordinates of the line segments in each set are used to determine whether to intersect, and the intersecting line segments are set in the intersecting line segment set.
After the intersection point judgment processing, one or more sets of the intersected line segments can be obtained, and the sets are independent line segment sets without intersection points. After traversing all the horizontal lines and the vertical lines, classifying according to whether the line segments are intersected or not and according to whether the line segments are intersected or not, generating N sets, wherein the line segments in the sets are intersected, and the line segments between the sets are not intersected. The disjoint set of these segments is the predecessor of the nested tables to be extracted.
And (5) extracting a set of crossed line segment intersections.
Traversing the set of all the intersections forms a rectangular table. The composition of the rectangle (upper left coordinates [ x1, y1], lower right coordinates [ x2, y2]) are four separate line segments: ([ x1, y1], [ x2, y1]), ([ x1, y2], [ x2, y2]), ([ x1, y1], [ x1, y2]), ([ x2, y1], [ x2, y2]), and whether or not these line segments exist is determined, and if they exist, it is determined that a rectangle is formed.
And screening out all intersection point sets with the rectangular areas larger than a third set threshold. The third threshold may be set according to the outline size of the text, that is, each table unit in the table can at least put the next smallest text, or at least put the next normal text, so that the rectangular frame with an excessively small area will be an error table due to a line segment analysis error.
The method comprises the steps of utilizing a set of rectangles to calculate form information, synthesizing extra-form data for analysis according to obtained coordinate information of the rectangles, calculating a cell width and height list, calculating nesting relations among the forms, determining whether nested forms exist in the forms or not based on the nesting relations, setting rectangular region borders and merging cells for the form units with nested structures based on a mode that the areas are from small to large, and generating nested form format information.
According to the coordinate information of the text, merging of the text and calculation of the outline of the text are performed, where the coordinate of the text is the coordinate value of the upper left of the character, and after processing, the position information of the character text box is obtained, as shown in fig. 6, and the square part in the figure represents the character text box.
And calculating the relationship of the nested tables, and performing sorting processing according to the relationship of the small area of the tables to the large area of the tables, wherein the sorting of the areas of the tables is used for conveniently recording the text information and the picture information.
Text and picture information within the table region is calculated. And recording the information of the characters and the pictures from the smallest area in the nested table. The matching of the text content of the nested child tables in the table structure to the parent table is avoided.
And calculating the structure information of the nested table and storing the structure information into the page table information structure.
And circulating the processes to analyze the data information of all the pages of the whole PDF file.
And writing the information according to all the analyzed information of the PDF file. The text pictures outside the table are data of one type, the data can be stored in a related text file or can be copied into other electronic equipment, and the data interface in the table is data of another type, and can be written into an Excel file step by step.
In the embodiment of the application, the analysis of attribute information such as the size of characters, whether colors are thickened and the like in the analysis table of character attributes can be conveniently written into corresponding rows and columns and text attribute values can be set.
The method and the device for extracting the nested table data can accurately restore the table effect in the file, and the nested table data can be effectively extracted and restored. Fig. 7 is a schematic diagram of a table extraction and reduction effect in a PDF according to an embodiment of the present application, and as shown in fig. 7, a schematic diagram of a display effect of extracting and reducing a table in a PDF into an Excel is shown.
According to the method and the device, the line segment information is extracted from the data content in the analyzed non-editable text, the line segments are merged based on the coordinate positions of the line segments, the intersection points between the merged line segments are determined, the corresponding table structure is determined and generated based on the intersection point information, and the corresponding characters or pictures are filled in the regenerated table structure according to the coordinate information of the analyzed text, so that the non-editable character text and the table content can be extracted, for example, the extracted table content can be imported into an Excel file according to the table structure, and the like. The embodiment of the application improves the accurate identification of the table in the non-editable text, can be directly used without redrawing, greatly facilitates the use of a user, improves the identification accuracy of the non-editable text, and greatly lightens the editing work of the user.
Fig. 8 is a schematic structural diagram of a composition of an extracting apparatus of a nested table provided in an embodiment of the present application, and as shown in fig. 8, the extracting apparatus of the nested table of the embodiment of the present application includes:
a reading and analyzing unit 80, configured to read and analyze data content in the first type file;
a first determining unit 81, configured to determine line segment coordinate information included in the analyzed data content;
a grouping unit 82, configured to group the line segments with reference to the presentation unit of the first type file;
the first merging unit 83 is configured to merge, for line segments in each group, line segments whose lateral or longitudinal distance between adjacent or adjacent line segments is smaller than a first set threshold and whose adjacent endpoint interval is smaller than a second set threshold based on coordinates of the line segments;
a set generating unit 84, configured to traverse all the merged segments, classify according to whether the segments intersect, and generate N sets, where the segments in the sets intersect, and the segments between the sets do not intersect; n is an integer greater than or equal to 1;
a second determining unit 85, configured to traverse line segments in the set, determine intersections between the merged intersecting line segments, traverse all the intersections, and determine intersections that form the rectangular frame;
a generating unit 86 configured to generate a table based on an intersection point at which the area of the rectangular frame is larger than a third threshold;
a forming unit 87, configured to calculate an inclusion relationship between the tables based on the generated tables, and form a nested table based on the inclusion relationship of the tables. .
As an implementation manner, on the basis of the extraction apparatus of the nested table shown in fig. 8, the extraction apparatus of the nested table according to the embodiment of the present application further includes:
an acquiring unit (not shown in fig. 8) configured to acquire text information included in the parsed data content and coordinate information of the text;
a second merging unit (not shown in fig. 8) configured to merge texts according to coordinate information of the texts and calculate text outlines;
a third determining unit (not shown in fig. 8) configured to determine text and/or picture information located in the table area according to coordinate information of an outline of a text and/or picture coordinate information included in the parsed data content;
a filling unit (not shown in fig. 8) for filling the determined text and/or picture in the corresponding area of the table.
As an implementation manner, the forming unit 87 is further configured to:
according to the coordinate information of the rectangular frame, integrating the data outside the table for analysis, and calculating a basic cell width and height list;
based on the basic cell width and height list, calculating the nesting relation between the tables, and setting rectangular area borders and merging cells according to the sequence of the rectangular area from small to large to generate a nested table format.
As an implementation manner, on the basis of the extraction apparatus of the nested table shown in fig. 8, the extraction apparatus of the nested table according to the embodiment of the present application further includes:
and a storage unit (not shown in fig. 8) configured to store text information outside the table in the first type file, and store the table and text and/or pictures in the table as table data.
As an implementation manner, the second determining unit 84 is further configured to:
dividing transverse line segments contained in the analyzed data content into a first set, dividing longitudinal line segments into a second set, and sequencing the line segments in the first set and the second set according to line segment coordinates;
and determining all line segments with intersection points for any line segment in the first set or the second set, and forming an intersection point set by intersection points formed among the line segments with the intersection points.
In an exemplary embodiment, each of the Processing units of the extraction apparatus of the nested table of the embodiment of the present Application may be implemented by one or more Central Processing Units (CPUs), Graphics Processing Units (GPUs), Baseband Processors (BPs), Application Specific Integrated Circuits (ASICs), DSPs, Programmable Logic Devices (PLDs), Complex Programmable Logic Devices (CPLDs), Field Programmable Gate Arrays (FPGAs), general purpose processors (GPUs), controllers, Micro Controllers (MCUs), microprocessors (microprocessors), or other electronic elements.
In the embodiment of the present disclosure, the specific manner in which each processing unit in the extraction apparatus of the nested table shown in fig. 8 performs operations has been described in detail in the embodiment related to the method, and will not be elaborated here.
The embodiment of the application also describes a storage medium, on which an executable program is stored, and the executable program realizes the steps of the extraction method of the nested table of the embodiment when being executed by a processor.
It should be appreciated that reference throughout this specification to "one embodiment" or "an embodiment" means that a particular feature, structure or characteristic described in connection with the embodiment is included in at least one embodiment of the present invention. Thus, the appearances of the phrases "in one embodiment" or "in an embodiment" in various places throughout this specification are not necessarily all referring to the same embodiment. Furthermore, the particular features, structures, or characteristics may be combined in any suitable manner in one or more embodiments. It should be understood that, in various embodiments of the present invention, the sequence numbers of the above-mentioned processes do not mean the execution sequence, and the execution sequence of each process should be determined by its function and inherent logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention. The above-mentioned serial numbers of the embodiments of the present invention are merely for description and do not represent the merits of the embodiments.
It should be noted that, in this document, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises the element.
In the several embodiments provided in the present application, it should be understood that the disclosed apparatus and method may be implemented in other ways. The above-described device embodiments are only illustrative, for example, the division of the unit is only one logical function division, and there may be other division ways in actual implementation, such as: multiple units or components may be combined, or may be integrated into another system, or some features may be omitted, or not implemented. In addition, the coupling, direct coupling or communication connection between the components shown or discussed may be through some interfaces, and the indirect coupling or communication connection between the devices or units may be electrical, mechanical or other forms.
The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units; can be located in one place or distributed on a plurality of network units; some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, all the functional units in the embodiments of the present invention may be integrated into one processing unit, or each unit may be separately regarded as one unit, or two or more units may be integrated into one unit; the integrated unit can be realized in a form of hardware, or in a form of hardware plus a software functional unit.
The above description is only an embodiment of the present invention, but the scope of the present invention is not limited thereto, and any person skilled in the art can easily conceive of changes or substitutions within the technical scope of the present invention, and all such changes or substitutions are included in the scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the appended claims.