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"\

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.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_lis = QTextEdit()
        self.text_lis.setPlaceholderText('Sensitive word information in the text...')

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

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

        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')



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
        directory = QFileDialog.getExistingDirectory(self, "Select Folder", self.cwd)
        self.dir_sens.setText(directory + '/')

    def get_sens_files(self):
        Get sensitive word file
        file_paths = []"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)"Complete batch file path processing")
        return file_paths

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

    def search_sens(self):
        Search for sensitive words
        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"Include sensitive words:" + text_lis)

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

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

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