Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use PyQT5 to make a sensitive word detection tool

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use PyQT5 to make a sensitive word detection tool. I hope you will get something after reading this article. Let's discuss it together.

Design idea: filter according to sensitive thesaurus files to see if the input text contains sensitive words. In order to filter out the relevant sensitive words.

Import application-related modules.

Import osimport loggingimport sys

Import modules related to the UI interface.

From PyQt5.QtWidgets import QApplication,QWidget,QVBoxLayout,QTextEdit,QGridLayout,QLineEdit,QPushButton,QFileDialogfrom PyQt5.QtGui import QIconimport resource

The resource module in this is a resource file in the form of .py generated by python. Importing this file directly into the module can prevent the problem that the resource file cannot be packaged during packaging. Show the code block of part of the resource.py file.

From PyQt5 import QtCoreqt_resource_data = b "\ X00\ X00\ X03\ X00\ X01\ X6a\ xb6\ x78\ X9c\ xed\ X5D\ X40\ X54\ xc5\ xfa\ X5C\ X95\ xf5\ X58\ xdd\ xb2\ x52\ xc1\ X7c\ X84\ xa9\ xa5\ x29\ xec\ X6a\ X58\ xf6\ xbc\ x69\ xb7\ x6b\ X5d\ X2b\ xb1\ xb2\ xb4\ X7c\ 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\ xe1 xec\ x39\ x67\ x97\ x05\ x76\ x17\ xb0\ xf3\ xd3\ X8f\ xd9\ x39\ 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\ xb4\ X6f\ xcf\ xcf\ x34\ x24\ x28\ x90\ x56\ xad\ x78\ x7f\ x97\

Next is the part of the UI interface, this time directly using the slot function of the main thread of the UI interface to complete the business logic, and not using a separate child thread of QThread.

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 gadget official account: [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 function parts, which mainly load all the sensitive words of the sensitive word file. Finally, the sensitive words are compared 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 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 ("contains sensitive words:" + text_lis) self.text_lis.setText (text_lis)

Finally, start the entire application directly using 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, partners in need directly copy all the code into their own development tools to start the main () function on it!

Input sensitive words are detected directly on the interface, and the detected sensitive words will be displayed in the text box below.

After reading this article, I believe you have a certain understanding of "how to use PyQT5 to make a sensitive word detection tool". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report