The production of sensitive word detection tool of PyQt5 is the gospel of the operator

Design idea: filter according to the sensitive thesaurus file to check whether the input text contains sensitive words. So as to filter out the relevant sensitive words.

[read the full text]

Import application related modules.

import os
import logging
import sys

Import modules related to UI interface.

from PyQt5.QtWidgets import QApplication,QWidget,QVBoxLayout,QTextEdit,QGridLayout,QLineEdit,QPushButton,QFileDialog
from PyQt5.QtGui import QIcon

import resource

The resource module in this module is generated using python py resource file. Importing this file directly into the module can prevent the problem that the resource file cannot be packaged when packaging. Show me some resources Code block of py file.

from PyQt5 import QtCore

qt_resource_data = b"\
\x00\x00\x2b\x03\
\x00\
\x01\x6a\xb6\x78\x9c\xed\x5d\x0b\x40\x54\xc5\xfa\x1f\x5c\x95\xf5\
\x11\x58\xdd\xb2\x52\xc1\x7c\x84\xa9\xa9\xa5\x29\xec\x6a\x58\x9a\
\xf6\xbc\x69\xb7\x6b\x5d\x2b\xb1\xb2\xb4\x7c\x01\x65\xa1\xc0\xee\
\xaa\x25\x18\xa4\x66\x6a\xf6\x34\x7a\x78\xcd\x5b\xa6\x66\xb9\x66\
\x25\xff\x44\x01\x33\x5f\xf8\xcc\x47\xf8\x7e\xc1\xee\x22\xa0\x28\
\xb0\xf3\xff\xcd\x39\x67\xe1\xec\x39\x67\x97\x05\x76\x17\xb0\xf3\
\xd3\x8f\xd9\x39\x67\xce\xcc\x37\xdf\x37\xdf\xbc\xce\x9c\x19\x42\
\x02\x48\x73\xd2\xab\x57\x2b\xb8\x8d\xc9\xb8\xa6\x84\x2c\x25\x84\
\xb4\x6f\xcf\xfb\xcf\x34\x22\x24\x28\x90\x90\x56\xad\x78\x7f\x97\

Next is the UI interface. This time, the slot function of the main thread of the UI interface is directly used to complete the business logic, instead of using a separate QThread sub thread.

    def init_ui(self):
        '''Initialize log manager'''
        self.logger = logging.getLogger("Sensitive word detection tool")
        self.logger.setLevel(logging.DEBUG)

        self.setFixedWidth(600)
        self.setWindowIcon(QIcon(':sens.ico'))
        self.setWindowTitle('Sensitive word detection: official account of small tools:[Python concentration camp]')

        vbox = QVBoxLayout()

        self.text_ = QTextEdit()
        self.text_.setPlaceholderText('Please enter the text information to be detected...')
        self.text_.setMaximumHeight(120)

        self.text_lis = QTextEdit()
        self.text_lis.setPlaceholderText('Sensitive word information in the text...')
        self.text_lis.setReadOnly(True)
        self.text_lis.setMaximumHeight(60)

        grid = QGridLayout()
        self.dir_sens = QLineEdit()
        self.dir_sens.setPlaceholderText('Sensitive thesaurus path')
        self.dir_sens.setReadOnly(True)

        self.dir_btn = QPushButton()
        self.dir_btn.setText('Get sensitive Thesaurus')
        self.dir_btn.clicked.connect(self.dir_btn_click)

        grid.addWidget(self.dir_sens, 0, 0, 1, 2)
        grid.addWidget(self.dir_btn, 0, 2, 1, 1)

        self.lis_btn = QPushButton()
        self.lis_btn.setText('Start detection')
        self.lis_btn.clicked.connect(self.search_sens)

        vbox.addWidget(self.text_)
        vbox.addWidget(self.text_lis)
        vbox.addLayout(grid)
        vbox.addWidget(self.lis_btn)

        self.setLayout(vbox)

The rest are four slot functions, which mainly load all sensitive words in the sensitive word file. Finally, compare the sensitive words with the input file.

    def dir_btn_click(self):
        '''
        Select Folder
        :return: 
        '''
        directory = QFileDialog.getExistingDirectory(self, "Select Folder", self.cwd)
        self.dir_sens.setText(directory + '/')

    def get_sens_files(self):
        '''
        Get sensitive word file
        :return: 
        '''
        file_paths = []
        self.logger.info("Start batch file path processing")
        list = os.listdir(self.dir_sens.text())
        for i in range(0, len(list)):
            path = os.path.join(self.dir_sens.text(), list[i])
            if os.path.isfile(path):
                file_paths.append(path)
        self.logger.info("Complete batch file path processing")
        return file_paths

    def load_sens(self):
        '''
        Load sensitive words
        :return: 
        '''
        paths = self.get_sens_files()
        sens = []
        self.logger.info("Start loading sensitive words")
        for path in paths:
            self.logger.info("The currently loaded file path is:" + path)
            with open(path, "rb") as file:
                data = file.readlines()
                datac = []
                for string in data:
                    try:
                        datac.append(string.decode('utf8').replace('\n', '').replace('\r', ''))
                    except:
                        self.logger.error("File:[" + path + "]Decoding exception")
                sens = sens + datac
            sens = sens + datac
        self.logger.info("Finish loading sensitive words")
        return sens

    def search_sens(self):
        '''
        Search for sensitive words
        :return: 
        '''
        text_lis = ""
        sens = self.load_sens()
        text = self.text_.toPlainText()
        for se in sens:
            if se in text and se not in text_lis:
                text_lis = text_lis + se
        self.logger.info("Include sensitive words:" + text_lis)
        self.text_lis.setText(text_lis)

Finally, start the whole application directly with the main() function.

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = SensListen()
    main.show()
    sys.exit(app.exec_())

The above is the complete implementation process. If you need it, you can directly copy all the code into your own development tool and start the main() function!

Input sensitive words and detect them directly on the interface. The detected sensitive words will be displayed in the following text box.

I am a [Python camp. I am glad you saw it. Finally, I am a official account focused on Python knowledge sharing. I hope you can get your attention.

[previous highlights]

Freehand picture generator Shuey Rhon Rhon as an example, a key to generate.

Bing dwen dwen, the mascot just released, is attached to the source.

The most beautiful form viewing plug-in: tabulate

The same tiktok roll call system is written in PyQt5, which is very simple.

Start! Batch add Chinese watermark to PDF file

On the second day of the lunar new year, I made a windows notification manager!

Baidu picture downloader 2.0

gif dynamic picture generator, multiple pictures are combined to generate dynamic pictures

python several common data processing operations, one line of code can be completed!

Chinese new year, use PyQt5 to generate a pair of Spring Festival couplets

Keywords: Python

Added by emrys404 on Mon, 14 Feb 2022 15:47:59 +0200