Method and device for identifying code segment in document
By identifying the continuity, font and code characteristics of the area to be identified in the document, the problem of difficult to automatically identify the document code segment in the prior art is solved, and high-accurate code segment recognition is achieved.
Patent Information
- Application Number
- CN202311095647.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-08-28
- Publication Date
- 2025-02-28
AI Technical Summary
It is difficult to automatically identify code segments in documents, especially when avoiding malicious code cyberattacks.
By determining the area to be identified in the target document, text line association detection, font recognition, fixed width attributes and unified code recognition are performed to determine whether the area to be identified has continuity, font and code characteristics.
It realizes the rapid and accurate identification of code segments in a document, avoids identifying two connected code segments as one, and improves the accuracy of code segment recognition.
Smart Images

Figure CN119540974A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of document recognition, and in particular to a method and device for recognizing code segments in a document. Background Art
[0002] With the rapid development of computer technology, various documents have been developed. Some documents usually contain a part of code segments, such as documents introducing computer languages, and documents carrying malicious codes used for network attacks.
[0003] Based on the needs of automatically adjusting the format of code segments in documents or avoiding network attacks by malicious codes in documents, how to automatically identify code segments in documents is a problem that needs to be solved urgently. Summary of the invention
[0004] In view of the problems existing in the prior art, an embodiment of the present invention provides a method and device for identifying code segments in a document.
[0005] The present invention provides a method for identifying code segments in a document, comprising:
[0006] Determine a region to be identified in a target document, wherein the region to be identified includes at least one connected text line;
[0007] Performing text line association detection on each of the text lines to obtain a detection result of whether the to-be-identified area has a continuity feature, wherein the continuity feature represents feature continuity between the text lines;
[0008] Recognize the font of the text object in each of the text lines to determine whether the area to be recognized has a first recognition result of the font feature;
[0009] Identify the fixed width attribute and the Unicode of the text object in each of the text lines to determine a second identification result of whether the area to be identified has a code feature;
[0010] According to the detection result, the first recognition result and the second recognition result, it is determined whether the to-be-recognized area in the target document is a code segment.
[0011] According to a method for identifying code segments in a document provided by the present invention, the fixed-width attribute and the Unicode of the text object in each of the text lines are identified, and the second identification result of determining whether the area to be identified has a code feature includes:
[0012] Identifying fixed-width attributes and Unicodes corresponding to each text object in each of the text lines;
[0013] Determine whether each of the text lines meets the code feature condition, wherein the code feature condition is that the fixed-width attributes corresponding to each text object in the text line all have a fixed width, and each of the Unicodes corresponding to each text object in the text line belongs to a set interval;
[0014] If so, determining that the second recognition result is that the area to be recognized has the code feature;
[0015] If not, it is determined that the second recognition result is that the area to be recognized does not have the code feature.
[0016] According to a method for identifying code segments in a document provided by the present invention, the font features include font size features and font cluster features, and the first recognition result includes a font size recognition result and a font cluster recognition result;
[0017] Accordingly, the step of identifying the font of the text object in each of the text lines and determining whether the area to be identified has a first identification result of the font feature includes:
[0018] For each text line, identify the font size of each text object in the text line, and determine a first average font size corresponding to the text line according to each font size; determine the font size recognition result of whether the area to be recognized has the font size feature according to each first average font size;
[0019] Identify the font cluster of the first text object in each text line; and determine, based on each font cluster, whether the area to be identified has the font cluster feature of the font cluster identification result.
[0020] According to a method for identifying code segments in a document provided by the present invention, the font size recognition result of determining whether the to-be-identified area has the font size feature according to each of the first average font sizes includes:
[0021] determining a ratio of a maximum average font size to a minimum average font size among the first average font sizes;
[0022] When the ratio is less than or equal to the set value, determining the font size recognition result as the area to be recognized having the font size feature;
[0023] When the ratio is greater than the set value, it is determined that the font size recognition result is that the area to be recognized does not have the font size feature.
[0024] According to a method for identifying code segments in a document provided by the present invention, the text line association detection includes content type detection and text line type detection, the continuity feature includes content type feature and text line type feature, and the detection result includes content type detection result and text line type detection result;
[0025] Correspondingly, the performing text line association detection on each of the text lines to obtain a detection result of whether the to-be-recognized area has a continuity feature includes:
[0026] Performing content type detection on each of the text lines to obtain the content type detection result of whether the area to be identified has the content type feature;
[0027] A text line type detection is performed on each of the text lines to obtain the text line type detection result of whether the area to be identified has the text line type feature.
[0028] According to a method for identifying code segments in a document provided by the present invention, the content type detection includes blank line detection and picture line detection, the content type feature includes blank line feature and picture line feature, and the content type detection result includes blank line detection result and picture line detection result;
[0029] Correspondingly, the performing content type detection on each of the text lines to obtain the content type detection result of whether the area to be identified has the content type feature includes:
[0030] Performing blank line detection on each of the text lines to determine whether each of the text lines is a blank line without text content; in the case where there is a blank line in each of the text lines, determining that the blank line detection result is that the area to be identified has the blank line feature; in the case where there is no blank line in each of the text lines, determining that the blank line detection result is that the area to be identified does not have the blank line feature;
[0031] Perform image row detection on each of the text lines to determine whether each of the text lines is a picture row; when a picture row exists in each of the text lines, determine that the picture row detection result is that the area to be identified has the picture row feature; when no picture row exists in each of the text lines, determine that the picture row detection result is that the area to be identified does not have the picture row feature.
[0032] According to a method for identifying code segments in a document provided by the present invention, the text line type detection includes writing direction detection, line intersection detection, font style detection and line spacing detection, the text line type features include writing direction features, line intersection features, font style features and line spacing features, and the text line type detection results include writing direction detection results, line intersection detection results, font style detection results and line spacing detection results;
[0033] Correspondingly, the performing text line type detection on each of the text lines to obtain the text line type detection result of whether the to-be-identified area has the text line type feature includes:
[0034] Perform writing direction detection on each of the text lines to determine the writing direction of each of the text lines; when the writing directions are the same, determine that the writing direction detection result is that the area to be identified has the writing direction feature; when the writing directions are different, determine that the writing direction detection result is that the area to be identified does not have the writing direction feature;
[0035] Performing a line intersection detection on each text line pair to determine whether the projections of the text lines in each text line pair in the line direction have an intersection, wherein the text line pair is two connected text lines in each text line; if not, determining that the line intersection detection result is that the area to be identified does not have the line intersection feature;
[0036] Performing font style detection on each of the text line pairs to determine whether each of the text line pairs meets the font style feature condition, wherein the font style feature condition is that there is an intersection in the font thickness ranges between the text lines in the text line pairs, and the italic attributes between the text lines are the same; if so, determining that the font style detection result is that the area to be identified has the font style feature; if not, determining that the font style detection result is that the area to be identified does not have the font style feature;
[0037] Perform line spacing detection on each of the text line pairs to determine the line spacing between the text lines in each of the text line pairs; and determine, based on each of the line spacings, whether the area to be identified has the line spacing feature of the line spacing detection result.
[0038] According to a method for identifying code segments in a document provided by the present invention, the line spacing detection result of determining whether the area to be identified has the line spacing feature according to each line spacing includes:
[0039] Determine an average line spacing of each of the line spacings, and calculate a second average font size of all text objects in each of the text lines;
[0040] Determine whether all the line spacings are target line spacings, the target line spacing is less than or equal to a first set multiple of the second average font size, and the absolute difference between the target line spacing and the average line spacing is less than or equal to a second set multiple of the second average font size, and the first set multiple is greater than the second set multiple;
[0041] If so, determining that the line spacing detection result is that the area to be identified has the line spacing feature;
[0042] If not, it is determined that the line spacing detection result is that the area to be identified does not have the line spacing feature.
[0043] According to a method for identifying code segments in a document provided by the present invention, after performing line intersection detection on each text line pair and determining whether the projections of the text lines in each text line pair in the line direction have an intersection, the method further includes:
[0044] If so, determine whether there is a target intersection in each of the intersections, the length of the target intersection is greater than or equal to a third set multiple of the projection length of the target text line, and the target text line is any text line corresponding to the target intersection;
[0045] If so, determining the row intersection detection result is that the to-be-identified area has the row intersection feature;
[0046] If not, it is determined that the row intersection detection result is that the to-be-identified area does not have the row intersection feature.
[0047] According to a method for identifying code segments in a document provided by the present invention, determining whether the to-be-identified area in the target document is a code segment according to the detection result, the first recognition result and the second recognition result includes:
[0048] comparing the detection result, the first recognition result and the second recognition result with set code recognition conditions respectively;
[0049] When the comparison result is that both are consistent, it is determined that the area to be identified in the target document is a code segment.
[0050] According to a method for identifying code segments in a document provided by the present invention, after comparing the detection result, the first recognition result and the second recognition result with set code recognition conditions respectively, the method further includes:
[0051] If the comparison result shows that there is a mismatch, determining that the area to be identified in the target document is not a code segment, and obtaining the number of text lines included in the area to be identified;
[0052] When the number of rows is not equal to the first preset value, subtract the second preset value from the number of rows to obtain an updated number of rows;
[0053] Determine each target sub-region in the to-be-recognized region, wherein the target sub-region contains the updated number of connected text lines;
[0054] The target sub-regions are respectively used as updated regions to be identified, and the step of performing text line association detection on the text lines is continued to obtain a detection result of whether the regions to be identified have continuity features.
[0055] The present invention also provides a device for identifying code segments in a document, comprising:
[0056] A first determination module is configured to determine a region to be identified in a target document, wherein the region to be identified includes at least one connected text line;
[0057] A detection module is configured to perform text line association detection on each of the text lines to obtain a detection result of whether the to-be-identified area has a continuity feature, wherein the continuity feature represents the feature continuity between the text lines;
[0058] A first recognition module is configured to recognize the font of the text object in each of the text lines and determine whether the area to be recognized has a first recognition result of the font feature;
[0059] A second recognition module is configured to recognize the fixed width attribute and the Unicode of the text object in each of the text lines, and determine a second recognition result of whether the area to be recognized has a code feature;
[0060] The second determination module is configured to determine whether the to-be-recognized area in the target document is a code segment according to the detection result, the first recognition result and the second recognition result.
[0061] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the method for identifying code segments in a document as described in any one of the above is implemented.
[0062] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the method for identifying a code segment in a document as described in any one of the above is implemented.
[0063] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the computer program implements any of the above methods for identifying code segments in a document.
[0064] The method and device for identifying code segments in a document provided by the present invention determine an area to be identified in a target document, wherein the area to be identified includes at least one connected text line; perform text line association detection on each of the text lines to obtain a detection result of whether the area to be identified has a continuity feature, wherein the continuity feature represents the feature continuity between the text lines; identify the font of the text object in each of the text lines to determine a first recognition result of whether the area to be identified has a font feature; identify the fixed-width attribute and Unicode of the text object in each of the text lines to determine a second recognition result of whether the area to be identified has a code feature; determine whether the area to be identified in the target document is a code segment based on the detection result, the first recognition result and the second recognition result, so as to quickly and accurately identify a number of code segments existing in each page of the document, avoid identifying two connected code segments as one code segment, and improve the accuracy of code segment recognition. BRIEF DESCRIPTION OF THE DRAWINGS
[0065] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0066] Figure 1 It is a flowchart of the method for identifying code segments in a document provided by the present invention;
[0067] Figure 2 It is a schematic diagram of the effect of the method for identifying code segments in a document provided by the present invention;
[0068] Figure 3 It is a schematic diagram of the structure of the device for identifying code segments in a document provided by the present invention;
[0069] Figure 4 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION
[0070] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0071] First, the terms involved in the present invention are explained.
[0072] Streaming documents refer to documents that can be freely edited, mainly including Word documents and TXT documents, etc. Layout documents refer to non-editable documents, that is, documents with fixed layouts, mainly including PDF (Portable Document Format) documents and OFD (Open Fixed-layout Document) documents, etc.
[0073] The text object is the basic element in the document. For layout documents, such as PDF, it refers to TextObject, and for streaming documents, such as Word documents, it refers to TextContainer. The text object contains the character code (CharCode) of the text content in the font, the font (Font), the character external coordinate box (CBox), and the unified character encoding Unicode.
[0074] A font is a set of visual representations of characters, including their appearance, style, and arrangement. A font is made up of glyphs, which define how characters are drawn, including information such as the character's outline, curves, and strokes. By combining glyphs, specific text can be formed. Each glyph represents a character in the font. There are many properties of a font that can be adjusted and set, including but not limited to the font family or cluster, font style, font size, font weight, letter spacing, line height, and fixed-pitch.
[0075] A font family or cluster is a category or classification of fonts that specifies that text should use a certain font or font sequence, such as Songti, Arial, Times New Roman, and Kaiti. Font styles include Regular, Italic, and Bold. Font size determines the display size of characters. Font weight specifies the thickness of characters. Character spacing refers to the horizontal spacing adjustment between characters. Line height refers to the vertical spacing adjustment between lines, etc. Fixed width means that the characters in a font have the same fixed width, regardless of the actual width of the characters.
[0076] Writing Mode is used to describe the direction of text layout, mainly including LrTb, RlTb and TbRl. Among them, LrTb means that the line direction is from left to right and the paragraph direction is from top to bottom. The writing direction of most documents is LrTb; RlTb means that the line direction is from right to left and the paragraph direction is from top to bottom, which is usually Arabic or Hebrew, that is, it is generally used in writing systems such as Arabic and Hebrew; TbRl means that the line direction is from top to bottom and the paragraph direction is from right to left, which is often used in writing systems such as Chinese (classical), Japanese and Mongolian. In addition, although the writing direction used for Mongolian is TbLr, there are only three types of LrTb, RlTb and TbRl in the PDF standard, so the writing direction of Mongolian is regarded as TbRl in PDF.
[0077] The paragraph direction refers to the direction in which the next line should be typeset after the current line is typeset. For example, in the common LrTb writing direction, the paragraph direction is from top to bottom and the line direction is from left to right.
[0078] Then, combine Figure 1-Figure 3 The present invention describes a method and device for identifying code segments in a document.
[0079] Figure 1 is a flow chart of the method for identifying code segments in a document provided by the present invention, see Figure 1 As shown, it includes steps 101 to 105, wherein:
[0080] Step 101: Determine a region to be recognized in a target document, wherein the region to be recognized includes at least one connected text line.
[0081] First of all, it should be noted that the execution subject of the present invention can be any electronic device that can identify code segments in documents, such as a smart phone, a smart watch, a desktop computer, a laptop computer, etc. Secondly, the method for identifying code segments in documents provided by the present invention can be applied to scenarios such as layout document conversion, layout document rearrangement, layout document editing, and streaming document display.
[0082] Specifically, the target document refers to a document that needs to be identified whether there is a code segment, which can be a layout document, a streaming document, a presentation document such as a slide document, or a picture document, and the present invention does not impose any limitation on this. The area to be identified refers to the area in the target document where the code segment is identified whether there is a code segment, which can be the entire display area of the target document, that is, all display areas, a specified partial area in the target document, or an area corresponding to the currently displayed page in the target document, and the present invention does not impose any limitation on this. A text line refers to a line in a document used to display content.
[0083] In practical applications, before determining the area to be identified in the target document, the target document must be acquired first. There are many methods for acquiring the target document, and the present invention does not limit this.
[0084] Exemplarily, a user uploads a target document through a document upload page, and accordingly, the execution subject obtains the target document.
[0085] Exemplarily, the execution subject receives a document acquisition instruction or a code segment identification instruction, and accordingly, the execution subject acquires the target document from the storage area pointed to by the document acquisition instruction or the code segment identification instruction.
[0086] On the basis of acquiring the target document, further, the area to be identified in the target document is determined: the entire display area of the target document can be used as the area to be identified; when a selection instruction is received, the area of the target document mapped with the selection instruction is used as the area to be identified.
[0087] It should be noted that before determining the area to be identified in the target document, it also includes: identifying the document type of the target document. If the document type is a layout document, such as PDF, then the text line and writing direction of the text object are identified according to the position of the text object in the target document, that is, a judgment is made, for example, using a line identifier to identify the text line and writing direction of the text object; or the document type of the layout document is converted into a streaming document. If it is a streaming document or a presentation document, the step of determining the area to be identified in the target document is executed. If it is a picture document, the optical character recognition (OCR) technology is used to extract the text content in the picture document, and a target document of the streaming document type is generated according to the text content. In this way, it is possible to ensure that the code segments for document recognition of different document types are recognized, thereby improving the scope of application of code segment recognition and the flexibility of code segment recognition.
[0088] Step 102: Perform text line association detection on each of the text lines to obtain a detection result of whether the area to be identified has a continuity feature, where the continuity feature represents the feature continuity between the text lines.
[0089] Specifically, text line association detection refers to the detection of whether the text lines in the area to be identified are associated, that is, the detection of whether the text lines in the area to be identified have continuity features. The continuity feature represents the continuity of features between the text lines. The detection result refers to the result of the text line association detection, that is, the result of whether the area to be identified has continuity features.
[0090] In practical applications, after the area to be identified is determined, text line association detection is performed on each text line in the area to be identified. For example, a line identifier can be used to perform text line association detection to obtain the detection result of the area to be identified, that is, to determine whether the area to be identified has continuity features.
[0091] Step 103: Identify the font of the text object in each of the text lines to determine whether the area to be identified has a first identification result of the font feature.
[0092] Specifically, the font feature indicates that the fonts of the text objects in the to-be-recognized area are relatively consistent. The first recognition result refers to the result of recognizing the fonts of the text objects in the text line, that is, the result of whether the to-be-recognized area has the font feature.
[0093] In actual applications, after the area to be identified is determined, the fonts of the text objects in each text line can be identified. For example, a line identifier can be used to identify the font size of the text objects in each text line, and the font clusters of the text objects in each text line can be identified. Based on the recognition results, the first recognition result of the area to be identified is determined, that is, whether the area to be identified has font features.
[0094] Step 104: Identify the fixed-width attribute and the Unicode of the text object in each of the text lines to determine a second recognition result of whether the area to be recognized has a code feature.
[0095] Specifically, the fixed width attribute refers to the attribute corresponding to the fixed width, which is a binary attribute, and has two types: "yes" (having the fixed width attribute) and "no" (not having the fixed width attribute). Code characteristic refers to the fact that the text object in the area to be identified has some of the same attributes as the code. The second recognition result refers to the result of identifying the fixed width attribute and the Unicode of the text object in the text line, that is, the result of whether the area to be identified has the code characteristic.
[0096] In actual applications, after determining the area to be identified, the fixed-width attributes and Unicode of the text objects in each text line can be identified. For example, a line identifier can be used to identify the fixed-width attributes of the text objects in each text line, and to identify the Unicode of the text objects in each text line, thereby obtaining a second recognition result, i.e., determining whether the area to be identified has code features.
[0097] It should be noted that the processes corresponding to step 102 to step 104 can be executed synchronously or in a set order, and the present invention does not limit this. For ease of understanding, the present invention takes the order of step 102 to step 104 as an example for explanation.
[0098] Step 105: Determine whether the to-be-recognized area in the target document is a code segment according to the detection result, the first recognition result, and the second recognition result.
[0099] Specifically, a code segment refers to a continuous section of code or program code.
[0100] In practical applications, on the basis of determining the detection result, the first recognition result and the second recognition result, it can be determined whether the area to be recognized is a code segment according to the detection result, the first recognition result and the second recognition result.
[0101] Exemplarily, the detection result, the first recognition result and the second recognition result are respectively compared with their corresponding set results or conditions. If they are the same, the area to be recognized in the target document is determined to be a code segment. If there are differences, it is determined that the area to be recognized in the target document is not a code segment.
[0102] The method for identifying code segments in a document provided by the present invention determines an area to be identified in a target document, wherein the area to be identified includes at least one connected text line; performs text line association detection on each of the text lines to obtain a detection result of whether the area to be identified has a continuity feature, wherein the continuity feature represents the feature continuity between the text lines; identifies the font of the text object in each of the text lines to determine a first recognition result of whether the area to be identified has a font feature; identifies the fixed-width attribute and the Unicode of the text object in each of the text lines to determine a second recognition result of whether the area to be identified has a code feature; determines whether the area to be identified in the target document is a code segment based on the detection result, the first recognition result and the second recognition result, thereby being able to quickly and accurately identify a number of code segments existing in each page of the document, avoid identifying two connected code segments as one code segment, and improve the accuracy of code segment recognition.
[0103] Identifying code segments in a document can facilitate special processing in subsequent document display, such as code highlighting, not re-arranging codes in layout documents, etc. It is also convenient to perform automatic indentation functions when editing documents, such as the reader can highlight key elements in the code segment area; the code segment can be treated as a whole in the PDF re-formatted version; it can be converted into a text style suitable for reading and editing in HTML-based documents such as Docx. In addition, the method for identifying code segments in a document provided by the present invention is applicable to layout documents (such as PDF) and streaming documents.
[0104] In one or more optional embodiments of the present invention, the fixed width attribute and the Unicode of the text object in each of the text lines are identified to determine whether the area to be identified has a second identification result of the code feature, and the specific implementation process may be as follows:
[0105] Identifying fixed-width attributes and Unicodes corresponding to each text object in each of the text lines;
[0106] Determine whether each of the text lines meets the code feature condition, wherein the code feature condition is that the fixed-width attributes corresponding to each text object in the text line all have a fixed width, and each of the Unicodes corresponding to each text object in the text line belongs to a set interval;
[0107] If so, determining that the second recognition result is that the area to be recognized has the code feature;
[0108] If not, it is determined that the second recognition result is that the area to be recognized does not have the code feature.
[0109] Specifically, the set interval segment may be a standard interval segment, that is, an interval segment defined by the Unicode standard, such as a Common / Latin interval segment, or may be an interval segment preset by a user.
[0110] In practical applications, for each text line in the area to be identified, the font corresponding to each text object in the text line is identified, and the fixed width attribute and Unicode of each text object in the text line can be obtained according to the font. Then, it is determined whether the text line meets the code feature conditions, that is, it is determined whether the fixed width attributes corresponding to the text line are all yes (with a fixed width), and whether the Unicodes corresponding to the text line belong to the set interval segment: if not, the text line has the code feature condition, that is, the second recognition result is that the area to be identified does not have the code feature; if so, the text line has the code feature condition, and the next text line is determined. When the fixed widths corresponding to each text line are all yes (with a fixed width) and the Unicodes corresponding to each text line belong to the set interval segment, that is, when each text line meets the code feature condition, the second recognition result is determined as the area to be identified has the code feature.
[0111] Exemplarily, if among all the given text lines (all the text lines in the area to be identified), there is any text line whose TextObject's Font does not have a fixed width or whose Unicode does not belong to the Common / Latin interval, then the second recognition result isCodeCharacteristic (code feature) = False (does not have code feature), otherwise the second recognition result is True (has code feature).
[0112] It should be noted that some fonts have a fixed width, such as Song typeface, Kai typeface, Courier New, Consolas, and Menlo, etc. Exemplarily, the characters under the Song typeface all have a fixed width, that is, the widths corresponding to different characters under the Song typeface are the same. For example, the fixed height and width corresponding to "one" and "Xi" under the Song typeface are the same. Another part of the fonts do not have a corresponding fixed width, such as Times New Roman. Exemplarily, the widths of "i" and "a" in "characteristic" under the Times New Roman font are different. Usually in a programming environment or when writing code, fonts with a fixed width are generally selected, such as Courier New, Consolas, or Menlo, etc. Therefore, determining whether the area to be recognized has code characteristics through the fixed-width attribute and Unicode is more reliable, thereby improving the efficiency of code recognition.
[0113] In one or more alternative embodiments of the present invention, the font includes a font size and a font family, the font feature includes a font size feature and a font family feature, and the first recognition result includes a font size recognition result and a font family recognition result; correspondingly, the process of recognizing the fonts of the text objects in each of the text lines to determine the first recognition result of whether the area to be recognized has a font feature can be specifically implemented as follows:
[0114] Recognize the font sizes of the text objects in each of the text lines to determine the font size recognition result of whether the area to be recognized has the font size feature;
[0115] Recognize the font families of the text objects in each of the text lines to determine the font family recognition result of whether the area to be recognized has the font family feature.
[0116] Specifically, the font size feature, that is, the ValidFontSize, characterizes that the font sizes corresponding to the area to be recognized are relatively consistent. The font family feature, that is, the SameFontFamily, characterizes that the font families corresponding to the area to be recognized are relatively consistent. The font size recognition result refers to the result of recognizing the font sizes of the text objects in the text line, that is, the result of whether the area to be recognized has the font size feature. The font family recognition result refers to the result of recognizing the font families of the text objects in the text line, that is, the result of whether the area to be recognized has the font family feature.
[0117] In actual applications, for each text line, the font size of the text object in the text line is identified, and then based on the font size of each text object in each text line, it is determined whether the area to be identified has font size characteristics, that is, the font size recognition result is determined. At the same time, for each text line, the font cluster of the text object in the text line is identified, and then based on the font cluster of the text object in each text line, it is determined whether the area to be identified has font cluster characteristics, that is, the font cluster feature result is determined. In this way, whether the area to be identified has font characteristics is determined in a more fine-grained manner from the two dimensions of font size and font cluster, thereby improving the accuracy and reliability of the first recognition result.
[0118] Optionally, the process of identifying the font cluster of the text object in each of the text lines and determining whether the to-be-identified area has the font cluster feature as the font cluster identification result may be specifically implemented as follows:
[0119] identifying a font cluster of a first said text object in each said text line;
[0120] According to each of the font clusters, it is determined whether the area to be identified has the font cluster identification result having the font cluster feature.
[0121] In practical applications, for each text line, the font of the first text object in the text line is identified to obtain the font cluster of the first text object.
[0122] Furthermore, it is determined whether the font clusters of the first text object in each text line are all equal, that is, whether the Font Family attribute in the Font attribute of the first text object is the same. If so, the font cluster recognition result is determined to be that the area to be recognized has font cluster characteristics; if not, the font cluster recognition result is determined to be that the area to be recognized does not have font cluster characteristics. Exemplarily, if the Font Family of the Font of the first TextObject in each text line in all given lines (all text lines in the area to be recognized) is equal, such as they are all Songti clusters, then the font cluster recognition result isSameFontFamily (font cluster characteristics) = True (having font cluster characteristics), otherwise it is False (not having font cluster characteristics).
[0123] In addition, the maximum number of first text objects with the same font cluster can be counted, and the ratio of the maximum number to the text lines in the area to be identified can be determined. When the ratio is greater than a set threshold, the font cluster identification result is determined to be that the area to be identified has font cluster characteristics. When the ratio is less than or equal to the set threshold, the font cluster identification result is determined to be that the area to be identified does not have font cluster characteristics.
[0124] In this way, only the font cluster of the first text object in each text line is identified, and there is no need to identify the font clusters of all text objects, which greatly reduces the amount of data processing and thereby improves the efficiency of determining the font cluster identification results.
[0125] Optionally, the font clusters of the text objects in each of the text lines are identified, and the font cluster identification result of determining whether the area to be identified has the font cluster characteristics can be: identifying the font clusters of each of the text objects in each of the text lines; and determining, based on each of the font clusters, whether the area to be identified has the font cluster characteristics.
[0126] In practical applications, for each text line, the font of each text object in the text line is identified, and the font cluster of each text object in the text line can be obtained.
[0127] Then, for each text line, determine the target font cluster from the font clusters of each text object, and the target font cluster is the font cluster with the largest number of text lines; determine whether the target font clusters corresponding to each text line are the same or equal, if so, determine the font cluster recognition result as the area to be recognized has font cluster characteristics, if not, determine the font cluster recognition result as the area to be recognized does not have font cluster characteristics. Alternatively, count the maximum number of text lines with the same target font cluster, determine the ratio of the maximum number to the text lines in the area to be recognized, if the ratio is greater than a set threshold, determine the font cluster recognition result as the area to be recognized has font cluster characteristics, if the ratio is less than or equal to the set threshold, determine the font cluster recognition result as the area to be recognized does not have font cluster characteristics.
[0128] In this way, by identifying the font cluster of each text object in each text line, the font cluster recognition result is determined from a full dimension, which is conducive to improving the accuracy of the font cluster recognition result.
[0129] In an achievable embodiment of the present invention, the font sizes of the text objects in each of the text lines are identified, and the font size recognition result of determining whether the area to be identified has the font size feature can be: for each text line, the font size of each text object in the text line is identified; the majority font size in each of the font sizes is determined; and based on the majority font size corresponding to each of the text lines, the font size recognition result of determining whether the area to be identified has the font size feature.
[0130] Specifically, the majority font size is the font size with the largest number among the font sizes of the text objects.
[0131] In practical applications, for each text line, the font of each text object in the text line is identified, and the font size of each text object in the text line can be obtained. Then, for each text line, the majority font size is determined from the font size of each text object. Further, it is determined whether the majority font size corresponding to each text line is the same or equal. If so, the font size recognition result is determined to be that the area to be recognized has the font size feature. If not, the font size recognition result is determined to be that the area to be recognized does not have the font size feature. In this way, the accuracy of the font size recognition result can be improved.
[0132] In another achievable embodiment of the present invention, the font size of the text object in each of the text lines is identified, and the font size identification result of determining whether the area to be identified has the font size feature is determined. The specific implementation process may be:
[0133] For each text line, identifying the font size of each text object in the text line, and determining a first average font size corresponding to the text line according to each of the font sizes;
[0134] The font size recognition result of determining whether the to-be-recognized area has the font size feature is determined according to each of the first average font sizes.
[0135] In practical applications, for each text line, the font of each text object in the text line is identified to obtain the font size of each text object in the text line. Then, for each text line, the font sizes of each text object are added and divided by the number of text objects in the text line to obtain the first average font size corresponding to the text line. Furthermore, based on the first average font size corresponding to each text line, the font size recognition result of whether the area to be recognized has a font size feature is determined. In this way, the accuracy of the font size recognition result can be improved.
[0136] Optionally, the font size recognition result of determining whether the area to be identified has the font size feature based on each of the first average font sizes may be: determining the average of each of the first average font sizes; determining the distance between each of the first average font sizes and the average; judging whether each of the distances is within a set distance range; if so, determining that the font size recognition result is that the area to be identified has the font size feature; if not, determining that the font size recognition result is that the area to be identified does not have the font size feature. In this way, based on the distance between the first average font size and the average, the discreteness of the font size can be determined, and thus the font size recognition result can be determined based on whether the discreteness (each distance) is within a controllable range (set distance range), which can improve the credibility of the font size recognition result.
[0137] Optionally, the process of determining the font size recognition result of whether the to-be-recognized area has the font size feature according to each of the first average font sizes may be specifically implemented as follows:
[0138] determining a ratio of a maximum average font size to a minimum average font size among the first average font sizes;
[0139] When the ratio is less than or equal to the set value, determining the font size recognition result as the area to be recognized having the font size feature;
[0140] When the ratio is greater than the set value, it is determined that the font size recognition result is that the area to be recognized does not have the font size feature.
[0141] Specifically, the set value is a preset value.
[0142] In practical applications, the maximum average font size (maximum average font size) and the minimum average font size (minimum average font size) are determined from each first average font size, and then the ratio of the maximum average font size to the minimum average font size is calculated. It is then determined whether the ratio is greater than a set value; if so, the font size recognition result is determined to be that the area to be recognized does not have a font size feature; if not, the font size recognition result is determined to be that the area to be recognized has a font size feature. In this way, by comparing the ratio of the maximum average font size to the minimum average font size with the set value to determine the font size recognition result, the accuracy of the font size recognition result can be improved and the processing flow can be simplified.
[0143] For example, if the average font size of each text line in all given lines (all text lines in the area to be identified): that is, there may be multiple Fonts in a line, then the average value of the current text line needs to be counted. If the ratio of the maximum average font size to the minimum average font size in all given lines is greater than 1.3 (set value), the font size recognition result isValidFontSize (font size feature) = False (no font size feature), otherwise the font size recognition result is True (with font size feature).
[0144] In one or more optional embodiments of the present invention, the text line association detection includes content type detection and text line type detection, the continuity feature includes content type feature and text line type feature, and the detection result includes content type detection result and text line type detection result; accordingly, the text line association detection is performed on each of the text lines to obtain a detection result of whether the area to be identified has a continuity feature, including:
[0145] Performing content type detection on each of the text lines to obtain the content type detection result of whether the area to be identified has the content type feature;
[0146] A text line type detection is performed on each of the text lines to obtain the text line type detection result of whether the area to be identified has the text line type feature.
[0147] Specifically, content type detection refers to the detection of the content in the text line. The content type feature indicates that the type of content in the text line is relatively consistent, wherein the content type includes picture type and blank line type, etc. The picture type indicates that the content is a picture, and the blank line type indicates that there is no substantial content. The content type detection result refers to the result of content type detection on the text line, that is, the result of whether the area to be identified has the content type feature. Text line type detection refers to the detection of the relationship between text lines or text line types, wherein the relationship between text lines includes whether the writing method between text lines is the same, whether there is an intersection, whether the font style is the same, and whether the line spacing meets the line spacing requirements; the text line type refers to the type of text line whose content is text, including the writing direction type, line intersection type, font style type and line spacing type. The text line type feature indicates that the types between text lines are relatively consistent. The text line type detection result refers to the result of text line type detection on the text line, that is, the result of whether the area to be identified has the text line type feature.
[0148] In practical applications, the line identifier can be used to detect the content type of the text line, and determine whether the area to be identified has content type features based on the content type detection results, that is, determine the content type detection results; at the same time, the line identifier can be used to detect the text line type of each text line, and determine whether the area to be identified has text line type features based on the text line type detection results, that is, determine the text line type detection results. In this way, the text line type detection results are determined more finely from the two dimensions of content type features and text line type features, thereby improving the accuracy and reliability of the text line type detection results.
[0149] Optionally, the content type detection includes blank line detection and picture line detection, the content type feature includes a blank line feature and a picture line feature, and the content type detection result includes a blank line detection result and a picture line detection result; accordingly, performing content type detection on each of the text lines to obtain the content type detection result of whether the area to be identified has the content type feature includes:
[0150] Obtaining the blank line detection result of whether the to-be-identified area has the blank line feature by performing blank line detection on each of the text lines;
[0151] By performing picture line detection on each of the text lines, the picture line detection result is obtained to determine whether the area to be identified has the picture line feature.
[0152] Specifically, an empty line refers to a text line without text content, such as a line without any characters, a line with only spaces, and a line with only lines. An image line refers to a text line with text content but no text type elements, that is, a text line with a picture as the text content. The empty line feature, i.e., content empty line (ContentEmptyLine), indicates that the area to be identified contains an empty line. The image line feature (FigureLine) indicates that the area to be identified contains an image line.
[0153] In practical applications, for each text line, a blank line detection can be performed on the text line according to the content or characters in the text line, that is, whether the text line is a blank line can be identified. For example, if there are only underlines or spaces modified by underlines in the current text line, then there is no specific text content, and the text line is a blank line. Thus, a blank line detection result is obtained to determine whether the area to be identified has blank line features. At the same time, for each text line, a picture line detection is performed on the text line, that is, whether the text line is a picture line is identified, thereby obtaining a picture line detection result to determine whether the area to be identified has picture line features. In this way, the content type detection result is determined in a more fine-grained manner from the two dimensions of blank line features and picture line features, thereby improving the accuracy and reliability of the content type detection result.
[0154] In one or more optional embodiments of the present invention, the blank line detection is performed on each of the text lines to obtain the blank line detection result of whether the area to be identified has the blank line feature. The specific implementation process may be as follows:
[0155] Performing blank line detection on each of the text lines to determine whether each of the text lines is a blank line without text content;
[0156] In the case where there is a blank line in each of the text lines, determining the blank line detection result as that the to-be-identified area has the blank line feature;
[0157] In the case that there is no blank line in each of the text lines, the blank line detection result is determined as the area to be identified does not have the blank line feature.
[0158] In practical applications, each text line can be simultaneously tested for blank lines to determine whether each text line is a blank line. If there is a blank line in each text line, the blank line detection result is determined to be that the area to be identified has blank line features. If there is no blank line in each text line, the blank line detection result is determined to be that the area to be identified does not have blank line features. In this way, the efficiency and accuracy of determining the blank line detection result can be improved.
[0159] Exemplarily, if there is no text content in any line of all given lines (all text lines in the area to be identified), the empty line detection result hasContentEmptyLine (empty line feature) = True (has empty line feature), otherwise the empty line detection result is False (does not have empty line feature).
[0160] In addition, each text line can be detected as blank line in a certain order. When the current text line is detected as a blank line, the blank line detection result can be determined as the area to be identified has blank line features. In this way, blank line detection can be avoided for all text lines, which reduces the amount of data processing to a certain extent and improves the efficiency of determining the blank line detection result.
[0161] In one or more optional embodiments of the present invention, the step of performing picture line detection on each of the text lines to obtain the picture line detection result of whether the area to be identified has the picture line feature includes:
[0162] Performing picture line detection on each of the text lines to determine whether each of the text lines is a picture line;
[0163] In the case where there is a picture line in each of the text lines, determining the picture line detection result as that the area to be identified has the picture line feature;
[0164] In the case that there is no picture row in each of the text rows, it is determined that the picture row detection result is that the to-be-identified area does not have the picture row feature.
[0165] In practical applications, each text line can be simultaneously subjected to image line detection to determine whether each text line is an image line. If an image line exists in each text line, the image line detection result is determined to be that the area to be identified has image line features; if an image line does not exist in each text line, the image line detection result is determined to be that the area to be identified does not have image line features. In this way, the efficiency and accuracy of determining the image line detection result can be improved.
[0166] Exemplarily, if there is any line in all given lines (all text lines in the area to be identified) that has text content but no text type elements, then there may be a picture as the text content of the current line, that is, the picture line detection result hasFigureLine (picture line feature) = True (has picture line feature), otherwise the picture line detection result is False (does not have picture line feature).
[0167] In addition, image line detection can be performed on each text line in a certain order. When the current text line is detected as an image line, the image line detection result can be determined as the area to be identified having image line features. In this way, image line detection can be avoided for all text lines, which reduces the amount of data processing to a certain extent and improves the efficiency of determining the image line detection result.
[0168] Optionally, the text line type detection includes writing direction detection, line intersection detection, font style detection and line spacing detection, the text line type features include writing direction features, line intersection features, font style features and line spacing features, and the text line type detection result includes writing direction detection results, line intersection detection results, font style detection results and line spacing detection results; accordingly, performing text line type detection on each of the text lines to obtain the text line type detection result of whether the area to be identified has the text line type features includes:
[0169] Performing writing direction detection on each of the text lines to obtain the writing direction detection result of whether the area to be identified has the writing direction feature;
[0170] Performing a line intersection detection on each of the text lines to obtain the line intersection detection result of whether the area to be identified has the line intersection feature;
[0171] Performing font style detection on each of the text lines to obtain the font style detection result of whether the area to be identified has the font style feature;
[0172] Perform line spacing detection on each of the text lines to obtain the line spacing detection result of whether the area to be identified has the line spacing feature.
[0173] Specifically, the line intersection refers to the overlapping part of the projection of two adjacent text lines in the line direction. The line spacing refers to the distance between two adjacent text lines. The writing direction feature, that is, the writing mode is consistent (writingModeConsistent), which indicates that the writing directions corresponding to each text line in the area to be identified are the same. The writing direction detection result refers to the result of writing direction detection for each text line, that is, the result of whether the area to be identified has the writing direction feature. The line intersection feature, that is, the intersection block feature (IntersectInBlockDir), indicates that there is a line intersection between text lines in the area to be identified. The line intersection detection result refers to the result of line intersection detection for each text line, that is, the result of whether the area to be identified has the line intersection feature. The font style feature, that is, font style consistency (isFontStyleConsistent), indicates that the font style corresponding to the area to be identified is relatively consistent. The font style detection result refers to the result of font style detection for each text line, that is, the result of whether the area to be identified has the font style feature. The line spacing feature, that is, line spacing consistency (LineSpacingConsistent), indicates that the line spacing corresponding to the area to be identified is relatively consistent. The line spacing detection result refers to the result of line spacing detection for each text line, that is, the result of whether the area to be identified has a line spacing feature.
[0174] In practical applications, for each text line, a line identifier can be used to detect the writing direction of the text line, that is, to identify the writing direction of the text line; then, according to the sub-writing direction detection results corresponding to each text line, determine whether the area to be identified has the writing direction feature, that is, the writing direction detection result. Line intersection detection is performed for each two adjacent text lines, and then, according to the sub-line intersection detection results corresponding to each text line, the line intersection detection result of whether the area to be identified has the line intersection feature is determined. Font style detection is performed for each two adjacent text lines, and then, according to the sub-font style detection results corresponding to each text line, the font style detection result of whether the area to be identified has the font style feature is determined. Line spacing detection is performed for each two adjacent text lines, and then, according to the sub-line spacing detection results corresponding to each text line, the line spacing detection result of whether the area to be identified has the line spacing feature is determined. In this way, the text line type detection result is determined more finely from the four dimensions of writing direction feature, line intersection feature, font style feature and line spacing feature, thereby improving the accuracy and reliability of the text line type detection result.
[0175] Optionally, the writing direction detection is performed on each of the text lines to obtain the writing direction detection result of whether the area to be identified has the writing direction feature. The specific implementation process may be as follows:
[0176] Performing writing direction detection on each of the text lines to determine the writing direction of each of the text lines;
[0177] In the case where all the writing directions are the same, determining the writing direction detection result as that the area to be identified has the writing direction feature;
[0178] In the case that different writing directions exist among the writing directions, it is determined that the writing direction detection result is that the area to be identified does not have a writing direction feature.
[0179] In practical applications, the existing or future achievable writing direction detection method can be used to simultaneously detect the writing direction of each text line, thereby determining the writing direction of each text line. It is determined whether the writing directions of each text line are the same; if so, the writing direction detection result is determined to be that the area to be identified has writing direction characteristics; if not, the writing direction detection result is determined to be that the area to be identified does not have writing direction characteristics. In this way, the efficiency and accuracy of determining the writing direction detection result can be improved.
[0180] Exemplarily, according to a method for quickly identifying the writing direction of text content in a PDF, the writing direction of each line in all text lines in the area to be identified is determined. If the writing direction of each line in all given lines (all text lines in the area to be identified) is the same, then the writing direction detection result writingModeConsistent (writing direction feature) = True (having the writing direction feature), otherwise the writing direction detection result is False (not having the writing direction feature).
[0181] In addition, the writing direction detection can be performed on each text line in a certain order. When the writing direction of the current text line is detected to be different from the writing direction that has been detected, the writing direction detection result can be determined as that the area to be identified does not have the writing direction feature. In this way, the writing direction detection can be avoided for all text lines, which reduces the amount of data processing to a certain extent and improves the efficiency of determining the writing direction detection result.
[0182] Optionally, the performing of line intersection detection on each of the text lines to obtain the line intersection detection result of whether the to-be-identified area has the line intersection feature may be specifically implemented as follows:
[0183] Performing line intersection detection on each text line pair to determine whether projections of the text lines in each text line pair in the line direction have an intersection, wherein the text line pair is two connected text lines in each text line;
[0184] If not, it is determined that the row intersection detection result is that the to-be-identified area does not have the row intersection feature.
[0185] Specifically, the row direction refers to the arrangement direction of the text line, which is perpendicular to the writing direction of the text line. For example, if the writing direction is from left to right, that is, horizontally, the row direction is vertically.
[0186] In practical applications, for each text line pair, the two text lines in the text line pair are projected in the line direction. If there is no corresponding intersection for each text line pair, the line intersection detection result is determined to be that the area to be identified does not have a line intersection feature. In this way, the efficiency and accuracy of determining the line intersection detection result can be improved.
[0187] Optionally, the font style detection is performed on each of the text lines to obtain the font style detection result of whether the area to be identified has the font style feature. The specific implementation process may be as follows:
[0188] Performing font style detection on each of the text line pairs to determine whether each of the text line pairs meets a font style feature condition, wherein the font style feature condition is that the font thickness ranges between the text lines in the text line pairs overlap, and the italic attributes between the text lines are the same;
[0189] If so, determining that the font style detection result is that the area to be identified has the font style feature;
[0190] If not, it is determined that the font style detection result shows that the to-be-identified area does not have the font style feature.
[0191] Specifically, the row direction refers to the arrangement direction of the text line, which is perpendicular to the writing direction of the text line. If the writing direction is from left to right, that is, horizontal, the row direction is vertical. The font weight range refers to the numerical range corresponding to the font weight. The italic attribute includes italic and non-italic.
[0192] In practical applications, for each text line pair, the fonts of each text line in the text line pair are identified, and the font thickness range and italic attribute corresponding to each text line in the text line pair are obtained accordingly. Then, it is determined whether each text line pair meets the font style feature conditions, that is, it is determined whether there is an intersection in the font thickness range corresponding to each text line in each text line pair, and whether the italic attributes corresponding to each text line in each text line pair are the same. If so, it is determined that the font style detection result is that the area to be identified has the font style feature. If not, it is determined that the font style detection result is that the area to be identified does not have the font style feature. In this way, the efficiency and accuracy of determining the font style detection result can be improved.
[0193] There are many ways to identify the font of each text line in the text line pair. For each text line, the fonts of the text contents in the text line can be counted, and the font with the largest number can be determined as the font of the text line, and the font thickness range and italic attributes corresponding to the font with the largest number can be determined as the font thickness range and italic attributes corresponding to the text line; it can also be other ways, which are not limited to the present invention.
[0194] Preferably, in order to reduce the amount of data processing, the font style detection is performed on each of the text line pairs, including: performing font style detection on the first text object pair in each of the text lines to obtain the font thickness range and italic attribute between each text line in each of the text line pairs. The font of the first text object in the text line is used to represent the font of this line. Because multiple text objects may be written in a text line, different text objects have different fonts.
[0195] Exemplarily, if within all given lines (all text lines in the area to be identified), there are any two consecutive lines, and there is no intersection between the Font and Font Weight given in the first TextObject in the line (font thickness is a range. For example, the previous line is given [400, 600], and the next line is given [500, 800], then the intersection is [500, 600]) or the Italic (both are italic or neither is italic) attributes are not equal, then the font style detection result isFontStyleConsistent (font style feature) = False (does not have font style features), otherwise the font style detection result is True (has font style features).
[0196] Optionally, the line spacing detection is performed on each of the text lines to obtain the line spacing detection result of whether the area to be identified has the line spacing feature. The specific implementation process may be as follows:
[0197] Performing line spacing detection on each of the text line pairs to determine the line spacing between the text lines in each of the text line pairs;
[0198] According to each of the line distances, it is determined whether the area to be identified has the line distance detection result of the line distance feature.
[0199] In practical applications, the line spacing between the two text lines in each text line pair can be detected to determine the line spacing between the two text lines in each text line pair. Then, based on each line spacing, the line spacing detection result is determined, that is, whether the area to be identified has a line spacing feature. In this way, the accuracy of determining the line spacing detection result can be improved.
[0200] In an optional embodiment of the present invention, the line spacing detection result of determining whether the area to be identified has the line spacing feature according to each line spacing includes: judging whether each line spacing is less than a set line spacing, if so, determining that the line spacing detection result is that the area to be identified has the line spacing feature; if not, determining that the line spacing detection result is that the area to be identified does not have the line spacing feature. In this way, by setting the line spacing to determine the line spacing detection result, the process of determining the line spacing detection result can be simplified, and the efficiency of determining the line spacing detection result can be improved.
[0201] In another optional embodiment of the present invention, the line spacing detection result of determining whether the to-be-identified area has the line spacing feature according to each line spacing may be specifically implemented as follows:
[0202] Determine an average line spacing of each of the line spacings, and calculate a second average font size of all text objects in each of the text lines;
[0203] Determine whether all the line spacings are target line spacings, the target line spacing is less than or equal to a first set multiple of the second average font size, and the absolute difference between the target line spacing and the average line spacing is less than or equal to a second set multiple of the second average font size, and the first set multiple is greater than the second set multiple;
[0204] If so, determining that the line spacing detection result is that the area to be identified has the line spacing feature;
[0205] If not, it is determined that the line spacing detection result is that the area to be identified does not have the line spacing feature.
[0206] In practical applications, the average line spacing is obtained by adding up the line spacing and dividing it by the number of line spacings. The font sizes of all text objects in the area to be identified are counted and the average is calculated, which is the second average font size. Then, it is determined whether each line spacing is the target line spacing, that is, whether each line spacing is less than or equal to the product of the second average font size and the first set multiple, and the absolute difference with the average line spacing is less than or equal to the product of the second average font size and the second set multiple. If so, it is determined that the line spacing detection result is that the area to be identified has line spacing characteristics; if not, it is determined that the line spacing detection result is that the area to be identified does not have line spacing characteristics. In this way, the accuracy and credibility of the line spacing detection results can be improved.
[0207] Exemplarily, the distance between every two consecutive lines in all given lines (all text lines in the area to be identified) is counted, and the average line spacing is obtained. The average font size (second average font size) of all text objects in all given lines is counted. If the line spacing (line spacing) between any two consecutive lines is greater than 1.3 times the average font size (first set multiple), or the absolute difference between the average line spacing and the average font size is greater than 0.2 times the average font (second set multiple), then the line spacing detection result isLineSpacingConsistent (line spacing feature) = False (no line spacing feature), otherwise the line spacing detection result is True (with line spacing feature).
[0208] It should be noted that if the projections of the text lines in each text line pair in the line direction have corresponding intersections, it is necessary to further determine the line intersection features, that is, after performing line intersection detection on each text line pair and determining whether the projections of the text lines in each text line pair in the line direction have intersections, the method further includes:
[0209] If so, determine whether there is a target intersection in each of the intersections, the length of the target intersection is greater than or equal to a third set multiple of the projection length of the target text line, and the target text line is any text line corresponding to the target intersection;
[0210] If so, determining the row intersection detection result is that the to-be-identified area has the row intersection feature;
[0211] If not, it is determined that the row intersection detection result is that the to-be-identified area does not have the row intersection feature.
[0212] In practical applications, if the projections of the text lines in each text line pair in the row direction intersect, it is necessary to determine whether there is a target intersection in each intersection whose length is greater than or equal to the third set multiple of the projection length of the target text line, where the target text line is any text line corresponding to the target intersection. If it exists, the row intersection detection result is determined to be that the area to be identified has a row intersection feature; if it does not exist, the row intersection detection result is determined to be that the area to be identified does not have a row intersection feature. In this way, by doubly determining the row intersection detection result based on whether there is an intersection and whether there is a target intersection, the credibility and accuracy of the row intersection detection result can be improved.
[0213] Exemplarily, if within all given rows (all text rows in the area to be identified), there are any two consecutive rows (i.e., the two rows are in a vertical / horizontal layout relationship, or a left / right / vertical layout relationship) or their corresponding circumscribed rectangles (BBoxes), and the projections of the rows in the row direction (usually LrTb, then its projection axis is the Y axis) have an intersection, and the length of the intersecting interval is greater than or equal to 0.7 (the third set multiple) of the projection length of any one of the two rows in the row direction, then the row intersection detection result hasIntersectInBlockDir (row intersection feature) = True (has row intersection feature), otherwise the row intersection detection result is False (does not have row intersection feature).
[0214] In one or more optional embodiments of the present invention, the process of determining whether the to-be-recognized area in the target document is a code segment according to the detection result, the first recognition result, and the second recognition result may be specifically implemented as follows:
[0215] comparing the detection result, the first recognition result and the second recognition result with set code recognition conditions respectively;
[0216] When the comparison results are all in line, determine the area to be recognized in the target document as a code segment.
[0217] Specifically, setting the code recognition condition means pre-setting the condition for determining whether it is a code segment.
[0218] In practical applications, compare the detection result, the first recognition result, and the second recognition result with the set code recognition condition respectively. The detection result can be compared with the set code recognition condition, the first recognition result can be compared with the set code recognition condition, and the second recognition result can be compared with the set code recognition condition to obtain three comparison results. Judge whether each comparison result is in line. If so, determine the area to be recognized in the target document as a code segment; if not, determine that the area to be recognized in the target document is not a code segment. The detection result, the first recognition result, and the second recognition result can be constructed into a matrix, and judge whether the matrix meets the set code recognition condition. If so, determine the area to be recognized in the target document as a code segment; if not, determine that the area to be recognized in the target document is not a code segment. In this way, by setting the code recognition condition, it is possible to quickly and accurately determine whether the area to be recognized is a code segment.
[0219] For example, assume that a total of N lines are recognized currently. Then a two-dimensional matrix matrix[m][n] can be constructed, and each element in it is initialized to False, where matrix[m][n] represents whether all lines from the m-th line to the n-th line can form a code segment, and 1 ≤ m < n ≤ N. Update each element in matrix[m][n] according to the detection result, the first recognition result, and the second recognition result. If all elements of matrix[m][n] meet the set code recognition condition, then matrix[m][n] = True, that is, the area to be recognized in the target document is a code segment.
[0220] It should be noted that when judging whether all elements of matrix[m][n] meet the set code recognition condition, it can be judged in sequence. If any one element does not meet the condition, there is no need to judge the remaining elements. At this time, it can be determined that the area to be recognized in the target document is not a code segment, that is, matrix[m][n] = False.
[0221] Preferably, setting the code recognition conditions includes: hasContentEmptyLine (empty line feature) = False (does not have the empty line feature), writingModeConsistent (writing direction feature) = True (has the writing direction feature), hasFigureLine (picture line feature) = False (does not have the picture line feature), hasIntersectInBlockDir (line intersection feature) = False (does not have the line intersection feature), isFontStyleConsistent (font style feature) = True (has the font style feature), isLineSpacingConsistent (line spacing feature) = True (has the line spacing feature), isCodeCharacteristic (code feature) = True (has the code feature), isValidFontSize (font size feature) = True (has the font size feature) and isSameFontFamily (font cluster feature) = True (has the font cluster feature).
[0222] Optionally, after comparing the detection result, the first recognition result and the second recognition result with the set code recognition condition respectively, the method further includes:
[0223] If the comparison result shows that there is a mismatch, determining that the area to be identified in the target document is not a code segment, and obtaining the number of text lines included in the area to be identified;
[0224] When the number of rows is not equal to the first preset value, subtract the second preset value from the number of rows to obtain an updated number of rows;
[0225] Determine each target sub-region in the to-be-recognized region, wherein the target sub-region contains the updated number of connected text lines;
[0226] The target sub-regions are respectively used as updated regions to be identified, and the step of performing text line association detection on the text lines is continued to obtain a detection result of whether the regions to be identified have continuity features.
[0227] Specifically, the first preset value and the second preset value are both pre-set values, which are used to adjust the accuracy of identifying the code segment, or to identify a code segment of a set length. The first preset value and the second preset value may be the same or different. Preferably, in order to improve the accuracy, the first preset value and the second preset value are both one.
[0228] In practical applications, the determination order should be in the order of the longest number of lines to the shortest, that is, first determine the interval with the longest number of lines (the area to be identified), and then determine the sub-interval (each target sub-area in the area to be identified) based on the result. In this way, it is possible to avoid splitting a code segment that occupies multiple text lines into multiple code segments, thereby improving the accuracy and completeness of the recognized code segments.
[0229] Optionally, the step of taking each target sub-region as an updated region to be identified, continuing to perform text line association detection on each text line, and obtaining a detection result of whether the region to be identified has a continuity feature may be specifically implemented as follows:
[0230] The first target sub-regions are respectively used as updated regions to be identified, and the step of performing text line association detection on each of the text lines to obtain a detection result of whether the regions to be identified have continuity features is continued, wherein the first target sub-region is any target sub-region;
[0231] If the first target sub-region is a code segment, it is determined that the second target sub-region is not a code segment, wherein a combination of the second target sub-region and the first target sub-region is any designated region to be identified before updating, and the designated region to be identified is not a code segment.
[0232] In practical applications, each target sub-region can be used as an updated area to be identified, and code segment identification can be continued for each area to be identified. If the first target sub-region is a code segment, it can be directly determined that the second target sub-region is not a code segment. In this way, code segment identification can be performed on the second target sub-region, which greatly reduces the amount of data processing and improves the efficiency of code segment identification while ensuring the accuracy of code segment identification.
[0233] Continuing with the above example, if matrix[i][j] = True, and (i,j) contains (k,l), then there is no need to determine matrix[k][1]. For example, assuming there are 10 rows in total, then matrix[1]
[10] should be determined first. Next, matrix[1][9] and matrix[2]
[10] , which have 9 rows, should be determined. Then matrix[1][8], matrix[2][8], and matrix[3]
[10] should be determined, and so on. If matrix[1][9] is False and matrix[1][8] is True, since the union of matrix[2][9] and matrix[1][8] is matrix[1][9], there is no need to judge matrix[2][9] based on the above process, and matrix[2][9] can be directly determined to be False. Similarly, if the union of matrix[1][8] and matrix[3]
[10] is matrix[1]
[10] , there is no need to judge matrix[3]
[10] based on the above process, and matrix[2][9] can be directly determined to be False. That is, when matrix[1][9] is False and matrix[1][8] is True, since both matrix[2][9] and matrix[3]
[10] have an intersection with matrix[1][9], there is no need to judge either of them, and only the result of matrix[9]
[10] needs to be judged. That is, if matrix[1][9] is False and matrix[1][8] is True, matrix[2][9] must be False, because if both are True, then matrix[1][9] should be True, which is a contradiction. Therefore, the present invention does not need to worry about missing the optimal result and can accurately identify each code segment.
[0234] Exemplarily, a method for identifying a code segment in a document is as follows:
[0235] 1. Precondition: Line Identifier. If it is a layout document, it needs to be determined based on the position of the TextObject in the document. If it is a flow document, no special identification is required.
[0236] The basic determination method is as follows, where (1)-(9) below are independent of each other. It should be noted that the following solution for the average value in each line is based on a single Glyph. For example, there may be two TextObjects in a line. The first TextObject has 3 words and a font size of 10, and the second TextObject has 2 words and a font size of 20. Then the average font size is (3*10+2*20) / (3+2)=14.
[0237] (1) If there is no text content in any of the given lines (all text lines in the area to be identified), the empty line detection result hasContentEmptyLine (empty line feature) = True (has empty line feature), otherwise it is False (does not have empty line feature).
[0238] (2) According to a method for quickly identifying the writing direction of text content in PDF, the writing direction of each line in all text lines in the area to be identified is determined. If the writing direction of each line in all given lines (all text lines in the area to be identified) is the same, then the writing direction detection result writingModeConsistent (writing direction feature) = True (having the writing direction feature), otherwise it is False (not having the writing direction feature).
[0239] (3) If there is any line in all the given lines (all the text lines in the area to be identified) that has text content but no text type element, then there may be a picture as the text content of the current line, that is, the picture line detection result hasFigureLine (picture line feature) = True (has picture line feature), otherwise it is False (does not have picture line feature).
[0240] (4) If, within all given lines (all text lines in the area to be identified), there exist any two consecutive lines (text line pairs) or their corresponding bounding rectangles (BBoxes), the projections of the lines in the row direction have an intersection, and the length of the intersecting interval is greater than or equal to 0.7 times (the third set multiple) of the projection length of any of the two lines in the row direction, then the row intersection detection result hasIntersectInBlockDir (row intersection feature) = True (having row intersection feature), otherwise it is False (not having row intersection feature).
[0241] (5) If there are any two consecutive lines in all given lines (all text lines in the area to be identified), there is no intersection between the Font Weights given in the first TextObject in the line, or the Italic attributes are not equal, then the font style detection result isFontStyleConsistent (font style feature) = False (does not have font style features), otherwise it is True (has font style features).
[0242] (6) Count the distance between every two consecutive lines in all given lines (all text lines in the area to be identified) and obtain the average line spacing. Count the average font size (second average font size) of all text objects in all given lines. If the line spacing (line spacing) between any two consecutive lines is greater than 1.3 times the average font size (first set multiple), or the absolute difference between the line spacing and the average line spacing is greater than 0.2 times the average font size (second set multiple), then the line spacing detection result isLineSpacingConsistent (line spacing feature) = False (no line spacing feature), otherwise it is True (with line spacing feature).
[0243] (7) If, among all the given text lines (all the text lines in the area to be identified), there is any text line whose TextObject's Font does not have a fixed width or whose Unicode does not belong to the Common / Latin interval, then the second recognition result isCodeCharacteristic (code feature) = False (does not have code feature), otherwise it is True (has code feature).
[0244] (8) If the average font size of each text line in all given lines (all text lines in the area to be identified): that is, there may be multiple Fonts in a line, then the average value of the current text line needs to be counted. If the ratio of the maximum average font size to the minimum average font size in all given lines is greater than 1.3 (set value), the font size recognition result isValidFontSize (font size feature) = False (no font size feature), otherwise it is True (with font size feature).
[0245] (9) If the Font Family of the Font of the first TextObject in each text line is the same in all given lines (all text lines in the area to be identified), such as they are all Songti clusters, then the font cluster identification result isSameFontFamily (font cluster feature) = True (having font cluster feature), otherwise it is False (not having font cluster feature).
[0246] 2. Based on the line recognizer, obtain all line information in the current document page, that is, the respective features corresponding to the above (1)-(9). Assume that a total of N lines are recognized currently. Then, a two-dimensional matrix matrix[m][n] can be constructed, and each element in it is initialized to False, where matrix[m][n] represents whether all lines from the m-th line to the n-th line can form a code segment, 1 ≤ m < n ≤ N. Update each element in matrix[m][n] according to the blank line detection result, writing direction detection result, picture line detection result, line intersection detection result, font style detection result, line spacing detection result, second recognition result, font size recognition result, and font cluster recognition result. If each element of matrix[m][n] meets the set code recognition conditions, then matrix[m][n] = True, that is, the to-be-recognized area in the target document is a code segment. That is, if all lines from the m-th line to the n-th line meet the following set code recognition conditions, then matrix[m][n] = True.
[0247] The determination order should be in the order of the line numbers from long to short, that is, first determine the interval segment with the longest line number (to-be-recognized area). If matrix[i][j] = True and (i, j) contains (k, l), then there is no need to determine matrix[k][l]. For example, assume there are 10 lines in total. Then, matrix[1]
[10] should be determined first. Secondly, determine matrix[1][9] and matrix[2]
[10] with 9 lines. Then, determine matrix[1][8], matrix[2][8], and matrix[3]
[10] , and so on. If matrix[1][9] is False and matrix[1][8] is True, since both matrix[2][9] and matrix[3]
[10] have intersections with matrix[1][9], there is no need to judge them, and only the result of matrix[9]
[10] needs to be determined. That is, when matrix[1][9] is False and matrix[1][8] is True, matrix[2][9] must be False, because if they were both True, then matrix[1][9] should be True. Therefore, the present invention does not need to worry about missing the optimal result and can accurately identify each code segment.
[0248] The code recognition conditions are set to include: hasContentEmptyLine (empty line feature) = False (does not have the empty line feature), writingModeConsistent (writing direction feature) = True (has the writing direction feature), hasFigureLine (picture line feature) = False (does not have the picture line feature), hasIntersectInBlockDir (line intersection feature) = False (does not have the line intersection feature), isFontStyleConsistent (font style feature) = True (has the font style feature), isLineSpacingConsistent (line spacing feature) = True (has the line spacing feature), isCodeCharacteristic (code feature) = True (has the code feature), isValidFontSize (font size feature) = True (has the font size feature) and isSameFontFamily (font cluster feature) = True (has the font cluster feature).
[0249] 3. Get the specific line number and area of the code segment based on the result of the matrix.
[0250] (10) First, obtain the number of rows that are True in the long interval segment, and obtain the intervals corresponding to several code segments (areas to be identified);
[0251] (11) Then obtain the area (area to be identified) that does not intersect with the above interval but has a matrix result of True, and obtain several code segments;
[0252] (12) Repeat the above operations (10) and (11) until all rows are traversed, and label each interval of the code segment (area to be identified) with a corresponding label to obtain the final recognition result.
[0253] See also Figure 2 , Figure 2 It is a schematic diagram of the effect of the method for identifying code segments in a document provided by the present invention: through the method for identifying code segments in a document provided by the present invention, not only two unconnected code segments can be identified, such as code segment 1 and code segment 2, but also two connected code segments can be identified, such as code segment 3 and code segment 4, thereby avoiding identifying two connected code segments as one code segment, thereby improving the accuracy of code segment identification.
[0254] The method for identifying code segments in a document provided by the present invention determines each text line interval that may be a code segment, that is, whether any m to n lines are code segments, based on the features between text lines. And a reasonable merging operation is performed on the text line interval that may be a code segment. For example, if 1-3 lines are identified as code segments, and 1-5 lines are identified as code segments, then 1-5 should be merged with 1-3. If 1-3 and 4-5 lines are code segments, but 1-5 lines are not code segments, they are not merged. All code segments in the document can be accurately identified, and the specific number of lines corresponding to each code segment can be identified. It is convenient to implement special processing (code highlighting, no re-arrangement of codes in layout documents, etc.) in subsequent document display, and it is also convenient to perform automatic indentation functions when editing documents, such as the reader can highlight key elements for the code segment area; the code segment can be treated as a whole in the PDF re-arrangement version; it can be converted into a text style suitable for reading and editing in HTML-based documents such as Docx. In addition, the method for identifying code segments in a document provided by the present invention is applicable to layout documents (such as PDF) and flow documents.
[0255] The following is a description of the device for identifying code segments in a document provided by the present invention. The device for identifying code segments in a document described below and the method for identifying code segments in a document described above can be referenced to each other.
[0256] Figure 3 is a schematic diagram of the structure of the device for identifying code segments in a document provided by the present invention, such as Figure 3 As shown, the device 300 for identifying code segments in a document includes: a first determination module 301, a detection module 302, a first identification module 303, a second identification module 304 and a second determination module 305, wherein:
[0257] A first determination module 301 is configured to determine a region to be identified in a target document, wherein the region to be identified includes at least one connected text line;
[0258] The detection module 302 is configured to perform text line association detection on each of the text lines to obtain a detection result of whether the to-be-identified area has a continuity feature, wherein the continuity feature represents the continuity of features between the text lines;
[0259] A first recognition module 303 is configured to recognize the font of the text object in each of the text lines and determine whether the area to be recognized has a first recognition result of the font feature;
[0260] The second recognition module 304 is configured to recognize the fixed width attribute and the Unicode of the text object in each of the text lines, and determine whether the area to be recognized has a second recognition result of the code feature;
[0261] The second determination module 305 is configured to determine whether the to-be-recognized area in the target document is a code segment according to the detection result, the first recognition result and the second recognition result.
[0262] The device for identifying code segments in documents provided by the present invention determines a region to be identified in a target document, wherein the region to be identified includes at least one connected text line; performs text line association detection on each of the text lines to obtain a detection result of whether the region to be identified has a continuity feature, wherein the continuity feature represents the feature continuity between the text lines; identifies the font of the text object in each of the text lines to determine a first recognition result of whether the region to be identified has a font feature; identifies the fixed-width attribute and the Unicode of the text object in each of the text lines to determine a second recognition result of whether the region to be identified has a code feature; determines whether the region to be identified in the target document is a code segment based on the detection result, the first recognition result and the second recognition result, thereby being able to quickly and accurately identify a number of code segments existing in each page of the document, avoid identifying two connected code segments as one code segment, and improve the accuracy of code segment recognition.
[0263] Optionally, the second recognition module 304 is further configured to identify the fixed-width attributes and Unicode corresponding to each text object in each of the text lines; determine whether each of the text lines meets the code feature conditions, the code feature conditions being that the fixed-width attributes corresponding to each text object in the text line all have a fixed width, and that the Unicodes corresponding to each text object in the text line all belong to a set interval; if so, determine that the second recognition result is that the area to be recognized has the code feature; if not, determine that the second recognition result is that the area to be recognized does not have the code feature.
[0264] Optionally, the font feature includes a font size feature and a font cluster feature, and the first recognition result includes a font size recognition result and a font cluster recognition result;
[0265] Accordingly, the first recognition module 303 includes a font size recognition module and a font cluster recognition module;
[0266] The font size recognition module is configured to recognize the font size of each text object in each text line, and determine the first average font size corresponding to the text line according to each font size; and determine the font size recognition result of whether the area to be recognized has the font size feature according to each first average font size;
[0267] The font cluster identification module is configured to identify the font cluster of the first text object in each text line; and determine the font cluster identification result of whether the area to be identified has the font cluster feature according to each font cluster.
[0268] Optionally, the font size recognition module is further configured to determine the ratio of the maximum average font size to the minimum average font size in each of the first average font sizes; when the ratio is less than or equal to a set value, determine that the font size recognition result is that the area to be recognized has the font size feature; when the ratio is greater than the set value, determine that the font size recognition result is that the area to be recognized does not have the font size feature.
[0269] Optionally, the text line association detection includes content type detection and text line type detection, the continuity feature includes content type feature and text line type feature, and the detection result includes content type detection result and text line type detection result;
[0270] Accordingly, the detection module 302 includes a content type detection module and a text line type detection module;
[0271] The content type detection module is configured to perform content type detection on each of the text lines to obtain the content type detection result of whether the area to be identified has the content type feature;
[0272] The text line type detection module is configured to perform text line type detection on each of the text lines to obtain the text line type detection result of whether the area to be identified has the text line type feature.
[0273] Optionally, the content type detection includes blank line detection and picture line detection, the content type feature includes a blank line feature and a picture line feature, and the content type detection result includes a blank line detection result and a picture line detection result;
[0274] Accordingly, the content type detection module includes a blank line detection module and a picture line detection module;
[0275] The blank line detection module is configured to perform blank line detection on each of the text lines to determine whether each of the text lines is a blank line without text content; if there is a blank line in each of the text lines, determine that the blank line detection result is that the area to be identified has the blank line feature; if there is no blank line in each of the text lines, determine that the blank line detection result is that the area to be identified does not have the blank line feature;
[0276] The picture row detection module is configured to perform picture row detection on each of the text rows to determine whether each of the text rows is a picture row; when a picture row exists in each of the text rows, determine that the picture row detection result is that the area to be identified has the picture row feature; when no picture row exists in each of the text rows, determine that the picture row detection result is that the area to be identified does not have the picture row feature.
[0277] Optionally, the text line type detection includes writing direction detection, line intersection detection, font style detection and line spacing detection, the text line type feature includes writing direction feature, line intersection feature, font style feature and line spacing feature, and the text line type detection result includes writing direction detection result, line intersection detection result, font style detection result and line spacing detection result;
[0278] Accordingly, the text line type detection module includes a writing direction detection module, a line intersection detection module, a font style detection module and a line spacing detection module;
[0279] The writing direction detection module is configured to perform writing direction detection on each of the text lines to determine the writing direction of each of the text lines; when the writing directions are the same, determine that the writing direction detection result is that the area to be identified has the writing direction feature; when the writing directions are different, determine that the writing direction detection result is that the area to be identified does not have the writing direction feature;
[0280] The row intersection detection module is configured to perform row intersection detection on each text row pair to determine whether the projections of the text rows in each text row pair in the row direction intersect, wherein the text row pair is two connected text rows in each text row; if not, determining that the row intersection detection result is that the area to be identified does not have the row intersection feature;
[0281] The font style detection module is configured to perform font style detection on each of the text line pairs to determine whether each of the text line pairs meets the font style feature condition, wherein the font style feature condition is that the font thickness ranges between the text lines in the text line pairs intersect, and the italic attributes between the text lines are the same; if so, determining that the font style detection result is that the area to be identified has the font style feature; if not, determining that the font style detection result is that the area to be identified does not have the font style feature;
[0282] The line spacing detection module is configured to perform line spacing detection on each of the text line pairs to determine the line spacing between the text lines in each of the text line pairs; and determine the line spacing detection result of whether the area to be identified has the line spacing feature based on each of the line spacings.
[0283] Optionally, the line spacing detection module is further configured to determine the average line spacing of each of the line spacings, and to count the second average font size of all text objects in each of the text lines; to determine whether each of the line spacings is a target line spacing, the target line spacing is less than or equal to a first set multiple of the second average font size, and the absolute difference between the target line spacing and the average line spacing is less than or equal to a second set multiple of the second average font size, and the first set multiple is greater than the second set multiple; if so, to determine that the line spacing detection result is that the area to be identified has the line spacing feature; if not, to determine that the line spacing detection result is that the area to be identified does not have the line spacing feature.
[0284] Optionally, the row intersection detection module is further configured to determine whether there is a target intersection in each of the intersections, the length of the target intersection is greater than or equal to a third set multiple of the projection length of the target text line, and the target text line is any text line corresponding to the target intersection; if so, determine that the row intersection detection result is that the area to be identified has the row intersection feature;
[0285] If not, it is determined that the row intersection detection result is that the to-be-identified area does not have the row intersection feature.
[0286] Optionally, the second determination module 305 is further configured to compare the detection result, the first recognition result and the second recognition result with set code recognition conditions respectively; when the comparison results are all in compliance, determine that the area to be recognized in the target document is a code segment.
[0287] Optionally, the second determination module 305 is further configured to, when the comparison result is that there is a mismatch, determine that the area to be identified in the target document is not a code segment, and obtain the number of text lines contained in the area to be identified; when the number of lines is not a first preset value, subtract a second preset value from the number of lines to obtain an updated number of lines; determine each target sub-area in the area to be identified, and the target sub-area contains the updated number of connected text lines; use each target sub-area as an updated area to be identified, and continue to perform the text line association detection on each of the text lines to obtain the detection result of whether the area to be identified has a continuity feature.
[0288] Figure 4 An example of a physical structure diagram of an electronic device is shown in FIG. Figure 4As shown, the electronic device may include: a processor 410, a communication interface 420, a memory 430 and a communication bus 440, wherein the processor 410, the communication interface 420 and the memory 430 communicate with each other through the communication bus 440. The processor 410 may call the logic instructions in the memory 430 to execute a method for identifying a code segment in a document, the method comprising: determining a region to be identified in a target document, wherein the region to be identified includes at least one connected text line; performing a text line association detection on each of the text lines to obtain a detection result of whether the region to be identified has a continuity feature, wherein the continuity feature represents the continuity of features between the text lines; identifying the font of the text object in each of the text lines to determine a first recognition result of whether the region to be identified has a font feature; identifying the fixed width attribute and the Unicode of the text object in each of the text lines to determine a second recognition result of whether the region to be identified has a code feature; determining whether the region to be identified in the target document is a code segment according to the detection result, the first recognition result and the second recognition result.
[0289] In addition, the logic instructions in the above-mentioned memory 430 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program codes.
[0290] On the other hand, the present invention also provides a computer program product, which includes a computer program, which can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the method for identifying code segments in a document provided by the above methods, the method including: determining an area to be identified in a target document, the area to be identified containing at least one connected text line; performing text line association detection on each of the text lines to obtain a detection result of whether the area to be identified has a continuity feature, the continuity feature characterizing the feature continuity between the text lines; identifying the font of the text object in each of the text lines to determine a first recognition result of whether the area to be identified has a font feature; identifying the fixed width attribute and Unicode of the text object in each of the text lines to determine a second recognition result of whether the area to be identified has a code feature; and determining whether the area to be identified in the target document is a code segment based on the detection result, the first recognition result and the second recognition result.
[0291] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute the method for identifying code segments in a document provided by the above-mentioned methods, the method comprising: determining an area to be identified in a target document, the area to be identified containing at least one connected text line; performing text line association detection on each of the text lines to obtain a detection result of whether the area to be identified has a continuity feature, the continuity feature characterizing the feature continuity between the text lines; identifying the font of the text object in each of the text lines to determine a first recognition result of whether the area to be identified has a font feature; identifying the fixed-width attribute and Unicode of the text object in each of the text lines to determine a second recognition result of whether the area to be identified has a code feature; and determining whether the area to be identified in the target document is a code segment based on the detection result, the first recognition result and the second recognition result.
[0292] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0293] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0294] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for identifying code segments in a document, characterized in that: include: Determine a region to be identified in a target document, wherein the region to be identified includes at least one connected text line; Performing text line association detection on each of the text lines to obtain a detection result of whether the to-be-identified area has a continuity feature, wherein the continuity feature represents feature continuity between the text lines; Recognize the font of the text object in each of the text lines to determine whether the area to be recognized has a first recognition result of the font feature; Identify the fixed width attribute and the Unicode of the text object in each of the text lines to determine a second identification result of whether the area to be identified has a code feature; According to the detection result, the first recognition result and the second recognition result, it is determined whether the to-be-recognized area in the target document is a code segment.
2. The method for identifying code segments in a document according to claim 1, characterized in that: The second recognition result of identifying the fixed width attribute and the Unicode of the text object in each of the text lines to determine whether the area to be identified has a code feature includes: Identifying fixed-width attributes and Unicodes corresponding to each text object in each of the text lines; Determine whether each of the text lines meets the code feature condition, wherein the code feature condition is that the fixed-width attributes corresponding to each text object in the text line all have a fixed width, and each of the Unicodes corresponding to each text object in the text line belongs to a set interval; If so, determining that the second recognition result is that the area to be recognized has the code feature; If not, it is determined that the second recognition result is that the area to be recognized does not have the code feature.
3. The method for identifying code segments in a document according to claim 1 or 2, characterized in that: The font features include font size features and font cluster features, and the first recognition result includes a font size recognition result and a font cluster recognition result; Accordingly, the step of identifying the font of the text object in each of the text lines and determining whether the area to be identified has a first identification result of the font feature includes: For each text line, identify the font size of each text object in the text line, and determine a first average font size corresponding to the text line according to each font size; determine the font size recognition result of whether the area to be recognized has the font size feature according to each first average font size; Identify the font cluster of the first text object in each text line; and determine, based on each font cluster, whether the area to be identified has the font cluster feature of the font cluster identification result.
4. The method for identifying code segments in a document according to claim 3, characterized in that: The step of determining the font size recognition result of whether the to-be-recognized area has the font size feature according to each of the first average font sizes includes: determining a ratio of a maximum average font size to a minimum average font size among the first average font sizes; When the ratio is less than or equal to the set value, determining the font size recognition result as the area to be recognized having the font size feature; When the ratio is greater than the set value, it is determined that the font size recognition result is that the area to be recognized does not have the font size feature.
5. The method for identifying code segments in a document according to claim 1 or 2, characterized in that: The text line association detection includes content type detection and text line type detection, the continuity feature includes content type feature and text line type feature, and the detection result includes content type detection result and text line type detection result; Correspondingly, the performing text line association detection on each of the text lines to obtain a detection result of whether the to-be-recognized area has a continuity feature includes: Performing content type detection on each of the text lines to obtain the content type detection result of whether the area to be identified has the content type feature; A text line type detection is performed on each of the text lines to obtain the text line type detection result of whether the area to be identified has the text line type feature.
6. The method for identifying code segments in a document according to claim 5, characterized in that: The content type detection includes blank line detection and picture line detection, the content type feature includes blank line feature and picture line feature, and the content type detection result includes blank line detection result and picture line detection result; Correspondingly, the performing content type detection on each of the text lines to obtain the content type detection result of whether the area to be identified has the content type feature includes: Performing blank line detection on each of the text lines to determine whether each of the text lines is a blank line without text content; in the case where there is a blank line in each of the text lines, determining that the blank line detection result is that the area to be identified has the blank line feature; in the case where there is no blank line in each of the text lines, determining that the blank line detection result is that the area to be identified does not have the blank line feature; Perform image row detection on each of the text lines to determine whether each of the text lines is a picture row; when a picture row exists in each of the text lines, determine that the picture row detection result is that the area to be identified has the picture row feature; when no picture row exists in each of the text lines, determine that the picture row detection result is that the area to be identified does not have the picture row feature.
7. The method for identifying code segments in a document according to claim 5, characterized in that: The text line type detection includes writing direction detection, line intersection detection, font style detection and line spacing detection, the text line type features include writing direction features, line intersection features, font style features and line spacing features, and the text line type detection results include writing direction detection results, line intersection detection results, font style detection results and line spacing detection results; Correspondingly, the performing text line type detection on each of the text lines to obtain the text line type detection result of whether the to-be-identified area has the text line type feature includes: Perform writing direction detection on each of the text lines to determine the writing direction of each of the text lines; when the writing directions are the same, determine that the writing direction detection result is that the area to be identified has the writing direction feature; when the writing directions are different, determine that the writing direction detection result is that the area to be identified does not have the writing direction feature; Performing a line intersection detection on each text line pair to determine whether the projections of the text lines in each text line pair in the line direction have an intersection, wherein the text line pair is two connected text lines in each text line; if not, determining that the line intersection detection result is that the area to be identified does not have the line intersection feature; Performing font style detection on each of the text line pairs to determine whether each of the text line pairs meets the font style feature condition, wherein the font style feature condition is that there is an intersection in the font thickness ranges between the text lines in the text line pairs, and the italic attributes between the text lines are the same; if so, determining that the font style detection result is that the area to be identified has the font style feature; if not, determining that the font style detection result is that the area to be identified does not have the font style feature; Perform line spacing detection on each of the text line pairs to determine the line spacing between the text lines in each of the text line pairs; and determine, based on each of the line spacings, whether the area to be identified has the line spacing feature of the line spacing detection result.
8. The method for identifying code segments in a document according to claim 7, characterized in that: The line spacing detection result of determining whether the to-be-identified area has the line spacing feature according to each line spacing includes: Determine an average line spacing of each of the line spacings, and calculate a second average font size of all text objects in each of the text lines; Determine whether all the line spacings are target line spacings, the target line spacing is less than or equal to a first set multiple of the second average font size, and the absolute difference between the target line spacing and the average line spacing is less than or equal to a second set multiple of the second average font size, and the first set multiple is greater than the second set multiple; If so, determining that the line spacing detection result is that the area to be identified has the line spacing feature; If not, it is determined that the line spacing detection result is that the area to be identified does not have the line spacing feature.
9. The method for identifying code segments in a document according to claim 7, characterized in that: After performing line intersection detection on each text line pair and determining whether the projections of the text lines in each text line pair in the line direction have an intersection, the method further includes: If so, determine whether there is a target intersection in each of the intersections, the length of the target intersection is greater than or equal to a third set multiple of the projection length of the target text line, and the target text line is any text line corresponding to the target intersection; If so, determining the row intersection detection result is that the to-be-identified area has the row intersection feature; If not, it is determined that the row intersection detection result is that the to-be-identified area does not have the row intersection feature.
10. The method for identifying code segments in a document according to claim 1 or 2, characterized in that: The determining, according to the detection result, the first recognition result, and the second recognition result, whether the to-be-recognized area in the target document is a code segment includes: comparing the detection result, the first recognition result and the second recognition result with set code recognition conditions respectively; When the comparison result is that both are consistent, it is determined that the area to be identified in the target document is a code segment.
11. The method for identifying code segments in a document according to claim 10, characterized in that: After comparing the detection result, the first recognition result and the second recognition result with the set code recognition condition respectively, the method further includes: If the comparison result shows that there is a mismatch, determining that the area to be identified in the target document is not a code segment, and obtaining the number of text lines included in the area to be identified; When the number of rows is not equal to the first preset value, subtract the second preset value from the number of rows to obtain an updated number of rows; Determine each target sub-region in the to-be-recognized region, wherein the target sub-region contains the updated number of connected text lines; The target sub-regions are respectively used as updated regions to be identified, and the step of performing text line association detection on the text lines is continued to obtain a detection result of whether the regions to be identified have continuity features.
12. A device for identifying code segments in a document, characterized in that: include: A first determination module is configured to determine a region to be identified in a target document, wherein the region to be identified includes at least one connected text line; A detection module is configured to perform text line association detection on each of the text lines to obtain a detection result of whether the to-be-identified area has a continuity feature, wherein the continuity feature represents feature continuity between the text lines; A first recognition module is configured to recognize the font of the text object in each of the text lines and determine whether the area to be recognized has a first recognition result of the font feature; A second recognition module is configured to recognize the fixed width attribute and the Unicode of the text object in each of the text lines, and determine a second recognition result of whether the area to be recognized has a code feature; The second determination module is configured to determine whether the to-be-recognized area in the target document is a code segment according to the detection result, the first recognition result and the second recognition result.