Python writes the principle of shielding foul language in each big chat system

Source: Internet
Author: User
Tags log log

Python writes the principle of shielding foul language in each big chat system

Suddenly think of a video inside the screen is a full of harmony of the * number feel very interesting, and then want to use Python to try to write and read, the results really play out a little effect, the idea is that you have to have a bad word storage warehouse good time to detect, then the individual still like to use the list, because the list of flexible use of extension are We're here to think about the core. How to block dirty words, you have to remember the player input of the type of the bomb screen is the first file and then the string if there is no special requirement on it, million change is always the data structure of it, there is a string replacement * Number what is easy to many, right, today is the whole set of structure, To make it clearer to everyone, I'll split the code and then assemble it so that everyone will have a deeper understanding of the core functionality of the replacement swearing code as follows:

#!/usr/bin/env python#-*-coding:utf-8-*-ImportDatetimetime=Datetime.datetime.now () speak='you son of a day, fuckr your mother Yo, you a fairy board, an old hemp batch'Dirty= ['Fuck','Dog Day's','Duzi','Hemp Batch','Cactus Board','R, your mother .','Screw you .','Grass You'] forIinchDirty:speak= Speak.replace (I,'*')Printspeak+" | "+str (Time)

is not simple and easy to remove the foul language, which imports the time module datetime is used to get the current local time, the later will be used to write the log to save, of course, many game dialogs will also show the current time, and then speak is the player to output the dirty language ( Now is the demo will be changed to input real import interaction, dirty is the definition of the Bad language library (of course, not write too much because each region is different too much to write), followed by a for loop to retrieve, as long as you say that the words contained inside the language library, then we replace with Replace, The final print is equal to the caption displayed on the public screen is * ... Of course, we also want to know how to track who has said what time, so the association will use the class, there will be doubts why use class, the first class is a category (such as table tables, table, conference table, desk, etc.) description of things, such as a group of player player, They all have their own name for the game, and then all have the chat function (which we call the action), so that it can be categorized and can achieve a differentiated effect. The code is as follows:

classPlayer (object):def __init__(self,name): Self.name=namedefTalk (self): self.string='whatever fuck no joke'self.log () self.string= Self.string.replace ('Fuck','ya minute fly butterfly')        Print "Public screen display:%s--%s"%(self.name,self.string)deflog (self):Print "log records are:%s--%s"%(self.name,self.string) T1= Player (' White') T1.talk ()

Here is a comprehensive introduction of the use of the class and workflow effect, here first create a class player player class, Init initializes his name name, which he has chat function talk, also has the logging function, then to chat operation Flow, Each player registered after the login game will have a game name (you can understand as login QQ), here Self.name=name equals to get to your personal name (screen name), and then hit the chat window into talk,self.string you enter the chat information, Self.log log, the focus is to prioritize your original words into the chat log so that you can find your records, self.string.replace replace the dirty words you say, then the screen will only show the replacement of the word such as this printing is originally fuck programming ya minute fly butterfly ... This workflow is not very clear, of course, you do not understand the class method here will be very confused, slowly, as long as the idea of learning the class after the very easy. Of course, this is not the end, and finally the basic complete code to put out the code is as follows:

ImportDatetimetime= str (Datetime.datetime.now ()) [:-7]dirty= ['Fuck','Dog Day's','Duzi','Hemp Batch','Cactus Board','R, your mother .','Screw you .','Grass You']classPlayer (object):def __init__(self,name): Self.name=namedefTalk (self): self.string= Raw_input ("Input-write:")        #self.string = ' You son of a day, R your mother yo, damn you a fairy board, an old hemp batch 'Self.log () forIinchdirty:self.string= Self.string.replace (I,'ya minute fly butterfly')        Print "{} {}-speak:{}". Format (time,self.name,self.string,)deflog (self): with open ('Zanghua.txt','a') as F:f.write ("{} {}-speak:{}\n". Format (time,self.name,self.string)) T1= Player (' White') T2= Player ('Black') T3= Player ('Green') whiletrue:n= Raw_input ('Change Player:')    ifn = ='1': T1.talk ()elifn = ='2': T2.talk ()elifn = ='3': T3.talk ()elifn = ='Q':        Print 'Bye'         Break    Else:        Print "Are you kidding me?"

The test results are as follows:

You can create a new file called Zanghua.txt empty text on the line, just to demonstrate the performance of the storage log, the above print that the change player is to demonstrate different user input to do a simple switch, in fact, the system you rarely switch users after login, and so on, just the current account chat , the code here adds a file-handling method with open (file name, open mode) called F, and then writes write (remember to add a newline character at the end of the write, otherwise the first line is hard to read) about Datetime[:7] Slices It is more concise to ignore the remainder of the time after printing. Basically the process is so, of course, there can be many extensions, such as the number of swearing more than how many prohibit speech, limit the speech for a few minutes ah, the log can be timed emptying ah and so on. This is to see the people themselves to study, the code is a bit large, here will not show.

Finally, the old lines thanks for watching, see you next time ~~~!!!

Python writes the principle of shielding foul language in each big chat system

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.