Sensitive word automatic detection and replacement method, desensitization device, computer equipment and computer readable storage medium
By establishing a search text library and a replacement text library in PDF files, combined with a strategy of dynamically calculating font size and margin value expansion, the layout and formatting problems caused by sensitive word filtering in existing technologies are solved. This enables automatic detection and replacement of sensitive information, improving processing efficiency and the readability and professionalism of documents.
Patent Information
- Application Number
- CN202510937439.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-08
- Publication Date
- 2025-10-17
AI Technical Summary
When processing complex PDF files, the existing technology's sensitive word filtering method easily leads to layout and formatting damage, affecting the readability and professionalism of the document.
By establishing a search text library and a replacement text library, and by inserting preset text into fixed areas of the page, sensitive text is located and replaced using precise matching or fuzzy matching. A strategy of dynamically calculating font size and margin value expansion is used to ensure that the replaced text is aligned with the original document layout.
It effectively prevents the leakage of sensitive information, protects personal privacy and corporate secrets, improves processing efficiency, ensures the integrity of document format and layout, and adapts to the needs of different scenarios.
Smart Images

Figure CN120805853A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of intelligent image-text retrieval, and in particular to a sensitive word automatic detection and replacement method, a desensitization device, a computer device and a computer readable storage medium. BACKGROUND
[0002] In the digital information era, PDF (Portable Document Format) files have become an important format for document sharing and storage due to their cross-platform compatibility and format stability. However, with the increasing awareness of data security, how to effectively protect sensitive information in PDF files has become a pressing problem. Currently, the processing of sensitive information in PDF files relies heavily on manual review, which is not only time-consuming and labor-intensive, but also prone to missing sensitive content due to human negligence. In addition, with the explosive growth of data volume, manual processing methods have been difficult to meet the requirements of efficiency and accuracy. Although there are some basic text replacement tools on the market, they often lack support for complex layouts and cannot flexibly handle different font sizes, multi-line matching, and page-specific area processing requirements.
[0003] Chinese patent CN10746366B discloses a sensitive word filtering method based on text content, including the following steps: constructing a Chinese sensitive word library, expanding Chinese words in the Chinese sensitive word library into Chinese-pinyin mixed words to form a Chinese-pinyin mixed sensitive word library; establishing a transition function for each sensitive word in the finite state automaton through a sensitive word lookup tree structure, and constructing the sensitive words in the Chinese-pinyin mixed sensitive word library into a sensitive word tree; searching for sensitive words in the text according to the structure of the sensitive word tree, and replacing the searched sensitive words with specified symbols to complete the sensitive word filtering.
[0004] The above-mentioned scheme improves the filtering efficiency through the construction of a Chinese-pinyin mixed sensitive word library and the use of a finite state automaton, but direct replacement of text may damage the layout and format of the original document. Especially when dealing with PDF files containing complex layout, charts, pictures and specific format requirements, simple text replacement operations are likely to cause document structure disorder, text and image misplacement and other problems, seriously affecting the readability and professionalism of the document. For example, in a business report containing multiple levels of headings, nested tables and mixed text and images, if only the sensitive words in the text are directly replaced, the heading levels may be disordered, the table data and table header may be incorrectly matched, and even the relative position relationship between the picture and the surrounding text may be damaged, making the entire document lose its original logic and aesthetics.
[0005] Therefore, we propose a sensitive word automatic detection and replacement method, a desensitization device, a computer device and a computer readable storage medium. SUMMARY
[0006] The main purpose of the present application is to provide a sensitive word automatic detection and replacement method, a desensitization device, a computer device and a computer readable storage medium, aiming to solve the problem of layout and format damage caused by the sensitive word filtering method in the prior art when processing complex PDF files, and improve the accuracy and efficiency of sensitive information processing.
[0007] To achieve the above-mentioned purpose, the present application provides a sensitive word automatic detection and replacement method, comprising the following steps: S1, a user pre-establishes a lookup text library and a replacement text library, the lookup text library is used to locate sensitive text in a PDF file, and the replacement text library is used to replace the found sensitive text; S2, a plurality of PDF files in a specified folder are sequentially traversed, and the content of each PDF file is read; S3, according to the preset lookup text library and the replacement text library, the sensitive text positioning and replacement of each page of each PDF file is performed, the preset text is inserted into the fixed area of the page, and the processed file is stored to the specified output path; Wherein, the sensitive text positioning and replacement includes the following steps: calling the text search interface of the PDF page object, performing accurate matching or fuzzy matching based on the keywords in the lookup text library; and expanding the matching area according to the preset margin value, generating a desensitization annotation covering the expanded area, and clearing the original content through the apply_redactions interface, finally dynamically calculating the font size according to the coordinates and height of the expanded area, and determining the text insertion reference point combined with the page baseline alignment parameter to ensure that the replacement text completely covers the original area.
[0008] Preferably, the margin value expansion strategy includes horizontal expansion and vertical expansion, the horizontal expansion is specifically: when the length of the replacement text exceeds the original text, the horizontal expansion range is automatically enlarged; the vertical expansion is specifically: when there are multiple line matches, the vertical range is expanded by the line height multiple.
[0009] Preferably, the step of dynamically calculating the font size is specifically: according to the width and height of the expanded area, combining the page baseline alignment parameter, adjusting the font size and line spacing of the replacement text, ensuring that the replacement content is completely displayed in the expanded area and aligned with the original layout.
[0010] Preferably, the fixed area text insertion is specifically: according to the page size parameter to calculate the coordinates of the preset rectangular area, after performing the content clearing operation on the area, the specified font properties are inserted into the fixed text content.
[0011] Preferably, the fixed region position calculation adopts a nine-square grid positioning method; wherein the nine-square grid positioning method comprises the following steps: dividing a PDF page into nine rectangular regions by equally dividing the width and height of the page, and defining the nine regions as left upper, middle upper, right upper, left middle, center, right middle, left lower, middle lower, and right lower; dynamically calculating the boundary coordinates of each positioning region according to the page size parameters and the preset margin ratio; when the preset insertion region overlaps the sensitive word replacement region, the sensitive word processing region is preferentially retained, and the insertion position of the fixed text is adjusted through a weight strategy.
[0012] Preferably, the weight strategy is specifically: traversing all nine-square grid regions, eliminating candidate regions overlapping the sensitive word replacement region, and selecting candidate regions with a width greater than or equal to the width of the fixed text and a height greater than or equal to the height of the fixed text; calculating the total weight score of the selected candidate regions according to total weight score = position priority x weight coefficient + distance adaptation degree + space redundancy - layout interference degree; wherein the position priority is configured as: the center region is defined as a core important region, and the middle upper / middle lower / left middle / right middle regions are defined as secondary important regions; the left upper / right upper / left lower / right lower regions are defined as edge regions; the weight coefficient is set according to the position priority, the default weight of the core important region is 1.0, the weight of the secondary important region is 0.7, and the weight of the edge region is 0.4; the distance adaptation degree = 1-(candidate region center distance / sensitive word region center distance / page diagonal length), which is used to measure the position relevance; the space redundancy = (candidate region remaining width x remaining height) / (fixed text width x fixed text height), which is used to evaluate the space utilization rate of the region; the layout interference degree is determined by detecting the text density (number of characters / unit area) around the candidate region through OCR, and the interference degree = surrounding text density / maximum text density threshold; selecting the candidate region with the highest total weight score as the insertion position of the fixed text according to the calculation results above, if the total weight scores are the same, then the core region is preferentially selected, the secondary region is preferentially selected vertically, and the edge region is preferentially selected in a clockwise order, and the insertion position of the fixed text is adjusted.
[0013] Preferably, the output path comprises at least one security check mechanism, and the security check mechanism is specifically: detecting whether the target file exists before covering the output, and if the target file exists, automatically generating a backup copy with a time stamp to prevent the original file from being accidentally covered.
[0014] To achieve the above object, the application provides a PDF file desensitization device, comprising: a file traversal module for traversing PDF files in a specified folder; a character search module for each page of each PDF file, locating target characters through a character search process according to a plurality of sets of search and replacement text pairs; a region expansion module for expanding the matching region according to a set margin value; a desensitization processing module for adding a desensitization annotation and calling a page.apply_redactions method to clear the original content; a text insertion module for calculating appropriate font size and insertion points according to the coordinates and height of the expanded region, and accurately inserting the replacement text; a fixed region processing module for determining the position of the fixed region according to page parameters, and performing the operations of adding an annotation and clearing the content in the region, and finally inserting fixed characters; and a file saving module for saving the processed file to a "RESULT" folder.
[0015] To achieve the above object, the application provides a computer device comprising a memory and a processor, wherein the memory stores a computer program, and the processor implements the sensitive word automatic detection and replacement method of any one of the above when executing the computer program.
[0016] To achieve the above object, the application provides a computer-readable storage medium storing a computer program, wherein the program implements the steps of the sensitive word automatic detection and replacement method of any one of the above when executed by a processor.
[0017] The technical scheme of the application has the following beneficial effects: By automatically detecting and replacing sensitive words in PDF documents, the leakage of sensitive information can be effectively prevented, and personal privacy and enterprise secrets can be protected. At the same time, during the processing, desensitization annotations and original content removal are used to ensure that sensitive information is completely covered and removed and cannot be recovered or stolen. On the other hand, the user only needs to pre-establish a search text library and a replacement text library, and can process PDF files according to the set process. The entire operation process is simple and intuitive, and does not require professional programming knowledge or complex operation skills. At the same time, the search text library and the replacement text library can be flexibly adjusted and updated according to actual needs, so that the user can add or modify sensitive words and their corresponding replacement content at any time, ensuring that the system can adapt to changing business needs and security requirements. And a plurality of PDF files in a specified folder can be sequentially traversed and processed without manual operation, greatly improving the processing efficiency.
[0018] The user can also set the text library to be searched, the text library to be replaced, and various processing parameters such as the margin value, font size, etc. according to actual conditions, finely control the processing process, and store the processed file to a specified output path for the user to manage and view, ensuring that the entire processing process is within the user's control range, further enhancing the security and reliability of the system. Both accurate matching and fuzzy matching are supported, which can adapt to the needs in different scenarios. For explicit sensitive words, accurate matching can be used for accurate positioning; for sensitive information that may have multiple forms of expression or spelling variations, fuzzy matching can more comprehensively identify them, effectively preventing the omission of sensitive information.
[0019] The dynamically adjusted margin value expansion strategy expands the horizontal expansion range when the length of the replacement text exceeds that of the original text, ensuring that the replacement text can completely and accurately cover the original sensitive words, avoiding the problem of incomplete replacement or format disorder due to text length mismatch. Further, in the vertical expansion aspect, for sensitive words that match multiple lines, the system automatically expands the vertical range by the line height multiple, effectively addressing the detection and replacement needs of sensitive information across lines, ensuring accurate identification and replacement of sensitive words in the context of multiple lines, and preventing the omission of sensitive information due to line-by-line processing. BRIEF DESCRIPTION OF DRAWINGS
[0020] Figure 1 A program logic diagram of the sensitive word automatic detection and replacement method in an embodiment of the present application; Figure 2 A system interface diagram of the sensitive word automatic detection and replacement method in an embodiment of the present application. DETAILED DESCRIPTION
[0021] Embodiments of the present application will be described in detail below, examples of which are shown in the accompanying drawings, wherein the same or similar reference numerals represent the same or similar elements or elements having the same or similar functions throughout. The embodiments described below by reference to the accompanying drawings are exemplary and are intended to explain the present application, and cannot be understood as limiting the present application. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without making creative efforts fall within the scope of protection of the present application.
[0022] In addition, if the description in the present application involves "first", "second", etc., it is only for the purpose of description, such as for distinguishing the same or similar elements, and cannot be understood as indicating or implying the relative importance of the indicated technical features or implicitly indicating the number of the indicated technical features. Therefore, the features defined with "first", "second" can explicitly or implicitly include at least one of the features. In addition, the technical solutions of various embodiments can be combined with each other, but it must be based on the realization of ordinary skilled in the art, when the combination of technical solutions appears contradictory or cannot be realized, it should be considered that the combination of technical solutions does not exist, nor in the protection scope required by the present application.
[0023] The present application provides a kind of sensitive word automatic detection replacement method, comprising the following steps: S1, user pre-establishes search text library and replacement text library, search text library is used to locate sensitive text in PDF file, and replacement text library is used to replace the sensitive text found; S2, to the several PDF files in the specified folder, sequentially traverse, and read the content of each PDF file; S3, according to the preset search text library and replacement text library, each page of each PDF file is executed sensitive text positioning and replacement, inserts preset text in page fixed area, and the processed file is stored to specified output path; Wherein, sensitive text positioning and replacement include the following steps: call the text search interface of PDF page object, based on the key word in search text library carries out accurate matching or fuzzy matching;And according to the preset margin value, the matching area is expanded, generates the desensitization annotation of covering expansion area, and clears the original content through apply_redactions interface, finally according to the coordinate and height of expansion area dynamically calculates font size, determines text insertion reference point in combination with page baseline alignment parameter, ensure that replacement text completely covers original area.
[0024] In this embodiment, by automatically detecting and replacing sensitive words in PDF documents, the leakage of sensitive information can be effectively prevented, and personal privacy and enterprise secrets can be protected. At the same time, during the processing, desensitization annotation and removal of original content are used to ensure that sensitive information is completely covered and removed and cannot be recovered or stolen. On the other hand, the user only needs to pre-establish a search text library and a replacement text library, and can process the PDF file according to the set process. The whole operation process is simple and intuitive, and does not require professional programming knowledge or complex operation skills. At the same time, the search text library and the replacement text library can be flexibly adjusted and updated according to actual needs, so that the user can add or modify sensitive words and their corresponding replacement content at any time, ensuring that the system can adapt to changing business needs and security requirements. And it can traverse and process a number of PDF files in a specified folder one by one without manual operation, greatly improving the processing efficiency.
[0025] In addition, the user can also set the search text library, replacement text library and various processing parameters such as margin value, font size, etc. according to the actual situation to finely control the processing process. At the same time, the processed file is stored to the specified output path, which is convenient for the user to manage and view, ensuring that the whole processing process is within the control of the user, further enhancing the security and reliability of the system. And it supports both exact match and fuzzy match, which can adapt to different scenarios. For explicit sensitive words, they can be accurately located through exact match; while for some sensitive information that may have multiple forms of expression or spelling variations, fuzzy match can identify them more comprehensively, effectively preventing the omission of sensitive information.
[0026] In one of the embodiments, the margin value expansion strategy includes horizontal expansion and vertical expansion. The horizontal expansion specifically refers to automatically expanding the horizontal expansion range when the length of the replacement text exceeds the original text. The vertical expansion specifically refers to expanding the vertical range by the line height multiple when there are multiple line matches.
[0027] In this embodiment, by introducing the dynamically adjusted margin value expansion strategy, the accuracy and adaptability of sensitive word replacement are significantly improved. In terms of horizontal expansion, when the length of the replacement text exceeds the original text, the system can intelligently expand the horizontal expansion range to ensure that the replacement text can completely and accurately cover the original sensitive words, avoiding the problem of incomplete replacement or format disorder caused by text length mismatch.
[0028] Further, in terms of vertical expansion, for sensitive words with multiple line matches, the system automatically expands the vertical range by the line height multiple, effectively addressing the detection and replacement needs of cross-line sensitive information, ensuring accurate identification and replacement of sensitive words in multi-line context, and preventing sensitive information from being missed due to line-by-line processing.
[0029] In one embodiment, the step of dynamically calculating the font size is specifically: adjusting the font size and line spacing of the replacement text according to the width and height of the expansion area, combining the page baseline alignment parameters, to ensure that the replacement content is completely displayed in the expansion area and aligned with the original layout.
[0030] In this embodiment, the step of dynamically calculating the font size further improves the accuracy and visual consistency of sensitive word replacement. Specifically, the system will intelligently adjust the font size and line spacing of the replacement text according to the width and height of the expansion area, combining the page baseline alignment parameters, to ensure that the replacement content not only can be completely displayed in the expansion area, but also can be aligned with the layout of the original document, so as to achieve seamless replacement in vision, avoiding the sense of strangeness caused by font or layout differences.
[0031] In one embodiment, the fixed region text insertion is specifically: calculating the coordinates of the preset rectangular region according to the page size parameters, performing a content clearing operation on the region, and then inserting the fixed text content according to the specified font properties. For example, inserting a preset copyright statement or watermark text in the header, footer or blank area of the page, and the font, color and position parameters of the text can be pre-configured.
[0032] In one embodiment, the fixed region position calculation uses the nine-square positioning method.
[0033] Specifically, the nine-square positioning method includes the following steps: Divide the PDF page into nine rectangular regions by width and height, and define them as left upper, middle upper, right upper, left middle, center, right middle, left lower, middle lower and right lower nine positioning areas; According to the page size parameters (such as page width W, height H) and the preset margin ratio (such as the upper and lower margin ratio α, the left and right margin ratio β), the boundary coordinates of each positioning area are dynamically calculated. For example, the left upper area coordinates are (β*W, α*H, (1 / 3)*W, (1 / 3)*H); When the preset insertion region overlaps with the sensitive word replacement region, the sensitive word processing region is given priority, and the insertion position of the fixed text is adjusted through the weight strategy.
[0034] The weight strategy is specifically: Iterate through all nine-square regions, eliminate the candidate regions overlapping with the sensitive word replacement region, and select the candidate regions with width ≥ fixed text width and height ≥ fixed text height; According to the weight total score = position priority × weight coefficient + distance adaptation degree + space redundancy degree - layout interference degree, the weight total score of the above selected candidate regions is calculated; In the position priority, the rectangular region in the center is represented as a core important region, and the rectangular regions in the upper middle, lower middle, left middle and right middle are represented as secondary important regions; the rectangular regions in the upper left, upper right, lower left and lower right are represented as edge regions; the weight coefficient is set according to the position priority, the default weight of the core important region is 1.0, the weight of the secondary important region is 0.7, and the weight of the edge region is 0.4; the distance adaptation degree = 1-(the distance between the center of the candidate region and the center of the sensitive word region / the length of the diagonal of the page), which is used to measure the position relevance; the space redundancy degree = (the remaining width of the candidate region x the remaining height) / (the fixed text width x the fixed text height), which is used to evaluate the space utilization rate of the region; the layout interference degree is determined by detecting the text density (the number of characters per unit area) around the candidate region through OCR, and the interference degree = the text density around the candidate region / the maximum text density threshold; According to the calculation results, the candidate region with the highest total weight score is selected as the insertion position of the fixed text, if the scores are the same, the core region is given priority, the secondary region is given vertical priority, and the edge region is given clockwise order, and the insertion position of the fixed text is adjusted.
[0035] For example, a fixed text with a size of 30mm x 10mm is inserted on an A4 size (210mm x 297mm) PDF page, while avoiding the sensitive word region (located in the center region). First, the A4 page is divided into nine rectangular regions by width and height, and the coordinates of each region are obtained, as shown in Table 1 below. Region Left border (xl) Upper border (yl) Right border (x2) Lower border (y2) Upper left 10.5 10.5 70 89.1 Upper middle 70 10.5 140 89.1 Upper right 140 10.5 199.5 89.1 Left middle 10.5 89.1 70 148.5 Center 70 89.1 140 148.5 Right middle 140 89.1 199.5 148.5 Lower left 10.5 148.5 70 267.3 Lower middle 70 148.5 140 267.3 Lower right 140 148.5 199.5 267.3 Table 1: Nine-grid region boundary coordinate table According to Table 1, after excluding the center region overlapping with the sensitive word region, all non-overlapping regions meet the minimum size requirement; and the weight calculation is performed taking the upper middle region as an example. The upper middle region belongs to the secondary important region, and the weight coefficient is 0.7. The distance adaptation degree calculation steps are as follows: The center coordinates of the sensitive word region (the center region) are:
[0036] The center coordinates of the upper middle region are:
[0037] The length of the diagonal of the page is: ; The distance adaptation degree is: ; The space redundancy degree calculation steps are as follows: The remaining width of the upper middle region is 140-70=70mm, and the remaining height is 89.1-10.5=78.6mm. The space redundancy degree is: ; The layout interference degree calculation step is as follows: Suppose the peripheral text density of the upper-middle area is detected by OCR as 50 characters / cm2, the maximum threshold is 100 characters / cm2, and the interference degree is 100 / 50 = 0.5; The total weight score is 0.7*0.1 (position weight) + 0.81 (distance adaptation degree) + 18.3 (spatial redundancy) - 0.5 (interference degree) = 19.31; The total weight score of other candidate areas (upper-left, upper-right, middle-left, middle-right, lower-left, middle, and lower-right) is calculated in the same way, and the results are shown in Table 2: Region Weight total Key calculation factors Upper left 18.9 Low spatial redundancy (12.0) Upper right 19.1 High distance adaptation (0.85) Left middle 17.8 High interference (0.7) Right middle 19.0 Moderate spatial redundancy (15.0) Lower left 18.5 Low distance adaptation (0.75) Lower middle 19.2 High spatial redundancy (20.0), low interference (0.4) Lower right 18.7 Moderate spatial redundancy (16.0) Table 2: Total weight score calculation table of nine-grid area According to Table 2, the highest score candidate area is the middle-lower area (total weight score 19.2), therefore, a fixed text with a size of 30 mm x 10 mm is directly inserted into the middle-lower area (coordinates: 70 mm x 148.5 mm ~ 140 mm x 267.3 mm), ensuring no overlap with the sensitive word area and minimal layout interference.
[0038] In this embodiment, through nine-grid partitioning and weight calculation, the system can automatically identify and avoid sensitive word processing areas, avoiding content overlap conflicts. For example, in the above A4 page case, the middle-lower area is selected as the best insertion position due to high spatial redundancy and low interference, which not only meets the size requirements of fixed text, but also maintains the visual balance of the page, and allows the copyright statement, watermark, and other preset content to be accurately embedded in the blank or low-interference area, improving the professionalism of the document while avoiding information obstruction. At the same time, the weight strategy considers position priority, distance adaptation degree, spatial redundancy, and layout interference degree in four dimensions, and realizes global optimal selection of candidate areas through quantitative scoring, effectively balancing functional requirements and page aesthetics, and reducing the cost of manual intervention.
[0039] In addition, combined with sensitive word desensitization annotation, original content removal, and fixed area coverage, even if the sensitive word replacement area is partially restored after inserting the watermark in the header, the superimposed fixed text can further cover up the traces, significantly reducing the risk of information leakage.
[0040] In one of the embodiments, the fixed area position calculation further includes: a page auxiliary analysis function, which is specifically: during the document processing process, drawing grid lines or coordinate annotations at specified positions on the page according to user instructions, for visualizing the page layout information.
[0041] In this embodiment, the relative positions of the text, image and sensitive word replacement area are intuitively observed through the page grid lines, which helps the user to quickly judge whether the candidate area is aligned with the text baseline, and avoids layout misplacement caused by coordinate calculation error. At the same time, coordinate information (such as "top-left area: x = 10.5mm, y = 10.5mm") can be marked in real time in the key area of the page, and the user can verify the positioning logic by comparing the numerical value with the visual mark and correct the parameter configuration error in time.
[0042] In one of the embodiments, the output path includes at least one security check mechanism, which is specifically: detecting whether the target file exists before covering the output, and if it exists, automatically generating a backup copy with a timestamp to prevent the original file from being accidentally covered.
[0043] In this embodiment, the target file is automatically detected before the output is covered, and if it exists, a backup copy with a timestamp is generated, which effectively prevents the original file from being accidentally covered due to misoperation or program exception, thereby significantly reducing the risk of data loss and forming an operation history chain to provide clear evidence for troubleshooting or data recovery.
[0044] Referring to Figure 1 and Figure 2 , the application also provides a PDF file desensitization device, which comprises: a file traversal module for traversing PDF files in a specified folder; a text search module for locating target text through a text search process according to a plurality of search and replace text pairs for each page of each PDF file; a region expansion module for expanding the matching region according to a set margin value; a desensitization processing module for adding desensitization annotations and calling the page.apply_redactions method to clear the original content; a text insertion module for calculating appropriate font size and insertion points according to the coordinates and height of the expanded region, and accurately inserting the replacement text; a fixed region processing module for determining the position of the fixed region according to page parameters, and performing the operations of adding annotations and clearing content in the region, and finally inserting fixed text; and a file saving module for saving the processed file to the "RESULT" folder.
[0045] In this embodiment, the text search module locates according to a plurality of search and replace text pairs, which can adapt to different types and complexity of sensitive information. Whether it is a specific keyword, phrase, or text content conforming to a certain format, it can be accurately identified and located to ensure that no information needs to be desensitized is missed.
[0046] The region expansion module expands the matching region according to the set margin value, effectively avoiding the problem of incomplete desensitization caused by factors such as text layout and font difference. By reasonably setting the margin value, the matching region can be appropriately enlarged, ensuring that the related content around the sensitive information is also included in the desensitization range, further improving the accuracy of desensitization.
[0047] The desensitization processing module adds a desensitization annotation and calls the page.apply_redactions method to clear the original content, completely deleting sensitive information while ensuring that the structure and format of the PDF file are not affected. Compared with the traditional simple deletion operation, the problem of page layout disorder and text misplacement caused by deleting content is avoided, ensuring that the desensitized PDF file still has good readability and integrity.
[0048] The text insertion module calculates the appropriate font size and insertion point according to the coordinates and height of the expanded region, and accurately inserts the replacement text. The inserted text can be naturally integrated with the surrounding content and will not appear conspicuous or inconsistent. For example, when replacing a long text, the system will automatically adjust the font size and insertion position to ensure that the presentation effect of the replaced text on the page is similar to the original content, maintaining the overall aesthetics of the document.
[0049] The fixed region processing module determines the position of the fixed region according to the page parameters, can accurately identify the fixed region in the PDF file that needs special processing, and performs targeted desensitization processing to ensure the consistency and standardization of the entire document. At the same time, after performing the operation of adding annotations and clearing content on the fixed region, it can accurately insert fixed text to ensure that the content of the fixed region meets the expected format and requirements, improving the quality and professionalism of the document.
[0050] In one of the embodiments, the text finding module uses the page.search_for method to quickly locate the target text. The text insertion module determines the appropriate font size and insertion point by considering the coordinates and height of the expanded region when determining the insertion position of the replacement text.
[0051] In one of the embodiments, the fixed region processing module is used to determine the region position according to the page size and the pre-input page parameters.
[0052] The present application also proposes a computer device comprising a memory and a processor, the memory storing a computer program, and the processor executing the computer program to realize the above-mentioned PDF file desensitization method.
[0053] The application further provides a computer readable storage medium storing a computer program, which, when executed by a processor, implements the steps of the above sensitive word automatic detection and replacement method. The computer program, when executed, can include the processes of the above respective sensitive word automatic detection and replacement method embodiments. Any reference to memory, storage, database, or other medium provided by the present application and used in the embodiments can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. As an illustration but not limitation, RAM is available in various forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (SSRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.
[0054] It should be noted that, in this document, the terms "comprise", "contain" or any other variant thereof are intended to cover non-exclusive inclusions, so that a process, device, article or sensitive word automatic detection and replacement method comprising a series of elements not only includes those elements, but also includes other elements not explicitly listed or inherent to such a process, device, article or sensitive word automatic detection and replacement method. Without more limitations, the element defined by the phrase "comprising a" does not exclude the presence of another identical element in the process, device, article or sensitive word automatic detection and replacement method comprising the element.
[0055] The above description is only the preferred embodiments of the present application, and does not limit the patent scope of the present application. Any equivalent structure or equivalent process transformation using the content of the present application specification and drawings, or direct or indirect application in other related technical fields, is also included in the patent protection scope of the present application.
Claims
1. A method for automatically detecting and replacing sensitive words, characterized in that: The following steps are involved: S1. The user pre-establishes a search text library and a replacement text library. The search text library is used to locate sensitive text in a PDF file, and the replacement text library is used to replace the found sensitive text. S2. Traverse the PDF files in the specified folder in sequence and read the content of each PDF file; S3. locating and replacing sensitive text on each page of each PDF file based on a preset search text library and a replacement text library, inserting preset text into a fixed area of the page, and storing the processed file to a specified output path; Among them, the sensitive text positioning and replacement includes the following steps: calling the text search interface of the PDF page object, performing exact matching or fuzzy matching based on the keywords in the search text library; and expanding the matching area according to the preset margin value, generating a desensitized annotation covering the extended area, and clearing the original content through the apply_redactions interface. Finally, the font size is dynamically calculated according to the coordinates and height of the extended area, and the text insertion reference point is determined in combination with the page baseline alignment parameters to ensure that the replacement text completely covers the original area.
2. The method for automatically detecting and replacing sensitive words according to claim 1, characterized in that: The margin value expansion strategy includes: horizontal expansion and vertical expansion. The horizontal expansion is specifically: when the replacement text length exceeds the original text, the horizontal expansion range is automatically expanded; the vertical expansion is specifically: when there are multiple lines of matching, the vertical range is expanded by a multiple of the line height.
3. The method for automatically detecting and replacing sensitive words according to claim 1, characterized in that: The steps of dynamically calculating the font size are specifically as follows: adjusting the font size and line spacing of the replacement text according to the width and height of the extended area in combination with the page baseline alignment parameters to ensure that the replacement content is fully displayed in the extended area and aligned with the original layout.
4. The method for automatically detecting and replacing sensitive words according to claim 1, wherein: The fixed area text insertion is specifically as follows: the coordinates of the preset rectangular area are calculated according to the page size parameters, the content clearing operation is performed on the area, and then the fixed text content is inserted according to the specified font attributes.
5. The method for automatically detecting and replacing sensitive words according to claim 4, characterized in that: The fixed area position calculation is set using the nine-square grid positioning method; wherein, the nine-square grid positioning method includes the following steps: dividing the PDF page into three equal parts according to width and height to form nine rectangular areas, which are defined as nine positioning areas: upper left, upper center, upper right, center left, center, center right, lower left, lower center, and lower right; dynamically calculating the boundary coordinates of each positioning area based on the page size parameters and the preset margin ratio; when the preset insertion area overlaps with the sensitive word replacement area, the sensitive word processing area is retained first, and the insertion position of the fixed text is adjusted through the weight strategy.
6. The method for automatically detecting and replacing sensitive words according to claim 5, characterized in that: The weight strategy is specifically as follows: Traverse all the nine-square grid areas, eliminate candidate areas that overlap with the sensitive word replacement area, and select candidate areas with a width ≥ the fixed text width and a height ≥ the fixed text height; The total weight score of the candidate areas selected above is calculated according to the total weight score = location priority × weight coefficient + distance adaptation + spatial redundancy - layout interference; Among them, the position priority is configured as follows: the rectangular area in the middle is represented as the core important area, and the rectangular areas in the upper middle / lower middle / left middle / right middle are all represented as secondary important areas; the rectangular areas in the upper left / upper right / lower left / lower right are all represented as edge areas; the weight coefficient is set according to the position priority, the default weight of the core important area is 1.0, the secondary important area is 0.7, and the edge area is 0.4; the distance adaptation degree = 1-(the distance between the center of the candidate area and the center of the sensitive word area / the length of the page diagonal), which is used to measure the position relevance; the spatial redundancy = (remaining width of the candidate area × remaining height) / (fixed text width × fixed text height), which evaluates the regional space utilization; the layout interference degree is determined by OCR detection of the text density (number of characters / unit area) around the candidate area, and the interference degree = surrounding text density / maximum text density threshold; According to the above calculation results, the candidate area with the highest total weight score is selected as the insertion position of the fixed text. If the scores are the same, the core area is prioritized, the secondary area is prioritized vertically, and the edge area is set in a clockwise order to adjust the insertion position of the fixed text.
7. The method for automatically detecting and replacing sensitive words according to claim 1, characterized in that: The output path includes at least one safety verification mechanism, which specifically includes: detecting whether the target file exists before overwriting the output, and automatically generating a backup copy with a timestamp if it exists, to prevent the original file from being accidentally overwritten.
8. A PDF file desensitization device, characterized in that: include: File traversal module, used to traverse PDF files in a specified folder; The text search module locates the target text on each page of each PDF file based on multiple sets of search and replace text pairs through the text search process; the area expansion module expands the matching area according to the set margin value; The desensitization processing module is used to add desensitization annotations and call the page.apply_redactions method to clear the original content; the text insertion module calculates the appropriate font size and insertion point based on the coordinates and height of the extended area, and accurately inserts the replacement text; the fixed area processing module determines the position of the fixed area based on the page parameters, adds annotations to the area, clears the content, and finally inserts fixed text; the file saving module saves the processed file to the "RESULT" folder.
9. A computer device, characterized in that: It includes a memory and a processor, the memory stores a computer program, and the processor implements the sensitive word automatic detection and replacement method of any one of claims 1 to 7 when executing the computer program.
10. A computer-readable storage medium, characterized in that A computer program is stored, and when the program is executed by a processor, the steps of the method for automatically detecting and replacing sensitive words in any one of claims 1 to 7 are implemented.